From 22ee71b0cf901cc5bfbac64390860f2c46acec5c Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 5 May 2020 12:40:57 -0500 Subject: [PATCH] refactor lua shenanigans in mainform, instead move frame based logic to lua console update hooks, control SupressScripts in lua itself not in mainform. Also, this breaks the logic that automatically stop countihng rerecords if loadstate happens from script, the logic appeared to be broken and can/should be managed by the script itself --- src/BizHawk.Client.Common/config/Config.cs | 2 + .../Api/Libraries/SaveStateAPI.cs | 5 +- src/BizHawk.Client.EmuHawk/MainForm.cs | 48 ++++---------- .../tools/BasicBot/BasicBot.cs | 6 +- .../tools/Lua/Libraries/ClientLuaLibrary.cs | 4 +- .../Lua/Libraries/SavestateLuaLibrary.cs | 40 ++++++++++-- .../tools/Lua/LuaConsole.cs | 49 +++++++++++++++ .../tools/Lua/LuaLibraries.cs | 1 + .../tools/Lua/Win32LuaLibraries.cs | 14 +++-- .../tools/ToolManager.cs | 63 ++++--------------- 10 files changed, 129 insertions(+), 103 deletions(-) diff --git a/src/BizHawk.Client.Common/config/Config.cs b/src/BizHawk.Client.Common/config/Config.cs index ea0b0313c1..5c9a199019 100644 --- a/src/BizHawk.Client.Common/config/Config.cs +++ b/src/BizHawk.Client.Common/config/Config.cs @@ -245,6 +245,8 @@ namespace BizHawk.Client.Common public RecentFiles RecentLua { get; set; } = new RecentFiles(8); public RecentFiles RecentLuaSession { get; set; } = new RecentFiles(8); public bool DisableLuaScriptsOnLoad { get; set; } + + // luaconsole-refactor TODO: move this to LuaConsole settings public bool RunLuaDuringTurbo { get; set; } = true; // Watch Settings diff --git a/src/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs b/src/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs index 723fdbc7f6..14aff2821d 100644 --- a/src/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs +++ b/src/BizHawk.Client.EmuHawk/Api/Libraries/SaveStateAPI.cs @@ -24,12 +24,13 @@ namespace BizHawk.Client.EmuHawk LogCallback($"could not find file: {path}"); return; } - GlobalWin.MainForm.LoadState(path, Path.GetFileName(path), true, suppressOSD); + + GlobalWin.MainForm.LoadState(path, Path.GetFileName(path), suppressOSD); } public void LoadSlot(int slotNum, bool suppressOSD) { - if (0.RangeTo(9).Contains(slotNum)) GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", true, suppressOSD); + if (0.RangeTo(9).Contains(slotNum)) GlobalWin.MainForm.LoadQuickSave($"QuickSave{slotNum}", suppressOSD); } public void Save(string path, bool suppressOSD) => GlobalWin.MainForm.SaveState(path, path, true, suppressOSD); diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index a0c1384fe0..58199e2e24 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -582,7 +582,7 @@ namespace BizHawk.Client.EmuHawk // autohold/autofire must not be affected by the following inputs Global.InputManager.ActiveController.Overrides(Global.InputManager.ButtonOverrideAdapter); - if (Tools.Has() && !SuppressLua) + if (Tools.Has()) { Tools.LuaConsole.ResumeScripts(false); } @@ -704,9 +704,6 @@ namespace BizHawk.Client.EmuHawk /// public bool InvisibleEmulation { get; set; } - // runloop won't exec lua - public bool SuppressLua { get; set; } - public long MouseWheelTracker { get; private set; } private int? _pauseOnFrame; @@ -721,9 +718,8 @@ namespace BizHawk.Client.EmuHawk if (value == null) // TODO: make an Event handler instead, but the logic here is that after turbo seeking, tools will want to do a real update when the emulator finally pauses { - bool skipScripts = !(Config.TurboSeek && !Config.RunLuaDuringTurbo && !SuppressLua); - Tools.UpdateToolsBefore(skipScripts); - Tools.UpdateToolsAfter(skipScripts); + Tools.UpdateToolsBefore(); + Tools.UpdateToolsAfter(); } } } @@ -1577,9 +1573,9 @@ namespace BizHawk.Client.EmuHawk } } - private void UpdateToolsAfter(bool fromLua = false) + private void UpdateToolsAfter() { - Tools.UpdateToolsAfter(fromLua); + Tools.UpdateToolsAfter(); HandleToggleLightAndLink(); } @@ -2916,11 +2912,6 @@ namespace BizHawk.Client.EmuHawk InputManager.ClickyVirtualPadController.FrameTick(); Global.InputManager.ButtonOverrideAdapter.FrameTick(); - if (Tools.Has() && !SuppressLua) - { - Tools.LuaConsole.LuaImp.CallFrameBeforeEvent(); - } - if (IsTurboing) { Tools.FastUpdateBefore(); @@ -3002,18 +2993,13 @@ namespace BizHawk.Client.EmuHawk PressFrameAdvance = false; - if (Tools.Has() && !SuppressLua) - { - Tools.LuaConsole.LuaImp.CallFrameAfterEvent(); - } - if (IsTurboing) { - Tools.FastUpdateAfter(SuppressLua); + Tools.FastUpdateAfter(); } else { - UpdateToolsAfter(SuppressLua); + UpdateToolsAfter(); } if (!PauseAvi && newFrame && !InvisibleEmulation) @@ -4042,7 +4028,7 @@ namespace BizHawk.Client.EmuHawk return int.Parse(slot.Substring(slot.Length - 1, 1)); } - public void LoadState(string path, string userFriendlyStateName, bool fromLua = false, bool suppressOSD = false) // Move to client.common + public void LoadState(string path, string userFriendlyStateName, bool suppressOSD = false) // Move to client.common { if (!Emulator.HasSavestates()) { @@ -4055,14 +4041,6 @@ namespace BizHawk.Client.EmuHawk return; } - // If from lua, disable counting rerecords - bool wasCountingRerecords = MovieSession.Movie.IsCountingRerecords; - - if (fromLua) - { - MovieSession.Movie.IsCountingRerecords = false; - } - if (SavestateManager.LoadStateFile(Emulator, path)) { GlobalWin.OSD.ClearGuiText(); @@ -4074,8 +4052,8 @@ namespace BizHawk.Client.EmuHawk } SetMainformMovieInfo(); - Tools.UpdateToolsBefore(fromLua); - UpdateToolsAfter(fromLua); + Tools.UpdateToolsBefore(); + UpdateToolsAfter(); UpdateToolsLoadstate(); InputManager.AutoFireController.ClearStarts(); @@ -4093,11 +4071,9 @@ namespace BizHawk.Client.EmuHawk { AddOnScreenMessage("Loadstate error!"); } - - MovieSession.Movie.IsCountingRerecords = wasCountingRerecords; } - public void LoadQuickSave(string quickSlotName, bool fromLua = false, bool suppressOSD = false) + public void LoadQuickSave(string quickSlotName, bool suppressOSD = false) { if (!Emulator.HasSavestates()) { @@ -4124,7 +4100,7 @@ namespace BizHawk.Client.EmuHawk return; } - LoadState(path, quickSlotName, fromLua, suppressOSD); + LoadState(path, quickSlotName, suppressOSD); } public void SaveState(string path, string userFriendlyStateName, bool fromLua = false, bool suppressOSD = false) diff --git a/src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs b/src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs index 30fe963046..9c55c79e81 100644 --- a/src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs +++ b/src/BizHawk.Client.EmuHawk/tools/BasicBot/BasicBot.cs @@ -449,7 +449,7 @@ namespace BizHawk.Client.EmuHawk StopBot(); _replayMode = true; _doNotUpdateValues = true; - MainForm.LoadQuickSave(SelectedSlot, false, true); // Triggers an UpdateValues call + MainForm.LoadQuickSave(SelectedSlot, true); // Triggers an UpdateValues call _doNotUpdateValues = false; _startFrame = Emulator.Frame; SetNormalSpeed(); @@ -797,7 +797,7 @@ namespace BizHawk.Client.EmuHawk } _currentBotAttempt = new BotAttempt { Attempt = Attempts }; - MainForm.LoadQuickSave(SelectedSlot, false, true); + MainForm.LoadQuickSave(SelectedSlot, true); } // Before this would have 2 additional hits before the frame even advanced, making the amount of inputs greater than the number of frames to test. @@ -936,7 +936,7 @@ namespace BizHawk.Client.EmuHawk } _doNotUpdateValues = true; - MainForm.LoadQuickSave(SelectedSlot, false, true); // Triggers an UpdateValues call + MainForm.LoadQuickSave(SelectedSlot, true); // Triggers an UpdateValues call _doNotUpdateValues = false; _targetFrame = Emulator.Frame + (int)FrameLengthNumeric.Value; diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs index aa90f467b9..c4c351da9b 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/ClientLuaLibrary.cs @@ -136,13 +136,13 @@ namespace BizHawk.Client.EmuHawk bool wasPaused = MainForm.EmulatorPaused; // can't re-enter lua while doing this - MainForm.SuppressLua = true; + GlobalWin.Tools.LuaConsole.LuaImp.SuppressLua = true; while (Emulator.Frame != frame) { MainForm.SeekFrameAdvance(); } - MainForm.SuppressLua = false; + GlobalWin.Tools.LuaConsole.LuaImp.SuppressLua = false; if (!wasPaused) { diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SavestateLuaLibrary.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SavestateLuaLibrary.cs index 98c8e04039..9d1b31d83f 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SavestateLuaLibrary.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Libraries/SavestateLuaLibrary.cs @@ -17,12 +17,44 @@ namespace BizHawk.Client.EmuHawk public override string Name => "savestate"; [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) => APIs.SaveState.Load(path, suppressOSD); + [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) + { + // TODO: find a non-global way to access LuaImp from Lua libraries! + var luaImp = GlobalWin.Tools.LuaConsole?.LuaImp; + if (luaImp != null) + { + luaImp.SuppressLua = true; + } + + APIs.SaveState.Load(path, suppressOSD); + + if (luaImp != null) + { + luaImp.SuppressLua = false; + } + } [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) => APIs.SaveState.LoadSlot(slotNum, suppressOSD); + [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) + { + // TODO: find a non-global way to access LuaImp from Lua libraries! + var luaImp = GlobalWin.Tools.LuaConsole?.LuaImp; + if (luaImp != null) + { + luaImp.SuppressLua = true; + } + + APIs.SaveState.LoadSlot(slotNum, suppressOSD); + + if (luaImp != null) + { + luaImp.SuppressLua = false; + } + } [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).")] diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index e238fff727..3bb507e41e 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -520,6 +520,45 @@ namespace BizHawk.Client.EmuHawk return result; } + protected override void UpdateBefore() + { + if (LuaImp.SuppressLua) + { + return; + } + + LuaImp.CallFrameBeforeEvent(); + LuaImp.StartLuaDrawing(); + } + + protected override void UpdateAfter() + { + if (LuaImp.SuppressLua) + { + return; + } + + LuaImp.CallFrameAfterEvent(); + ResumeScripts(true); + LuaImp.EndLuaDrawing(); + } + + protected override void FastUpdateBefore() + { + if (Config.RunLuaDuringTurbo) + { + UpdateBefore(); + } + } + + protected override void FastUpdateAfter() + { + if (Config.RunLuaDuringTurbo) + { + UpdateAfter(); + } + } + /// /// resumes suspended Co-routines /// @@ -531,6 +570,16 @@ namespace BizHawk.Client.EmuHawk return; } + if (LuaImp.SuppressLua) + { + return; + } + + if (MainForm.IsTurboing && !Config.RunLuaDuringTurbo) + { + return; + } + if (LuaImp.GuiLibrary?.SurfaceIsNull == true) { LuaImp.GuiLibrary.DrawNew("emu"); diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs index 3443e3ebaa..4f13afdcaa 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs @@ -18,6 +18,7 @@ namespace BizHawk.Client.EmuHawk public readonly LuaFileList ScriptList = new LuaFileList(); public bool IsRebootingCore { get; set; } // pretty hacky.. we don't want a lua script to be able to restart itself by rebooting the core + public bool SuppressLua { get; set; } public EventWaitHandle LuaWait { get; protected set; } public abstract void CallExitEvent(LuaFile lf); diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs index 4456858baa..834217990b 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs @@ -122,7 +122,7 @@ namespace BizHawk.Client.EmuHawk public override void StartLuaDrawing() { - if (ScriptList.Count != 0 && GuiLibrary.SurfaceIsNull) + if (ScriptList.Count != 0 && GuiLibrary.SurfaceIsNull && !SuppressLua) { GuiLibrary.DrawNew("emu"); } @@ -130,7 +130,7 @@ namespace BizHawk.Client.EmuHawk public override void EndLuaDrawing() { - if (ScriptList.Count != 0) + if (ScriptList.Count != 0 && !SuppressLua) { GuiLibrary.DrawFinish(); } @@ -157,12 +157,18 @@ namespace BizHawk.Client.EmuHawk public override void CallFrameBeforeEvent() { - EventsLibrary.CallFrameBeforeEvent(); + if (!SuppressLua) + { + EventsLibrary.CallFrameBeforeEvent(); + } } public override void CallFrameAfterEvent() { - EventsLibrary.CallFrameAfterEvent(); + if (!SuppressLua) + { + EventsLibrary.CallFrameAfterEvent(); + } } public override void CallExitEvent(LuaFile lf) diff --git a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs index e508198616..ee44e8e394 100644 --- a/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs +++ b/src/BizHawk.Client.EmuHawk/tools/ToolManager.cs @@ -461,30 +461,6 @@ namespace BizHawk.Client.EmuHawk .Where(t => typeof(IToolForm).IsAssignableFrom(t)) .Where(t => !t.IsInterface) .Where(IsAvailable); - - private void UpdateBefore() - { - foreach (var tool in _tools) - { - if (!tool.IsDisposed - || (tool is RamWatch && _config.DisplayRamWatch)) // RAM Watch hack, on screen display should run even if RAM Watch is closed - { - tool.UpdateValues(ToolFormUpdateType.PreFrame); - } - } - } - - private void UpdateAfter() - { - foreach (var tool in _tools) - { - if (!tool.IsDisposed - || (tool is RamWatch && _config.DisplayRamWatch)) // RAM Watch hack, on screen display should run even if RAM Watch is closed - { - tool.UpdateValues(ToolFormUpdateType.PostFrame); - } - } - } /// /// Calls UpdateValues() on an instance of T, if it exists @@ -665,33 +641,26 @@ namespace BizHawk.Client.EmuHawk return tool; } - public void UpdateToolsBefore(bool fromLua = false) + public void UpdateToolsBefore() { - if (Has()) + foreach (var tool in _tools) { - if (!fromLua) + if (!tool.IsDisposed + || (tool is RamWatch && _config.DisplayRamWatch)) // RAM Watch hack, on screen display should run even if RAM Watch is closed { - LuaConsole.LuaImp.StartLuaDrawing(); + tool.UpdateValues(ToolFormUpdateType.PreFrame); } } - - UpdateBefore(); } - public void UpdateToolsAfter(bool fromLua = false) + public void UpdateToolsAfter() { - if (!fromLua && Has()) + foreach (var tool in _tools) { - LuaConsole.ResumeScripts(true); - } - - UpdateAfter(); - - if (Has()) - { - if (!fromLua) + if (!tool.IsDisposed + || (tool is RamWatch && _config.DisplayRamWatch)) // RAM Watch hack, on screen display should run even if RAM Watch is closed { - LuaConsole.LuaImp.EndLuaDrawing(); + tool.UpdateValues(ToolFormUpdateType.PostFrame); } } } @@ -708,13 +677,8 @@ namespace BizHawk.Client.EmuHawk } } - public void FastUpdateAfter(bool fromLua = false) + public void FastUpdateAfter() { - if (!fromLua && _config.RunLuaDuringTurbo && Has()) - { - LuaConsole.ResumeScripts(true); - } - foreach (var tool in _tools) { if (!tool.IsDisposed @@ -723,11 +687,6 @@ namespace BizHawk.Client.EmuHawk tool.UpdateValues(ToolFormUpdateType.FastPostFrame); } } - - if (_config.RunLuaDuringTurbo && Has()) - { - LuaConsole.LuaImp.EndLuaDrawing(); - } } private static readonly Lazy> LazyAsmTypes = new Lazy>(() =>