Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UniqueId.cs
Go to the documentation of this file.
3
4namespace System.Xml;
5
6public class UniqueId
7{
8 private long _idLow;
9
10 private long _idHigh;
11
12 private string _s;
13
14 private const int guidLength = 16;
15
16 private const int uuidLength = 45;
17
18 private static readonly short[] s_char2val = new short[256]
19 {
20 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
21 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
22 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
23 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
24 256, 256, 256, 256, 256, 256, 256, 256, 0, 16,
25 32, 48, 64, 80, 96, 112, 128, 144, 256, 256,
26 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
27 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
28 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
29 256, 256, 256, 256, 256, 256, 256, 160, 176, 192,
30 208, 224, 240, 256, 256, 256, 256, 256, 256, 256,
31 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
32 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
33 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
34 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
35 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
36 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
37 256, 256, 256, 256, 256, 256, 0, 1, 2, 3,
38 4, 5, 6, 7, 8, 9, 256, 256, 256, 256,
39 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
40 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
41 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
42 256, 256, 256, 256, 256, 10, 11, 12, 13, 14,
43 15, 256, 256, 256, 256, 256, 256, 256, 256, 256,
44 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
45 256, 256, 256, 256, 256, 256
46 };
47
48 public int CharArrayLength
49 {
50 get
51 {
52 if (_s != null)
53 {
54 return _s.Length;
55 }
56 return 45;
57 }
58 }
59
60 public bool IsGuid => (_idLow | _idHigh) != 0;
61
62 public UniqueId()
63 : this(Guid.NewGuid())
64 {
65 }
66
67 public UniqueId(Guid guid)
68 : this(guid.ToByteArray())
69 {
70 }
71
72 public UniqueId(byte[] guid)
73 : this(guid, 0)
74 {
75 }
76
77 public unsafe UniqueId(byte[] guid, int offset)
78 {
79 if (guid == null)
80 {
82 }
83 if (offset < 0)
84 {
86 }
87 if (offset > guid.Length)
88 {
90 }
91 if (16 > guid.Length - offset)
92 {
94 }
95 fixed (byte* ptr = &guid[offset])
96 {
99 }
100 }
101
102 public unsafe UniqueId(string value)
103 {
104 if (value == null)
105 {
107 }
108 if (value.Length == 0)
109 {
111 }
112 fixed (char* chars = value)
113 {
114 UnsafeParse(chars, value.Length);
115 }
116 _s = value;
117 }
118
154
155 private unsafe int UnsafeDecode(short* char2val, char ch1, char ch2)
156 {
157 if ((ch1 | ch2) >= 128)
158 {
159 return 256;
160 }
161 return char2val[(int)ch1] | char2val[128 + ch2];
162 }
163
164 private unsafe static void UnsafeEncode(byte b, char* pch)
165 {
168 }
169
170 private unsafe void UnsafeParse(char* chars, int charCount)
171 {
172 if (charCount != 45 || *chars != 'u' || chars[1] != 'r' || chars[2] != 'n' || chars[3] != ':' || chars[4] != 'u' || chars[5] != 'u' || chars[6] != 'i' || chars[7] != 'd' || chars[8] != ':' || chars[17] != '-' || chars[22] != '-' || chars[27] != '-' || chars[32] != '-')
173 {
174 return;
175 }
176 byte* ptr = stackalloc byte[16];
177 int num = 0;
178 int num2 = 0;
179 fixed (short* ptr2 = &s_char2val[0])
180 {
181 short* char2val = ptr2;
182 num = UnsafeDecode(char2val, chars[15], chars[16]);
183 *ptr = (byte)num;
184 num2 |= num;
185 num = UnsafeDecode(char2val, chars[13], chars[14]);
186 ptr[1] = (byte)num;
187 num2 |= num;
188 num = UnsafeDecode(char2val, chars[11], chars[12]);
189 ptr[2] = (byte)num;
190 num2 |= num;
191 num = UnsafeDecode(char2val, chars[9], chars[10]);
192 ptr[3] = (byte)num;
193 num2 |= num;
194 num = UnsafeDecode(char2val, chars[20], chars[21]);
195 ptr[4] = (byte)num;
196 num2 |= num;
197 num = UnsafeDecode(char2val, chars[18], chars[19]);
198 ptr[5] = (byte)num;
199 num2 |= num;
200 num = UnsafeDecode(char2val, chars[25], chars[26]);
201 ptr[6] = (byte)num;
202 num2 |= num;
203 num = UnsafeDecode(char2val, chars[23], chars[24]);
204 ptr[7] = (byte)num;
205 num2 |= num;
206 num = UnsafeDecode(char2val, chars[28], chars[29]);
207 ptr[8] = (byte)num;
208 num2 |= num;
209 num = UnsafeDecode(char2val, chars[30], chars[31]);
210 ptr[9] = (byte)num;
211 num2 |= num;
212 num = UnsafeDecode(char2val, chars[33], chars[34]);
213 ptr[10] = (byte)num;
214 num2 |= num;
215 num = UnsafeDecode(char2val, chars[35], chars[36]);
216 ptr[11] = (byte)num;
217 num2 |= num;
218 num = UnsafeDecode(char2val, chars[37], chars[38]);
219 ptr[12] = (byte)num;
220 num2 |= num;
221 num = UnsafeDecode(char2val, chars[39], chars[40]);
222 ptr[13] = (byte)num;
223 num2 |= num;
224 num = UnsafeDecode(char2val, chars[41], chars[42]);
225 ptr[14] = (byte)num;
226 num2 |= num;
227 num = UnsafeDecode(char2val, chars[43], chars[44]);
228 ptr[15] = (byte)num;
229 num2 |= num;
230 if (num2 >= 256)
231 {
232 return;
233 }
236 }
237 }
238
261
262 private unsafe void ToSpan(Span<char> chars)
263 {
264 if (_s != null)
265 {
266 _s.CopyTo(chars);
267 return;
268 }
269 byte* ptr = stackalloc byte[16];
272 fixed (char* ptr2 = &chars[0])
273 {
274 char* ptr3 = ptr2;
275 *ptr3 = 'u';
276 ptr3[1] = 'r';
277 ptr3[2] = 'n';
278 ptr3[3] = ':';
279 ptr3[4] = 'u';
280 ptr3[5] = 'u';
281 ptr3[6] = 'i';
282 ptr3[7] = 'd';
283 ptr3[8] = ':';
284 ptr3[17] = '-';
285 ptr3[22] = '-';
286 ptr3[27] = '-';
287 ptr3[32] = '-';
288 UnsafeEncode(*ptr, ptr3 + 15);
289 UnsafeEncode(ptr[1], ptr3 + 13);
290 UnsafeEncode(ptr[2], ptr3 + 11);
291 UnsafeEncode(ptr[3], ptr3 + 9);
292 UnsafeEncode(ptr[4], ptr3 + 20);
293 UnsafeEncode(ptr[5], ptr3 + 18);
294 UnsafeEncode(ptr[6], ptr3 + 25);
295 UnsafeEncode(ptr[7], ptr3 + 23);
296 UnsafeEncode(ptr[8], ptr3 + 28);
297 UnsafeEncode(ptr[9], ptr3 + 30);
298 UnsafeEncode(ptr[10], ptr3 + 33);
299 UnsafeEncode(ptr[11], ptr3 + 35);
300 UnsafeEncode(ptr[12], ptr3 + 37);
301 UnsafeEncode(ptr[13], ptr3 + 39);
302 UnsafeEncode(ptr[14], ptr3 + 41);
303 UnsafeEncode(ptr[15], ptr3 + 43);
304 }
305 }
306
307 public bool TryGetGuid(out Guid guid)
308 {
309 byte[] array = new byte[16];
310 if (!TryGetGuid(array, 0))
311 {
312 guid = Guid.Empty;
313 return false;
314 }
315 guid = new Guid(array);
316 return true;
317 }
318
319 public unsafe bool TryGetGuid(byte[] buffer, int offset)
320 {
321 if (!IsGuid)
322 {
323 return false;
324 }
325 if (buffer == null)
326 {
328 }
329 if (offset < 0)
330 {
332 }
333 if (offset > buffer.Length)
334 {
336 }
337 if (16 > buffer.Length - offset)
338 {
340 }
341 fixed (byte* ptr = &buffer[offset])
342 {
345 }
346 return true;
347 }
348
349 public override string ToString()
350 {
351 return _s ?? (_s = string.Create(CharArrayLength, this, delegate(Span<char> destination, UniqueId thisRef)
352 {
353 thisRef.ToSpan(destination);
354 }));
355 }
356
357 public static bool operator ==(UniqueId? id1, UniqueId? id2)
358 {
359 if ((object)id1 == id2)
360 {
361 return true;
362 }
363 if ((object)id1 == null || (object)id2 == null)
364 {
365 return false;
366 }
367 if (id1.IsGuid && id2.IsGuid)
368 {
369 if (id1._idLow == id2._idLow)
370 {
371 return id1._idHigh == id2._idHigh;
372 }
373 return false;
374 }
375 return id1.ToString() == id2.ToString();
376 }
377
378 public static bool operator !=(UniqueId? id1, UniqueId? id2)
379 {
380 return !(id1 == id2);
381 }
382
383 public override bool Equals([NotNullWhen(true)] object? obj)
384 {
385 return this == obj as UniqueId;
386 }
387
388 public override int GetHashCode()
389 {
390 if (IsGuid)
391 {
392 long num = _idLow ^ _idHigh;
393 return (int)(num >> 32) ^ (int)num;
394 }
395 return ToString().GetHashCode();
396 }
397
398 private unsafe long UnsafeGetInt64(byte* pb)
399 {
400 int num = UnsafeGetInt32(pb);
401 int num2 = UnsafeGetInt32(pb + 4);
402 return ((long)num2 << 32) | (uint)num;
403 }
404
405 private unsafe int UnsafeGetInt32(byte* pb)
406 {
407 int num = pb[3];
408 num <<= 8;
409 num |= pb[2];
410 num <<= 8;
411 num |= pb[1];
412 num <<= 8;
413 return num | *pb;
414 }
415
416 private unsafe void UnsafeSetInt64(long value, byte* pb)
417 {
418 UnsafeSetInt32((int)value, pb);
419 UnsafeSetInt32((int)(value >> 32), pb + 4);
420 }
421
422 private unsafe void UnsafeSetInt32(int value, byte* pb)
423 {
424 *pb = (byte)value;
425 value >>= 8;
426 pb[1] = (byte)value;
427 value >>= 8;
428 pb[2] = (byte)value;
429 value >>= 8;
430 pb[3] = (byte)value;
431 }
432}
static char ToCharLower(int value)
static string XmlInvalidUniqueId
Definition SR.cs:444
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ValueMustBeNonNegative
Definition SR.cs:296
static string SizeExceedsRemainingBufferSpace
Definition SR.cs:324
static string XmlArrayTooSmallInput
Definition SR.cs:350
static string XmlArrayTooSmallOutput
Definition SR.cs:328
static string OffsetExceedsBufferSize
Definition SR.cs:322
Definition SR.cs:7
unsafe UniqueId(char[] chars, int offset, int count)
Definition UniqueId.cs:119
UniqueId(Guid guid)
Definition UniqueId.cs:67
static readonly short[] s_char2val
Definition UniqueId.cs:18
unsafe UniqueId(byte[] guid, int offset)
Definition UniqueId.cs:77
const int guidLength
Definition UniqueId.cs:14
unsafe int UnsafeGetInt32(byte *pb)
Definition UniqueId.cs:405
UniqueId(byte[] guid)
Definition UniqueId.cs:72
override string ToString()
Definition UniqueId.cs:349
unsafe void UnsafeSetInt32(int value, byte *pb)
Definition UniqueId.cs:422
static bool operator==(UniqueId? id1, UniqueId? id2)
Definition UniqueId.cs:357
override int GetHashCode()
Definition UniqueId.cs:388
unsafe void UnsafeSetInt64(long value, byte *pb)
Definition UniqueId.cs:416
static bool operator!=(UniqueId? id1, UniqueId? id2)
Definition UniqueId.cs:378
unsafe int UnsafeDecode(short *char2val, char ch1, char ch2)
Definition UniqueId.cs:155
unsafe long UnsafeGetInt64(byte *pb)
Definition UniqueId.cs:398
unsafe void ToSpan(Span< char > chars)
Definition UniqueId.cs:262
override bool Equals([NotNullWhen(true)] object? obj)
Definition UniqueId.cs:383
unsafe UniqueId(string value)
Definition UniqueId.cs:102
bool TryGetGuid(out Guid guid)
Definition UniqueId.cs:307
static unsafe void UnsafeEncode(byte b, char *pch)
Definition UniqueId.cs:164
const int uuidLength
Definition UniqueId.cs:16
unsafe bool TryGetGuid(byte[] buffer, int offset)
Definition UniqueId.cs:319
unsafe void UnsafeParse(char *chars, int charCount)
Definition UniqueId.cs:170
int ToCharArray(char[] chars, int offset)
Definition UniqueId.cs:239
static readonly Guid Empty
Definition Guid.cs:86