From 4bb60ec8c9d30fd9b2ee82304016c3005e536fe6 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 31 Oct 2013 18:09:40 +0000 Subject: [PATCH] move emu library to its own object, remove emu_onsnoop (should use event library instead), moved many multiclient only functions into the client library instead (todo: document those) --- .../Lua/Libraries/EmuLuaLibrary.Client.cs | 86 +++++++++ .../tools/Lua/Libraries/EmuLuaLibrary.Emu.cs | 163 +++++------------- .../tools/Lua/Libraries/EmuLuaLibrary.cs | 65 ++----- .../tools/Lua/Libraries/LuaLibraryBase.cs | 18 ++ 4 files changed, 164 insertions(+), 168 deletions(-) diff --git a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Client.cs b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Client.cs index 61806c612e..3a1451c477 100644 --- a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Client.cs +++ b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Client.cs @@ -21,7 +21,10 @@ namespace BizHawk.MultiClient return new [] { "closerom", + "enablerewind", + "frameskip", "getwindowsize", + "ispaused", "opencheats", "openhexeditor", "openramwatch", @@ -30,6 +33,7 @@ namespace BizHawk.MultiClient "opentasstudio", "opentoolbox", "opentracelogger", + "pause", "pause_av", "reboot_core", "screenheight", @@ -38,6 +42,9 @@ namespace BizHawk.MultiClient "screenwidth", "setscreenshotosd", "setwindowsize", + "speedmode", + "togglepause", + "unpause", "unpause_av", "xpos", "ypos", @@ -52,6 +59,51 @@ namespace BizHawk.MultiClient GlobalWinF.MainForm.CloseROM(); } + public static void client_enablerewind(object boolean) + { + string temp = boolean.ToString(); + if (!String.IsNullOrWhiteSpace(temp)) + { + if (temp == "0" || temp.ToLower() == "false") + { + GlobalWinF.MainForm.RewindActive = false; + GlobalWinF.OSD.AddMessage("Rewind suspended"); + } + else + { + GlobalWinF.MainForm.RewindActive = true; + GlobalWinF.OSD.AddMessage("Rewind enabled"); + } + } + } + + public void client_frameskip(object num_frames) + { + try + { + string temp = num_frames.ToString(); + int frames = Convert.ToInt32(temp); + if (frames > 0) + { + Global.Config.FrameSkip = frames; + GlobalWinF.MainForm.FrameSkipMessage(); + } + else + { + ConsoleLuaLibrary.console_log("Invalid frame skip value"); + } + } + catch + { + ConsoleLuaLibrary.console_log("Invalid frame skip value"); + } + } + + public static bool client_ispaused() + { + return GlobalWinF.MainForm.EmulatorPaused; + } + public static int client_getwindowsize() { return Global.Config.TargetZoomFactor; @@ -97,6 +149,11 @@ namespace BizHawk.MultiClient GlobalWinF.MainForm.LoadTraceLogger(); } + public static void client_pause() + { + GlobalWinF.MainForm.PauseEmulator(); + } + public static void client_pause_av() { GlobalWinF.MainForm.PauseAVI = true; @@ -163,7 +220,36 @@ namespace BizHawk.MultiClient { LogOutputCallback("Invalid window size"); } + } + public void client_speedmode(object percent) + { + try + { + int speed = Convert.ToInt32(percent.ToString()); + if (speed > 0 && speed < 1600) //arbituarily capping it at 1600% + { + GlobalWinF.MainForm.ClickSpeedItem(speed); + } + else + { + ConsoleLuaLibrary.console_log("Invalid speed value"); + } + } + catch + { + ConsoleLuaLibrary.console_log("Invalid speed value"); + } + } + + public static void client_togglepause() + { + GlobalWinF.MainForm.TogglePause(); + } + + public static void client_unpause() + { + GlobalWinF.MainForm.UnpauseEmulator(); } public static void client_unpause_av() diff --git a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Emu.cs b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Emu.cs index 1d7db1cf33..168e676c91 100644 --- a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Emu.cs +++ b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Emu.cs @@ -5,12 +5,40 @@ using BizHawk.Emulation.Consoles.Nintendo; namespace BizHawk.MultiClient { - public partial class EmuLuaLibrary + public partial class EmulatorLuaLibrary : LuaLibraryBase { - #region Emu Library Helpers + public EmulatorLuaLibrary(Action frameAdvanceCallback, Action yieldCallback) + : base() + { + _frameAdvanceCallback = frameAdvanceCallback; + _yieldCallback = yieldCallback; + } - // TODO: error handling for argument count mismatch - private void emu_setrenderplanes_do(object[] lua_p) + public override string Name { get { return "emu"; } } + public override string[] Functions + { + get + { + return new[] + { + "displayvsync", + "frameadvance", + "framecount", + "getsystemid", + "islagged", + "lagcount", + "limitframerate", + "minimizeframeskip", + "setrenderplanes", + "yield", + }; + } + } + + private Action _frameAdvanceCallback; + private Action _yieldCallback; + + private static void emu_setrenderplanes_do(object[] lua_p) { if (Global.Emulator is NES) { @@ -34,9 +62,7 @@ namespace BizHawk.MultiClient } } - #endregion - - public void emu_displayvsync(object boolean) + public static void emu_displayvsync(object boolean) { string temp = boolean.ToString(); if (!String.IsNullOrWhiteSpace(temp)) @@ -53,78 +79,32 @@ namespace BizHawk.MultiClient } } - public void emu_enablerewind(object boolean) - { - string temp = boolean.ToString(); - if (!String.IsNullOrWhiteSpace(temp)) - { - if (temp == "0" || temp.ToLower() == "false") - { - GlobalWinF.MainForm.RewindActive = false; - GlobalWinF.OSD.AddMessage("Rewind suspended"); - } - else - { - GlobalWinF.MainForm.RewindActive = true; - GlobalWinF.OSD.AddMessage("Rewind enabled"); - } - } - } - public void emu_frameadvance() { - FrameAdvanceRequested = true; - currThread.Yield(0); + _frameAdvanceCallback(); } - public int emu_framecount() + public static int emu_framecount() { return Global.Emulator.Frame; } - public void emu_frameskip(object num_frames) - { - try - { - string temp = num_frames.ToString(); - int frames = Convert.ToInt32(temp); - if (frames > 0) - { - Global.Config.FrameSkip = frames; - GlobalWinF.MainForm.FrameSkipMessage(); - } - else - { - ConsoleLuaLibrary.console_log("Invalid frame skip value"); - } - } - catch - { - ConsoleLuaLibrary.console_log("Invalid frame skip value"); - } - } - - public string emu_getsystemid() + public static string emu_getsystemid() { return Global.Emulator.SystemId; } - public bool emu_islagged() + public static bool emu_islagged() { return Global.Emulator.IsLagFrame; } - public bool emu_ispaused() - { - return GlobalWinF.MainForm.EmulatorPaused; - } - - public int emu_lagcount() + public static int emu_lagcount() { return Global.Emulator.LagCount; } - public void emu_limitframerate(object boolean) + public static void emu_limitframerate(object boolean) { string temp = boolean.ToString(); if (!String.IsNullOrWhiteSpace(temp)) @@ -141,7 +121,7 @@ namespace BizHawk.MultiClient } } - public void emu_minimizeframeskip(object boolean) + public static void emu_minimizeframeskip(object boolean) { string temp = boolean.ToString(); if (!String.IsNullOrWhiteSpace(temp)) @@ -158,75 +138,16 @@ namespace BizHawk.MultiClient } } - public void emu_on_snoop(LuaFunction luaf) - { - if (luaf != null) - { - Global.Emulator.CoreComm.InputCallback = delegate - { - try - { - luaf.Call(); - } - catch (SystemException e) - { - GlobalWinF.MainForm.LuaConsole1.WriteToOutputWindow( - "error running function attached by lua function emu.on_snoop" + - "\nError message: " + e.Message); - } - }; - } - else - Global.Emulator.CoreComm.InputCallback = null; - } - - public void emu_pause() - { - GlobalWinF.MainForm.PauseEmulator(); - } - - public void emu_setrenderplanes( // For now, it accepts arguments up to 5. + public static void emu_setrenderplanes( // For now, it accepts arguments up to 5. object lua_p0, object lua_p1 = null, object lua_p2 = null, object lua_p3 = null, object lua_p4 = null) { emu_setrenderplanes_do(LuaVarArgs(lua_p0, lua_p1, lua_p2, lua_p3, lua_p4)); } - public void emu_speedmode(object percent) - { - try - { - string temp = percent.ToString(); - int speed = Convert.ToInt32(temp); - if (speed > 0 && speed < 1000) //arbituarily capping it at 1000% - { - GlobalWinF.MainForm.ClickSpeedItem(speed); - } - else - { - ConsoleLuaLibrary.console_log("Invalid speed value"); - } - } - catch - { - ConsoleLuaLibrary.console_log("Invalid speed value"); - } - } - - public void emu_togglepause() - { - GlobalWinF.MainForm.TogglePause(); - } - - public void emu_unpause() - { - GlobalWinF.MainForm.UnpauseEmulator(); - } - public void emu_yield() { - GlobalWinF.DisplayManager.NeedsToPaint = true; - currThread.Yield(0); + _yieldCallback(); } } } diff --git a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs index 6ee6774dbc..21556910b7 100644 --- a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -86,28 +86,6 @@ namespace BizHawk.MultiClient "text", }; - public static string[] EmuFunctions = new[] - { - "displayvsync", - "enablerewind", - "frameadvance", - "framecount", - "frameskip", - "getsystemid", - "islagged", - "ispaused", - "lagcount", - "limitframerate", - "minimizeframeskip", - "on_snoop", - "pause", - "setrenderplanes", - "speedmode", - "togglepause", - "unpause", - "yield", - }; - public void LuaRegister(Lua lua) { lua.RegisterFunction("print", this, GetType().GetMethod("print")); @@ -115,6 +93,12 @@ namespace BizHawk.MultiClient new BitLuaLibrary().LuaRegister(lua, Docs); new MultiClientLuaLibrary(ConsoleLuaLibrary.console_log).LuaRegister(lua, Docs); new ConsoleLuaLibrary().LuaRegister(lua, Docs); + + new EmulatorLuaLibrary( + new Action(Frameadvance), + new Action(EmuYield) + ).LuaRegister(lua, Docs); + _eventLibrary.LuaRegister(lua, Docs); _formsLibrary.LuaRegister(lua, Docs); new InputLuaLibrary(_lua).LuaRegister(lua, Docs); @@ -133,13 +117,6 @@ namespace BizHawk.MultiClient Docs.Add("gui", t, GetType().GetMethod("gui_" + t)); } - lua.NewTable("emu"); - foreach (string t in EmuFunctions) - { - lua.RegisterFunction("emu." + t, this, GetType().GetMethod("emu_" + t)); - Docs.Add("emu", t, GetType().GetMethod("emu_" + t)); - } - Docs.Sort(); } @@ -156,24 +133,6 @@ namespace BizHawk.MultiClient return t; } - /// - /// LuaInterface requires the exact match of parameter count, except optional parameters. - /// So, if you want to support variable arguments, declare them as optional and pass - /// them to this method. - /// - /// - /// - private object[] LuaVarArgs(params object[] lua_args) - { - int n = lua_args.Length; - int trim = 0; - for (int i = n - 1; i >= 0; --i) - if (lua_args[i] == null) ++trim; - object[] lua_result = new object[n - trim]; - Array.Copy(lua_args, lua_result, n - trim); - return lua_result; - } - public class ResumeResult { public bool WaitForFrame; @@ -205,6 +164,18 @@ namespace BizHawk.MultiClient _caller.AddText(s); } + private void Frameadvance() + { + FrameAdvanceRequested = true; + currThread.Yield(0); + } + + private void EmuYield() + { + GlobalWinF.DisplayManager.NeedsToPaint = true; + currThread.Yield(0); + } + #endregion } } diff --git a/BizHawk.MultiClient/tools/Lua/Libraries/LuaLibraryBase.cs b/BizHawk.MultiClient/tools/Lua/Libraries/LuaLibraryBase.cs index 42af94d7b0..868560993e 100644 --- a/BizHawk.MultiClient/tools/Lua/Libraries/LuaLibraryBase.cs +++ b/BizHawk.MultiClient/tools/Lua/Libraries/LuaLibraryBase.cs @@ -39,5 +39,23 @@ namespace BizHawk.MultiClient { return Convert.ToUInt32((double)lua_arg); } + + /// + /// LuaInterface requires the exact match of parameter count, except optional parameters. + /// So, if you want to support variable arguments, declare them as optional and pass + /// them to this method. + /// + /// + /// + protected static object[] LuaVarArgs(params object[] lua_args) + { + int n = lua_args.Length; + int trim = 0; + for (int i = n - 1; i >= 0; --i) + if (lua_args[i] == null) ++trim; + object[] lua_result = new object[n - trim]; + Array.Copy(lua_args, lua_result, n - trim); + return lua_result; + } } }