Repository navigation
Expand file tree
/
Copy pathUnityVersion.cs
More file actions
314 lines (271 loc) · 9.7 KB
/
Copy pathUnityVersion.cs
File metadata and controls
314 lines (271 loc) · 9.7 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
using System;
using System.IO;
namespace LibCpp2IL
{
public readonly struct UnityVersion : IEquatable<UnityVersion>, IComparable, IComparable<UnityVersion>
{
private readonly ulong m_data;
#region Constructors
public UnityVersion(int major)
{
m_data = (ulong)(major & 0xFFFF) << 48;
}
public UnityVersion(int major, int minor)
{
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40);
}
public UnityVersion(int major, int minor, int build)
{
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32);
}
public UnityVersion(int major, int minor, int build, UnityVersionType type)
{
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32)
| ((ulong)((int)type & 0xFF) << 24);
}
public UnityVersion(int major, int minor, int build, UnityVersionType type, int typeNumber)
{
m_data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32)
| ((ulong)((int)type & 0xFF) << 24) | ((ulong)(typeNumber & 0xFF) << 16);
}
private UnityVersion(ulong data)
{
m_data = data;
}
#endregion
#region Properties
public static UnityVersion MinVersion => new UnityVersion(0UL);
public static UnityVersion MaxVersion => new UnityVersion(ulong.MaxValue);
/// <summary> 2017.3.0f3 </summary>
public static UnityVersion DefaultVersion => new UnityVersion(2017, 3, 0, UnityVersionType.Final, 3);
public int Major => unchecked((int)((m_data >> 48) & 0xFFFFUL));
public int Minor => unchecked((int)((m_data >> 40) & 0xFFUL));
public int Build => unchecked((int)((m_data >> 32) & 0xFFUL));
public UnityVersionType Type => (UnityVersionType)unchecked((int)((m_data >> 24) & 0xFFUL));
public int TypeNumber => unchecked((int)((m_data >> 16) & 0xFFUL));
#endregion
#region Miscellaneous Methods
public int[] ToArray() => new int[] { Major, Minor, Build };
public override string ToString()
{
return $"{Major}.{Minor}.{Build}{Type.ToLiteral()}{TypeNumber}";
}
public static UnityVersion Max(UnityVersion left, UnityVersion right)
{
if (left > right) return left;
else return right;
}
public static UnityVersion Min(UnityVersion left, UnityVersion right)
{
if (left < right) return left;
else return right;
}
public static UnityVersion Parse(string version)
{
if (string.IsNullOrEmpty(version))
{
throw new Exception($"Invalid version number {version}");
}
int major = 0;
int minor = 0;
int build = 0;
UnityVersionType versionType = UnityVersionType.Final;
int typeNumber = 0;
using (StringReader reader = new StringReader(version))
{
while (true)
{
int symb = reader.Read();
if (symb == -1)
{
throw new Exception($"Invalid version format");
}
char c = (char)symb;
if (c == '.')
{
break;
}
major = major * 10 + c.ParseDigit();
}
while (true)
{
int symb = reader.Read();
if (symb == -1)
{
break;
}
char c = (char)symb;
if (c == '.')
{
break;
}
minor = minor * 10 + c.ParseDigit();
}
while (true)
{
int symb = reader.Read();
if (symb == -1)
{
break;
}
char c = (char)symb;
if (char.IsDigit(c))
{
build = build * 10 + c.ParseDigit();
}
else
{
switch (c)
{
case 'a':
versionType = UnityVersionType.Alpha;
break;
case 'b':
versionType = UnityVersionType.Beta;
break;
case 'p':
versionType = UnityVersionType.Patch;
break;
case 'f':
versionType = UnityVersionType.Final;
break;
default:
throw new Exception($"Unsupported version type {c} for version '{version}'");
}
break;
}
}
while (true)
{
int symb = reader.Read();
if (symb == -1)
{
break;
}
char c = (char)symb;
typeNumber = typeNumber * 10 + c.ParseDigit();
}
}
return new UnityVersion(major, minor, build, versionType, typeNumber);
}
#endregion
#region Comparison Operators
public static bool operator ==(UnityVersion left, UnityVersion right)
{
return left.m_data == right.m_data;
}
public static bool operator !=(UnityVersion left, UnityVersion right)
{
return left.m_data != right.m_data;
}
public static bool operator >(UnityVersion left, UnityVersion right)
{
return left.m_data > right.m_data;
}
public static bool operator >=(UnityVersion left, UnityVersion right)
{
return left.m_data >= right.m_data;
}
public static bool operator <(UnityVersion left, UnityVersion right)
{
return left.m_data < right.m_data;
}
public static bool operator <=(UnityVersion left, UnityVersion right)
{
return left.m_data <= right.m_data;
}
#endregion
#region IEquatable and IComparable
public int CompareTo(object obj)
{
if (obj is UnityVersion version)
return CompareTo(version);
else
return 1;
}
public int CompareTo(UnityVersion other)
{
if (this > other)
return 1;
else if (this < other)
return -1;
else
return 0;
}
public override bool Equals(object obj)
{
if (obj is UnityVersion version)
return this == version;
else
return false;
}
public bool Equals(UnityVersion other) => this == other;
public override int GetHashCode()
{
return unchecked(827 + 911 * m_data.GetHashCode());
}
#endregion
#region Comparison Methods
public bool IsEqual(int major) => this == From(major);
public bool IsEqual(int major, int minor) => this == From(major, minor);
public bool IsEqual(int major, int minor, int build) => this == From(major, minor, build);
public bool IsEqual(int major, int minor, int build, UnityVersionType type) => this == From(major, minor, build, type);
public bool IsEqual(int major, int minor, int build, UnityVersionType type, int typeNumber) => this == new UnityVersion(major, minor, build, type, typeNumber);
public bool IsEqual(string version) => this == Parse(version);
public bool IsLess(int major) => this < From(major);
public bool IsLess(int major, int minor) => this < From(major, minor);
public bool IsLess(int major, int minor, int build) => this < From(major, minor, build);
public bool IsLess(int major, int minor, int build, UnityVersionType type) => this < From(major, minor, build, type);
public bool IsLess(int major, int minor, int build, UnityVersionType type, int typeNumber) => this < new UnityVersion(major, minor, build, type, typeNumber);
public bool IsLess(string version) => this < Parse(version);
public bool IsLessEqual(int major) => this <= From(major);
public bool IsLessEqual(int major, int minor) => this <= From(major, minor);
public bool IsLessEqual(int major, int minor, int build) => this <= From(major, minor, build);
public bool IsLessEqual(int major, int minor, int build, UnityVersionType type) => this <= From(major, minor, build, type);
public bool IsLessEqual(int major, int minor, int build, UnityVersionType type, int typeNumber) => this <= new UnityVersion(major, minor, build, type, typeNumber);
public bool IsLessEqual(string version) => this <= Parse(version);
public bool IsGreater(int major) => this > From(major);
public bool IsGreater(int major, int minor) => this > From(major, minor);
public bool IsGreater(int major, int minor, int build) => this > From(major, minor, build);
public bool IsGreater(int major, int minor, int build, UnityVersionType type) => this > From(major, minor, build, type);
public bool IsGreater(int major, int minor, int build, UnityVersionType type, int typeNumber) => this > new UnityVersion(major, minor, build, type, typeNumber);
public bool IsGreater(string version) => this > Parse(version);
public bool IsGreaterEqual(int major) => this >= From(major);
public bool IsGreaterEqual(int major, int minor) => this >= From(major, minor);
public bool IsGreaterEqual(int major, int minor, int build) => this >= From(major, minor, build);
public bool IsGreaterEqual(int major, int minor, int build, UnityVersionType type) => this >= From(major, minor, build, type);
public bool IsGreaterEqual(int major, int minor, int build, UnityVersionType type, int typeNumber) => this >= new UnityVersion(major, minor, build, type, typeNumber);
public bool IsGreaterEqual(string version) => this >= Parse(version);
#endregion
#region From
private UnityVersion From(int major)
{
ulong data = ((ulong)(major & 0xFFFF) << 48) | (0x0000FFFFFFFFFFFFUL & m_data);
return new UnityVersion(data);
}
private UnityVersion From(int major, int minor)
{
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | (0x000000FFFFFFFFFFUL & m_data);
return new UnityVersion(data);
}
private UnityVersion From(int major, int minor, int build)
{
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32) |
(0x00000000FFFFFFFFUL & m_data);
return new UnityVersion(data);
}
private UnityVersion From(int major, int minor, int build, UnityVersionType type)
{
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32) |
((ulong)((int)type & 0xFF) << 24) | (0x0000000000FFFFFFUL & m_data);
return new UnityVersion(data);
}
private UnityVersion From(int major, int minor, int build, UnityVersionType type, int typeNumber)
{
ulong data = ((ulong)(major & 0xFFFF) << 48) | ((ulong)(minor & 0xFF) << 40) | ((ulong)(build & 0xFF) << 32)
| ((ulong)((int)type & 0xFF) << 24) | ((ulong)(typeNumber & 0xFF) << 16) | (0x000000000000FFFFUL & m_data);
return new UnityVersion(data);
}
#endregion
}
}