diff --git a/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj b/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj index 81770041f7..f59446d021 100644 --- a/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj +++ b/BizHawk.Emulation.Common/BizHawk.Emulation.Common.csproj @@ -141,6 +141,7 @@ + diff --git a/BizHawk.Emulation.Common/SaveController.cs b/BizHawk.Emulation.Common/SaveController.cs new file mode 100644 index 0000000000..d54e99f62c --- /dev/null +++ b/BizHawk.Emulation.Common/SaveController.cs @@ -0,0 +1,100 @@ +using System; +using System.IO; +using System.Linq; + +using BizHawk.Common; + +namespace BizHawk.Emulation.Common +{ + /// + /// Can freeze a copy of a controller input set and serialize\deserialize it + /// + public class SaveController : IController + { + private readonly WorkingDictionary _buttons = new WorkingDictionary(); + + public SaveController() + { + Definition = null; + } + + public SaveController(ControllerDefinition def) + { + Definition = def; + } + + /// + /// Gets the current definition. + /// Invalid until CopyFrom has been called + /// + public ControllerDefinition Definition { get; private set; } + + public void Serialize(BinaryWriter b) + { + b.Write(_buttons.Keys.Count); + foreach (var k in _buttons.Keys) + { + b.Write(k); + b.Write(_buttons[k]); + } + } + + /// + /// No checking to see if the deserialized controls match any definition + /// + public void DeSerialize(BinaryReader b) + { + _buttons.Clear(); + int numbuttons = b.ReadInt32(); + for (int i = 0; i < numbuttons; i++) + { + string k = b.ReadString(); + float v = b.ReadSingle(); + _buttons.Add(k, v); + } + } + + /// + /// This controller's definition changes to that of source + /// + public void CopyFrom(IController source) + { + Definition = source.Definition; + _buttons.Clear(); + foreach (var k in Definition.BoolButtons) + { + _buttons.Add(k, source.IsPressed(k) ? 1.0f : 0); + } + + foreach (var k in Definition.FloatControls) + { + if (_buttons.Keys.Contains(k)) + { + throw new Exception("name collision between bool and float lists!"); + } + + _buttons.Add(k, source.GetFloat(k)); + } + } + + public void Clear() + { + _buttons.Clear(); + } + + public void Set(string button) + { + _buttons[button] = 1.0f; + } + + public bool IsPressed(string button) + { + return _buttons[button] != 0; + } + + public float GetFloat(string name) + { + return _buttons[name]; + } + } +} diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs index ee2c510cc7..06bfcfe30b 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/Gameboy/GambatteLink.cs @@ -73,8 +73,8 @@ namespace BizHawk.Emulation.Cores.Nintendo.Gameboy const int SampPerFrame = 35112; - LibsnesCore.SnesSaveController LCont = new LibsnesCore.SnesSaveController(Gameboy.GbController); - LibsnesCore.SnesSaveController RCont = new LibsnesCore.SnesSaveController(Gameboy.GbController); + SaveController LCont = new SaveController(Gameboy.GbController); + SaveController RCont = new SaveController(Gameboy.GbController); public bool IsCGBMode(bool right) { diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IEmulator.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IEmulator.cs index 0ebc11b1fb..549fe06788 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IEmulator.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IEmulator.cs @@ -39,7 +39,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES var bw = new BinaryWriter(ms); bw.Write(CoreSaveState()); bw.Write(false); // not framezero - var ssc = new SnesSaveController(); + var ssc = new SaveController(); ssc.CopyFrom(Controller); ssc.Serialize(bw); bw.Close(); diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs index 594854bceb..de565bf3d7 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.IStatable.cs @@ -58,7 +58,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES bw.Write(framezero); if (!framezero) { - var ssc = new SnesSaveController(ControllerDefinition); + var ssc = new SaveController(ControllerDefinition); ssc.DeSerialize(reader); IController tmp = Controller; Controller = ssc; diff --git a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs index 5e9ce8c735..79f9c3e666 100644 --- a/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs +++ b/BizHawk.Emulation.Cores/Consoles/Nintendo/SNES/LibsnesCore.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Xml; using System.IO; -using BizHawk.Common; using BizHawk.Common.BufferExtensions; using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Components.W65816; @@ -557,108 +556,5 @@ namespace BizHawk.Emulation.Cores.Nintendo.SNES { SetPalette((SnesColors.ColorType)Enum.Parse(typeof(SnesColors.ColorType), _settings.Palette, false)); } - - /// - /// can freeze a copy of a controller input set and serialize\deserialize it - /// - public class SnesSaveController : IController - { - // this is all rather general, so perhaps should be moved out of LibsnesCore - ControllerDefinition _def; - - public SnesSaveController() - { - _def = null; - } - - public SnesSaveController(ControllerDefinition def) - { - _def = def; - } - - private readonly WorkingDictionary buttons = new WorkingDictionary(); - - /// - /// invalid until CopyFrom has been called - /// - public ControllerDefinition Definition - { - get { return _def; } - } - - public void Serialize(BinaryWriter b) - { - b.Write(buttons.Keys.Count); - foreach (var k in buttons.Keys) - { - b.Write(k); - b.Write(buttons[k]); - } - } - - /// - /// no checking to see if the deserialized controls match any definition - /// - /// - public void DeSerialize(BinaryReader b) - { - buttons.Clear(); - int numbuttons = b.ReadInt32(); - for (int i = 0; i < numbuttons; i++) - { - string k = b.ReadString(); - float v = b.ReadSingle(); - buttons.Add(k, v); - } - } - - /// - /// this controller's definition changes to that of source - /// - public void CopyFrom(IController source) - { - this._def = source.Definition; - buttons.Clear(); - foreach (var k in _def.BoolButtons) - { - buttons.Add(k, source.IsPressed(k) ? 1.0f : 0); - } - - foreach (var k in _def.FloatControls) - { - if (buttons.Keys.Contains(k)) - { - throw new Exception("name collision between bool and float lists!"); - } - - buttons.Add(k, source.GetFloat(k)); - } - } - - public void Clear() - { - buttons.Clear(); - } - - public void Set(string button) - { - buttons[button] = 1.0f; - } - - public bool this[string button] - { - get { return buttons[button] != 0; } - } - - public bool IsPressed(string button) - { - return buttons[button] != 0; - } - - public float GetFloat(string name) - { - return buttons[name]; - } - } } }