summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/ProtoBuf/BclHelpers.cs
blob: 50452c07c11e5c1d452c2b4d96ffe2841cfffe4d (plain)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
using System;

namespace ProtoBuf
{
	public static class BclHelpers
	{
		private const int FieldTimeSpanValue = 1;

		private const int FieldTimeSpanScale = 2;

		internal static readonly DateTime EpochOrigin = new DateTime(1970, 1, 1, 0, 0, 0, 0);

		private const int FieldDecimalLow = 1;

		private const int FieldDecimalHigh = 2;

		private const int FieldDecimalSignScale = 3;

		private const int FieldGuidLow = 1;

		private const int FieldGuidHigh = 2;

		private const int FieldExistingObjectKey = 1;

		private const int FieldNewObjectKey = 2;

		private const int FieldExistingTypeKey = 3;

		private const int FieldNewTypeKey = 4;

		private const int FieldTypeName = 8;

		private const int FieldObject = 10;

		[Flags]
		public enum NetObjectOptions : byte
		{
			None = 0,
			AsReference = 1,
			DynamicType = 2,
			UseConstructor = 4,
			LateSet = 8
		}

		public static object GetUninitializedObject(Type type)
		{
			throw new NotSupportedException("Constructor-skipping is not supported on this platform");
		}

		public static void WriteTimeSpan(TimeSpan timeSpan, ProtoWriter dest)
		{
			bool flag = dest == null;
			if (flag)
			{
				throw new ArgumentNullException("dest");
			}
			WireType wireType = dest.WireType;
			if (wireType != WireType.Fixed64)
			{
				if (wireType - WireType.String > 1)
				{
					throw new ProtoException("Unexpected wire-type: " + dest.WireType.ToString());
				}
				long num = timeSpan.Ticks;
				bool flag2 = timeSpan == TimeSpan.MaxValue;
				TimeSpanScale timeSpanScale;
				if (flag2)
				{
					num = 1L;
					timeSpanScale = TimeSpanScale.MinMax;
				}
				else
				{
					bool flag3 = timeSpan == TimeSpan.MinValue;
					if (flag3)
					{
						num = -1L;
						timeSpanScale = TimeSpanScale.MinMax;
					}
					else
					{
						bool flag4 = num % 864000000000L == 0L;
						if (flag4)
						{
							timeSpanScale = TimeSpanScale.Days;
							num /= 864000000000L;
						}
						else
						{
							bool flag5 = num % 36000000000L == 0L;
							if (flag5)
							{
								timeSpanScale = TimeSpanScale.Hours;
								num /= 36000000000L;
							}
							else
							{
								bool flag6 = num % 600000000L == 0L;
								if (flag6)
								{
									timeSpanScale = TimeSpanScale.Minutes;
									num /= 600000000L;
								}
								else
								{
									bool flag7 = num % 10000000L == 0L;
									if (flag7)
									{
										timeSpanScale = TimeSpanScale.Seconds;
										num /= 10000000L;
									}
									else
									{
										bool flag8 = num % 10000L == 0L;
										if (flag8)
										{
											timeSpanScale = TimeSpanScale.Milliseconds;
											num /= 10000L;
										}
										else
										{
											timeSpanScale = TimeSpanScale.Ticks;
										}
									}
								}
							}
						}
					}
				}
				SubItemToken token = ProtoWriter.StartSubItem(null, dest);
				bool flag9 = num != 0L;
				if (flag9)
				{
					ProtoWriter.WriteFieldHeader(1, WireType.SignedVariant, dest);
					ProtoWriter.WriteInt64(num, dest);
				}
				bool flag10 = timeSpanScale > TimeSpanScale.Days;
				if (flag10)
				{
					ProtoWriter.WriteFieldHeader(2, WireType.Variant, dest);
					ProtoWriter.WriteInt32((int)timeSpanScale, dest);
				}
				ProtoWriter.EndSubItem(token, dest);
			}
			else
			{
				ProtoWriter.WriteInt64(timeSpan.Ticks, dest);
			}
		}

		public static TimeSpan ReadTimeSpan(ProtoReader source)
		{
			long num = BclHelpers.ReadTimeSpanTicks(source);
			bool flag = num == long.MinValue;
			TimeSpan result;
			if (flag)
			{
				result = TimeSpan.MinValue;
			}
			else
			{
				bool flag2 = num == long.MaxValue;
				if (flag2)
				{
					result = TimeSpan.MaxValue;
				}
				else
				{
					result = TimeSpan.FromTicks(num);
				}
			}
			return result;
		}

		public static DateTime ReadDateTime(ProtoReader source)
		{
			long num = BclHelpers.ReadTimeSpanTicks(source);
			bool flag = num == long.MinValue;
			DateTime result;
			if (flag)
			{
				result = DateTime.MinValue;
			}
			else
			{
				bool flag2 = num == long.MaxValue;
				if (flag2)
				{
					result = DateTime.MaxValue;
				}
				else
				{
					result = BclHelpers.EpochOrigin.AddTicks(num);
				}
			}
			return result;
		}

		public static void WriteDateTime(DateTime value, ProtoWriter dest)
		{
			bool flag = dest == null;
			if (flag)
			{
				throw new ArgumentNullException("dest");
			}
			WireType wireType = dest.WireType;
			TimeSpan timeSpan;
			if (wireType - WireType.String > 1)
			{
				timeSpan = value - BclHelpers.EpochOrigin;
			}
			else
			{
				bool flag2 = value == DateTime.MaxValue;
				if (flag2)
				{
					timeSpan = TimeSpan.MaxValue;
				}
				else
				{
					bool flag3 = value == DateTime.MinValue;
					if (flag3)
					{
						timeSpan = TimeSpan.MinValue;
					}
					else
					{
						timeSpan = value - BclHelpers.EpochOrigin;
					}
				}
			}
			BclHelpers.WriteTimeSpan(timeSpan, dest);
		}

		private static long ReadTimeSpanTicks(ProtoReader source)
		{
			WireType wireType = source.WireType;
			long result;
			if (wireType != WireType.Fixed64)
			{
				if (wireType - WireType.String > 1)
				{
					throw new ProtoException("Unexpected wire-type: " + source.WireType.ToString());
				}
				SubItemToken token = ProtoReader.StartSubItem(source);
				TimeSpanScale timeSpanScale = TimeSpanScale.Days;
				long num = 0L;
				int num2;
				while ((num2 = source.ReadFieldHeader()) > 0)
				{
					int num3 = num2;
					if (num3 != 1)
					{
						if (num3 != 2)
						{
							source.SkipField();
						}
						else
						{
							timeSpanScale = (TimeSpanScale)source.ReadInt32();
						}
					}
					else
					{
						source.Assert(WireType.SignedVariant);
						num = source.ReadInt64();
					}
				}
				ProtoReader.EndSubItem(token, source);
				TimeSpanScale timeSpanScale2 = timeSpanScale;
				switch (timeSpanScale2)
				{
				case TimeSpanScale.Days:
					result = num * 864000000000L;
					break;
				case TimeSpanScale.Hours:
					result = num * 36000000000L;
					break;
				case TimeSpanScale.Minutes:
					result = num * 600000000L;
					break;
				case TimeSpanScale.Seconds:
					result = num * 10000000L;
					break;
				case TimeSpanScale.Milliseconds:
					result = num * 10000L;
					break;
				case TimeSpanScale.Ticks:
					result = num;
					break;
				default:
				{
					if (timeSpanScale2 != TimeSpanScale.MinMax)
					{
						throw new ProtoException("Unknown timescale: " + timeSpanScale.ToString());
					}
					long num4 = num;
					if (num4 != -1L)
					{
						if (num4 != 1L)
						{
							throw new ProtoException("Unknown min/max value: " + num.ToString());
						}
						result = long.MaxValue;
					}
					else
					{
						result = long.MinValue;
					}
					break;
				}
				}
			}
			else
			{
				result = source.ReadInt64();
			}
			return result;
		}

		public static decimal ReadDecimal(ProtoReader reader)
		{
			ulong num = 0UL;
			uint num2 = 0u;
			uint num3 = 0u;
			SubItemToken token = ProtoReader.StartSubItem(reader);
			int num4;
			while ((num4 = reader.ReadFieldHeader()) > 0)
			{
				switch (num4)
				{
				case 1:
					num = reader.ReadUInt64();
					break;
				case 2:
					num2 = reader.ReadUInt32();
					break;
				case 3:
					num3 = reader.ReadUInt32();
					break;
				default:
					reader.SkipField();
					break;
				}
			}
			ProtoReader.EndSubItem(token, reader);
			bool flag = num == 0UL && num2 == 0u;
			decimal result;
			if (flag)
			{
				result = 0m;
			}
			else
			{
				int lo = (int)(num & /*(ulong)-1 */ulong.MaxValue);
				int mid = (int)(num >> 32 & /*(ulong)-1*/ulong.MaxValue);
				int hi = (int)num2;
				bool isNegative = (num3 & 1u) == 1u;
				byte scale = (byte)((num3 & 510u) >> 1);
				result = new decimal(lo, mid, hi, isNegative, scale);
			}
			return result;
		}

		public static void WriteDecimal(decimal value, ProtoWriter writer)
		{
			int[] bits = decimal.GetBits(value);
			ulong num = (ulong)((ulong)((long)bits[1]) << 32);
			ulong num2 = (ulong)((long)bits[0] & (long)(long.MaxValue));
			ulong num3 = num | num2;
			uint num4 = (uint)bits[2];
			uint num5 = (uint)((bits[3] >> 15 & 510) | (bits[3] >> 31 & 1));
			SubItemToken token = ProtoWriter.StartSubItem(null, writer);
			bool flag = num3 > 0UL;
			if (flag)
			{
				ProtoWriter.WriteFieldHeader(1, WireType.Variant, writer);
				ProtoWriter.WriteUInt64(num3, writer);
			}
			bool flag2 = num4 > 0u;
			if (flag2)
			{
				ProtoWriter.WriteFieldHeader(2, WireType.Variant, writer);
				ProtoWriter.WriteUInt32(num4, writer);
			}
			bool flag3 = num5 > 0u;
			if (flag3)
			{
				ProtoWriter.WriteFieldHeader(3, WireType.Variant, writer);
				ProtoWriter.WriteUInt32(num5, writer);
			}
			ProtoWriter.EndSubItem(token, writer);
		}

		public static void WriteGuid(Guid value, ProtoWriter dest)
		{
			byte[] data = value.ToByteArray();
			SubItemToken token = ProtoWriter.StartSubItem(null, dest);
			bool flag = value != Guid.Empty;
			if (flag)
			{
				ProtoWriter.WriteFieldHeader(1, WireType.Fixed64, dest);
				ProtoWriter.WriteBytes(data, 0, 8, dest);
				ProtoWriter.WriteFieldHeader(2, WireType.Fixed64, dest);
				ProtoWriter.WriteBytes(data, 8, 8, dest);
			}
			ProtoWriter.EndSubItem(token, dest);
		}

		public static Guid ReadGuid(ProtoReader source)
		{
			ulong num = 0UL;
			ulong num2 = 0UL;
			SubItemToken token = ProtoReader.StartSubItem(source);
			int num3;
			while ((num3 = source.ReadFieldHeader()) > 0)
			{
				int num4 = num3;
				if (num4 != 1)
				{
					if (num4 != 2)
					{
						source.SkipField();
					}
					else
					{
						num2 = source.ReadUInt64();
					}
				}
				else
				{
					num = source.ReadUInt64();
				}
			}
			ProtoReader.EndSubItem(token, source);
			bool flag = num == 0UL && num2 == 0UL;
			Guid result;
			if (flag)
			{
				result = Guid.Empty;
			}
			else
			{
				uint num5 = (uint)(num >> 32);
				uint a = (uint)num;
				uint num6 = (uint)(num2 >> 32);
				uint num7 = (uint)num2;
				result = new Guid((int)a, (short)num5, (short)(num5 >> 16), (byte)num7, (byte)(num7 >> 8), (byte)(num7 >> 16), (byte)(num7 >> 24), (byte)num6, (byte)(num6 >> 8), (byte)(num6 >> 16), (byte)(num6 >> 24));
			}
			return result;
		}

		public static object ReadNetObject(object value, ProtoReader source, int key, Type type, BclHelpers.NetObjectOptions options)
		{
			SubItemToken token = ProtoReader.StartSubItem(source);
			int num = -1;
			int num2 = -1;
			int num3;
			while ((num3 = source.ReadFieldHeader()) > 0)
			{
				switch (num3)
				{
				case 1:
				{
					int key2 = source.ReadInt32();
					value = source.NetCache.GetKeyedObject(key2);
					break;
				}
				case 2:
					num = source.ReadInt32();
					break;
				case 3:
				{
					int key2 = source.ReadInt32();
					type = (Type)source.NetCache.GetKeyedObject(key2);
					key = source.GetTypeKey(ref type);
					break;
				}
				case 4:
					num2 = source.ReadInt32();
					break;
				case 5:
				case 6:
				case 7:
				case 9:
					goto IL_27E;
				case 8:
				{
					string text = source.ReadString();
					type = source.DeserializeType(text);
					bool flag = type == null;
					if (flag)
					{
						throw new ProtoException("Unable to resolve type: " + text + " (you can use the TypeModel.DynamicTypeFormatting event to provide a custom mapping)");
					}
					bool flag2 = type == typeof(string);
					if (flag2)
					{
						key = -1;
					}
					else
					{
						key = source.GetTypeKey(ref type);
						bool flag3 = key < 0;
						if (flag3)
						{
							throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
						}
					}
					break;
				}
				case 10:
				{
					bool flag4 = type == typeof(string);
					bool flag5 = value == null;
					bool flag6 = flag5 && (flag4 || (options & BclHelpers.NetObjectOptions.LateSet) > BclHelpers.NetObjectOptions.None);
					bool flag7 = num >= 0 && !flag6;
					if (flag7)
					{
						bool flag8 = value == null;
						if (flag8)
						{
							source.TrapNextObject(num);
						}
						else
						{
							source.NetCache.SetKeyedObject(num, value);
						}
						bool flag9 = num2 >= 0;
						if (flag9)
						{
							source.NetCache.SetKeyedObject(num2, type);
						}
					}
					object obj = value;
					bool flag10 = flag4;
					if (flag10)
					{
						value = source.ReadString();
					}
					else
					{
						value = ProtoReader.ReadTypedObject(obj, key, source, type);
					}
					bool flag11 = num >= 0;
					if (flag11)
					{
						bool flag12 = flag5 && !flag6;
						if (flag12)
						{
							obj = source.NetCache.GetKeyedObject(num);
						}
						bool flag13 = flag6;
						if (flag13)
						{
							source.NetCache.SetKeyedObject(num, value);
							bool flag14 = num2 >= 0;
							if (flag14)
							{
								source.NetCache.SetKeyedObject(num2, type);
							}
						}
					}
					bool flag15 = num >= 0 && !flag6 && obj != value;
					if (flag15)
					{
						throw new ProtoException("A reference-tracked object changed reference during deserialization");
					}
					bool flag16 = num < 0 && num2 >= 0;
					if (flag16)
					{
						source.NetCache.SetKeyedObject(num2, type);
					}
					break;
				}
				default:
					goto IL_27E;
				}
				continue;
				IL_27E:
				source.SkipField();
			}
			bool flag17 = num >= 0 && (options & BclHelpers.NetObjectOptions.AsReference) == BclHelpers.NetObjectOptions.None;
			if (flag17)
			{
				throw new ProtoException("Object key in input stream, but reference-tracking was not expected");
			}
			ProtoReader.EndSubItem(token, source);
			return value;
		}

		public static void WriteNetObject(object value, ProtoWriter dest, int key, BclHelpers.NetObjectOptions options)
		{
			bool flag = dest == null;
			if (flag)
			{
				throw new ArgumentNullException("dest");
			}
			bool flag2 = (options & BclHelpers.NetObjectOptions.DynamicType) > BclHelpers.NetObjectOptions.None;
			bool flag3 = (options & BclHelpers.NetObjectOptions.AsReference) > BclHelpers.NetObjectOptions.None;
			WireType wireType = dest.WireType;
			SubItemToken token = ProtoWriter.StartSubItem(null, dest);
			bool flag4 = true;
			bool flag5 = flag3;
			if (flag5)
			{
				bool flag6;
				int value2 = dest.NetCache.AddObjectKey(value, out flag6);
				ProtoWriter.WriteFieldHeader(flag6 ? 1 : 2, WireType.Variant, dest);
				ProtoWriter.WriteInt32(value2, dest);
				bool flag7 = flag6;
				if (flag7)
				{
					flag4 = false;
				}
			}
			bool flag8 = flag4;
			if (flag8)
			{
				bool flag9 = flag2;
				if (flag9)
				{
					Type type = value.GetType();
					bool flag10 = !(value is string);
					if (flag10)
					{
						key = dest.GetTypeKey(ref type);
						bool flag11 = key < 0;
						if (flag11)
						{
							throw new InvalidOperationException("Dynamic type is not a contract-type: " + type.Name);
						}
					}
					bool flag12;
					int value3 = dest.NetCache.AddObjectKey(type, out flag12);
					ProtoWriter.WriteFieldHeader(flag12 ? 3 : 4, WireType.Variant, dest);
					ProtoWriter.WriteInt32(value3, dest);
					bool flag13 = !flag12;
					if (flag13)
					{
						ProtoWriter.WriteFieldHeader(8, WireType.String, dest);
						ProtoWriter.WriteString(dest.SerializeType(type), dest);
					}
				}
				ProtoWriter.WriteFieldHeader(10, wireType, dest);
				bool flag14 = value is string;
				if (flag14)
				{
					ProtoWriter.WriteString((string)value, dest);
				}
				else
				{
					ProtoWriter.WriteObject(value, key, dest);
				}
			}
			ProtoWriter.EndSubItem(token, dest);
		}
	}
}