Repository navigation
Expand file tree
/
Copy pathUnityVersion.cs
More file actions
113 lines (102 loc) · 3.4 KB
/
Copy pathUnityVersion.cs
File metadata and controls
113 lines (102 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
namespace AssetsTools.NET.Extra
{
public class UnityVersion
{
public int major;
public int minor;
public int patch;
public string type;
public int typeNum;
public UnityVersion() { }
public UnityVersion(string version)
{
major = 0;
minor = 0;
patch = 0;
type = "";
typeNum = 0;
string[] versionSplit = version.Split('.');
if (versionSplit.Length >= 1)
major = int.Parse(versionSplit[0]);
if (versionSplit.Length >= 2)
minor = int.Parse(versionSplit[1]);
if (versionSplit.Length <= 2)
return;
int verTypeIndex = versionSplit[2].IndexOfAny(new[] { 'f', 'p', 'a', 'b', 'c', 't', 'x' });
if (verTypeIndex != -1)
{
type = versionSplit[2][verTypeIndex].ToString();
patch = int.Parse(versionSplit[2].Substring(0, verTypeIndex));
string patchNumString = versionSplit[2].Substring(verTypeIndex + 1);
if (!int.TryParse(patchNumString, out typeNum))
{
// sometimes pesky custom engine versions add text to the end
string newPatchNumString = "";
for (int i = 0; i < patchNumString.Length; i++)
{
if (patchNumString[i] >= '0' && patchNumString[i] <= '9')
{
newPatchNumString += patchNumString[i];
}
else
{
break;
}
}
if (newPatchNumString.Length > 0)
{
typeNum = int.Parse(newPatchNumString);
}
}
}
else
{
patch = int.Parse(versionSplit[2]);
type = "";
typeNum = 0;
}
}
public override string ToString()
{
if (type == string.Empty)
return $"{major}.{minor}.{patch}";
else
return $"{major}.{minor}.{patch}{type}{typeNum}";
}
public ulong ToUInt64()
{
byte typeByte = type switch
{
"a" => 0,
"b" => 1,
"c" => 2,
"f" => 3,
"p" => 4,
"x" => 5,
_ => 0xff
};
return ((ulong)major << 48) | ((ulong)minor << 32) | ((ulong)patch << 16) | ((ulong)typeByte << 8) | (uint)typeNum;
}
public static UnityVersion FromUInt64(ulong data)
{
UnityVersion version = new UnityVersion()
{
major = (int)((data >> 48) & 0xffff),
minor = (int)((data >> 32) & 0xffff),
patch = (int)((data >> 16) & 0xffff),
type = ((data >> 8) & 0xff) switch
{
0 => "a",
1 => "b",
2 => "c",
3 => "f",
4 => "p",
5 => "x",
_ => "?"
},
typeNum = (int)(data & 0xff)
};
return version;
}
}
}