From 71916f698d7a7a7408e4d372204e6b993ca574b1 Mon Sep 17 00:00:00 2001 From: goyuken Date: Sun, 23 Sep 2012 16:56:11 +0000 Subject: [PATCH] i was going to fix gambatte savestates in a clever way, but i ended up just changing around a few things and then giving up --- .../Consoles/Nintendo/Gameboy/Gambatte.cs | 58 +++++++++++++------ 1 file changed, 40 insertions(+), 18 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs index 76b55bf4ff..84dc73f968 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs @@ -222,21 +222,11 @@ namespace BizHawk.Emulation.Consoles.GB #region savestates - public void SaveStateText(System.IO.TextWriter writer) - { - var temp = SaveStateBinary(); - temp.SaveAsHex(writer); - } - - public void LoadStateText(System.IO.TextReader reader) - { - string hex = reader.ReadLine(); - byte[] state = new byte[hex.Length / 2]; - state.ReadFromHex(hex); - LoadStateBinary(new BinaryReader(new MemoryStream(state))); - } - - public void SaveStateBinary(System.IO.BinaryWriter writer) + /// + /// handles the core-portion of savestating + /// + /// private binary data corresponding to a savestate + byte[] SaveCoreBinary() { uint nlen = 0; IntPtr ndata = IntPtr.Zero; @@ -251,7 +241,40 @@ namespace BizHawk.Emulation.Consoles.GB System.Runtime.InteropServices.Marshal.Copy(ndata, data, 0, (int)nlen); LibGambatte.gambatte_savestate_destroy(ndata); - writer.Write((int)nlen); + return data; + } + + /// + /// handles the core portion of loadstating + /// + /// private binary data previously returned from SaveCoreBinary() + void LoadCoreBinary(byte[] data) + { + if (!LibGambatte.gambatte_loadstate(GambatteState, data, (uint)data.Length)) + throw new Exception("Gambatte failed to load the savestate!"); + } + + public void SaveStateText(System.IO.TextWriter writer) + { + var temp = SaveStateBinary(); + temp.SaveAsHex(writer); + // write extra copy of stuff we don't use + writer.WriteLine("Frame {0}", Frame); + } + + public void LoadStateText(System.IO.TextReader reader) + { + string hex = reader.ReadLine(); + byte[] state = new byte[hex.Length / 2]; + state.ReadFromHex(hex); + LoadStateBinary(new BinaryReader(new MemoryStream(state))); + } + + public void SaveStateBinary(System.IO.BinaryWriter writer) + { + byte[] data = SaveCoreBinary(); + + writer.Write(data.Length); writer.Write(data); // other variables @@ -265,8 +288,7 @@ namespace BizHawk.Emulation.Consoles.GB int length = reader.ReadInt32(); byte[] data = reader.ReadBytes(length); - if (!LibGambatte.gambatte_loadstate(GambatteState, data, (uint)length)) - throw new Exception("Gambatte failed to load the savestate!"); + LoadCoreBinary(data); // other variables IsLagFrame = reader.ReadBoolean();