diff --git a/BizHawk.Client.Common/Api/Classes/MemorySaveStateApi.cs b/BizHawk.Client.Common/Api/Classes/MemorySaveStateApi.cs index fb0352ff6b..bdfb8ca361 100644 --- a/BizHawk.Client.Common/Api/Classes/MemorySaveStateApi.cs +++ b/BizHawk.Client.Common/Api/Classes/MemorySaveStateApi.cs @@ -8,8 +8,14 @@ namespace BizHawk.Client.Common { public sealed class MemorySaveStateApi : IMemorySaveState { - public MemorySaveStateApi() - { } + public MemorySaveStateApi(Action logCallback) + { + LogCallback = logCallback; + } + + public MemorySaveStateApi() : this(Console.WriteLine) {} + + private readonly Action 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"); } } diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs index acd08d9a4c..df61af309d 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs @@ -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 _memorySavestates = new Dictionary(); - [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(); } }