StructReader.cs 887 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using Ryujinx.Cpu;
  2. using Ryujinx.Memory;
  3. using System;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.HLE.Utilities
  6. {
  7. class StructReader
  8. {
  9. private IVirtualMemoryManager _memory;
  10. public ulong Position { get; private set; }
  11. public StructReader(IVirtualMemoryManager memory, ulong position)
  12. {
  13. _memory = memory;
  14. Position = position;
  15. }
  16. public T Read<T>() where T : unmanaged
  17. {
  18. T value = MemoryHelper.Read<T>(_memory, Position);
  19. Position += (uint)Marshal.SizeOf<T>();
  20. return value;
  21. }
  22. public ReadOnlySpan<T> Read<T>(int size) where T : unmanaged
  23. {
  24. ReadOnlySpan<byte> data = _memory.GetSpan(Position, size);
  25. Position += (uint)size;
  26. return MemoryMarshal.Cast<byte, T>(data);
  27. }
  28. }
  29. }