From 1e3324cfab61086df5d06be5b5fe611bf6f9a8db Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 12 Jul 2020 13:19:48 -0500 Subject: [PATCH] Registered Lua Functions dialog - update when a lua script unregisters a file, make dupating more robust to catch other possible scenarios where it failed to update --- .../lua/LuaFunctionList.cs | 27 ++++++++++++++++--- .../tools/Lua/LuaConsole.cs | 10 ++----- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/BizHawk.Client.Common/lua/LuaFunctionList.cs b/src/BizHawk.Client.Common/lua/LuaFunctionList.cs index aa535b19e1..e7b92fa819 100644 --- a/src/BizHawk.Client.Common/lua/LuaFunctionList.cs +++ b/src/BizHawk.Client.Common/lua/LuaFunctionList.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using System.Linq; @@ -9,11 +10,17 @@ namespace BizHawk.Client.Common public class LuaFunctionList : IEnumerable { private readonly List _functions = new List(); + + public Action ChangedCallback { get; set; } public NamedLuaFunction this[string guid] => _functions.FirstOrDefault(nlf => nlf.Guid.ToString() == guid); - public void Add(NamedLuaFunction nlf) => _functions.Add(nlf); + public void Add(NamedLuaFunction nlf) + { + _functions.Add(nlf); + Changed(); + } public bool Remove(NamedLuaFunction function, IEmulator emulator) { @@ -27,7 +34,13 @@ namespace BizHawk.Client.Common emulator.AsDebuggable().MemoryCallbacks.Remove(function.MemCallback); } - return _functions.Remove(function); + var result = _functions.Remove(function); + if (result) + { + Changed(); + } + + return result; } public void RemoveForFile(LuaFile file, IEmulator emulator) @@ -40,6 +53,11 @@ namespace BizHawk.Client.Common { Remove(function, emulator); } + + if (functionsToRemove.Count != 0) + { + Changed(); + } } public void Clear(IEmulator emulator) @@ -56,10 +74,13 @@ namespace BizHawk.Client.Common } _functions.Clear(); + Changed(); } public IEnumerator GetEnumerator() => _functions.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _functions.GetEnumerator(); + + private void Changed() => ChangedCallback?.Invoke(); } public static class LuaFunctionListExtensions diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index aee1ddf02b..349c8f2f87 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -195,6 +195,7 @@ namespace BizHawk.Client.EmuHawk SetColumns(); splitContainer1.SetDistanceOrDefault(Settings.SplitDistance, _defaultSplitDistance); + LuaImp.RegisteredFunctions.ChangedCallback = UpdateRegisteredFunctionsDialog; } private void BranchesMarkersSplit_SplitterMoved(object sender, SplitterEventArgs e) @@ -225,10 +226,7 @@ namespace BizHawk.Client.EmuHawk foreach (var file in runningScripts) { LuaImp.CallExitEvent(file); - LuaImp.RegisteredFunctions.RemoveForFile(file, Emulator); - UpdateRegisteredFunctionsDialog(); - file.Stop(); } } @@ -1009,8 +1007,7 @@ namespace BizHawk.Client.EmuHawk LuaImp.RegisteredFunctions.RemoveForFile(item, Emulator); LuaImp.ScriptList.Remove(item); } - - UpdateRegisteredFunctionsDialog(); + UpdateDialog(); } } @@ -1558,14 +1555,11 @@ namespace BizHawk.Client.EmuHawk { LuaImp.RegisteredFunctions.RemoveForFile(file, Emulator); // First remove any existing registered functions for this file EnableLuaFile(file); - UpdateRegisteredFunctionsDialog(); } else if (!file.Enabled && file.Thread != null) { LuaImp.CallExitEvent(file); LuaImp.RegisteredFunctions.RemoveForFile(file, Emulator); - UpdateRegisteredFunctionsDialog(); - LuaImp.CallExitEvent(file); file.Stop(); ReDraw();