using System.IO;
using System.Runtime.InteropServices;
namespace FF8
{
class MemoryReader : BinaryReader
{
public long Length => BaseStream.Length;
public long Position => BaseStream.Position;
public MemoryReader(byte[] buffer) : base(new MemoryStream(buffer)) { }
public void Seek(long offset, SeekOrigin origin)
{
BaseStream.Seek(offset, origin);
}
///
/// Allows C/C++ classes and structs to be read directly from a stream
///
///
///
public T Read()
{
var buffer = ReadBytes(Marshal.SizeOf());
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
var result = Marshal.PtrToStructure(handle.AddrOfPinnedObject());
handle.Free();
return result;
}
///
/// Allows an array of C/C++ classes and structs to be read directly from a stream
///
///
///
public T[] Read(int count)
{
T[] array = new T[count];
var size = Marshal.SizeOf();
for (var i = 0; i < count; i++)
{
var buffer = ReadBytes(size);
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
array[i] = Marshal.PtrToStructure(handle.AddrOfPinnedObject());
handle.Free();
}
return array;
}
}
}