From f8dc18c1bda75460fc950c3f3d3f8a56c7e065f5 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Sun, 15 Dec 2019 04:39:16 +1000 Subject: [PATCH] Migrate SavestateLuaLibrary to ApiHawk delegation includes backwards-compatible API change --- .../Api/Interfaces/ISaveState.cs | 8 ++-- .../Api/Libraries/SaveStateAPI.cs | 28 ++++++++------ .../Lua/Libraries/EmuLuaLibrary.Savestate.cs | 38 +++---------------- 3 files changed, 27 insertions(+), 47 deletions(-) diff --git a/BizHawk.Client.Common/Api/Interfaces/ISaveState.cs b/BizHawk.Client.Common/Api/Interfaces/ISaveState.cs index 067ee87543..bd9d3ed423 100644 --- a/BizHawk.Client.Common/Api/Interfaces/ISaveState.cs +++ b/BizHawk.Client.Common/Api/Interfaces/ISaveState.cs @@ -2,9 +2,9 @@ { public interface ISaveState : IExternalApi { - void Load(string path); - void LoadSlot(int slotNum); - void Save(string path); - void SaveSlot(int slotNum); + void Load(string path, bool suppressOSD = false); + void LoadSlot(int slotNum, bool suppressOSD = false); + void Save(string path, bool suppressOSD = false); + void SaveSlot(int slotNum, bool suppressOSD = false); } } diff --git a/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs b/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs index ca51cb0dd0..e3802de5a0 100644 --- a/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs +++ b/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs @@ -9,39 +9,45 @@ namespace BizHawk.Client.EmuHawk { public sealed class SaveStateApi : ISaveState { - public SaveStateApi() : base() - { } + public SaveStateApi(Action logCallback) + { + LogCallback = logCallback; + } - public void Load(string path) + public SaveStateApi() : this(Console.WriteLine) {} + + private readonly Action LogCallback; + + public void Load(string path, bool suppressOSD) { if (!File.Exists(path)) { - Console.WriteLine($"could not find file: {path}"); + LogCallback($"could not find file: {path}"); } else { - GlobalWin.MainForm.LoadState(path, Path.GetFileName(path), true); + GlobalWin.MainForm.LoadState(path, Path.GetFileName(path), true, suppressOSD); } } - public void LoadSlot(int slotNum) + public void LoadSlot(int slotNum, bool suppressOSD) { if (slotNum >= 0 && slotNum <= 9) { - GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", true); + GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", true, suppressOSD); } } - public void Save(string path) + public void Save(string path, bool suppressOSD) { - GlobalWin.MainForm.SaveState(path, path, true); + GlobalWin.MainForm.SaveState(path, path, true, suppressOSD); } - public void SaveSlot(int slotNum) + public void SaveSlot(int slotNum, bool suppressOSD) { if (slotNum >= 0 && slotNum <= 9) { - GlobalWin.MainForm.SaveQuickSave($"QuickSave{slotNum}"); + GlobalWin.MainForm.SaveQuickSave($"QuickSave{slotNum}", true, suppressOSD); } } } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Savestate.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Savestate.cs index 87830add11..98c8e04039 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Savestate.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Savestate.cs @@ -1,13 +1,12 @@ using System; -using System.Collections.Generic; -using System.IO; using NLua; + using BizHawk.Client.Common; namespace BizHawk.Client.EmuHawk { - public sealed class SavestateLuaLibrary : LuaLibraryBase + public sealed class SavestateLuaLibrary : DelegatingLuaLibraryEmu { public SavestateLuaLibrary(Lua lua) : base(lua) { } @@ -19,43 +18,18 @@ namespace BizHawk.Client.EmuHawk [LuaMethodExample("savestate.load( \"C:\\state.bin\" );")] [LuaMethod("load", "Loads a savestate with the given path. If EmuHawk is deferring quicksaves, to TAStudio for example, that form will do what it likes (and the path is ignored).")] - public void Load(string path, bool suppressOSD = false) - { - if (!File.Exists(path)) - { - Log($"could not find file: {path}"); - } - else - { - GlobalWin.MainForm.LoadState(path, Path.GetFileName(path), true, suppressOSD); - } - } + public void Load(string path, bool suppressOSD = false) => APIs.SaveState.Load(path, suppressOSD); [LuaMethodExample("savestate.loadslot( 7 );")] [LuaMethod("loadslot", "Loads the savestate at the given slot number (must be an integer between 0 and 9). If EmuHawk is deferring quicksaves, to TAStudio for example, that form will do what it likes with the slot number.")] - public void LoadSlot(int slotNum, bool suppressOSD = false) - { - if (slotNum >= 0 && slotNum <= 9) - { - GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", true, suppressOSD); - } - } + public void LoadSlot(int slotNum, bool suppressOSD = false) => APIs.SaveState.LoadSlot(slotNum, suppressOSD); [LuaMethodExample("savestate.save( \"C:\\state.bin\" );")] [LuaMethod("save", "Saves a state at the given path. If EmuHawk is deferring quicksaves, to TAStudio for example, that form will do what it likes (and the path is ignored).")] - public void Save(string path, bool suppressOSD = false) - { - GlobalWin.MainForm.SaveState(path, path, true, suppressOSD); - } + public void Save(string path, bool suppressOSD = false) => APIs.SaveState.Save(path, suppressOSD); [LuaMethodExample("savestate.saveslot( 7 );")] [LuaMethod("saveslot", "Saves a state at the given save slot (must be an integer between 0 and 9). If EmuHawk is deferring quicksaves, to TAStudio for example, that form will do what it likes with the slot number.")] - public void SaveSlot(int slotNum, bool suppressOSD = false) - { - if (slotNum >= 0 && slotNum <= 9) - { - GlobalWin.MainForm.SaveQuickSave($"QuickSave{slotNum}", true, suppressOSD); - } - } + public void SaveSlot(int slotNum, bool suppressOSD = false) => APIs.SaveState.SaveSlot(slotNum, suppressOSD); } }