Move ReadStruct/WriteStruct to Ryujinx.Common

This commit is contained in:
Thog 2019-01-05 02:42:11 +01:00
commit e6b3b22981
No known key found for this signature in database
GPG key ID: 0CD291558FAFDBC6
3 changed files with 51 additions and 42 deletions

View file

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Ryujinx.Common
{
public static class StructIOExtension
{
public unsafe static T ReadStruct<T>(this BinaryReader reader) where T : struct
{
int size = Marshal.SizeOf<T>();
byte[] data = reader.ReadBytes(size);
fixed (byte* ptr = data)
{
return Marshal.PtrToStructure<T>((IntPtr)ptr);
}
}
public unsafe static void WriteStruct<T>(this BinaryWriter writer, T value) where T : struct
{
long size = Marshal.SizeOf<T>();
byte[] data = new byte[size];
fixed (byte* ptr = data)
{
Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false);
}
writer.Write(data);
}
}
}

View file

@ -1,3 +1,4 @@
using Ryujinx.Common;
using System;
using System.IO;
using System.Runtime.InteropServices;
@ -141,7 +142,7 @@ namespace Ryujinx.HLE.HOS.Services.Android
public GbpBuffer(BinaryReader reader)
{
Header = NvFlinger.ReadStruct<GraphicBufferHeader>(reader);
Header = reader.ReadStruct<GraphicBufferHeader>();
// ignore fds
// TODO: check if that is used in official implementation
@ -152,13 +153,13 @@ namespace Ryujinx.HLE.HOS.Services.Android
throw new System.NotImplementedException($"Unexpected Graphic Buffer ints count (expected 0x51, found 0x{Header.IntsCount:x}");
}
Buffer = NvFlinger.ReadStruct<NvGraphicBuffer>(reader);
Buffer = reader.ReadStruct<NvGraphicBuffer>();
}
public void Write(BinaryWriter writer)
{
NvFlinger.WriteStruct(writer, Header);
NvFlinger.WriteStruct(writer, Buffer);
writer.WriteStruct(Header);
writer.WriteStruct(Buffer);
}
}
}

View file

@ -325,34 +325,6 @@ namespace Ryujinx.HLE.HOS.Services.Android
}
}
// FIXME: move this (extension?)
public unsafe static T ReadStruct<T>(BinaryReader reader) where T : struct
{
int size = Marshal.SizeOf<T>();
byte[] data = reader.ReadBytes(size);
fixed (byte* ptr = data)
{
return Marshal.PtrToStructure<T>((IntPtr)ptr);
}
}
// FIXME: move this (extension?)
public unsafe static void WriteStruct<T>(BinaryWriter writer, T value) where T : struct
{
long size = Marshal.SizeOf<T>();
byte[] data = new byte[size];
fixed (byte* ptr = data)
{
Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false);
}
writer.Write(data);
}
private long MakeReplyParcel(ServiceCtx context, params int[] ints)
{
using (MemoryStream ms = new MemoryStream())
@ -429,7 +401,6 @@ namespace Ryujinx.HLE.HOS.Services.Android
int right = crop.Right;
int bottom = crop.Bottom;
NvGpuVmm vmm = NvGpuASIoctl.GetASCtx(context).Vmm;
_renderer.QueueAction(() =>