diff options
Diffstat (limited to 'Ryujinx.Common/Extensions/BinaryReaderExtensions.cs')
-rw-r--r-- | Ryujinx.Common/Extensions/BinaryReaderExtensions.cs | 43 |
1 files changed, 5 insertions, 38 deletions
diff --git a/Ryujinx.Common/Extensions/BinaryReaderExtensions.cs b/Ryujinx.Common/Extensions/BinaryReaderExtensions.cs index 2c24678d..ea404d2a 100644 --- a/Ryujinx.Common/Extensions/BinaryReaderExtensions.cs +++ b/Ryujinx.Common/Extensions/BinaryReaderExtensions.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Ryujinx.Common @@ -7,49 +8,15 @@ namespace Ryujinx.Common public static class BinaryReaderExtensions { public unsafe static T ReadStruct<T>(this BinaryReader reader) - where T : struct + where T : unmanaged { - int size = Marshal.SizeOf<T>(); - - byte[] data = reader.ReadBytes(size); - - fixed (byte* ptr = data) - { - return Marshal.PtrToStructure<T>((IntPtr)ptr); - } - } - - public unsafe static T[] ReadStructArray<T>(this BinaryReader reader, int count) - where T : struct - { - int size = Marshal.SizeOf<T>(); - - T[] result = new T[count]; - - for (int i = 0; i < count; i++) - { - byte[] data = reader.ReadBytes(size); - - fixed (byte* ptr = data) - { - result[i] = Marshal.PtrToStructure<T>((IntPtr)ptr); - } - } - - return result; + return MemoryMarshal.Cast<byte, T>(reader.ReadBytes(Unsafe.SizeOf<T>()))[0]; } public unsafe static void WriteStruct<T>(this BinaryWriter writer, T value) - where T : struct + where T : unmanaged { - long size = Marshal.SizeOf<T>(); - - byte[] data = new byte[size]; - - fixed (byte* ptr = data) - { - Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false); - } + ReadOnlySpan<byte> data = MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateReadOnlySpan(ref value, 1)); writer.Write(data); } |