Rename SnesSaveController to SaveController and move it to Emulation.Common since it is used by multiple cores and is general in design

This commit is contained in:
adelikat 2017-04-19 12:44:02 -05:00
parent ee5d35bb34
commit 3f866f6d7f
6 changed files with 105 additions and 108 deletions

View File

@ -141,6 +141,7 @@
<Compile Include="Interfaces\Services\ITraceable.cs" />
<Compile Include="Interfaces\Services\IVideoProvider.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SaveController.cs" />
<Compile Include="ServiceAttributes.cs" />
<Compile Include="ServiceInjector.cs" />
<Compile Include="Sound\Utilities\BlipBuffer.cs" />

View File

@ -0,0 +1,100 @@
using System;
using System.IO;
using System.Linq;
using BizHawk.Common;
namespace BizHawk.Emulation.Common
{
/// <summary>
/// Can freeze a copy of a controller input set and serialize\deserialize it
/// </summary>
public class SaveController : IController
{
private readonly WorkingDictionary<string, float> _buttons = new WorkingDictionary<string, float>();
public SaveController()
{
Definition = null;
}
public SaveController(ControllerDefinition def)
{
Definition = def;
}
/// <summary>
/// Gets the current definition.
/// Invalid until CopyFrom has been called
/// </summary>
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]);
}
}
/// <summary>
/// No checking to see if the deserialized controls match any definition
/// </summary>
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);
}
}
/// <summary>
/// This controller's definition changes to that of source
/// </summary>
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];
}
}
}

View File

@ -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)
{

View File

@ -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();

View File

@ -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;

View File

@ -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));
}
/// <summary>
/// can freeze a copy of a controller input set and serialize\deserialize it
/// </summary>
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<string, float> buttons = new WorkingDictionary<string, float>();
/// <summary>
/// invalid until CopyFrom has been called
/// </summary>
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]);
}
}
/// <summary>
/// no checking to see if the deserialized controls match any definition
/// </summary>
/// <param name="b"></param>
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);
}
}
/// <summary>
/// this controller's definition changes to that of source
/// </summary>
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];
}
}
}
}