Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BinaryFormatterWriter.cs
Go to the documentation of this file.
3using System.IO;
5using System.Text;
6
8
9internal sealed class BinaryFormatterWriter
10{
11 private sealed class ObjectMapInfo
12 {
13 internal readonly int _objectId;
14
15 private readonly int _numMembers;
16
17 private readonly string[] _memberNames;
18
19 private readonly Type[] _memberTypes;
20
28
29 internal bool IsCompatible(int numMembers, string[] memberNames, Type[] memberTypes)
30 {
32 {
33 return false;
34 }
35 for (int i = 0; i < numMembers; i++)
36 {
37 if (!_memberNames[i].Equals(memberNames[i]))
38 {
39 return false;
40 }
41 if (memberTypes != null && _memberTypes[i] != memberTypes[i])
42 {
43 return false;
44 }
45 }
46 return true;
47 }
48 }
49
50 private readonly Stream _outputStream;
51
53
54 private readonly ObjectWriter _objectWriter;
55
56 private readonly BinaryWriter _dataWriter;
57
59
61
63
65
67
69
71
72 private byte[] _byteBuffer;
73
75
77
79
81
83
91
92 internal void WriteBegin()
93 {
94 }
95
96 internal void WriteEnd()
97 {
99 }
100
101 internal void WriteBoolean(bool value)
102 {
104 }
105
106 internal void WriteByte(byte value)
107 {
109 }
110
111 private void WriteBytes(byte[] value)
112 {
114 }
115
116 private void WriteBytes(byte[] byteA, int offset, int size)
117 {
119 }
120
121 internal void WriteChar(char value)
122 {
124 }
125
126 internal void WriteChars(char[] value)
127 {
129 }
130
131 internal void WriteDecimal(decimal value)
132 {
134 }
135
136 internal void WriteSingle(float value)
137 {
139 }
140
141 internal void WriteDouble(double value)
142 {
144 }
145
146 internal void WriteInt16(short value)
147 {
149 }
150
151 internal void WriteInt32(int value)
152 {
154 }
155
156 internal void WriteInt64(long value)
157 {
159 }
160
161 internal void WriteSByte(sbyte value)
162 {
163 WriteByte((byte)value);
164 }
165
166 internal void WriteString(string value)
167 {
169 }
170
172 {
173 WriteInt64(value.Ticks);
174 }
175
177 {
178 long value2 = Unsafe.As<DateTime, long>(ref value);
180 }
181
182 internal void WriteUInt16(ushort value)
183 {
185 }
186
187 internal void WriteUInt32(uint value)
188 {
190 }
191
192 internal void WriteUInt64(ulong value)
193 {
195 }
196
200
202 {
204 messageEnd.Write(this);
205 }
206
212
214 {
216 int num = (int)nameInfo._objectId;
217 string text = ((num < 0) ? typeNameInfo.NIname : nameInfo.NIname);
218 if (_objectMapTable == null)
219 {
221 }
223 {
224 if (_binaryObject == null)
225 {
227 }
228 _binaryObject.Set(num, value._objectId);
229 _binaryObject.Write(this);
230 return;
231 }
232 int assemId;
233 if (!typeNameInfo._transmitTypeOnObject)
234 {
235 if (_binaryObjectWithMap == null)
236 {
238 }
239 assemId = (int)typeNameInfo._assemId;
242 if (value == null)
243 {
245 }
246 return;
247 }
249 object[] array2 = new object[numMembers];
250 int[] array3 = new int[numMembers];
251 for (int i = 0; i < numMembers; i++)
252 {
253 object typeInformation = null;
256 array3[i] = assemId;
257 }
258 if (_binaryObjectWithMapTyped == null)
259 {
261 }
262 assemId = (int)typeNameInfo._assemId;
265 if (value == null)
266 {
268 }
269 }
270
271 internal void WriteObjectString(int objectId, string value)
272 {
274 if (_binaryObjectString == null)
275 {
277 }
280 }
281
283 {
285 int[] lengthA = new int[1] { length };
286 int[] lowerBoundA = null;
287 object typeInformation = null;
289 if (lowerBound == 0)
290 {
292 }
293 else
294 {
296 lowerBoundA = new int[1] { lowerBound };
297 }
298 int assemId;
300 if (_binaryArray == null)
301 {
303 }
305 _binaryArray.Write(this);
306 if (Converter.IsWriteAsByteArray(arrayElemTypeNameInfo._primitiveTypeEnum) && lowerBound == 0)
307 {
308 if (arrayElemTypeNameInfo._primitiveTypeEnum == InternalPrimitiveTypeE.Byte)
309 {
310 WriteBytes((byte[])array);
311 }
312 else if (arrayElemTypeNameInfo._primitiveTypeEnum == InternalPrimitiveTypeE.Char)
313 {
314 WriteChars((char[])array);
315 }
316 else
317 {
319 }
320 }
321 }
322
324 {
326 int i = 0;
327 if (_byteBuffer == null)
328 {
329 _byteBuffer = new byte[4096];
330 }
331 int num;
332 for (; i < array.Length; i += num)
333 {
334 num = Math.Min(4096 / typeLength, array.Length - i);
335 int num2 = num * typeLength;
338 {
339 for (int j = 0; j < num2; j += typeLength)
340 {
341 for (int k = 0; k < typeLength / 2; k++)
342 {
343 byte b = _byteBuffer[j + k];
344 _byteBuffer[j + k] = _byteBuffer[j + typeLength - 1 - k];
345 _byteBuffer[j + typeLength - 1 - k] = b;
346 }
347 }
348 }
350 }
351 }
352
378
401
407
431
433 {
435 if (_objectNull == null)
436 {
437 _objectNull = new ObjectNull();
438 }
439 if (!memberNameInfo._isArrayItem)
440 {
442 _objectNull.Write(this);
444 }
445 }
446
448 {
450 if (_memberReference == null)
451 {
453 }
456 }
457
462
468
474
480
481 internal void WriteDelayedNullItem()
482 {
484 }
485
486 internal void WriteItemEnd()
487 {
489 }
490
492 {
494 {
495 if (_objectNull == null)
496 {
497 _objectNull = new ObjectNull();
498 }
500 _objectNull.Write(this);
502 }
503 }
504
510
511 internal void WriteAssembly(Type type, string assemblyString, int assemId, bool isNew)
512 {
514 if (assemblyString == null)
515 {
516 assemblyString = string.Empty;
517 }
518 if (isNew)
519 {
520 if (_binaryAssembly == null)
521 {
523 }
526 }
527 }
528
530 {
531 switch (code)
532 {
533 case InternalPrimitiveTypeE.Boolean:
535 break;
536 case InternalPrimitiveTypeE.Byte:
538 break;
539 case InternalPrimitiveTypeE.Char:
541 break;
542 case InternalPrimitiveTypeE.Double:
544 break;
545 case InternalPrimitiveTypeE.Int16:
547 break;
548 case InternalPrimitiveTypeE.Int32:
550 break;
551 case InternalPrimitiveTypeE.Int64:
553 break;
554 case InternalPrimitiveTypeE.SByte:
556 break;
557 case InternalPrimitiveTypeE.Single:
559 break;
560 case InternalPrimitiveTypeE.UInt16:
562 break;
563 case InternalPrimitiveTypeE.UInt32:
565 break;
566 case InternalPrimitiveTypeE.UInt64:
568 break;
569 case InternalPrimitiveTypeE.Decimal:
571 break;
572 case InternalPrimitiveTypeE.TimeSpan:
574 break;
575 case InternalPrimitiveTypeE.DateTime:
577 break;
578 default:
580 }
581 }
582}
static readonly bool IsLittleEndian
static void BlockCopy(Array src, int srcOffset, Array dst, int dstOffset, int count)
Definition Buffer.cs:102
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
void Add(TKey key, TValue value)
static decimal ToDecimal(object? value)
Definition Convert.cs:2101
static long ToInt64(object? value)
Definition Convert.cs:1623
static float ToSingle(object? value)
Definition Convert.cs:1881
static int ToInt32(object? value)
Definition Convert.cs:1320
static short ToInt16(object? value)
Definition Convert.cs:1038
static byte ToByte(object? value)
Definition Convert.cs:900
static uint ToUInt32(object? value)
Definition Convert.cs:1470
static ulong ToUInt64(object? value)
Definition Convert.cs:1738
static char ToChar(object? value)
Definition Convert.cs:618
static ushort ToUInt16(object? value)
Definition Convert.cs:1177
static sbyte ToSByte(object? value)
Definition Convert.cs:745
static double ToDouble(object? value)
Definition Convert.cs:1991
static bool ToBoolean([NotNullWhen(true)] object? value)
Definition Convert.cs:508
static CultureInfo InvariantCulture
virtual void Write(bool value)
static byte Min(byte val1, byte val2)
Definition Math.cs:912
void Set(int objectId, int rank, int[] lengthA, int[] lowerBoundA, BinaryTypeEnum binaryTypeEnum, object typeInformation, BinaryArrayTypeEnum binaryArrayTypeEnum, int assemId)
ObjectMapInfo(int objectId, int numMembers, string[] memberNames, Type[] memberTypes)
bool IsCompatible(int numMembers, string[] memberNames, Type[] memberTypes)
void WriteMemberString(NameInfo memberNameInfo, NameInfo typeNameInfo, string value)
void WriteSerializationHeader(int topId, int headerId, int minorVersion, int majorVersion)
BinaryFormatterWriter(Stream outputStream, ObjectWriter objectWriter, FormatterTypeStyle formatterTypeStyle)
void WriteItem(NameInfo itemNameInfo, NameInfo typeNameInfo, object value)
void WriteObjectByteArray(NameInfo memberNameInfo, NameInfo arrayNameInfo, WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, int length, int lowerBound, byte[] byteA)
void WriteSingleArray(NameInfo memberNameInfo, NameInfo arrayNameInfo, WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, int length, int lowerBound, Array array)
void WriteNullItem(NameInfo itemNameInfo, NameInfo typeNameInfo)
void WriteRectangleArray(NameInfo memberNameInfo, NameInfo arrayNameInfo, WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, int rank, int[] lengthA, int[] lowerBoundA)
void WriteMember(NameInfo memberNameInfo, NameInfo typeNameInfo, object value)
void WriteAssembly(Type type, string assemblyString, int assemId, bool isNew)
void WriteJaggedArray(NameInfo memberNameInfo, NameInfo arrayNameInfo, WriteObjectInfo objectInfo, NameInfo arrayElemTypeNameInfo, int length, int lowerBound)
void WriteObject(NameInfo nameInfo, NameInfo typeNameInfo, int numMembers, string[] memberNames, Type[] memberTypes, WriteObjectInfo[] memberObjectInfos)
void WriteNullMember(NameInfo memberNameInfo, NameInfo typeNameInfo)
void WriteObjectEnd(NameInfo memberNameInfo, NameInfo typeNameInfo)
void Set(int objectId, string name, int numMembers, string[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, object[] typeInformationA, int[] memberAssemIds, int assemId)
void Set(int objectId, string name, int numMembers, string[] memberNames, int assemId)
static BinaryTypeEnum GetBinaryTypeInfo(Type type, WriteObjectInfo objectInfo, string typeName, ObjectWriter objectWriter, out object typeInformation, out int assemId)
static int TypeLength(InternalPrimitiveTypeE code)
Definition Converter.cs:152
static bool IsWriteAsByteArray(InternalPrimitiveTypeE code)
Definition Converter.cs:130
void Set(InternalPrimitiveTypeE primitiveTypeEnum, object value)
void Set(InternalPrimitiveTypeE typeInformation, object value)
static string Serialization_TypeCode
Definition SR.cs:96
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526