From 40ad7ed67e16ce3ca6107f9b169055730d9975b1 Mon Sep 17 00:00:00 2001 From: adelikat Date: Sun, 12 Jul 2020 12:58:39 -0500 Subject: [PATCH] Lua - account for callbacks with no parent script creating more callbacks by generating a mock lua file with a new thread, and register the thread with the sandbox. Fixes #1983. Not super proud of this. --- .../lua/LuaHelperLibs/EventsLuaLibrary.cs | 8 +++---- .../lua/NamedLuaFunction.cs | 23 ++++++++++++++++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs index 43a2b89740..58f5a2b995 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs @@ -48,7 +48,7 @@ namespace BizHawk.Client.Common public void CallSaveStateEvent(string name) { - var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateSave"); + var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateSave").ToList(); try { foreach (var lf in lfs) @@ -64,7 +64,7 @@ namespace BizHawk.Client.Common public void CallLoadStateEvent(string name) { - var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateLoad"); + var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateLoad").ToList(); try { foreach (var lf in lfs) @@ -80,7 +80,7 @@ namespace BizHawk.Client.Common public void CallFrameBeforeEvent() { - var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameStart"); + var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameStart").ToList(); try { foreach (var lf in lfs) @@ -96,7 +96,7 @@ namespace BizHawk.Client.Common public void CallFrameAfterEvent() { - var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameEnd"); + var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameEnd").ToList(); try { foreach (var lf in lfs) diff --git a/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs b/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs index 7174108dfa..089e9949cc 100644 --- a/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs +++ b/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs @@ -14,7 +14,28 @@ namespace BizHawk.Client.Common _function = function; Name = name ?? "Anonymous"; Event = theEvent; - LuaFile = luaFile; + + // When would a file be null? + // When a script is loaded with a callback, but no infinite loop so it closes + // Then that callback proceeds to register more callbacks + // In these situations, we will generate a thread for this new callback on the fly here + // Scenarios like this suggest that a thread being managed by a LuaFile is a bad idea, + // and we should refactor + if (luaFile == null) + { + var thread = new Lua(); + + // Current dir will have to do for now, but this will inevitably not be desired + // Users will expect it to be the same directly as the thread that spawned this callback + // But how do we know what that directory was? + LuaSandbox.CreateSandbox(thread, "."); + LuaFile = new LuaFile(".") { Thread = thread }; + } + else + { + LuaFile = luaFile; + } + Guid = Guid.NewGuid(); Callback = () =>