Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UnmanagedMemoryAccessor.cs
Go to the documentation of this file.
3
4namespace System.IO;
5
7{
9
10 private long _offset;
11
12 private long _capacity;
13
15
16 private bool _isOpen;
17
18 private bool _canRead;
19
20 private bool _canWrite;
21
22 public long Capacity => _capacity;
23
24 public bool CanRead
25 {
26 get
27 {
28 if (_isOpen)
29 {
30 return _canRead;
31 }
32 return false;
33 }
34 }
35
36 public bool CanWrite
37 {
38 get
39 {
40 if (_isOpen)
41 {
42 return _canWrite;
43 }
44 return false;
45 }
46 }
47
48 protected bool IsOpen => _isOpen;
49
51 {
52 }
53
58
60 {
62 }
63
64 protected unsafe void Initialize(SafeBuffer buffer, long offset, long capacity, FileAccess access)
65 {
66 if (buffer == null)
67 {
68 throw new ArgumentNullException("buffer");
69 }
70 if (offset < 0)
71 {
73 }
74 if (capacity < 0)
75 {
77 }
78 if (buffer.ByteLength < (ulong)(offset + capacity))
79 {
81 }
82 if (access < FileAccess.Read || access > FileAccess.ReadWrite)
83 {
84 throw new ArgumentOutOfRangeException("access");
85 }
86 if (_isOpen)
87 {
89 }
90 byte* pointer = null;
91 try
92 {
93 buffer.AcquirePointer(ref pointer);
94 if ((nuint)((long)pointer + offset + capacity) < (nuint)pointer)
95 {
97 }
98 }
99 finally
100 {
101 if (pointer != null)
102 {
103 buffer.ReleasePointer();
104 }
105 }
106 _offset = offset;
107 _buffer = buffer;
109 _access = access;
110 _isOpen = true;
111 _canRead = (_access & FileAccess.Read) != 0;
112 _canWrite = (_access & FileAccess.Write) != 0;
113 }
114
115 protected virtual void Dispose(bool disposing)
116 {
117 _isOpen = false;
118 }
119
120 public void Dispose()
121 {
122 Dispose(disposing: true);
123 GC.SuppressFinalize(this);
124 }
125
126 public bool ReadBoolean(long position)
127 {
128 return ReadByte(position) != 0;
129 }
130
131 public unsafe byte ReadByte(long position)
132 {
133 EnsureSafeToRead(position, 1);
134 byte* pointer = null;
135 try
136 {
138 return (pointer + _offset)[position];
139 }
140 finally
141 {
142 if (pointer != null)
143 {
145 }
146 }
147 }
148
149 public char ReadChar(long position)
150 {
151 return (char)ReadInt16(position);
152 }
153
154 public unsafe short ReadInt16(long position)
155 {
156 EnsureSafeToRead(position, 2);
157 byte* pointer = null;
158 try
159 {
161 return Unsafe.ReadUnaligned<short>(pointer + _offset + position);
162 }
163 finally
164 {
165 if (pointer != null)
166 {
168 }
169 }
170 }
171
172 public unsafe int ReadInt32(long position)
173 {
174 EnsureSafeToRead(position, 4);
175 byte* pointer = null;
176 try
177 {
179 return Unsafe.ReadUnaligned<int>(pointer + _offset + position);
180 }
181 finally
182 {
183 if (pointer != null)
184 {
186 }
187 }
188 }
189
190 public unsafe long ReadInt64(long position)
191 {
192 EnsureSafeToRead(position, 8);
193 byte* pointer = null;
194 try
195 {
197 return Unsafe.ReadUnaligned<long>(pointer + _offset + position);
198 }
199 finally
200 {
201 if (pointer != null)
202 {
204 }
205 }
206 }
207
208 public unsafe decimal ReadDecimal(long position)
209 {
210 EnsureSafeToRead(position, 16);
211 byte* pointer = null;
212 int lo;
213 int mid;
214 int hi;
215 int num;
216 try
217 {
219 pointer += _offset + position;
220 lo = Unsafe.ReadUnaligned<int>(pointer);
221 mid = Unsafe.ReadUnaligned<int>(pointer + 4);
222 hi = Unsafe.ReadUnaligned<int>(pointer + 8);
223 num = Unsafe.ReadUnaligned<int>(pointer + 12);
224 }
225 finally
226 {
227 if (pointer != null)
228 {
230 }
231 }
232 if (((uint)num & 0x7F00FFFFu) != 0 || (num & 0xFF0000) > 1835008)
233 {
235 }
236 bool isNegative = (num & int.MinValue) != 0;
237 byte scale = (byte)(num >> 16);
238 return new decimal(lo, mid, hi, isNegative, scale);
239 }
240
241 public float ReadSingle(long position)
242 {
243 return BitConverter.Int32BitsToSingle(ReadInt32(position));
244 }
245
246 public double ReadDouble(long position)
247 {
248 return BitConverter.Int64BitsToDouble(ReadInt64(position));
249 }
250
251 [CLSCompliant(false)]
252 public sbyte ReadSByte(long position)
253 {
254 return (sbyte)ReadByte(position);
255 }
256
257 [CLSCompliant(false)]
258 public ushort ReadUInt16(long position)
259 {
260 return (ushort)ReadInt16(position);
261 }
262
263 [CLSCompliant(false)]
264 public uint ReadUInt32(long position)
265 {
266 return (uint)ReadInt32(position);
267 }
268
269 [CLSCompliant(false)]
270 public ulong ReadUInt64(long position)
271 {
272 return (ulong)ReadInt64(position);
273 }
274
275 public void Read<T>(long position, out T structure) where T : struct
276 {
277 if (position < 0)
278 {
280 }
281 if (!_isOpen)
282 {
283 throw new ObjectDisposedException("UnmanagedMemoryAccessor", SR.ObjectDisposed_ViewAccessorClosed);
284 }
285 if (!_canRead)
286 {
288 }
289 uint num = SafeBuffer.SizeOf<T>();
290 if (position > _capacity - num)
291 {
292 if (position >= _capacity)
293 {
295 }
296 throw new ArgumentException(SR.Format(SR.Argument_NotEnoughBytesToRead, typeof(T)), "position");
297 }
298 structure = _buffer.Read<T>((ulong)(_offset + position));
299 }
300
301 public int ReadArray<T>(long position, T[] array, int offset, int count) where T : struct
302 {
303 if (array == null)
304 {
305 throw new ArgumentNullException("array", SR.ArgumentNull_Buffer);
306 }
307 if (offset < 0)
308 {
310 }
311 if (count < 0)
312 {
314 }
315 if (array.Length - offset < count)
316 {
318 }
319 if (!_isOpen)
320 {
321 throw new ObjectDisposedException("UnmanagedMemoryAccessor", SR.ObjectDisposed_ViewAccessorClosed);
322 }
323 if (!_canRead)
324 {
326 }
327 if (position < 0)
328 {
330 }
331 uint num = SafeBuffer.AlignedSizeOf<T>();
332 if (position >= _capacity)
333 {
335 }
336 int num2 = count;
337 long num3 = _capacity - position;
338 if (num3 < 0)
339 {
340 num2 = 0;
341 }
342 else
343 {
344 ulong num4 = (ulong)(num * count);
345 if ((ulong)num3 < num4)
346 {
347 num2 = (int)(num3 / num);
348 }
349 }
350 _buffer.ReadArray((ulong)(_offset + position), array, offset, num2);
351 return num2;
352 }
353
354 public void Write(long position, bool value)
355 {
356 Write(position, (byte)(value ? 1u : 0u));
357 }
358
359 public unsafe void Write(long position, byte value)
360 {
361 EnsureSafeToWrite(position, 1);
362 byte* pointer = null;
363 try
364 {
366 (pointer + _offset)[position] = value;
367 }
368 finally
369 {
370 if (pointer != null)
371 {
373 }
374 }
375 }
376
377 public void Write(long position, char value)
378 {
379 Write(position, (short)value);
380 }
381
382 public unsafe void Write(long position, short value)
383 {
384 EnsureSafeToWrite(position, 2);
385 byte* pointer = null;
386 try
387 {
389 Unsafe.WriteUnaligned(pointer + _offset + position, value);
390 }
391 finally
392 {
393 if (pointer != null)
394 {
396 }
397 }
398 }
399
400 public unsafe void Write(long position, int value)
401 {
402 EnsureSafeToWrite(position, 4);
403 byte* pointer = null;
404 try
405 {
407 Unsafe.WriteUnaligned(pointer + _offset + position, value);
408 }
409 finally
410 {
411 if (pointer != null)
412 {
414 }
415 }
416 }
417
418 public unsafe void Write(long position, long value)
419 {
420 EnsureSafeToWrite(position, 8);
421 byte* pointer = null;
422 try
423 {
425 Unsafe.WriteUnaligned(pointer + _offset + position, value);
426 }
427 finally
428 {
429 if (pointer != null)
430 {
432 }
433 }
434 }
435
436 public unsafe void Write(long position, decimal value)
437 {
438 EnsureSafeToWrite(position, 16);
439 Span<int> destination = stackalloc int[4];
440 decimal.TryGetBits(value, destination, out var _);
441 byte* pointer = null;
442 try
443 {
445 pointer += _offset + position;
446 Unsafe.WriteUnaligned(pointer, destination[0]);
447 Unsafe.WriteUnaligned(pointer + 4, destination[1]);
448 Unsafe.WriteUnaligned(pointer + 8, destination[2]);
449 Unsafe.WriteUnaligned(pointer + 12, destination[3]);
450 }
451 finally
452 {
453 if (pointer != null)
454 {
456 }
457 }
458 }
459
460 public void Write(long position, float value)
461 {
463 }
464
465 public void Write(long position, double value)
466 {
468 }
469
470 [CLSCompliant(false)]
471 public void Write(long position, sbyte value)
472 {
473 Write(position, (byte)value);
474 }
475
476 [CLSCompliant(false)]
477 public void Write(long position, ushort value)
478 {
479 Write(position, (short)value);
480 }
481
482 [CLSCompliant(false)]
483 public void Write(long position, uint value)
484 {
485 Write(position, (int)value);
486 }
487
488 [CLSCompliant(false)]
489 public void Write(long position, ulong value)
490 {
491 Write(position, (long)value);
492 }
493
494 public void Write<T>(long position, ref T structure) where T : struct
495 {
496 if (position < 0)
497 {
499 }
500 if (!_isOpen)
501 {
502 throw new ObjectDisposedException("UnmanagedMemoryAccessor", SR.ObjectDisposed_ViewAccessorClosed);
503 }
504 if (!_canWrite)
505 {
507 }
508 uint num = SafeBuffer.SizeOf<T>();
509 if (position > _capacity - num)
510 {
511 if (position >= _capacity)
512 {
514 }
515 throw new ArgumentException(SR.Format(SR.Argument_NotEnoughBytesToWrite, typeof(T)), "position");
516 }
517 _buffer.Write((ulong)(_offset + position), structure);
518 }
519
520 public void WriteArray<T>(long position, T[] array, int offset, int count) where T : struct
521 {
522 if (array == null)
523 {
524 throw new ArgumentNullException("array", SR.ArgumentNull_Buffer);
525 }
526 if (offset < 0)
527 {
529 }
530 if (count < 0)
531 {
533 }
534 if (array.Length - offset < count)
535 {
537 }
538 if (position < 0)
539 {
541 }
542 if (position >= Capacity)
543 {
545 }
546 if (!_isOpen)
547 {
548 throw new ObjectDisposedException("UnmanagedMemoryAccessor", SR.ObjectDisposed_ViewAccessorClosed);
549 }
550 if (!_canWrite)
551 {
553 }
554 _buffer.WriteArray((ulong)(_offset + position), array, offset, count);
555 }
556
557 private void EnsureSafeToRead(long position, int sizeOfType)
558 {
559 if (!_isOpen)
560 {
561 throw new ObjectDisposedException("UnmanagedMemoryAccessor", SR.ObjectDisposed_ViewAccessorClosed);
562 }
563 if (!_canRead)
564 {
566 }
567 if (position < 0)
568 {
570 }
571 if (position > _capacity - sizeOfType)
572 {
573 if (position >= _capacity)
574 {
576 }
578 }
579 }
580
581 private void EnsureSafeToWrite(long position, int sizeOfType)
582 {
583 if (!_isOpen)
584 {
585 throw new ObjectDisposedException("UnmanagedMemoryAccessor", SR.ObjectDisposed_ViewAccessorClosed);
586 }
587 if (!_canWrite)
588 {
590 }
591 if (position < 0)
592 {
594 }
595 if (position > _capacity - sizeOfType)
596 {
597 if (position >= _capacity)
598 {
600 }
602 }
603 }
604}
static unsafe float Int32BitsToSingle(int value)
static unsafe int SingleToInt32Bits(float value)
static unsafe double Int64BitsToDouble(long value)
static unsafe long DoubleToInt64Bits(double value)
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
void Write(long position, bool value)
unsafe void Write(long position, long value)
void EnsureSafeToWrite(long position, int sizeOfType)
void WriteArray< T >(long position, T[] array, int offset, int count)
void Write(long position, char value)
void Write(long position, double value)
void EnsureSafeToRead(long position, int sizeOfType)
UnmanagedMemoryAccessor(SafeBuffer buffer, long offset, long capacity, FileAccess access)
void Write(long position, sbyte value)
UnmanagedMemoryAccessor(SafeBuffer buffer, long offset, long capacity)
unsafe void Write(long position, byte value)
unsafe void Write(long position, int value)
void Read< T >(long position, out T structure)
unsafe void Initialize(SafeBuffer buffer, long offset, long capacity, FileAccess access)
void Write(long position, ushort value)
unsafe decimal ReadDecimal(long position)
void Write(long position, uint value)
unsafe void Write(long position, short value)
void Write(long position, ulong value)
void Write(long position, float value)
int ReadArray< T >(long position, T[] array, int offset, int count)
void Write< T >(long position, ref T structure)
unsafe void Write(long position, decimal value)
unsafe void AcquirePointer(ref byte *pointer)
Definition SafeBuffer.cs:58
static string ArgumentOutOfRange_PositionLessThanCapacityRequired
Definition SR.cs:1094
static string InvalidOperation_CalledTwice
Definition SR.cs:1412
static string NotSupported_Reading
Definition SR.cs:1706
static string Argument_UnmanagedMemAccessorWrapAround
Definition SR.cs:906
static string NotSupported_Writing
Definition SR.cs:1736
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_NotEnoughBytesToRead
Definition SR.cs:806
static string Arg_BadDecimal
Definition SR.cs:80
static string Argument_NotEnoughBytesToWrite
Definition SR.cs:808
static string ArgumentNull_Buffer
Definition SR.cs:22
static string ObjectDisposed_ViewAccessorClosed
Definition SR.cs:70
static string Argument_InvalidOffLen
Definition SR.cs:22
static string ArgumentOutOfRange_NeedNonNegNum
Definition SR.cs:32
static string Argument_OffsetAndCapacityOutOfBounds
Definition SR.cs:822
Definition SR.cs:7