From dd993b3c37948f679e0cd635773ecf80b5fdaa3a Mon Sep 17 00:00:00 2001 From: SuuperW Date: Thu, 17 Oct 2019 11:04:44 -0500 Subject: [PATCH] MelonDS: IStatable (and add forgotten file MelonDS_InputPollable.cs) --- .../BizHawk.Emulation.Cores.csproj | 1 + .../Nintendo/NDS/MelonDS_InputPollable.cs | 25 +++++++ .../Consoles/Nintendo/NDS/MelonDS_Statable.cs | 68 +++++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_InputPollable.cs create mode 100644 BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_Statable.cs diff --git a/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj b/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj index 13432f693f..65ed94013a 100644 --- a/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj +++ b/BizHawk.Emulation.Cores/BizHawk.Emulation.Cores.csproj @@ -988,6 +988,7 @@ + diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_InputPollable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_InputPollable.cs new file mode 100644 index 0000000000..d883ddeabe --- /dev/null +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_InputPollable.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + +using BizHawk.Emulation.Common; + +namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS +{ + partial class MelonDS : IInputPollable + { + public int LagCount { get => GetLagFrameCount(); set => throw new NotImplementedException(); } + public bool IsLagFrame { get => _IsLagFrame(); set => throw new NotImplementedException(); } + + public IInputCallbackSystem InputCallbacks => throw new NotImplementedException(); + + [DllImport(dllPath, EntryPoint = "IsLagFrame")] + private static extern bool _IsLagFrame(); + + [DllImport(dllPath)] + private static extern int GetLagFrameCount(); + } +} diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_Statable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_Statable.cs new file mode 100644 index 0000000000..c698f69433 --- /dev/null +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS_Statable.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + +using BizHawk.Emulation.Common; +using System.IO; + +namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS +{ + unsafe partial class MelonDS : IStatable + { + public bool BinarySaveStatesPreferred => true; + + public void LoadStateBinary(BinaryReader reader) + { + MemoryStream mStream = new MemoryStream(); + reader.BaseStream.CopyTo(mStream); + + LoadStateByteArray(mStream.GetBuffer(), (int)mStream.Length); + } + public void LoadStateText(TextReader reader) + { + string str = reader.ReadToEnd(); + LoadStateByteArray(Convert.FromBase64String(str)); + } + private void LoadStateByteArray(byte[] data, int length = -1) + { + if (length == -1) length = data.Length; + fixed (byte* ptr = data) + { + UseSavestate(ptr, length); + } + } + + public byte[] SaveStateBinary() + { + int len = GetSavestateSize(); + byte[] ret = new byte[len]; + fixed (byte* ptr = ret) + { + GetSavestateData(ptr, len); + } + return ret; + } + + public void SaveStateBinary(BinaryWriter writer) + { + byte[] data = SaveStateBinary(); + writer.Write(data); + } + + public void SaveStateText(TextWriter writer) + { + string str = Convert.ToBase64String(SaveStateBinary()); + writer.Write(str); + } + + [DllImport(dllPath)] + private static extern void UseSavestate(byte* data, int len); + [DllImport(dllPath)] + private static extern int GetSavestateSize(); + [DllImport(dllPath)] + private static extern void GetSavestateData(byte* data, int size); + } +}