diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs index 8f8f42a539..f8571f7c7f 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/Gambatte.cs @@ -527,10 +527,10 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy savebuff2 = new byte[savebuff.Length + 4 + 21]; } - JsonSerializer ser = new JsonSerializer() { Formatting = Formatting.Indented }; + JsonSerializer ser = new JsonSerializer { Formatting = Formatting.Indented }; // other data in the text state besides core - class TextStateData + internal class TextStateData { public int Frame; public int LagCount; @@ -539,7 +539,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy public uint frameOverflow; } - public void SaveStateText(System.IO.TextWriter writer) + internal TextState SaveState() { var s = new TextState(); s.Prepare(); @@ -550,7 +550,24 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy s.ExtraData.Frame = Frame; s.ExtraData.frameOverflow = frameOverflow; s.ExtraData._cycleCount = _cycleCount; + return s; + } + internal void LoadState(TextState s) + { + s.Prepare(); + var ff = s.GetFunctionPointersLoad(); + LibGambatte.gambatte_newstateload_ex(GambatteState, ref ff); + IsLagFrame = s.ExtraData.IsLagFrame; + LagCount = s.ExtraData.LagCount; + Frame = s.ExtraData.Frame; + frameOverflow = s.ExtraData.frameOverflow; + _cycleCount = s.ExtraData._cycleCount; + } + + public void SaveStateText(System.IO.TextWriter writer) + { + var s = SaveState(); ser.Serialize(writer, s); // write extra copy of stuff we don't use writer.WriteLine(); @@ -560,14 +577,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy public void LoadStateText(System.IO.TextReader reader) { var s = (TextState)ser.Deserialize(reader, typeof(TextState)); - s.Prepare(); - var ff = s.GetFunctionPointersLoad(); - LibGambatte.gambatte_newstateload_ex(GambatteState, ref ff); - IsLagFrame = s.ExtraData.IsLagFrame; - LagCount = s.ExtraData.LagCount; - Frame = s.ExtraData.Frame; - frameOverflow = s.ExtraData.frameOverflow; - _cycleCount = s.ExtraData._cycleCount; + LoadState(s); } public void SaveStateBinary(System.IO.BinaryWriter writer) diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs index 3f2cd639c9..274302ad49 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs @@ -7,6 +7,8 @@ using BizHawk.Common.BufferExtensions; using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Nintendo.SNES; +using Newtonsoft.Json; + namespace BizHawk.Emulation.Cores.Nintendo.Gameboy { [CoreAttributes( @@ -291,20 +293,61 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy #region savestates + JsonSerializer ser = new JsonSerializer { Formatting = Formatting.Indented }; + + private class DGBSerialized + { + public TextState L; + public TextState R; + // other data + public bool IsLagFrame; + public int LagCount; + public int Frame; + public int overflowL; + public int overflowR; + public int LatchL; + public int LatchR; + public bool cableconnected; + public bool cablediscosignal; + } + public void SaveStateText(TextWriter writer) { - var temp = SaveStateBinary(); - temp.SaveAsHex(writer); + var s = new DGBSerialized + { + L = L.SaveState(), + R = R.SaveState(), + IsLagFrame = IsLagFrame, + LagCount = LagCount, + Frame = Frame, + overflowL = overflowL, + overflowR = overflowR, + LatchL = LatchL, + LatchR = LatchR, + cableconnected = cableconnected, + cablediscosignal = cablediscosignal + }; + ser.Serialize(writer, s); // write extra copy of stuff we don't use + // is this needed anymore?? + writer.WriteLine(); writer.WriteLine("Frame {0}", Frame); } public void LoadStateText(TextReader reader) { - string hex = reader.ReadLine(); - byte[] state = new byte[hex.Length / 2]; - state.ReadFromHex(hex); - LoadStateBinary(new BinaryReader(new MemoryStream(state))); + var s = (DGBSerialized)ser.Deserialize(reader, typeof(DGBSerialized)); + L.LoadState(s.L); + R.LoadState(s.R); + IsLagFrame = s.IsLagFrame; + LagCount = s.LagCount; + Frame = s.Frame; + overflowL = s.overflowL; + overflowR = s.overflowR; + LatchL = s.LatchL; + LatchR = s.LatchR; + cableconnected = s.cableconnected; + cablediscosignal = s.cablediscosignal; } public void SaveStateBinary(BinaryWriter writer)