StructReader.cs 1016 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using ARMeilleure.Memory;
  2. using System.Runtime.InteropServices;
  3. namespace Ryujinx.HLE.Utilities
  4. {
  5. class StructReader
  6. {
  7. private IMemoryManager _memory;
  8. public long Position { get; private set; }
  9. public StructReader(IMemoryManager memory, long position)
  10. {
  11. _memory = memory;
  12. Position = position;
  13. }
  14. public T Read<T>() where T : struct
  15. {
  16. T value = MemoryHelper.Read<T>(_memory, Position);
  17. Position += Marshal.SizeOf<T>();
  18. return value;
  19. }
  20. public T[] Read<T>(int size) where T : struct
  21. {
  22. int structSize = Marshal.SizeOf<T>();
  23. int count = size / structSize;
  24. T[] output = new T[count];
  25. for (int index = 0; index < count; index++)
  26. {
  27. output[index] = MemoryHelper.Read<T>(_memory, Position);
  28. Position += structSize;
  29. }
  30. return output;
  31. }
  32. }
  33. }