From 44fa93e15fee91355d4e5fe70520a74ee045aae3 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 4 Dec 2016 12:08:21 -0600 Subject: [PATCH] Lua - make the MemorySavestate library dependent on IStatable --- .../lua/EmuLuaLibrary.MemorySavestate.cs | 44 +++++++------------ 1 file changed, 16 insertions(+), 28 deletions(-) diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs index c4108b6d00..c0602d8da3 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.MemorySavestate.cs @@ -3,8 +3,9 @@ using System.Collections.Generic; using System.IO; using LuaInterface; -using BizHawk.Emulation.Common.IEmulatorExtensions; +using BizHawk.Emulation.Common; +using BizHawk.Emulation.Common.IEmulatorExtensions; namespace BizHawk.Client.Common { @@ -20,26 +21,21 @@ namespace BizHawk.Client.Common private readonly Dictionary MemorySavestates = new Dictionary(); + [RequiredService] + private IStatable _statableCore { get; set; } + [LuaMethodAttributes( "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() { - if (Global.Emulator.HasSavestates()) - { - var guid = Guid.NewGuid(); - var bytes = (byte[])Global.Emulator.AsStatable().SaveStateBinary().Clone(); + var guid = Guid.NewGuid(); + var bytes = (byte[])_statableCore.SaveStateBinary().Clone(); - MemorySavestates.Add(guid, bytes); + MemorySavestates.Add(guid, bytes); - return guid.ToString(); - } - else - { - Log("Savestates not supported on this core"); - return Guid.Empty.ToString(); - } + return guid.ToString(); } [LuaMethodAttributes( @@ -50,27 +46,19 @@ namespace BizHawk.Client.Common { var guid = new Guid(identifier); - if (Global.Emulator.HasSavestates()) + try { - try - { - var statableCore = Global.Emulator.AsStatable(); - var state = MemorySavestates[guid]; + var state = MemorySavestates[guid]; - using (MemoryStream ms = new MemoryStream(state)) - using (BinaryReader br = new BinaryReader(ms)) - { - statableCore.LoadStateBinary(br); - } - } - catch + using (MemoryStream ms = new MemoryStream(state)) + using (BinaryReader br = new BinaryReader(ms)) { - Log("Unable to find the given savestate in memory"); + _statableCore.LoadStateBinary(br); } } - else + catch { - Log("Savestates not supported on this core"); + Log("Unable to find the given savestate in memory"); } }