diff --git a/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs b/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs index 7e2a55cc84..527cbbe551 100644 --- a/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs +++ b/BizHawk.Client.Common/lua/EmuLuaLibrary.Events.cs @@ -33,9 +33,12 @@ namespace BizHawk.Client.Common #region Events Library Helpers - public void CallExitEvent(Lua thread) + public void CallExitEvent(LuaFile luaFile) { - var exitCallbacks = RegisteredFunctions.Where(l => l.Lua == thread && l.Event == "OnExit"); + var exitCallbacks = RegisteredFunctions + .ForFile(luaFile) + .ForEvent("OnExit"); + foreach (var exitCallback in exitCallbacks) { exitCallback.Call(); @@ -125,7 +128,7 @@ namespace BizHawk.Client.Common [LuaMethod("onframeend", "Calls the given lua function at the end of each frame, after all emulation and drawing has completed. Note: this is the default behavior of lua scripts")] public string OnFrameEnd(LuaFunction luaf, string name = null) { - var nlf = new NamedLuaFunction(luaf, "OnFrameEnd", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnFrameEnd", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); return nlf.Guid.ToString(); } @@ -134,7 +137,7 @@ namespace BizHawk.Client.Common [LuaMethod("onframestart", "Calls the given lua function at the beginning of each frame before any emulation and drawing occurs")] public string OnFrameStart(LuaFunction luaf, string name = null) { - var nlf = new NamedLuaFunction(luaf, "OnFrameStart", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnFrameStart", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); return nlf.Guid.ToString(); } @@ -143,7 +146,7 @@ namespace BizHawk.Client.Common [LuaMethod("oninputpoll", "Calls the given lua function after each time the emulator core polls for input")] public string OnInputPoll(LuaFunction luaf, string name = null) { - var nlf = new NamedLuaFunction(luaf, "OnInputPoll", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnInputPoll", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); if (InputPollableCore != null) @@ -173,7 +176,7 @@ namespace BizHawk.Client.Common [LuaMethod("onloadstate", "Fires after a state is loaded. Receives a lua function name, and registers it to the event immediately following a successful savestate event")] public string OnLoadState(LuaFunction luaf, string name = null) { - var nlf = new NamedLuaFunction(luaf, "OnSavestateLoad", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnSavestateLoad", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); return nlf.Guid.ToString(); } @@ -187,7 +190,7 @@ namespace BizHawk.Client.Common if (DebuggableCore != null && DebuggableCore.MemoryCallbacksAvailable() && DebuggableCore.MemoryCallbacks.ExecuteCallbacksAvailable) { - var nlf = new NamedLuaFunction(luaf, "OnMemoryExecute", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnMemoryExecute", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); if (string.IsNullOrWhiteSpace(domain)) @@ -221,7 +224,7 @@ namespace BizHawk.Client.Common { if (DebuggableCore != null && DebuggableCore.MemoryCallbacksAvailable()) { - var nlf = new NamedLuaFunction(luaf, "OnMemoryRead", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnMemoryRead", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); if (string.IsNullOrWhiteSpace(domain)) @@ -255,7 +258,7 @@ namespace BizHawk.Client.Common { if (DebuggableCore != null && DebuggableCore.MemoryCallbacksAvailable()) { - var nlf = new NamedLuaFunction(luaf, "OnMemoryWrite", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnMemoryWrite", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); if (string.IsNullOrWhiteSpace(domain)) @@ -285,7 +288,7 @@ namespace BizHawk.Client.Common [LuaMethod("onsavestate", "Fires after a state is saved")] public string OnSaveState(LuaFunction luaf, string name = null) { - var nlf = new NamedLuaFunction(luaf, "OnSavestateSave", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnSavestateSave", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); return nlf.Guid.ToString(); } @@ -294,7 +297,7 @@ namespace BizHawk.Client.Common [LuaMethod("onexit", "Fires after the calling script has stopped")] public string OnExit(LuaFunction luaf, string name = null) { - var nlf = new NamedLuaFunction(luaf, "OnExit", LogOutputCallback, CurrentThread, name); + var nlf = new NamedLuaFunction(luaf, "OnExit", LogOutputCallback, CurrentFile, name); RegisteredFunctions.Add(nlf); return nlf.Guid.ToString(); } diff --git a/BizHawk.Client.Common/lua/LuaFunctionList.cs b/BizHawk.Client.Common/lua/LuaFunctionList.cs index 87d36e9d3b..ab78b6fd3c 100644 --- a/BizHawk.Client.Common/lua/LuaFunctionList.cs +++ b/BizHawk.Client.Common/lua/LuaFunctionList.cs @@ -7,13 +7,8 @@ namespace BizHawk.Client.Common { public class LuaFunctionList : List { - public NamedLuaFunction this[string guid] - { - get - { - return this.FirstOrDefault(nlf => nlf.Guid.ToString() == guid); - } - } + public NamedLuaFunction this[string guid] => + this.FirstOrDefault(nlf => nlf.Guid.ToString() == guid); public new bool Remove(NamedLuaFunction function) { @@ -30,6 +25,18 @@ namespace BizHawk.Client.Common return base.Remove(function); } + public void RemoveForFile(LuaFile file) + { + var functionsToRemove = this + .ForFile(file) + .ToList(); + + foreach (var function in functionsToRemove) + { + Remove(function); + } + } + public void ClearAll() { if (Global.Emulator.InputCallbacksAvailable()) @@ -46,4 +53,18 @@ namespace BizHawk.Client.Common Clear(); } } + + public static class LuaFunctionListExtensions + { + public static IEnumerable ForFile(this IEnumerable list, LuaFile luaFile) + { + return list + .Where(l => l.LuaFile.Thread == luaFile.Thread); + } + + public static IEnumerable ForEvent(this IEnumerable list, string eventName) + { + return list.Where(l => l.Event == eventName); + } + } } diff --git a/BizHawk.Client.Common/lua/LuaLibraryBase.cs b/BizHawk.Client.Common/lua/LuaLibraryBase.cs index 04fe2ff03d..bc044f5652 100644 --- a/BizHawk.Client.Common/lua/LuaLibraryBase.cs +++ b/BizHawk.Client.Common/lua/LuaLibraryBase.cs @@ -21,7 +21,7 @@ namespace BizHawk.Client.Common LogOutputCallback = logOutputCallback; } - protected static Lua CurrentThread { get; private set; } + protected static LuaFile CurrentFile { get; private set; } private static Thread CurrentHostThread; private static readonly object ThreadMutex = new object(); @@ -35,11 +35,11 @@ namespace BizHawk.Client.Common lock (ThreadMutex) { CurrentHostThread = null; - CurrentThread = null; + CurrentFile = null; } } - public static void SetCurrentThread(Lua luaThread) + public static void SetCurrentThread(LuaFile luaFile) { lock (ThreadMutex) { @@ -49,7 +49,7 @@ namespace BizHawk.Client.Common } CurrentHostThread = Thread.CurrentThread; - CurrentThread = luaThread; + CurrentFile = luaFile; } } diff --git a/BizHawk.Client.Common/lua/NamedLuaFunction.cs b/BizHawk.Client.Common/lua/NamedLuaFunction.cs index 7fab13e7a4..adaf443a48 100644 --- a/BizHawk.Client.Common/lua/NamedLuaFunction.cs +++ b/BizHawk.Client.Common/lua/NamedLuaFunction.cs @@ -9,12 +9,12 @@ namespace BizHawk.Client.Common { private readonly LuaFunction _function; - public NamedLuaFunction(LuaFunction function, string theEvent, Action logCallback, Lua lua, string name = null) + public NamedLuaFunction(LuaFunction function, string theEvent, Action logCallback, LuaFile luaFile, string name = null) { _function = function; Name = name ?? "Anonymous"; Event = theEvent; - Lua = lua; + LuaFile = luaFile; Guid = Guid.NewGuid(); Callback = delegate @@ -39,7 +39,7 @@ namespace BizHawk.Client.Common public string Name { get; } - public Lua Lua { get; } + public LuaFile LuaFile { get; } public string Event { get; } @@ -49,7 +49,7 @@ namespace BizHawk.Client.Common public void Call(string name = null) { - LuaSandbox.Sandbox(Lua, () => + LuaSandbox.Sandbox(LuaFile.Thread, () => { _function.Call(name); }); diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Forms.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Forms.cs index 869843689a..f1a53542b8 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Forms.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Forms.cs @@ -302,9 +302,9 @@ namespace BizHawk.Client.EmuHawk { if (control.Handle == ptr) { - if (control is LuaCheckbox) + if (control is LuaCheckbox checkbox) { - return (control as LuaCheckbox).Checked; + return checkbox.Checked; } return false; @@ -360,7 +360,7 @@ namespace BizHawk.Client.EmuHawk "newform", "creates a new default dialog, if both width and height are specified it will create a dialog of the specified size. If title is specified it will be the caption of the dialog, else the dialog caption will be 'Lua Dialog'. The function will return an int representing the handle of the dialog created.")] public int NewForm(int? width = null, int? height = null, string title = null, LuaFunction onClose = null) { - var form = new LuaWinform(CurrentThread); + var form = new LuaWinform(CurrentFile); _luaForms.Add(form); if (width.HasValue && height.HasValue) { diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs index 0a70354297..f89149e2ae 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -162,14 +162,9 @@ namespace BizHawk.Client.EmuHawk EventsLibrary.CallFrameAfterEvent(); } - public void CallExitEvent(Lua thread) - { - EventsLibrary.CallExitEvent(thread); - } - public override void CallExitEvent(LuaFile lf) { - CallExitEvent(lf.Thread); + EventsLibrary.CallExitEvent(lf); } public override void Close() @@ -214,7 +209,7 @@ namespace BizHawk.Client.EmuHawk try { - LuaLibraryBase.SetCurrentThread(_currThread); + LuaLibraryBase.SetCurrentThread(lf); var execResult = _currThread.Resume(0); diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs index 508140e9a6..d273911047 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaConsole.cs @@ -183,8 +183,7 @@ namespace BizHawk.Client.EmuHawk { LuaImp.CallExitEvent(file); - LuaImp.RegisteredFunctions.RemoveAll(lf => lf.Lua == file.Thread); - + LuaImp.RegisteredFunctions.RemoveForFile(file); UpdateRegisteredFunctionsDialog(); file.Stop(); @@ -904,12 +903,10 @@ namespace BizHawk.Client.EmuHawk { foreach (var item in items) { - var temp = item; - LuaImp.RegisteredFunctions.RemoveAll(x => x.Lua == temp.Thread); - + LuaImp.RegisteredFunctions.RemoveForFile(item); LuaImp.ScriptList.Remove(item); } - + UpdateRegisteredFunctionsDialog(); UpdateDialog(); } @@ -1475,8 +1472,7 @@ namespace BizHawk.Client.EmuHawk foreach (var selectedItem in SelectedItems) { - var temp = selectedItem; - LuaImp.RegisteredFunctions.RemoveAll(lf => lf.Lua == temp.Thread); + LuaImp.RegisteredFunctions.RemoveForFile(selectedItem); UpdateRegisteredFunctionsDialog(); } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/LuaWinform.cs b/BizHawk.Client.EmuHawk/tools/Lua/LuaWinform.cs index e2c4b21e85..e47325c0d3 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/LuaWinform.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/LuaWinform.cs @@ -13,12 +13,12 @@ namespace BizHawk.Client.EmuHawk public List ControlEvents { get; } = new List(); private readonly string _currentDirectory = Environment.CurrentDirectory; - private readonly Lua _ownerThread; + private readonly LuaFile _ownerFile; - public LuaWinform(Lua ownerThread) + public LuaWinform(LuaFile ownerFile) { InitializeComponent(); - _ownerThread = ownerThread; + _ownerFile = ownerFile; StartPosition = FormStartPosition.CenterParent; Closing += (o, e) => CloseThis(); } @@ -34,7 +34,7 @@ namespace BizHawk.Client.EmuHawk public void DoLuaEvent(IntPtr handle) { - LuaSandbox.Sandbox(_ownerThread, () => + LuaSandbox.Sandbox(_ownerFile.Thread, () => { Environment.CurrentDirectory = _currentDirectory; foreach (LuaEvent luaEvent in ControlEvents) @@ -49,9 +49,9 @@ namespace BizHawk.Client.EmuHawk public class LuaEvent { - public LuaEvent(IntPtr handle, LuaFunction lfunction) + public LuaEvent(IntPtr handle, LuaFunction luaFunction) { - Event = lfunction; + Event = luaFunction; Control = handle; }