forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityVersion.cs
More file actions
369 lines (315 loc) · 13 KB
/
Copy pathUnityVersion.cs
File metadata and controls
369 lines (315 loc) · 13 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
namespace UnityEditor.Scripting.ScriptCompilation
{
internal struct UnityVersion : IVersion<UnityVersion>
{
// ReleaseType enum must stay in sync with the native enum ReleaseType in UnityVersion.h
public enum UnityReleaseType
{
kAlphaRelease = 0,
kBetaRelease,
kPublicRelease,
kChinaPublicRelease,
kPatchRelease,
kExperimentalRelease,
kNumUnityReleaseTypes
}
public bool IsInitialized { get; }
public int Major { get; }
public int Minor { get; }
public int Revision { get; }
public UnityReleaseType ReleaseType { get; }
public int IncrementalVersion { get; }
// Suffix following the parse-able part of a string UnityVersion - ignored for any functional purpose. E.g. "2018.4.3f1-prefab-asset-editing"
public string Suffix { get; }
public UnityVersion(int major, int minor = 0, int revision = 0, UnityReleaseType releaseType = UnityReleaseType.kPublicRelease, int incrementalVersion = 0, string suffix = "")
{
Major = major;
Minor = minor;
Revision = revision;
ReleaseType = releaseType;
IncrementalVersion = incrementalVersion;
Suffix = suffix;
IsInitialized = true;
}
public static int Compare(UnityVersion versionA, UnityVersion versionB)
{
return versionA.CompareTo(versionB);
}
public static bool operator==(UnityVersion left, UnityVersion right)
{
return left.Equals(right);
}
public static bool operator!=(UnityVersion left, UnityVersion right)
{
return !(left == right);
}
public static bool operator>(UnityVersion left, UnityVersion right)
{
return Compare(left, right) > 0;
}
public static bool operator>=(UnityVersion left, UnityVersion right)
{
return left == right || left > right;
}
public static bool operator<(UnityVersion left, UnityVersion right)
{
return Compare(left, right) < 0;
}
public static bool operator<=(UnityVersion left, UnityVersion right)
{
return left == right || left < right;
}
public int CompareTo(object obj)
{
return CompareTo((UnityVersion)obj);
}
public int CompareTo(UnityVersion other)
{
var result = Major.CompareTo(other.Major);
if (result != 0)
{
return result;
}
result = Minor.CompareTo(other.Minor);
if (result != 0)
{
return result;
}
result = Revision.CompareTo(other.Revision);
if (result != 0)
{
return result;
}
result = CompareReleaseType(ReleaseType, other.ReleaseType);
if (result != 0)
{
return result;
}
result = IncrementalVersion.CompareTo(other.IncrementalVersion);
if (result != 0)
{
return result;
}
// We do not compare the Suffix
return 0;
}
private static int CompareReleaseType(UnityReleaseType current, UnityReleaseType other)
{
var rt1 = current != UnityReleaseType.kChinaPublicRelease ? current : UnityReleaseType.kPublicRelease;
var rt2 = other != UnityReleaseType.kChinaPublicRelease ? other : UnityReleaseType.kPublicRelease;
return (int)rt1 - (int)rt2;
}
public bool Equals(UnityVersion other)
{
// We do not compare the suffix
return Major == other.Major &&
Minor == other.Minor &&
Revision == other.Revision &&
CompareReleaseType(ReleaseType, other.ReleaseType) == 0 &&
IncrementalVersion == other.IncrementalVersion;
}
public override bool Equals(object other)
{
if (ReferenceEquals(null, other))
return false;
return other.GetType() == GetType() && Equals((UnityVersion)other);
}
public override string ToString()
{
var version = $"{Major}.{Minor}.{Revision}";
if (ReleaseType != UnityReleaseType.kPublicRelease || IncrementalVersion != 0)
{
version += $"{UnityVersionTypeTraits.k_ValidReleaseTypeSymbols[(int)ReleaseType]}";
if (IncrementalVersion != 0)
{
version += $"{IncrementalVersion}";
}
}
return version;
}
public string ToStringFull()
{
var version = ToString();
if (!string.IsNullOrEmpty(Suffix))
{
version += $"{Suffix}";
}
return version;
}
public override int GetHashCode()
{
unchecked
{
int result = Major.GetHashCode();
result = result * 31 + Minor.GetHashCode();
result = result * 31 + Revision.GetHashCode();
result = result * 31 + GetReleaseTypeHashCode(ReleaseType);
result = result * 31 + IncrementalVersion.GetHashCode();
// We do not compare the suffix, so we do not hash it either
return result;
}
}
private static int GetReleaseTypeHashCode(UnityReleaseType releaseType)
{
var rt = releaseType != UnityReleaseType.kChinaPublicRelease ? releaseType : UnityReleaseType.kPublicRelease;
return rt.GetHashCode();
}
public UnityVersion Parse(string version, bool strict = false)
{
return UnityVersionParser.Parse(version, strict);
}
public static UnityVersionTypeTraits VersionTypeTraits { get; } = new UnityVersionTypeTraits();
public IVersionTypeTraits GetVersionTypeTraits()
{
return VersionTypeTraits;
}
}
internal class UnityVersionTypeTraits : IVersionTypeTraits
{
public bool IsAllowedFirstCharacter(char c, bool strict = false)
{
return VersionTypeTraitsUtils.IsCharDigit(c);
}
public bool IsAllowedLastCharacter(char c, bool strict = false)
{
return strict ? (VersionTypeTraitsUtils.IsCharDigit(c) || VersionTypeTraitsUtils.IsCharLetter(c)) : IsAllowedCharacter(c);
}
public bool IsAllowedCharacter(char c)
{
return VersionTypeTraitsUtils.IsCharDigit(c) || VersionTypeTraitsUtils.IsCharLetter(c)
|| c == '.' || c == '-' || c == '/';
}
public static readonly char[] k_ValidReleaseTypeSymbols = new[]
{
'a', // UnityReleaseType.kAlphaRelease
'b', // UnityReleaseType.kBetaRelease
'f', // UnityReleaseType.kPublicRelease
'c', // China release, treat as 'f'
'p', // UnityReleaseType.kPatchRelease
'x', // UnityReleaseType.kExperimentalRelease
};
static UnityVersionTypeTraits()
{
UnityEngine.Assertions.Assert.IsTrue(k_ValidReleaseTypeSymbols.Length == (int)UnityVersion.UnityReleaseType.kNumUnityReleaseTypes);
}
public static bool IsAllowedUnityReleaseTypeIdentifier(char c)
{
for (int i = 0; i < k_ValidReleaseTypeSymbols.Length; i++)
if (k_ValidReleaseTypeSymbols[i] == c)
return true;
return false;
}
public static UnityVersion.UnityReleaseType ParseUnityReleaseType(char releaseType)
{
if (TryParseUnityReleaseType(releaseType, out var result))
{
return result;
}
throw new ArgumentException($"UnityVersionTypeTraits does not recognize release type symbol '{releaseType}'.");
}
public static bool TryParseUnityReleaseType(char releaseType, out UnityVersion.UnityReleaseType result)
{
for (int i = (int)UnityVersion.UnityReleaseType.kAlphaRelease; i < (int)UnityVersion.UnityReleaseType.kNumUnityReleaseTypes; ++i)
{
if (releaseType == k_ValidReleaseTypeSymbols[i])
{
result = (UnityVersion.UnityReleaseType)i;
return true;
}
}
result = UnityVersion.UnityReleaseType.kNumUnityReleaseTypes;
return false;
}
}
internal static class UnityVersionParser
{
private static bool TryReadVersionNumber(string version, ref int cursor, ref int versionComponent)
{
string nextVersionComponent = VersionUtils.ConsumeVersionComponentFromString(version, ref cursor, x => !char.IsDigit(x));
if (!String.IsNullOrEmpty(nextVersionComponent))
{
return int.TryParse(nextVersionComponent, out versionComponent);
}
return false;
}
private static bool TrySkipVersionSeparator(string version, ref int cursor)
{
if (cursor < version.Length && version[cursor] == '.')
{
cursor++;
return cursor < version.Length; // . is not allowed to be the last symbol
}
return false;
}
private static bool TryReadVersionReleaseType(string version, ref int cursor, ref UnityVersion.UnityReleaseType releaseType)
{
if (cursor < version.Length && UnityVersionTypeTraits.IsAllowedUnityReleaseTypeIdentifier(version[cursor]))
{
if (UnityVersionTypeTraits.TryParseUnityReleaseType(version[cursor], out releaseType))
{
cursor++;
return true;
}
}
return false;
}
public static UnityVersion Parse(string version, bool strict = false)
{
if (TryParse(version, out var result, strict) && result.HasValue)
{
return result.Value;
}
throw new ArgumentException($"'{version}' is not a valid Unity Version");
}
public static bool TryParse(string version, out UnityVersion? result, bool strict = false)
{
int cursor = 0;
int major = 0;
int minor = 0;
int revision = 0;
// missing release type identifier means 'f' - public release, so that is the default as well
UnityVersion.UnityReleaseType releaseType = UnityVersion.UnityReleaseType.kPublicRelease;
int incrementalVersion = 0;
string suffix = "";
bool isValid = true;
// Doing this instead because RegEx is impressively slow
try
{
isValid = cursor < version.Length && TryReadVersionNumber(version, ref cursor, ref major);
isValid = isValid && (cursor == version.Length || TrySkipVersionSeparator(version, ref cursor));
isValid = isValid && (cursor == version.Length || TryReadVersionNumber(version, ref cursor, ref minor));
isValid = isValid && (cursor == version.Length || TrySkipVersionSeparator(version, ref cursor));
isValid = isValid && (cursor == version.Length || TryReadVersionNumber(version, ref cursor, ref revision));
isValid = isValid && (cursor == version.Length || TryReadVersionReleaseType(version, ref cursor, ref releaseType));
isValid = isValid && (cursor == version.Length ||
// experimental 'x' releases can have any characters following the 'x', so we don't read incrementalVersion but save the rest into the suffix
releaseType == UnityVersion.UnityReleaseType.kExperimentalRelease ||
TryReadVersionNumber(version, ref cursor, ref incrementalVersion));
// if there's anything left, read the rest as suffix if experimental release or allowSuffix parse request
if (isValid && cursor < version.Length)
{
if (releaseType == UnityVersion.UnityReleaseType.kExperimentalRelease || !strict)
suffix = version.Substring(cursor);
else
isValid = false;
}
}
catch (Exception)
{
result = null;
return false;
}
if (!isValid)
{
result = null;
return false;
}
result = new UnityVersion(major, minor, revision, releaseType, incrementalVersion, suffix);
return true;
}
}
}