diff --git a/BizHawk.MultiClient/LuaImplementation.cs b/BizHawk.MultiClient/LuaImplementation.cs index 3ead7d8dc6..430e947040 100644 --- a/BizHawk.MultiClient/LuaImplementation.cs +++ b/BizHawk.MultiClient/LuaImplementation.cs @@ -19,8 +19,9 @@ namespace BizHawk.MultiClient public string LuaLibraryList = ""; public EventWaitHandle LuaWait; public bool isRunning; - private Thread LuaThread; private int CurrentMemoryDomain = 0; //Main memory by default + List runningThreads = new List(); + Lua currThread; public LuaImplementation(LuaConsole passed) { @@ -33,8 +34,7 @@ namespace BizHawk.MultiClient public void Close() { lua = new Lua(); - LuaKillThread(); - LuaWait.Dispose(); + runningThreads.Clear(); } public void LuaRegister(Lua lua) @@ -105,36 +105,13 @@ namespace BizHawk.MultiClient LuaLibraryList += "client." + MultiClientFunctions[i] + "\n"; } } - private void LuaThreadFunction(object File) - { - string F = File.ToString(); - isRunning = true; - try - { - if (LuaThread != null) - lua.DoFile(F); - } - catch (Exception e) - { - if (LuaThread.ThreadState.ToString() != "AbortRequested") - { - MessageBox.Show("Exception caught. " + e.ToString()); - } - } - isRunning = false; - LuaWait.Set(); - } - - public void LuaKillThread() - { - if (LuaThread != null) - LuaThread.Abort(); - } - public void DoLuaFile(string File) { - LuaThread = new Thread(new ParameterizedThreadStart(LuaThreadFunction)); - LuaThread.Start(File); + var t = lua.NewThread(); + runningThreads.Add(t); + LuaRegister(t); + var main = t.LoadFile(File); + t.Push(main); //push main function on to stack for subsequent resuming } private int LuaInt(object lua_arg) @@ -164,6 +141,15 @@ namespace BizHawk.MultiClient return lua_result; } + public void ResumeScripts() + { + foreach (var t in runningThreads) + { + currThread = t; + t.Resume(0); + currThread = null; + } + } public void print(string s) { @@ -356,8 +342,7 @@ namespace BizHawk.MultiClient //---------------------------------------------------- public void emu_frameadvance() { - Global.MainForm.MainWait.WaitOne(); - LuaWait.Set(); + currThread.Yield(0); } public void emu_pause() diff --git a/BizHawk.MultiClient/LuaInterface.dll b/BizHawk.MultiClient/LuaInterface.dll index 903c9daa6d..7b7f101159 100644 Binary files a/BizHawk.MultiClient/LuaInterface.dll and b/BizHawk.MultiClient/LuaInterface.dll differ diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index d9290b43f9..d32b81e2e5 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -1683,16 +1683,9 @@ namespace BizHawk.MultiClient Global.RenderPanel.ClearGUIText(); //client input-related duties #if WINDOWS - if (LuaConsole1.IsRunning()) + //if (LuaConsole1.IsRunning()) { - Global.MainForm.MainWait.Set(); - for (; ; ) - { - //we need to run DoEvents in here so that we can use Control.Invoke to interact with the gui - //its all a godawful mess - if (LuaConsole1.WaitOne(0)) break; - Application.DoEvents(); - } + LuaConsole1.ResumeScripts(); } #endif diff --git a/BizHawk.MultiClient/lua51.dll b/BizHawk.MultiClient/lua51.dll index 70551fad95..2d1d374295 100644 Binary files a/BizHawk.MultiClient/lua51.dll and b/BizHawk.MultiClient/lua51.dll differ diff --git a/BizHawk.MultiClient/tools/LuaConsole.cs b/BizHawk.MultiClient/tools/LuaConsole.cs index c7ade74b42..eae41166ad 100644 --- a/BizHawk.MultiClient/tools/LuaConsole.cs +++ b/BizHawk.MultiClient/tools/LuaConsole.cs @@ -720,6 +720,11 @@ namespace BizHawk.MultiClient OpenLuaSession(); } + public void ResumeScripts() + { + LuaImp.ResumeScripts(); + } + public bool IsRunning() { if (!this.IsHandleCreated || this.IsDisposed)