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.

This commit is contained in:
adelikat 2020-07-12 12:58:39 -05:00
parent 021b1639c2
commit 40ad7ed67e
2 changed files with 26 additions and 5 deletions

View File

@ -48,7 +48,7 @@ namespace BizHawk.Client.Common
public void CallSaveStateEvent(string name) public void CallSaveStateEvent(string name)
{ {
var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateSave"); var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateSave").ToList();
try try
{ {
foreach (var lf in lfs) foreach (var lf in lfs)
@ -64,7 +64,7 @@ namespace BizHawk.Client.Common
public void CallLoadStateEvent(string name) public void CallLoadStateEvent(string name)
{ {
var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateLoad"); var lfs = RegisteredFunctions.Where(l => l.Event == "OnSavestateLoad").ToList();
try try
{ {
foreach (var lf in lfs) foreach (var lf in lfs)
@ -80,7 +80,7 @@ namespace BizHawk.Client.Common
public void CallFrameBeforeEvent() public void CallFrameBeforeEvent()
{ {
var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameStart"); var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameStart").ToList();
try try
{ {
foreach (var lf in lfs) foreach (var lf in lfs)
@ -96,7 +96,7 @@ namespace BizHawk.Client.Common
public void CallFrameAfterEvent() public void CallFrameAfterEvent()
{ {
var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameEnd"); var lfs = RegisteredFunctions.Where(l => l.Event == "OnFrameEnd").ToList();
try try
{ {
foreach (var lf in lfs) foreach (var lf in lfs)

View File

@ -14,7 +14,28 @@ namespace BizHawk.Client.Common
_function = function; _function = function;
Name = name ?? "Anonymous"; Name = name ?? "Anonymous";
Event = theEvent; 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(); Guid = Guid.NewGuid();
Callback = () => Callback = () =>