From d7d83c9275f26e187194fc13e5bb96a589c81695 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 31 Oct 2013 16:58:56 +0000 Subject: [PATCH] move event library to its own object, slight tweak to anonymous function name, double clicking on registered functions dialog invokes the method clicked --- BizHawk.Client.Common/lua/NamedLuaFunction.cs | 2 +- BizHawk.MultiClient/MainForm.cs | 8 +-- .../Lua/Libraries/EmuLuaLibrary.Events.cs | 51 ++++++++++++++++--- .../tools/Lua/Libraries/EmuLuaLibrary.cs | 49 ++++++++++-------- .../LuaRegisteredFunctionsList.Designer.cs | 1 + .../tools/Lua/LuaRegisteredFunctionsList.cs | 5 ++ 6 files changed, 85 insertions(+), 31 deletions(-) diff --git a/BizHawk.Client.Common/lua/NamedLuaFunction.cs b/BizHawk.Client.Common/lua/NamedLuaFunction.cs index f36e173d42..a111901a57 100644 --- a/BizHawk.Client.Common/lua/NamedLuaFunction.cs +++ b/BizHawk.Client.Common/lua/NamedLuaFunction.cs @@ -12,7 +12,7 @@ namespace BizHawk.Client.Common public NamedLuaFunction(LuaFunction function, string theevent, string name = null) { _function = function; - _name = name ?? "Anonymous Function"; + _name = name ?? "Anonymous"; _event = theevent; GUID = Guid.NewGuid(); } diff --git a/BizHawk.MultiClient/MainForm.cs b/BizHawk.MultiClient/MainForm.cs index 4adbe27f03..457b897025 100644 --- a/BizHawk.MultiClient/MainForm.cs +++ b/BizHawk.MultiClient/MainForm.cs @@ -2351,7 +2351,7 @@ namespace BizHawk.MultiClient if (_luaconsole != null) { if (!fromLua) LuaConsole1.StartLuaDrawing(); - LuaConsole1.LuaImp.FrameRegisterBefore(); + LuaConsole1.LuaImp.CallFrameBeforeEvent(); } #endif if (_nesnametableview != null) NESNameTableViewer1.UpdateValues(); @@ -2392,7 +2392,7 @@ namespace BizHawk.MultiClient #if WINDOWS if (_luaconsole != null) { - LuaConsole1.LuaImp.FrameRegisterAfter(); + LuaConsole1.LuaImp.CallFrameAfterEvent(); if (!fromLua) { GlobalWinF.DisplayManager.PreFrameUpdateLuaSource(); @@ -2492,7 +2492,7 @@ namespace BizHawk.MultiClient } SaveStateFile(path, name, false); - LuaConsole1.LuaImp.SavestateRegisterSave(name); + LuaConsole1.LuaImp.CallSaveStateEvent(name); } public void SaveStateFile(string filename, string name, bool fromLua) @@ -2672,7 +2672,7 @@ namespace BizHawk.MultiClient UpdateToolsAfter(fromLua); UpdateToolsLoadstate(); GlobalWinF.OSD.AddMessage("Loaded state: " + name); - LuaConsole1.LuaImp.SavestateRegisterLoad(name); + LuaConsole1.LuaImp.CallLoadStateEvent(name); } public void LoadState(string name, bool fromLua = false) diff --git a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Events.cs b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Events.cs index e13d400de2..08edc45159 100644 --- a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Events.cs +++ b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.Events.cs @@ -6,15 +6,35 @@ using BizHawk.Client.Common; namespace BizHawk.MultiClient { - public partial class EmuLuaLibrary + public class EventLuaLibrary : LuaLibraryBase { + public override string Name { get { return "event"; } } + public override string[] Functions + { + get + { + return new[] + { + "onframeend", + "onframestart", + "oninputpoll", + "onloadstate", + "onmemoryread", + "onmemorywrite", + "onsavestate", + "unregisterbyid", + "unregisterbyname", + }; + } + } + #region Events Library Helpers private readonly LuaFunctionList lua_functions = new LuaFunctionList(); public LuaFunctionList RegisteredFunctions { get { return lua_functions; } } - public void SavestateRegisterSave(string name) + public void CallSaveStateEvent(string name) { List lfs = lua_functions.Where(x => x.Event == "OnSavestateSave").ToList(); if (lfs.Any()) @@ -35,7 +55,7 @@ namespace BizHawk.MultiClient } } - public void SavestateRegisterLoad(string name) + public void CallLoadStateEvent(string name) { List lfs = lua_functions.Where(x => x.Event == "OnSavestateLoad").ToList(); if (lfs.Any()) @@ -56,7 +76,7 @@ namespace BizHawk.MultiClient } } - public void FrameRegisterBefore() + public void CallFrameBeforeEvent() { List lfs = lua_functions.Where(x => x.Event == "OnFrameStart").ToList(); if (lfs.Any()) @@ -77,7 +97,7 @@ namespace BizHawk.MultiClient } } - public void FrameRegisterAfter() + public void CallFrameAfterEvent() { List lfs = lua_functions.Where(x => x.Event == "OnFrameEnd").ToList(); if (lfs.Any()) @@ -116,7 +136,26 @@ namespace BizHawk.MultiClient public void event_oninputpoll(LuaFunction luaf) { - emu_on_snoop(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 string event_onloadstate(LuaFunction luaf, object name = null) diff --git a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs index 5a61e863ac..76da267ad8 100644 --- a/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs +++ b/BizHawk.MultiClient/tools/Lua/Libraries/EmuLuaLibrary.cs @@ -1,6 +1,8 @@ using System; using System.Threading; + using LuaInterface; +using BizHawk.Client.Common; namespace BizHawk.MultiClient { @@ -10,6 +12,7 @@ namespace BizHawk.MultiClient private readonly LuaConsole _caller; private Lua currThread; private FormsLuaLibrary _formsLibrary = new FormsLuaLibrary(); + private EventLuaLibrary _eventLibrary = new EventLuaLibrary(); public LuaDocumentation Docs = new LuaDocumentation(); public bool IsRunning; @@ -21,6 +24,31 @@ namespace BizHawk.MultiClient _formsLibrary.WindowClosed(handle); } + public void CallSaveStateEvent(string name) + { + _eventLibrary.CallSaveStateEvent(name); + } + + public void CallLoadStateEvent(string name) + { + _eventLibrary.CallLoadStateEvent(name); + } + + public LuaFunctionList RegisteredFunctions + { + get { return _eventLibrary.RegisteredFunctions; } + } + + public void CallFrameBeforeEvent() + { + _eventLibrary.CallFrameBeforeEvent(); + } + + public void CallFrameAfterEvent() + { + _eventLibrary.CallFrameAfterEvent(); + } + public EmuLuaLibrary(LuaConsole passed) { LuaWait = new AutoResetEvent(false); @@ -80,19 +108,6 @@ namespace BizHawk.MultiClient "yield", }; - public static string[] EventFunctions = new[] - { - "onframeend", - "onframestart", - "oninputpoll", - "onloadstate", - "onmemoryread", - "onmemorywrite", - "onsavestate", - "unregisterbyid", - "unregisterbyname", - }; - public void LuaRegister(Lua lua) { lua.RegisterFunction("print", this, GetType().GetMethod("print")); @@ -100,6 +115,7 @@ namespace BizHawk.MultiClient new BitLuaLibrary().LuaRegister(lua, Docs); new MultiClientLuaLibrary(ConsoleLuaLibrary.console_log).LuaRegister(lua, Docs); new ConsoleLuaLibrary().LuaRegister(lua, Docs); + _eventLibrary.LuaRegister(lua, Docs); _formsLibrary.LuaRegister(lua, Docs); new InputLuaLibrary(_lua).LuaRegister(lua, Docs); new JoypadLuaLibrary(_lua).LuaRegister(lua, Docs); @@ -124,13 +140,6 @@ namespace BizHawk.MultiClient Docs.Add("emu", t, GetType().GetMethod("emu_" + t)); } - lua.NewTable("event"); - foreach (string t in EventFunctions) - { - lua.RegisterFunction("event." + t, this, GetType().GetMethod("event_" + t)); - Docs.Add("event", t, GetType().GetMethod("event_" + t)); - } - Docs.Sort(); } diff --git a/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.Designer.cs b/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.Designer.cs index 7528c3f182..835b7b63b6 100644 --- a/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.Designer.cs +++ b/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.Designer.cs @@ -61,6 +61,7 @@ this.FunctionView.UseCompatibleStateImageBehavior = false; this.FunctionView.View = System.Windows.Forms.View.Details; this.FunctionView.SelectedIndexChanged += new System.EventHandler(this.FunctionView_SelectedIndexChanged); + this.FunctionView.DoubleClick += new System.EventHandler(this.FunctionView_DoubleClick); // // FunctionsEvent // diff --git a/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs b/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs index aa35639b48..a5e7ba0a94 100644 --- a/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs +++ b/BizHawk.MultiClient/tools/Lua/LuaRegisteredFunctionsList.cs @@ -78,5 +78,10 @@ namespace BizHawk.MultiClient CallButton.Enabled = indexes.Count > 0; RemoveButton.Enabled = indexes.Count > 0; } + + private void FunctionView_DoubleClick(object sender, EventArgs e) + { + CallFunction(); + } } }