Migrate MemorySavestateEmuLuaLibrary to ApiHawk delegation
This commit is contained in:
parent
8da543b1a5
commit
bd7dfbd487
|
@ -8,8 +8,14 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
public sealed class MemorySaveStateApi : IMemorySaveState
|
||||
{
|
||||
public MemorySaveStateApi()
|
||||
{ }
|
||||
public MemorySaveStateApi(Action<string> logCallback)
|
||||
{
|
||||
LogCallback = logCallback;
|
||||
}
|
||||
|
||||
public MemorySaveStateApi() : this(Console.WriteLine) {}
|
||||
|
||||
private readonly Action<string> LogCallback;
|
||||
|
||||
[RequiredService]
|
||||
private IStatable StatableCore { get; set; }
|
||||
|
@ -40,7 +46,7 @@ namespace BizHawk.Client.Common
|
|||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("Unable to find the given savestate in memory");
|
||||
LogCallback("Unable to find the given savestate in memory");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@ using BizHawk.Emulation.Common;
|
|||
// ReSharper disable UnusedAutoPropertyAccessor.Local
|
||||
namespace BizHawk.Client.Common
|
||||
{
|
||||
public sealed class MemorySavestateEmuLuaLibrary : LuaLibraryBase
|
||||
public sealed class MemorySavestateEmuLuaLibrary : DelegatingLuaLibrary
|
||||
{
|
||||
public MemorySavestateEmuLuaLibrary(Lua lua)
|
||||
: base(lua) { }
|
||||
|
@ -20,56 +20,20 @@ namespace BizHawk.Client.Common
|
|||
|
||||
public override string Name => "memorysavestate";
|
||||
|
||||
[RequiredService]
|
||||
private IStatable StatableCore { get; set; }
|
||||
|
||||
private readonly Dictionary<Guid, byte[]> _memorySavestates = new Dictionary<Guid, byte[]>();
|
||||
|
||||
[LuaMethodExample("local mmsvstsvcst = memorysavestate.savecorestate( );")]
|
||||
[LuaMethod("savecorestate", "creates a core savestate and stores it in memory. Note: a core savestate is only the raw data from the core, and not extras such as movie input logs, or framebuffers. Returns a unique identifer for the savestate")]
|
||||
public string SaveCoreStateToMemory()
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
var bytes = (byte[])StatableCore.SaveStateBinary().Clone();
|
||||
|
||||
_memorySavestates.Add(guid, bytes);
|
||||
|
||||
return guid.ToString();
|
||||
}
|
||||
public string SaveCoreStateToMemory() => APIs.MemorySaveState.SaveCoreStateToMemory();
|
||||
|
||||
[LuaMethodExample("memorysavestate.loadcorestate( \"3fcf120f-0778-43fd-b2c5-460fb7d34184\" );")]
|
||||
[LuaMethod("loadcorestate", "loads an in memory state with the given identifier")]
|
||||
public void LoadCoreStateFromMemory(string identifier)
|
||||
{
|
||||
var guid = new Guid(identifier);
|
||||
|
||||
try
|
||||
{
|
||||
var state = _memorySavestates[guid];
|
||||
|
||||
using var ms = new MemoryStream(state);
|
||||
using var br = new BinaryReader(ms);
|
||||
StatableCore.LoadStateBinary(br);
|
||||
}
|
||||
catch
|
||||
{
|
||||
Log("Unable to find the given savestate in memory");
|
||||
}
|
||||
}
|
||||
public void LoadCoreStateFromMemory(string identifier) => APIs.MemorySaveState.LoadCoreStateFromMemory(identifier);
|
||||
|
||||
[LuaMethodExample("memorysavestate.removestate( \"3fcf120f-0778-43fd-b2c5-460fb7d34184\" );")]
|
||||
[LuaMethod("removestate", "removes the savestate with the given identifier from memory")]
|
||||
public void DeleteState(string identifier)
|
||||
{
|
||||
var guid = new Guid(identifier);
|
||||
_memorySavestates.Remove(guid);
|
||||
}
|
||||
public void DeleteState(string identifier) => APIs.MemorySaveState.DeleteState(identifier);
|
||||
|
||||
[LuaMethodExample("memorysavestate.clearstatesfrommemory( );")]
|
||||
[LuaMethod("clearstatesfrommemory", "clears all savestates stored in memory")]
|
||||
public void ClearInMemoryStates()
|
||||
{
|
||||
_memorySavestates.Clear();
|
||||
}
|
||||
public void ClearInMemoryStates() => APIs.MemorySaveState.ClearInMemoryStates();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue