From bb327261aedfc2d341915edd8e6d76ae5a2bc5ee Mon Sep 17 00:00:00 2001 From: pgrimsrud Date: Tue, 15 Dec 2015 01:22:44 -0700 Subject: [PATCH] Add a return value to the main function for various purposes. Add Lua function ExitCode to allow script writers to terminate the client with an exit code. --- BizHawk.Client.EmuHawk/GlobalWin.cs | 2 ++ BizHawk.Client.EmuHawk/MainForm.Events.cs | 6 ++++++ BizHawk.Client.EmuHawk/MainForm.cs | 4 +++- BizHawk.Client.EmuHawk/Program.cs | 20 +++++++++++-------- .../Lua/Libraries/EmuLuaLibrary.Client.cs | 9 +++++++++ output/Lua/tasjudy.lua | 2 +- output/Lua/tasjudy.py | 18 +++++++++++++++++ 7 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 output/Lua/tasjudy.py diff --git a/BizHawk.Client.EmuHawk/GlobalWin.cs b/BizHawk.Client.EmuHawk/GlobalWin.cs index 490c2a5a86..757abd40dc 100644 --- a/BizHawk.Client.EmuHawk/GlobalWin.cs +++ b/BizHawk.Client.EmuHawk/GlobalWin.cs @@ -24,5 +24,7 @@ namespace BizHawk.Client.EmuHawk public static OSDManager OSD = new OSDManager(); public static DisplayManager DisplayManager; public static GLManager GLManager; + + public static int ExitCode; } } diff --git a/BizHawk.Client.EmuHawk/MainForm.Events.cs b/BizHawk.Client.EmuHawk/MainForm.Events.cs index f7a1f38488..2851239f8b 100644 --- a/BizHawk.Client.EmuHawk/MainForm.Events.cs +++ b/BizHawk.Client.EmuHawk/MainForm.Events.cs @@ -662,6 +662,12 @@ namespace BizHawk.Client.EmuHawk _exit = true; } + public void CloseEmulator(int exitCode) + { + _exit = true; + _exitCode = exitCode; + } + #endregion #region Emulation Menu diff --git a/BizHawk.Client.EmuHawk/MainForm.cs b/BizHawk.Client.EmuHawk/MainForm.cs index 037e451dca..087fae8184 100644 --- a/BizHawk.Client.EmuHawk/MainForm.cs +++ b/BizHawk.Client.EmuHawk/MainForm.cs @@ -462,7 +462,7 @@ namespace BizHawk.Client.EmuHawk private bool _supressSyncSettingsWarning = false; - public void ProgramRunLoop() + public int ProgramRunLoop() { CheckMessages(); //can someone leave a note about why this is needed? LogConsole.PositionConsole(); @@ -545,6 +545,7 @@ namespace BizHawk.Client.EmuHawk } Shutdown(); + return _exitCode; } /// @@ -1322,6 +1323,7 @@ namespace BizHawk.Client.EmuHawk private bool _avwriterpad; private bool _exit; + private int _exitCode; private bool _exitRequestPending; private bool _runloopFrameProgress; private long _frameAdvanceTimestamp; diff --git a/BizHawk.Client.EmuHawk/Program.cs b/BizHawk.Client.EmuHawk/Program.cs index 96c37c97c6..407ed5fe98 100644 --- a/BizHawk.Client.EmuHawk/Program.cs +++ b/BizHawk.Client.EmuHawk/Program.cs @@ -34,15 +34,17 @@ namespace BizHawk.Client.EmuHawk } [STAThread] - static void Main(string[] args) + static int Main(string[] args) { - SubMain(args); + return SubMain(args); } //NoInlining should keep this code from getting jammed into Main() which would create dependencies on types which havent been setup by the resolver yet... or something like that [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)] - static void SubMain(string[] args) + static int SubMain(string[] args) { + GlobalWin.ExitCode = 0; + // this check has to be done VERY early. i stepped through a debug build with wrong .dll versions purposely used, // and there was a TypeLoadException before the first line of SubMain was reached (some static ColorType init?) // zero 25-dec-2012 - only do for public builds. its annoying during development @@ -55,7 +57,7 @@ namespace BizHawk.Client.EmuHawk if (thisversion != utilversion || thisversion != emulversion) { MessageBox.Show("Conflicting revisions found! Don't mix .dll versions!"); - return; + return -1; } } @@ -138,7 +140,7 @@ namespace BizHawk.Client.EmuHawk mf.Show(); mf.Text = title; - mf.ProgramRunLoop(); + GlobalWin.ExitCode = mf.ProgramRunLoop(); } } } @@ -169,13 +171,13 @@ namespace BizHawk.Client.EmuHawk if (System.Diagnostics.Debugger.IsAttached) { - mf.ProgramRunLoop(); + GlobalWin.ExitCode = mf.ProgramRunLoop(); } else { try { - mf.ProgramRunLoop(); + GlobalWin.ExitCode = mf.ProgramRunLoop(); } catch (Exception e) { @@ -227,6 +229,8 @@ namespace BizHawk.Client.EmuHawk // GlobalWin.GL.Dispose(); //((IDisposable)GlobalWin.IGL_GL).Dispose(); + //return 0 assuming things have gone well, non-zero values could be used as error codes or for scripting purposes + return GlobalWin.ExitCode; } //SubMain //declared here instead of a more usual place to avoid dependencies on the more usual place @@ -319,7 +323,7 @@ namespace BizHawk.Client.EmuHawk var title = MainForm.Text; MainForm.Show(); MainForm.Text = title; - (MainForm as MainForm).ProgramRunLoop(); + GlobalWin.ExitCode = (MainForm as MainForm).ProgramRunLoop(); } } diff --git a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs index e4dd65f42b..4b7b7e52ae 100644 --- a/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs +++ b/BizHawk.Client.EmuHawk/tools/Lua/Libraries/EmuLuaLibrary.Client.cs @@ -45,6 +45,15 @@ namespace BizHawk.Client.EmuHawk GlobalWin.MainForm.CloseEmulator(); } + [LuaMethodAttributes( + "exitCode", + "Closes the emulator and returns the provided code" + )] + public void CloseEmulatorWithCode(int exitCode) + { + GlobalWin.MainForm.CloseEmulator(exitCode); + } + [LuaMethodAttributes( "borderheight", "Gets the current height in pixels of the letter/pillarbox area (top side only) around the emu display surface, excluding the gameExtraPadding you've set. This function (the whole lot of them) should be renamed or refactored since the padding areas have got more complex." diff --git a/output/Lua/tasjudy.lua b/output/Lua/tasjudy.lua index 9cbfb5379d..8591a4711a 100644 --- a/output/Lua/tasjudy.lua +++ b/output/Lua/tasjudy.lua @@ -73,7 +73,7 @@ function endScript() client.pause() if exitOnResults then client.closerom() - client.exit() + client.exitCode(emu.framecount()) end end diff --git a/output/Lua/tasjudy.py b/output/Lua/tasjudy.py new file mode 100644 index 0000000000..50e334c44e --- /dev/null +++ b/output/Lua/tasjudy.py @@ -0,0 +1,18 @@ +import os +import datetime +from multiprocessing import Pool + +bizhawkPath = "" +romPath = "" +moviePath = "" + +def emu(arg): + ret = os.system(bizhawkPath + " " + romPath + " --movie=" + moviePath) + print("Ending %d with %d at %s" % (arg,ret,datetime.datetime.now().time())) + +if __name__ == '__main__': + print("Starting at %s" % datetime.datetime.now().time()) + p = Pool(processes=4) + p.map(emu,range(1)) + print("Ending parent at %s" % datetime.datetime.now().time()) +