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 = () =>