2019-01-05 13:26:16 -08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2022-11-24 06:26:29 -08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2019-01-05 13:26:16 -08:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Common
|
|
|
|
|
{
|
2019-02-11 04:00:32 -08:00
|
|
|
|
public static class BinaryReaderExtensions
|
2019-01-05 13:26:16 -08:00
|
|
|
|
{
|
2019-02-11 04:00:32 -08:00
|
|
|
|
public unsafe static T ReadStruct<T>(this BinaryReader reader)
|
2022-11-24 06:26:29 -08:00
|
|
|
|
where T : unmanaged
|
2019-01-05 13:26:16 -08:00
|
|
|
|
{
|
2022-11-24 06:26:29 -08:00
|
|
|
|
return MemoryMarshal.Cast<byte, T>(reader.ReadBytes(Unsafe.SizeOf<T>()))[0];
|
2019-06-27 04:11:51 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 04:00:32 -08:00
|
|
|
|
public unsafe static void WriteStruct<T>(this BinaryWriter writer, T value)
|
2022-11-24 06:26:29 -08:00
|
|
|
|
where T : unmanaged
|
2019-01-05 13:26:16 -08:00
|
|
|
|
{
|
2022-11-24 06:26:29 -08:00
|
|
|
|
ReadOnlySpan<byte> data = MemoryMarshal.Cast<T, byte>(MemoryMarshal.CreateReadOnlySpan(ref value, 1));
|
2019-01-05 13:26:16 -08:00
|
|
|
|
|
|
|
|
|
writer.Write(data);
|
|
|
|
|
}
|
2022-11-09 11:22:43 -08:00
|
|
|
|
|
|
|
|
|
public static void Write(this BinaryWriter writer, UInt128 value)
|
|
|
|
|
{
|
|
|
|
|
writer.Write((ulong)value);
|
|
|
|
|
writer.Write((ulong)(value >> 64));
|
|
|
|
|
}
|
2019-01-05 13:26:16 -08:00
|
|
|
|
}
|
|
|
|
|
}
|