Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BinaryParser.cs
Go to the documentation of this file.
3using System.IO;
5using System.Text;
6
8
9internal sealed class BinaryParser
10{
12
14
15 internal Stream _input;
16
17 internal long _topId;
18
19 internal long _headerId;
20
22
24
25 internal SerStack _stack = new SerStack("ObjectProgressStack");
26
28
29 internal object _expectedTypeInformation;
30
31 internal ParseRecord _prs;
32
34
35 private readonly BinaryReader _dataReader;
36
38
40
42
44
46
48
50
51 private byte[] _byteBuffer;
52
54
56
58
59 internal static volatile MessageEnd _messageEnd;
60
62
64
66
67 internal ParseRecord PRs => _prs ?? (_prs = new ParseRecord());
68
75
76 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
77 internal void Run()
78 {
79 try
80 {
81 bool flag = true;
82 ReadBegin();
84 while (flag)
85 {
87 switch (_expectedType)
88 {
89 case BinaryTypeEnum.String:
90 case BinaryTypeEnum.Object:
91 case BinaryTypeEnum.ObjectUrt:
92 case BinaryTypeEnum.ObjectUser:
93 case BinaryTypeEnum.ObjectArray:
94 case BinaryTypeEnum.StringArray:
95 case BinaryTypeEnum.PrimitiveArray:
96 {
97 byte b = _dataReader.ReadByte();
99 switch (binaryHeaderEnum)
100 {
101 case BinaryHeaderEnum.Assembly:
102 case BinaryHeaderEnum.CrossAppDomainAssembly:
104 break;
105 case BinaryHeaderEnum.Object:
106 ReadObject();
107 break;
108 case BinaryHeaderEnum.CrossAppDomainMap:
110 break;
111 case BinaryHeaderEnum.ObjectWithMap:
112 case BinaryHeaderEnum.ObjectWithMapAssemId:
114 break;
115 case BinaryHeaderEnum.ObjectWithMapTyped:
116 case BinaryHeaderEnum.ObjectWithMapTypedAssemId:
118 break;
119 case BinaryHeaderEnum.ObjectString:
120 case BinaryHeaderEnum.CrossAppDomainString:
122 break;
123 case BinaryHeaderEnum.Array:
124 case BinaryHeaderEnum.ArraySinglePrimitive:
125 case BinaryHeaderEnum.ArraySingleObject:
126 case BinaryHeaderEnum.ArraySingleString:
128 break;
129 case BinaryHeaderEnum.MemberPrimitiveTyped:
131 break;
132 case BinaryHeaderEnum.MemberReference:
134 break;
135 case BinaryHeaderEnum.ObjectNull:
136 case BinaryHeaderEnum.ObjectNullMultiple256:
137 case BinaryHeaderEnum.ObjectNullMultiple:
139 break;
140 case BinaryHeaderEnum.MessageEnd:
141 flag = false;
143 ReadEnd();
144 break;
145 default:
147 }
148 break;
149 }
150 case BinaryTypeEnum.Primitive:
152 break;
153 default:
155 }
156 if (binaryHeaderEnum == BinaryHeaderEnum.Assembly)
157 {
158 continue;
159 }
160 bool flag2 = false;
161 while (!flag2)
162 {
164 if (objectProgress == null)
165 {
166 _expectedType = BinaryTypeEnum.ObjectUrt;
168 flag2 = true;
169 continue;
170 }
171 flag2 = objectProgress.GetNext(out objectProgress._expectedType, out objectProgress._expectedTypeInformation);
172 _expectedType = objectProgress._expectedType;
173 _expectedTypeInformation = objectProgress._expectedTypeInformation;
174 if (!flag2)
175 {
176 PRs.Init();
177 if (objectProgress._memberValueEnum == InternalMemberValueE.Nested)
178 {
180 PRs._memberTypeEnum = objectProgress._memberTypeEnum;
181 PRs._memberValueEnum = objectProgress._memberValueEnum;
183 }
184 else
185 {
187 PRs._memberTypeEnum = objectProgress._memberTypeEnum;
188 PRs._memberValueEnum = objectProgress._memberValueEnum;
190 }
191 _stack.Pop();
193 }
194 }
195 }
196 }
198 {
200 }
201 }
202
203 internal void ReadBegin()
204 {
205 }
206
207 internal void ReadEnd()
208 {
209 }
210
211 internal bool ReadBoolean()
212 {
213 return _dataReader.ReadBoolean();
214 }
215
216 internal byte ReadByte()
217 {
218 return _dataReader.ReadByte();
219 }
220
221 internal byte[] ReadBytes(int length)
222 {
224 }
225
226 internal void ReadBytes(byte[] byteA, int offset, int size)
227 {
228 while (size > 0)
229 {
230 int num = _dataReader.Read(byteA, offset, size);
231 if (num == 0)
232 {
234 }
235 offset += num;
236 size -= num;
237 }
238 }
239
240 internal char ReadChar()
241 {
242 return _dataReader.ReadChar();
243 }
244
245 internal char[] ReadChars(int length)
246 {
248 }
249
250 internal decimal ReadDecimal()
251 {
252 return decimal.Parse(_dataReader.ReadString(), CultureInfo.InvariantCulture);
253 }
254
255 internal float ReadSingle()
256 {
257 return _dataReader.ReadSingle();
258 }
259
260 internal double ReadDouble()
261 {
262 return _dataReader.ReadDouble();
263 }
264
265 internal short ReadInt16()
266 {
267 return _dataReader.ReadInt16();
268 }
269
270 internal int ReadInt32()
271 {
272 return _dataReader.ReadInt32();
273 }
274
275 internal long ReadInt64()
276 {
277 return _dataReader.ReadInt64();
278 }
279
280 internal sbyte ReadSByte()
281 {
282 return (sbyte)ReadByte();
283 }
284
285 internal string ReadString()
286 {
287 return _dataReader.ReadString();
288 }
289
291 {
292 return new TimeSpan(ReadInt64());
293 }
294
296 {
297 return FromBinaryRaw(ReadInt64());
298 }
299
300 private static DateTime FromBinaryRaw(long dateData)
301 {
303 return MemoryMarshal.Cast<long, DateTime>(MemoryMarshal.CreateReadOnlySpan(ref dateData, 1))[0];
304 }
305
306 internal ushort ReadUInt16()
307 {
308 return _dataReader.ReadUInt16();
309 }
310
311 internal uint ReadUInt32()
312 {
313 return _dataReader.ReadUInt32();
314 }
315
316 internal ulong ReadUInt64()
317 {
318 return _dataReader.ReadUInt64();
319 }
320
328
349
350 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
351 private void ReadObject()
352 {
353 if (_binaryObject == null)
354 {
356 }
357 _binaryObject.Read(this);
359 if (objectMap == null)
360 {
362 }
364 ParseRecord pr = op._pr;
365 _stack.Push(op);
367 op._binaryTypeEnumA = objectMap._binaryTypeEnumA;
368 op._memberNames = objectMap._memberNames;
369 op._memberTypes = objectMap._memberTypes;
370 op._typeInformationA = objectMap._typeInformationA;
371 op._memberLength = op._binaryTypeEnumA.Length;
373 if (objectProgress == null || objectProgress._isInitial)
374 {
375 op._name = objectMap._objectName;
378 }
379 else
380 {
384 switch (objectProgress._objectTypeEnum)
385 {
386 case InternalObjectTypeE.Object:
387 pr._name = objectProgress._name;
390 break;
391 case InternalObjectTypeE.Array:
394 break;
395 default:
396 throw new SerializationException(System.SR.Format(System.SR.Serialization_Map, objectProgress._objectTypeEnum.ToString()));
397 }
398 }
400 pr._objectInfo = objectMap.CreateObjectInfo(ref pr._si, ref pr._memberData);
401 if (pr._objectId == _topId)
402 {
404 }
406 pr._keyDt = objectMap._objectName;
407 pr._dtType = objectMap._objectType;
410 }
411
412 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
430
431 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
433 {
434 if (_bowm == null)
435 {
437 }
438 else
439 {
441 }
442 _bowm.Read(this);
444 }
445
446 [RequiresUnreferencedCode("Types might be removed")]
448 {
451 ParseRecord pr = op._pr;
452 _stack.Push(op);
453 if (record._binaryHeaderEnum == BinaryHeaderEnum.ObjectWithMapAssemId)
454 {
455 if (record._assemId < 1)
456 {
458 }
460 if (binaryAssemblyInfo == null)
461 {
463 }
464 }
465 else if (record._binaryHeaderEnum == BinaryHeaderEnum.ObjectWithMap)
466 {
468 }
471 ObjectMapIdTable[record._objectId] = objectMap;
473 op._binaryTypeEnumA = objectMap._binaryTypeEnumA;
474 op._typeInformationA = objectMap._typeInformationA;
475 op._memberLength = op._binaryTypeEnumA.Length;
476 op._memberNames = objectMap._memberNames;
477 op._memberTypes = objectMap._memberTypes;
479 if (objectProgress == null || objectProgress._isInitial)
480 {
481 op._name = record._name;
484 }
485 else
486 {
490 switch (objectProgress._objectTypeEnum)
491 {
492 case InternalObjectTypeE.Object:
493 pr._name = objectProgress._name;
496 break;
497 case InternalObjectTypeE.Array:
500 break;
501 default:
503 }
504 }
507 pr._objectInfo = objectMap.CreateObjectInfo(ref pr._si, ref pr._memberData);
508 if (pr._objectId == _topId)
509 {
511 }
512 pr._keyDt = record._name;
513 pr._dtType = objectMap._objectType;
516 }
517
518 [RequiresUnreferencedCode("Types might be removed")]
520 {
521 if (_bowmt == null)
522 {
524 }
525 else
526 {
528 }
529 _bowmt.Read(this);
531 }
532
533 [RequiresUnreferencedCode("Types might be removed")]
535 {
538 ParseRecord pr = op._pr;
539 _stack.Push(op);
540 if (record._binaryHeaderEnum == BinaryHeaderEnum.ObjectWithMapTypedAssemId)
541 {
542 if (record._assemId < 1)
543 {
545 }
547 if (binaryAssemblyInfo == null)
548 {
550 }
551 }
552 else if (record._binaryHeaderEnum == BinaryHeaderEnum.ObjectWithMapTyped)
553 {
555 }
556 ObjectMap objectMap = ObjectMap.Create(record._name, record._memberNames, record._binaryTypeEnumA, record._typeInformationA, record._memberAssemIds, _objectReader, record._objectId, binaryAssemblyInfo, AssemIdToAssemblyTable);
557 ObjectMapIdTable[record._objectId] = objectMap;
559 op._binaryTypeEnumA = objectMap._binaryTypeEnumA;
560 op._typeInformationA = objectMap._typeInformationA;
561 op._memberLength = op._binaryTypeEnumA.Length;
562 op._memberNames = objectMap._memberNames;
563 op._memberTypes = objectMap._memberTypes;
565 if (objectProgress == null || objectProgress._isInitial)
566 {
567 op._name = record._name;
570 }
571 else
572 {
576 switch (objectProgress._objectTypeEnum)
577 {
578 case InternalObjectTypeE.Object:
579 pr._name = objectProgress._name;
582 break;
583 case InternalObjectTypeE.Array:
586 break;
587 default:
589 }
590 }
592 pr._objectInfo = objectMap.CreateObjectInfo(ref pr._si, ref pr._memberData);
594 if (pr._objectId == _topId)
595 {
597 }
598 pr._keyDt = record._name;
599 pr._dtType = objectMap._objectType;
602 }
603
604 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
606 {
607 if (_objectString == null)
608 {
610 }
611 if (binaryHeaderEnum == BinaryHeaderEnum.ObjectString)
612 {
613 _objectString.Read(this);
614 }
615 else
616 {
617 if (_crossAppDomainString == null)
618 {
620 }
623 if (_objectString._value == null)
624 {
626 }
628 }
629 PRs.Init();
632 if (PRs._objectId == _topId)
633 {
635 }
639 PRs._keyDt = "System.String";
643 if (objectProgress == null)
644 {
646 PRs._name = "System.String";
647 }
648 else
649 {
652 switch (objectProgress._objectTypeEnum)
653 {
654 case InternalObjectTypeE.Object:
657 break;
658 case InternalObjectTypeE.Array:
660 break;
661 default:
663 }
664 }
666 }
667
668 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
707
708 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
710 {
713 binaryArray.Read(this);
714 if (binaryArray._binaryTypeEnum == BinaryTypeEnum.ObjectUser)
715 {
716 if (binaryArray._assemId < 1)
717 {
719 }
721 }
722 else
723 {
725 }
727 ParseRecord pr = op._pr;
729 op._binaryTypeEnum = binaryArray._binaryTypeEnum;
730 op._typeInformation = binaryArray._typeInformation;
732 if (objectProgress == null || binaryArray._objectId > 0)
733 {
734 op._name = "System.Array";
737 }
738 else
739 {
743 switch (objectProgress._objectTypeEnum)
744 {
745 case InternalObjectTypeE.Object:
746 pr._name = objectProgress._name;
750 pr._dtType = objectProgress._dtType;
751 break;
752 case InternalObjectTypeE.Array:
755 break;
756 default:
758 }
759 }
761 if (pr._objectId == _topId)
762 {
764 }
765 else if (_headerId > 0 && pr._objectId == _headerId)
766 {
768 }
769 else
770 {
772 }
774 BinaryTypeConverter.TypeFromInfo(binaryArray._binaryTypeEnum, binaryArray._typeInformation, _objectReader, binaryAssemblyInfo, out pr._arrayElementTypeCode, out pr._arrayElementTypeString, out pr._arrayElementType, out pr._isArrayVariant);
776 pr._rank = binaryArray._rank;
777 pr._lengthA = binaryArray._lengthA;
778 pr._lowerBoundA = binaryArray._lowerBoundA;
779 bool flag = false;
780 switch (binaryArray._binaryArrayTypeEnum)
781 {
782 case BinaryArrayTypeEnum.Single:
783 case BinaryArrayTypeEnum.SingleOffset:
784 op._numItems = binaryArray._lengthA[0];
786 if (Converter.IsWriteAsByteArray(pr._arrayElementTypeCode) && binaryArray._lowerBoundA[0] == 0)
787 {
788 flag = true;
790 }
791 break;
792 case BinaryArrayTypeEnum.Jagged:
793 case BinaryArrayTypeEnum.JaggedOffset:
794 op._numItems = binaryArray._lengthA[0];
796 break;
797 case BinaryArrayTypeEnum.Rectangular:
798 case BinaryArrayTypeEnum.RectangularOffset:
799 {
800 int num = 1;
801 for (int i = 0; i < binaryArray._rank; i++)
802 {
803 num *= binaryArray._lengthA[i];
804 }
805 op._numItems = num;
807 break;
808 }
809 default:
810 throw new SerializationException(System.SR.Format(System.SR.Serialization_ArrayType, binaryArray._binaryArrayTypeEnum.ToString()));
811 }
812 if (!flag)
813 {
814 _stack.Push(op);
815 }
816 else
817 {
818 PutOp(op);
819 }
821 if (flag)
822 {
825 }
826 }
827
829 {
830 if (pr._arrayElementTypeCode == InternalPrimitiveTypeE.Byte)
831 {
832 pr._newObj = ReadBytes(pr._lengthA[0]);
833 return;
834 }
835 if (pr._arrayElementTypeCode == InternalPrimitiveTypeE.Char)
836 {
837 pr._newObj = ReadChars(pr._lengthA[0]);
838 return;
839 }
840 int num = Converter.TypeLength(pr._arrayElementTypeCode);
841 pr._newObj = Converter.CreatePrimitiveArray(pr._arrayElementTypeCode, pr._lengthA[0]);
842 Array array = (Array)pr._newObj;
843 int i = 0;
844 if (_byteBuffer == null)
845 {
846 _byteBuffer = new byte[4096];
847 }
848 int num2;
849 for (; i < array.Length; i += num2)
850 {
851 num2 = Math.Min(4096 / num, array.Length - i);
852 int num3 = num2 * num;
855 {
856 for (int j = 0; j < num3; j += num)
857 {
858 for (int k = 0; k < num / 2; k++)
859 {
860 byte b = _byteBuffer[j + k];
861 _byteBuffer[j + k] = _byteBuffer[j + num - 1 - k];
862 _byteBuffer[j + num - 1 - k] = b;
863 }
864 }
865 }
866 Buffer.BlockCopy(_byteBuffer, 0, array, i * num, num3);
867 }
868 }
869
870 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
897
898 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
923
924 [RequiresUnreferencedCode("ObjectReader requires unreferenced code")]
950
951 private void ReadMessageEnd()
952 {
953 if (_messageEnd == null)
954 {
955 _messageEnd = new MessageEnd();
956 }
957 _messageEnd.Read(this);
958 if (!_stack.IsEmpty())
959 {
961 }
962 }
963
986
988 {
990 if (_opPool != null && !_opPool.IsEmpty())
991 {
993 objectProgress.Init();
994 }
995 else
996 {
998 }
999 return objectProgress;
1000 }
1001
1003 {
1004 if (_opPool == null)
1005 {
1006 _opPool = new SerStack("opPool");
1007 }
1008 _opPool.Push(op);
1009 }
1010}
static readonly bool IsLittleEndian
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
static CultureInfo InvariantCulture
virtual bool ReadBoolean()
virtual double ReadDouble()
virtual byte ReadByte()
virtual string ReadString()
virtual ulong ReadUInt64()
virtual float ReadSingle()
virtual ushort ReadUInt16()
virtual char[] ReadChars(int count)
virtual int ReadInt32()
virtual short ReadInt16()
virtual byte[] ReadBytes(int count)
virtual long ReadInt64()
virtual uint ReadUInt32()
virtual char ReadChar()
static byte Min(byte val1, byte val2)
Definition Math.cs:912
void ReadObjectWithMapTyped(BinaryHeaderEnum binaryHeaderEnum)
void ReadBytes(byte[] byteA, int offset, int size)
void ReadObjectString(BinaryHeaderEnum binaryHeaderEnum)
void ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
BinaryParser(Stream stream, ObjectReader objectReader)
void ReadObjectWithMap(BinaryHeaderEnum binaryHeaderEnum)
void ReadAssembly(BinaryHeaderEnum binaryHeaderEnum)
void ReadObjectNull(BinaryHeaderEnum binaryHeaderEnum)
void ReadArray(BinaryHeaderEnum binaryHeaderEnum)
static void TypeFromInfo(BinaryTypeEnum binaryTypeEnum, object typeInformation, ObjectReader objectReader, BinaryAssemblyInfo assemblyInfo, out InternalPrimitiveTypeE primitiveTypeEnum, out string typeString, out Type type, out bool isVariant)
static Array CreatePrimitiveArray(InternalPrimitiveTypeE code, int length)
Definition Converter.cs:208
static int TypeLength(InternalPrimitiveTypeE code)
Definition Converter.cs:152
static bool IsWriteAsByteArray(InternalPrimitiveTypeE code)
Definition Converter.cs:130
static string ToComType(InternalPrimitiveTypeE code)
Definition Converter.cs:298
static Type ToType(InternalPrimitiveTypeE code)
Definition Converter.cs:199
void Set(InternalPrimitiveTypeE typeInformation, object value)
static ObjectMap Create(string name, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objectType, string[] memberNames, ObjectReader objectReader, int objectId, BinaryAssemblyInfo assemblyInfo)
Definition ObjectMap.cs:91
void Read(BinaryParser input, BinaryHeaderEnum binaryHeaderEnum)
Definition ObjectNull.cs:34
Type GetType(BinaryAssemblyInfo assemblyInfo, string name)
static string Serialization_BinaryHeader
Definition SR.cs:78
static string Serialization_TypeExpected
Definition SR.cs:80
static string Serialization_CrossAppDomainError
Definition SR.cs:84
static string Serialization_ArrayType
Definition SR.cs:94
static string Serialization_TypeCode
Definition SR.cs:96
static string IO_EOF_ReadBeyondEOF
Definition SR.cs:56
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Serialization_Map
Definition SR.cs:86
static string Serialization_StreamEnd
Definition SR.cs:82
static string Serialization_ObjectTypeEnum
Definition SR.cs:90
static string Serialization_AssemblyId
Definition SR.cs:92
static string Serialization_Assembly
Definition SR.cs:88
Definition SR.cs:7