From 08f761662bd18a2fe8bdfff6b496fd50f9dcabad Mon Sep 17 00:00:00 2001 From: gdkchan Date: Thu, 15 Mar 2018 21:06:24 -0300 Subject: [PATCH] Improvements to audout (#58) * Some audout refactoring and improvements * More audio improvements * Change ReadAsciiString to use long for the Size, avoids some casting --- Memory/AMemoryHelper.cs | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/Memory/AMemoryHelper.cs b/Memory/AMemoryHelper.cs index 219aeeb..1e34629 100644 --- a/Memory/AMemoryHelper.cs +++ b/Memory/AMemoryHelper.cs @@ -1,4 +1,6 @@ +using System; using System.IO; +using System.Runtime.InteropServices; using System.Text; namespace ChocolArm64.Memory @@ -20,11 +22,11 @@ namespace ChocolArm64.Memory } } - public static byte[] ReadBytes(AMemory Memory, long Position, int Size) + public static byte[] ReadBytes(AMemory Memory, long Position, long Size) { byte[] Data = new byte[Size]; - for (int Offs = 0; Offs < Size; Offs++) + for (long Offs = 0; Offs < Size; Offs++) { Data[Offs] = (byte)Memory.ReadByte(Position + Offs); } @@ -40,11 +42,39 @@ namespace ChocolArm64.Memory } } - public static string ReadAsciiString(AMemory Memory, long Position, int MaxSize = -1) + public unsafe static T Read(AMemory Memory, long Position) where T : struct + { + long Size = Marshal.SizeOf(); + + if ((ulong)(Position + Size) > AMemoryMgr.AddrSize) + { + throw new ArgumentOutOfRangeException(nameof(Position)); + } + + IntPtr Ptr = new IntPtr((byte*)Memory.Ram + Position); + + return Marshal.PtrToStructure(Ptr); + } + + public unsafe static void Write(AMemory Memory, long Position, T Value) where T : struct + { + long Size = Marshal.SizeOf(); + + if ((ulong)(Position + Size) > AMemoryMgr.AddrSize) + { + throw new ArgumentOutOfRangeException(nameof(Position)); + } + + IntPtr Ptr = new IntPtr((byte*)Memory.Ram + Position); + + Marshal.StructureToPtr(Value, Ptr, false); + } + + public static string ReadAsciiString(AMemory Memory, long Position, long MaxSize = -1) { using (MemoryStream MS = new MemoryStream()) { - for (int Offs = 0; Offs < MaxSize || MaxSize == -1; Offs++) + for (long Offs = 0; Offs < MaxSize || MaxSize == -1; Offs++) { byte Value = (byte)Memory.ReadByte(Position + Offs);