Turn WaterboxHostNative ReturnData to a struct, misc cleanups elsewhere

This commit is contained in:
CasualPokePlayer 2023-05-18 22:21:17 -07:00
parent 332143ea8d
commit 8a13793d00
2 changed files with 53 additions and 69 deletions

View File

@ -120,15 +120,13 @@ namespace BizHawk.Emulation.Cores.Waterbox
data = File.ReadAllBytes(path); data = File.ReadAllBytes(path);
} }
var retobj = new ReturnData(); NativeImpl.wbx_create_host(nativeOpts, opt.Filename, Reader(new MemoryStream(data, false)), IntPtr.Zero, out var retobj);
NativeImpl.wbx_create_host(nativeOpts, opt.Filename, Reader(new MemoryStream(data, false)), IntPtr.Zero, retobj);
_nativeHost = retobj.GetDataOrThrow(); _nativeHost = retobj.GetDataOrThrow();
} }
public IntPtr GetProcAddrOrZero(string entryPoint) public IntPtr GetProcAddrOrZero(string entryPoint)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_get_proc_addr_raw(_nativeHost, entryPoint, out var retobj);
NativeImpl.wbx_get_proc_addr_raw(_nativeHost, entryPoint, retobj);
return retobj.GetDataOrThrow(); return retobj.GetDataOrThrow();
} }
@ -147,22 +145,19 @@ namespace BizHawk.Emulation.Cores.Waterbox
public IntPtr GetCallbackProcAddr(IntPtr exitPoint, int slot) public IntPtr GetCallbackProcAddr(IntPtr exitPoint, int slot)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_get_callback_addr(_nativeHost, exitPoint, slot, out var retobj);
NativeImpl.wbx_get_callback_addr(_nativeHost, exitPoint, slot, retobj);
return retobj.GetDataOrThrow(); return retobj.GetDataOrThrow();
} }
public IntPtr GetCallinProcAddr(IntPtr entryPoint) public IntPtr GetCallinProcAddr(IntPtr entryPoint)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_get_callin_addr(_nativeHost, entryPoint, out var retobj);
NativeImpl.wbx_get_callin_addr(_nativeHost, entryPoint, retobj);
return retobj.GetDataOrThrow(); return retobj.GetDataOrThrow();
} }
public void Seal() public void Seal()
{ {
var retobj = new ReturnData(); NativeImpl.wbx_seal(_nativeHost, out var retobj);
NativeImpl.wbx_seal(_nativeHost, retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
Console.WriteLine("WaterboxHost Sealed!"); Console.WriteLine("WaterboxHost Sealed!");
} }
@ -174,8 +169,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// <param name="name">the filename that the unmanaged core will access the file by</param> /// <param name="name">the filename that the unmanaged core will access the file by</param>
public void AddReadonlyFile(byte[] data, string name) public void AddReadonlyFile(byte[] data, string name)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_mount_file(_nativeHost, name, Reader(new MemoryStream(data, false)), IntPtr.Zero, false, out var retobj);
NativeImpl.wbx_mount_file(_nativeHost, name, Reader(new MemoryStream(data, false)), IntPtr.Zero, false, retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
} }
@ -185,8 +179,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// </summary> /// </summary>
public void RemoveReadonlyFile(string name) public void RemoveReadonlyFile(string name)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_unmount_file(_nativeHost, name, null, IntPtr.Zero, out var retobj);
NativeImpl.wbx_unmount_file(_nativeHost, name, null, IntPtr.Zero, retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
} }
@ -196,8 +189,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// </summary> /// </summary>
public void AddTransientFile(byte[] data, string name) public void AddTransientFile(byte[] data, string name)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_mount_file(_nativeHost, name, Reader(new MemoryStream(data, false)), IntPtr.Zero, true, out var retobj);
NativeImpl.wbx_mount_file(_nativeHost, name, Reader(new MemoryStream(data, false)), IntPtr.Zero, true, retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
} }
@ -207,9 +199,8 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// <returns>The state of the file when it was removed</returns> /// <returns>The state of the file when it was removed</returns>
public byte[] RemoveTransientFile(string name) public byte[] RemoveTransientFile(string name)
{ {
var retobj = new ReturnData();
var ms = new MemoryStream(); var ms = new MemoryStream();
NativeImpl.wbx_unmount_file(_nativeHost, name, Writer(ms), IntPtr.Zero, retobj); NativeImpl.wbx_unmount_file(_nativeHost, name, Writer(ms), IntPtr.Zero, out var retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
return ms.ToArray(); return ms.ToArray();
} }
@ -262,13 +253,13 @@ namespace BizHawk.Emulation.Cores.Waterbox
private class WaterboxPagesDomain : MemoryDomain private class WaterboxPagesDomain : MemoryDomain
{ {
protected readonly WaterboxHost _host; private readonly WaterboxHost _host;
public WaterboxPagesDomain(WaterboxHost host) public WaterboxPagesDomain(WaterboxHost host)
{ {
_host = host; _host = host;
var retobj = new ReturnData(); NativeImpl.wbx_get_page_len(_host._nativeHost, out var retobj);
NativeImpl.wbx_get_page_len(_host._nativeHost, retobj);
Name = "Waterbox PageData"; Name = "Waterbox PageData";
Size = (long)retobj.GetDataOrThrow(); Size = (long)retobj.GetDataOrThrow();
@ -279,8 +270,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
public override byte PeekByte(long addr) public override byte PeekByte(long addr)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_get_page_data(_host._nativeHost, Z.SU(addr), out var retobj);
NativeImpl.wbx_get_page_data(_host._nativeHost, Z.SU(addr), retobj);
return (byte)retobj.GetDataOrThrow(); return (byte)retobj.GetDataOrThrow();
} }
@ -292,15 +282,13 @@ namespace BizHawk.Emulation.Cores.Waterbox
public void SaveStateBinary(BinaryWriter bw) public void SaveStateBinary(BinaryWriter bw)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_save_state(_nativeHost, Writer(bw.BaseStream), IntPtr.Zero, out var retobj);
NativeImpl.wbx_save_state(_nativeHost, Writer(bw.BaseStream), IntPtr.Zero, retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
} }
public void LoadStateBinary(BinaryReader br) public void LoadStateBinary(BinaryReader br)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_load_state(_nativeHost, Reader(br.BaseStream), IntPtr.Zero, out var retobj);
NativeImpl.wbx_load_state(_nativeHost, Reader(br.BaseStream), IntPtr.Zero, retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
} }
@ -308,8 +296,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
{ {
if (_enterCount == 0) if (_enterCount == 0)
{ {
var retobj = new ReturnData(); NativeImpl.wbx_activate_host(_nativeHost, out var retobj);
NativeImpl.wbx_activate_host(_nativeHost, retobj);
retobj.GetDataOrThrow(); retobj.GetDataOrThrow();
} }
_enterCount++; _enterCount++;
@ -317,16 +304,16 @@ namespace BizHawk.Emulation.Cores.Waterbox
public void Exit() public void Exit()
{ {
if (_enterCount <= 0) switch (_enterCount)
{ {
throw new InvalidOperationException(); case <= 0:
} throw new InvalidOperationException();
else if (_enterCount == 1) case 1:
{ NativeImpl.wbx_deactivate_host(_nativeHost, out var retobj);
var retobj = new ReturnData(); retobj.GetDataOrThrow();
NativeImpl.wbx_deactivate_host(_nativeHost, retobj); break;
retobj.GetDataOrThrow();
} }
_enterCount--; _enterCount--;
} }
@ -334,13 +321,12 @@ namespace BizHawk.Emulation.Cores.Waterbox
{ {
if (_nativeHost != IntPtr.Zero) if (_nativeHost != IntPtr.Zero)
{ {
var retobj = new ReturnData();
if (_enterCount != 0) if (_enterCount != 0)
{ {
NativeImpl.wbx_deactivate_host(_nativeHost, retobj); NativeImpl.wbx_deactivate_host(_nativeHost, out _);
Console.Error.WriteLine("Warn: Disposed of WaterboxHost which was active"); Console.Error.WriteLine("Warn: Disposed of WaterboxHost which was active");
} }
NativeImpl.wbx_destroy_host(_nativeHost, retobj); NativeImpl.wbx_destroy_host(_nativeHost, out _);
_enterCount = 0; _enterCount = 0;
_nativeHost = IntPtr.Zero; _nativeHost = IntPtr.Zero;
GC.SuppressFinalize(this); GC.SuppressFinalize(this);

View File

@ -6,21 +6,19 @@ using BizHawk.Common;
namespace BizHawk.Emulation.Cores.Waterbox namespace BizHawk.Emulation.Cores.Waterbox
{ {
public unsafe abstract class WaterboxHostNative public abstract class WaterboxHostNative
{ {
[StructLayout(LayoutKind.Explicit)] [StructLayout(LayoutKind.Sequential)]
public class ReturnData public unsafe struct ReturnData
{ {
[FieldOffset(0)] public fixed byte ErrorMessage[1024];
public byte ErrorMessageStart;
[FieldOffset(1024)]
public IntPtr Data; public IntPtr Data;
public IntPtr GetDataOrThrow() public IntPtr GetDataOrThrow()
{ {
if (ErrorMessageStart != 0) if (ErrorMessage[0] != 0)
{ {
fixed(byte* p = &ErrorMessageStart) fixed (byte* p = ErrorMessage)
throw new InvalidOperationException(Mershul.PtrToStringUtf8((IntPtr)p)); throw new InvalidOperationException(Mershul.PtrToStringUtf8((IntPtr)p));
} }
return Data; return Data;
@ -73,15 +71,15 @@ namespace BizHawk.Emulation.Cores.Waterbox
// public delegate UIntPtr /*MissingFileResult*/ FileCallback(IntPtr userdata, UIntPtr /*string*/ name); // public delegate UIntPtr /*MissingFileResult*/ FileCallback(IntPtr userdata, UIntPtr /*string*/ name);
public static WriteCallback MakeCallbackForWriter(Stream s) public static unsafe WriteCallback MakeCallbackForWriter(Stream s)
{ {
var ss = SpanStream.GetOrBuild(s); var ss = SpanStream.GetOrBuild(s);
return (_unused, data, size) => return (_, data, size) =>
{ {
try try
{ {
var count = (int)size; var count = (int)size;
ss.Write(new ReadOnlySpan<byte>((void*)data, (int)size)); ss.Write(new((void*)data, (int)size));
return 0; return 0;
} }
catch catch
@ -90,15 +88,15 @@ namespace BizHawk.Emulation.Cores.Waterbox
} }
}; };
} }
public static ReadCallback MakeCallbackForReader(Stream s) public static unsafe ReadCallback MakeCallbackForReader(Stream s)
{ {
var ss = SpanStream.GetOrBuild(s); var ss = SpanStream.GetOrBuild(s);
return (_unused, data, size) => return (_, data, size) =>
{ {
try try
{ {
var count = (int)size; var count = (int)size;
var n = ss.Read(new Span<byte>((void*)data, count)); var n = ss.Read(new((void*)data, count));
return Z.SS(n); return Z.SS(n);
} }
catch catch
@ -124,13 +122,13 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// which will not be used after this call. /// which will not be used after this call.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_create_host(MemoryLayoutTemplate layout, string moduleName, ReadCallback wbx, IntPtr userdata, ReturnData /*WaterboxHost*/ ret); public abstract void wbx_create_host(MemoryLayoutTemplate layout, string moduleName, ReadCallback wbx, IntPtr userdata, out ReturnData /*WaterboxHost*/ ret);
/// <summary> /// <summary>
/// Tear down a host environment. If called while the environment is active, will deactivate it first. /// Tear down a host environment. If called while the environment is active, will deactivate it first.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_destroy_host(IntPtr /*WaterboxHost*/ obj, ReturnData /*void*/ ret); public abstract void wbx_destroy_host(IntPtr /*WaterboxHost*/ obj, out ReturnData /*void*/ ret);
/// <summary> /// <summary>
/// Activate a host environment. This swaps it into memory and makes it available for use. /// Activate a host environment. This swaps it into memory and makes it available for use.
@ -139,21 +137,21 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// Ignored if host is already active. /// Ignored if host is already active.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_activate_host(IntPtr /*WaterboxHost*/ obj, ReturnData /*void*/ ret); public abstract void wbx_activate_host(IntPtr /*WaterboxHost*/ obj, out ReturnData /*void*/ ret);
/// <summary> /// <summary>
/// Deactivates a host environment, and releases the mutex. /// Deactivates a host environment, and releases the mutex.
/// Ignored if host is not active /// Ignored if host is not active
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_deactivate_host(IntPtr /*WaterboxHost*/ obj, ReturnData /*void*/ ret); public abstract void wbx_deactivate_host(IntPtr /*WaterboxHost*/ obj, out ReturnData /*void*/ ret);
/// <summary> /// <summary>
/// Returns a thunk suitable for calling an exported function from the guest executable. This pointer is only valid /// Returns a thunk suitable for calling an exported function from the guest executable. This pointer is only valid
/// while the host is active. A missing proc is not an error and simply returns 0. The guest function must be, /// while the host is active. A missing proc is not an error and simply returns 0. The guest function must be,
/// and the returned callback will be, sysv abi, and will only pass up to 6 int/ptr args and no other arg types. /// and the returned callback will be, sysv abi, and will only pass up to 6 int/ptr args and no other arg types.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_get_proc_addr(IntPtr /*WaterboxHost*/ obj, string name, ReturnData /*UIntPtr*/ ret); public abstract void wbx_get_proc_addr(IntPtr /*WaterboxHost*/ obj, string name, out ReturnData /*UIntPtr*/ ret);
/// <summary> /// <summary>
/// Returns a thunk suitable for calling an arbitrary entry point into the guest executable. This pointer is only valid /// Returns a thunk suitable for calling an arbitrary entry point into the guest executable. This pointer is only valid
/// while the host is active. wbx_get_proc_addr already calls this internally on pointers it returns, so this call is /// while the host is active. wbx_get_proc_addr already calls this internally on pointers it returns, so this call is
@ -161,14 +159,14 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// a pointer to another function). /// a pointer to another function).
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_get_callin_addr(IntPtr /*WaterboxHost*/ obj, IntPtr ptr, ReturnData /*UIntPtr*/ ret); public abstract void wbx_get_callin_addr(IntPtr /*WaterboxHost*/ obj, IntPtr ptr, out ReturnData /*UIntPtr*/ ret);
/// <summary> /// <summary>
/// Returns the raw address of a function exported from the guest. `wbx_get_proc_addr()` is equivalent to /// Returns the raw address of a function exported from the guest. `wbx_get_proc_addr()` is equivalent to
/// `wbx_get_callin_addr(wbx_get_proc_addr_raw()). Most things should not use this directly, as the returned /// `wbx_get_callin_addr(wbx_get_proc_addr_raw()). Most things should not use this directly, as the returned
/// pointer will not have proper stack hygiene and will crash on syscalls from the guest. /// pointer will not have proper stack hygiene and will crash on syscalls from the guest.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_get_proc_addr_raw(IntPtr /*WaterboxHost*/ obj, string name, ReturnData /*UIntPtr*/ ret); public abstract void wbx_get_proc_addr_raw(IntPtr /*WaterboxHost*/ obj, string name, out ReturnData /*UIntPtr*/ ret);
/// <summary> /// <summary>
/// Returns a function pointer suitable for passing to the guest to allow it to call back while active. /// Returns a function pointer suitable for passing to the guest to allow it to call back while active.
/// Slot number is an integer that is used to keep pointers consistent across runs: If the host is loaded /// Slot number is an integer that is used to keep pointers consistent across runs: If the host is loaded
@ -177,12 +175,12 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// The returned thunk will be, and the callback must be, sysv abi and will only pass up to 6 int/ptr args and no other arg types. /// The returned thunk will be, and the callback must be, sysv abi and will only pass up to 6 int/ptr args and no other arg types.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_get_callback_addr(IntPtr /*WaterboxHost*/ obj, IntPtr callback, int slot, ReturnData /*UIntPtr*/ ret); public abstract void wbx_get_callback_addr(IntPtr /*WaterboxHost*/ obj, IntPtr callback, int slot, out ReturnData /*UIntPtr*/ ret);
/// <summary> /// <summary>
/// Calls the seal operation, which is a one time action that prepares the host to save states. /// Calls the seal operation, which is a one time action that prepares the host to save states.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_seal(IntPtr /*WaterboxHost*/ obj, ReturnData /*void*/ ret); public abstract void wbx_seal(IntPtr /*WaterboxHost*/ obj, out ReturnData /*void*/ ret);
/// <summary> /// <summary>
/// Mounts a file in the environment. All data will be immediately consumed from the reader, which will not be used after this call. /// Mounts a file in the environment. All data will be immediately consumed from the reader, which will not be used after this call.
@ -190,7 +188,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// in every savestate, or never appear in any savestates. All savestateable files must be added in the same order for every run. /// in every savestate, or never appear in any savestates. All savestateable files must be added in the same order for every run.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_mount_file(IntPtr /*WaterboxHost*/ obj, string name, ReadCallback reader, IntPtr userdata, bool writable, ReturnData /*void*/ ret); public abstract void wbx_mount_file(IntPtr /*WaterboxHost*/ obj, string name, ReadCallback reader, IntPtr userdata, bool writable, out ReturnData /*void*/ ret);
/// <summary> /// <summary>
/// Remove a file previously added. Writer is optional; if provided, the contents of the file at time of removal will be dumped to it. /// Remove a file previously added. Writer is optional; if provided, the contents of the file at time of removal will be dumped to it.
@ -198,7 +196,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// If the file has been used in savestates, it does not make sense to remove it here, but nothing will stop you. /// If the file has been used in savestates, it does not make sense to remove it here, but nothing will stop you.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_unmount_file(IntPtr /*WaterboxHost*/ obj, string name, WriteCallback writer, IntPtr userdata, ReturnData /*void*/ ret); public abstract void wbx_unmount_file(IntPtr /*WaterboxHost*/ obj, string name, WriteCallback writer, IntPtr userdata, out ReturnData /*void*/ ret);
#if false #if false
/// <summary> /// <summary>
@ -217,7 +215,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// Must always be called with the same sequence and contents of readonly files. /// Must always be called with the same sequence and contents of readonly files.
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_save_state(IntPtr /*WaterboxHost*/ obj, WriteCallback writer, IntPtr userdata, ReturnData /*void*/ ret); public abstract void wbx_save_state(IntPtr /*WaterboxHost*/ obj, WriteCallback writer, IntPtr userdata, out ReturnData /*void*/ ret);
/// <summary> /// <summary>
/// Load state. Must not be called before seal. Must not be called with any writable files mounted. /// Load state. Must not be called before seal. Must not be called with any writable files mounted.
@ -226,7 +224,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// Errors generally poison the environment; sorry! /// Errors generally poison the environment; sorry!
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_load_state(IntPtr /*WaterboxHost*/ obj, ReadCallback reader, IntPtr userdata, ReturnData /*void*/ ret); public abstract void wbx_load_state(IntPtr /*WaterboxHost*/ obj, ReadCallback reader, IntPtr userdata, out ReturnData /*void*/ ret);
/// <summary> /// <summary>
/// Control whether the host automatically evicts blocks from memory when they are not active. For the best performance, /// Control whether the host automatically evicts blocks from memory when they are not active. For the best performance,
@ -240,7 +238,7 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// Retrieve the number of pages of guest memory that this host is tracking /// Retrieve the number of pages of guest memory that this host is tracking
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_get_page_len(IntPtr /*WaterboxHost*/ obj, ReturnData /*UIntPtr*/ ret); public abstract void wbx_get_page_len(IntPtr /*WaterboxHost*/ obj, out ReturnData /*UIntPtr*/ ret);
/// <summary> /// <summary>
/// Retrieve basic information for a tracked guest page. Index should be in 0..wbx_get_page_len(). /// Retrieve basic information for a tracked guest page. Index should be in 0..wbx_get_page_len().
@ -253,6 +251,6 @@ namespace BizHawk.Emulation.Cores.Waterbox
/// 0x80 - dirty /// 0x80 - dirty
/// </summary> /// </summary>
[BizImport(CallingConvention.Cdecl)] [BizImport(CallingConvention.Cdecl)]
public abstract void wbx_get_page_data(IntPtr /*WaterboxHost*/ obj, UIntPtr index, ReturnData /*byte*/ ret); public abstract void wbx_get_page_data(IntPtr /*WaterboxHost*/ obj, UIntPtr index, out ReturnData /*byte*/ ret);
} }
} }