Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BlobWriter.cs
Go to the documentation of this file.
2using System.IO;
5
7
8public struct BlobWriter
9{
10 private readonly byte[] _buffer;
11
12 private readonly int _start;
13
14 private readonly int _end;
15
16 private int _position;
17
18 internal bool IsDefault => _buffer == null;
19
20 public int Offset
21 {
22 get
23 {
24 return _position - _start;
25 }
26 set
27 {
28 if (value < 0 || _start > _end - value)
29 {
31 }
33 }
34 }
35
36 public int Length => _end - _start;
37
38 public int RemainingBytes => _end - _position;
39
40 public Blob Blob => new Blob(_buffer, _start, Length);
41
42 public BlobWriter(int size)
43 : this(new byte[size])
44 {
45 }
46
47 public BlobWriter(byte[] buffer)
48 : this(buffer, 0, buffer.Length)
49 {
50 }
51
52 public BlobWriter(Blob blob)
53 : this(blob.Buffer, blob.Start, blob.Length)
54 {
55 }
56
57 public BlobWriter(byte[] buffer, int start, int count)
58 {
60 _start = start;
62 _end = start + count;
63 }
64
66 {
67 if (Length == other.Length)
68 {
69 return ByteSequenceComparer.Equals(_buffer, _start, other._buffer, other._start, Length);
70 }
71 return false;
72 }
73
74 public byte[] ToArray()
75 {
76 return ToArray(0, Offset);
77 }
78
79 public byte[] ToArray(int start, int byteCount)
80 {
82 byte[] array = new byte[byteCount];
84 return array;
85 }
86
91
97
98 private int Advance(int value)
99 {
100 int position = _position;
101 if (position > _end - value)
102 {
104 }
105 _position = position + value;
106 return position;
107 }
108
109 public unsafe void WriteBytes(byte value, int byteCount)
110 {
111 if (byteCount < 0)
112 {
113 Throw.ArgumentOutOfRange("byteCount");
114 }
115 int num = Advance(byteCount);
116 fixed (byte* ptr = _buffer)
117 {
118 byte* ptr2 = ptr + num;
119 for (int i = 0; i < byteCount; i++)
120 {
121 ptr2[i] = value;
122 }
123 }
124 }
125
126 public unsafe void WriteBytes(byte* buffer, int byteCount)
127 {
128 if (buffer == null)
129 {
130 Throw.ArgumentNull("buffer");
131 }
132 if (byteCount < 0)
133 {
134 Throw.ArgumentOutOfRange("byteCount");
135 }
137 }
138
139 private unsafe void WriteBytesUnchecked(byte* buffer, int byteCount)
140 {
143 }
144
146 {
147 if (source == null)
148 {
149 Throw.ArgumentNull("source");
150 }
151 source.WriteContentTo(ref this);
152 }
153
155 {
156 if (source == null)
157 {
158 Throw.ArgumentNull("source");
159 }
160 if (byteCount < 0)
161 {
162 Throw.ArgumentOutOfRange("byteCount");
163 }
164 int num = Advance(byteCount);
165 int num2 = source.TryReadAll(_buffer, num, byteCount);
166 _position = num + num2;
167 return num2;
168 }
169
171 {
172 WriteBytes(buffer, 0, (!buffer.IsDefault) ? buffer.Length : 0);
173 }
174
179
180 public void WriteBytes(byte[] buffer)
181 {
182 WriteBytes(buffer, 0, (buffer != null) ? buffer.Length : 0);
183 }
184
185 public unsafe void WriteBytes(byte[] buffer, int start, int byteCount)
186 {
187 if (buffer == null)
188 {
189 Throw.ArgumentNull("buffer");
190 }
191 BlobUtilities.ValidateRange(buffer.Length, start, byteCount, "byteCount");
192 if (buffer.Length != 0)
193 {
194 fixed (byte* ptr = &buffer[0])
195 {
196 WriteBytes(ptr + start, byteCount);
197 }
198 }
199 }
200
201 public void PadTo(int offset)
202 {
204 }
205
206 public void Align(int alignment)
207 {
208 int offset = Offset;
209 WriteBytes(0, BitArithmetic.Align(offset, alignment) - offset);
210 }
211
212 public void WriteBoolean(bool value)
213 {
214 WriteByte((byte)(value ? 1u : 0u));
215 }
216
217 public void WriteByte(byte value)
218 {
219 int num = Advance(1);
220 _buffer[num] = value;
221 }
222
223 public void WriteSByte(sbyte value)
224 {
225 WriteByte((byte)value);
226 }
227
228 public void WriteDouble(double value)
229 {
230 int start = Advance(8);
231 _buffer.WriteDouble(start, value);
232 }
233
234 public void WriteSingle(float value)
235 {
236 int start = Advance(4);
237 _buffer.WriteSingle(start, value);
238 }
239
240 public void WriteInt16(short value)
241 {
242 WriteUInt16((ushort)value);
243 }
244
245 public void WriteUInt16(ushort value)
246 {
247 int start = Advance(2);
248 _buffer.WriteUInt16(start, value);
249 }
250
251 public void WriteInt16BE(short value)
252 {
253 WriteUInt16BE((ushort)value);
254 }
255
256 public void WriteUInt16BE(ushort value)
257 {
258 int start = Advance(2);
259 _buffer.WriteUInt16BE(start, value);
260 }
261
262 public void WriteInt32BE(int value)
263 {
264 WriteUInt32BE((uint)value);
265 }
266
267 public void WriteUInt32BE(uint value)
268 {
269 int start = Advance(4);
270 _buffer.WriteUInt32BE(start, value);
271 }
272
273 public void WriteInt32(int value)
274 {
275 WriteUInt32((uint)value);
276 }
277
278 public void WriteUInt32(uint value)
279 {
280 int start = Advance(4);
281 _buffer.WriteUInt32(start, value);
282 }
283
284 public void WriteInt64(long value)
285 {
286 WriteUInt64((ulong)value);
287 }
288
289 public void WriteUInt64(ulong value)
290 {
291 int start = Advance(8);
292 _buffer.WriteUInt64(start, value);
293 }
294
295 public void WriteDecimal(decimal value)
296 {
297 int start = Advance(13);
298 _buffer.WriteDecimal(start, value);
299 }
300
301 public void WriteGuid(Guid value)
302 {
303 int start = Advance(16);
304 _buffer.WriteGuid(start, value);
305 }
306
308 {
309 WriteInt64(value.Ticks);
310 }
311
312 public void WriteReference(int reference, bool isSmall)
313 {
314 if (isSmall)
315 {
316 WriteUInt16((ushort)reference);
317 }
318 else
319 {
320 WriteInt32(reference);
321 }
322 }
323
324 public unsafe void WriteUTF16(char[] value)
325 {
326 if (value == null)
327 {
328 Throw.ArgumentNull("value");
329 }
330 if (value.Length == 0)
331 {
332 return;
333 }
335 {
336 fixed (char* buffer = &value[0])
337 {
338 WriteBytesUnchecked((byte*)buffer, value.Length * 2);
339 }
340 return;
341 }
342 for (int i = 0; i < value.Length; i++)
343 {
344 WriteUInt16(value[i]);
345 }
346 }
347
348 public unsafe void WriteUTF16(string value)
349 {
350 if (value == null)
351 {
352 Throw.ArgumentNull("value");
353 }
355 {
356 fixed (char* buffer = value)
357 {
358 WriteBytesUnchecked((byte*)buffer, value.Length * 2);
359 }
360 return;
361 }
362 for (int i = 0; i < value.Length; i++)
363 {
364 WriteUInt16(value[i]);
365 }
366 }
367
368 public void WriteSerializedString(string? str)
369 {
370 if (str == null)
371 {
372 WriteByte(byte.MaxValue);
373 }
374 else
375 {
376 WriteUTF8(str, 0, str.Length, allowUnpairedSurrogates: true, prependSize: true);
377 }
378 }
379
380 public void WriteUserString(string value)
381 {
382 if (value == null)
383 {
384 throw new ArgumentNullException("value");
385 }
389 }
390
391 public void WriteUTF8(string value, bool allowUnpairedSurrogates)
392 {
393 if (value == null)
394 {
395 Throw.ArgumentNull("value");
396 }
397 WriteUTF8(value, 0, value.Length, allowUnpairedSurrogates, prependSize: false);
398 }
399
400 private unsafe void WriteUTF8(string str, int start, int length, bool allowUnpairedSurrogates, bool prependSize)
401 {
402 fixed (char* ptr = str)
403 {
404 char* ptr2 = ptr + start;
405 int uTF8ByteCount = BlobUtilities.GetUTF8ByteCount(ptr2, length);
406 if (prependSize)
407 {
408 WriteCompressedInteger(uTF8ByteCount);
409 }
410 int start2 = Advance(uTF8ByteCount);
411 _buffer.WriteUTF8(start2, ptr2, length, uTF8ByteCount, allowUnpairedSurrogates);
412 }
413 }
414
419
421 {
423 }
424
425 public void WriteConstant(object? value)
426 {
428 }
429
430 public void Clear()
431 {
433 }
434}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
static readonly bool IsLittleEndian
static void ValidateRange(int bufferLength, int start, int byteCount, string byteCountParameterName)
static byte GetUserStringTrailingByte(string str)
static unsafe int GetUTF8ByteCount(string str)
static int GetUserStringByteLength(int characterCount)
static uint Align(uint position, uint alignment)
static bool Equals(ImmutableArray< byte > x, ImmutableArray< byte > y)
static ? byte[] DangerousGetUnderlyingArray(ImmutableArray< byte > array)
static ImmutableArray< byte > DangerousCreateFromUnderlyingArray(ref byte[]? array)
static void WriteCompressedSignedInteger(ref BlobWriter writer, int value)
static void WriteCompressedInteger(ref BlobWriter writer, uint value)
static void WriteConstant(ref BlobWriter writer, object? value)
static void ArgumentNull(string parameterName)
Definition Throw.cs:110
static void ArgumentOutOfRange(string parameterName)
Definition Throw.cs:145
static void OutOfBounds()
Definition Throw.cs:187
static void ValueArgumentOutOfRange()
Definition Throw.cs:180
static void Copy(int[] source, int startIndex, IntPtr destination, int length)
Definition Marshal.cs:800
unsafe void WriteBytesUnchecked(byte *buffer, int byteCount)
bool ContentEquals(BlobWriter other)
Definition BlobWriter.cs:65
void WriteBytes(ImmutableArray< byte > buffer, int start, int byteCount)
unsafe void WriteUTF16(char[] value)
int WriteBytes(Stream source, int byteCount)
ImmutableArray< byte > ToImmutableArray(int start, int byteCount)
Definition BlobWriter.cs:92
void WriteBytes(ImmutableArray< byte > buffer)
void WriteBytes(BlobBuilder source)
unsafe void WriteUTF16(string value)
unsafe void WriteBytes(byte *buffer, int byteCount)
void WriteReference(int reference, bool isSmall)
unsafe void WriteBytes(byte[] buffer, int start, int byteCount)
void WriteUTF8(string value, bool allowUnpairedSurrogates)
byte[] ToArray(int start, int byteCount)
Definition BlobWriter.cs:79
BlobWriter(byte[] buffer, int start, int count)
Definition BlobWriter.cs:57
unsafe void WriteUTF8(string str, int start, int length, bool allowUnpairedSurrogates, bool prependSize)
ImmutableArray< byte > ToImmutableArray()
Definition BlobWriter.cs:87
unsafe void WriteBytes(byte value, int byteCount)