From 79d43dab1d62640eacde21cff14ea49ac01f5059 Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Thu, 29 Sep 2022 08:54:09 +1000 Subject: [PATCH] Extract constants used for `NamedLuaFunction.Event` --- .../lua/LuaHelperLibs/EventsLuaLibrary.cs | 22 +++++++++---------- .../lua/NamedLuaFunction.cs | 22 +++++++++++++++++++ .../tools/Lua/Win32LuaLibraries.cs | 13 ++++++----- 3 files changed, 40 insertions(+), 17 deletions(-) diff --git a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs index 8d4729c0db..8c88d6a5f2 100644 --- a/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs +++ b/src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs @@ -53,14 +53,14 @@ namespace BizHawk.Client.Common [LuaMethod("onframeend", "Calls the given lua function at the end of each frame, after all emulation and drawing has completed. Note: this is the default behavior of lua scripts")] [return: LuaASCIIStringParam] public string OnFrameEnd(LuaFunction luaf, [LuaArbitraryStringParam] string name = null) - => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnFrameEnd", LogOutputCallback, CurrentFile, name) + => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_POSTFRAME, LogOutputCallback, CurrentFile, name) .Guid.ToString(); [LuaMethodExample("local steveonf = event.onframestart(\r\n\tfunction()\r\n\t\tconsole.log( \"Calls the given lua function at the beginning of each frame before any emulation and drawing occurs\" );\r\n\tend\r\n\t, \"Frame name\" );")] [LuaMethod("onframestart", "Calls the given lua function at the beginning of each frame before any emulation and drawing occurs")] [return: LuaASCIIStringParam] public string OnFrameStart(LuaFunction luaf, [LuaArbitraryStringParam] string name = null) - => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnFrameStart", LogOutputCallback, CurrentFile, name) + => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_PREFRAME, LogOutputCallback, CurrentFile, name) .Guid.ToString(); [LuaMethodExample("local steveoni = event.oninputpoll(\r\n\tfunction()\r\n\t\tconsole.log( \"Calls the given lua function after each time the emulator core polls for input\" );\r\n\tend\r\n\t, \"Frame name\" );")] @@ -68,7 +68,7 @@ namespace BizHawk.Client.Common [return: LuaASCIIStringParam] public string OnInputPoll(LuaFunction luaf, [LuaArbitraryStringParam] string name = null) { - var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnInputPoll", LogOutputCallback, CurrentFile, name); + var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_INPUTPOLL, LogOutputCallback, CurrentFile, name); //TODO should we bother registering the function if the service isn't supported? none of the other events work this way --yoshi if (InputPollableCore != null) @@ -98,7 +98,7 @@ namespace BizHawk.Client.Common [LuaMethod("onloadstate", "Fires after a state is loaded. Receives a lua function name, and registers it to the event immediately following a successful savestate event")] [return: LuaASCIIStringParam] public string OnLoadState(LuaFunction luaf, [LuaArbitraryStringParam] string name = null) - => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnSavestateLoad", LogOutputCallback, CurrentFile, name) + => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_LOADSTATE, LogOutputCallback, CurrentFile, name) .Guid.ToString(); [LuaMethodExample("local steveonm = event.onmemoryexecute(\r\n\tfunction()\r\n\t\tconsole.log( \"Fires after the given address is executed by the core\" );\r\n\tend\r\n\t, 0x200, \"Frame name\", \"System Bus\" );")] @@ -121,7 +121,7 @@ namespace BizHawk.Client.Common return Guid.Empty.ToString(); } - var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnMemoryExecute", LogOutputCallback, CurrentFile, name); + var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXEC, LogOutputCallback, CurrentFile, name); DebuggableCore.MemoryCallbacks.Add( new MemoryCallback(ProcessScope(scope), MemoryCallbackType.Execute, "Lua Hook", nlf.MemCallback, address, null)); return nlf.Guid.ToString(); @@ -156,7 +156,7 @@ namespace BizHawk.Client.Common return Guid.Empty.ToString(); } - var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnMemoryExecuteAny", LogOutputCallback, CurrentFile, name); + var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXECANY, LogOutputCallback, CurrentFile, name); DebuggableCore.MemoryCallbacks.Add(new MemoryCallback( ProcessScope(scope), MemoryCallbackType.Execute, @@ -196,7 +196,7 @@ namespace BizHawk.Client.Common return Guid.Empty.ToString(); } - var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnMemoryRead", LogOutputCallback, CurrentFile, name); + var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMREAD, LogOutputCallback, CurrentFile, name); DebuggableCore.MemoryCallbacks.Add( new MemoryCallback(ProcessScope(scope), MemoryCallbackType.Read, "Lua Hook", nlf.MemCallback, address, null)); return nlf.Guid.ToString(); @@ -231,7 +231,7 @@ namespace BizHawk.Client.Common return Guid.Empty.ToString(); } - var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnMemoryWrite", LogOutputCallback, CurrentFile, name); + var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMWRITE, LogOutputCallback, CurrentFile, name); DebuggableCore.MemoryCallbacks.Add( new MemoryCallback(ProcessScope(scope), MemoryCallbackType.Write, "Lua Hook", nlf.MemCallback, address, null)); return nlf.Guid.ToString(); @@ -251,21 +251,21 @@ namespace BizHawk.Client.Common [LuaMethod("onsavestate", "Fires after a state is saved")] [return: LuaASCIIStringParam] public string OnSaveState(LuaFunction luaf, [LuaArbitraryStringParam] string name = null) - => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnSavestateSave", LogOutputCallback, CurrentFile, name) + => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_SAVESTATE, LogOutputCallback, CurrentFile, name) .Guid.ToString(); [LuaMethodExample("local steveone = event.onexit(\r\n\tfunction()\r\n\t\tconsole.log( \"Fires after the calling script has stopped\" );\r\n\tend\r\n\t, \"Frame name\" );")] [LuaMethod("onexit", "Fires after the calling script has stopped")] [return: LuaASCIIStringParam] public string OnExit(LuaFunction luaf, [LuaArbitraryStringParam] string name = null) - => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnExit", LogOutputCallback, CurrentFile, name) + => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_ENGINESTOP, LogOutputCallback, CurrentFile, name) .Guid.ToString(); [LuaMethodExample("local closeGuid = event.onconsoleclose(\r\n\tfunction()\r\n\t\tconsole.log( \"Fires when the emulator console closes\" );\r\n\tend\r\n\t, \"Frame name\" );")] [LuaMethod("onconsoleclose", "Fires when the emulator console closes")] [return: LuaASCIIStringParam] public string OnConsoleClose(LuaFunction luaf, [LuaArbitraryStringParam] string name = null) - => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnConsoleClose", LogOutputCallback, CurrentFile, name) + => _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_CONSOLECLOSE, LogOutputCallback, CurrentFile, name) .Guid.ToString(); [LuaMethodExample("if ( event.unregisterbyid( \"4d1810b7 - 0d28 - 4acb - 9d8b - d87721641551\" ) ) then\r\n\tconsole.log( \"Removes the registered function that matches the guid.If a function is found and remove the function will return true.If unable to find a match, the function will return false.\" );\r\nend;")] diff --git a/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs b/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs index 9ad566c7f4..4450ec41a2 100644 --- a/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs +++ b/src/BizHawk.Client.Common/lua/NamedLuaFunction.cs @@ -7,6 +7,28 @@ namespace BizHawk.Client.Common { public sealed class NamedLuaFunction : INamedLuaFunction { + public const string EVENT_TYPE_CONSOLECLOSE = "OnConsoleClose"; + + public const string EVENT_TYPE_ENGINESTOP = "OnExit"; + + public const string EVENT_TYPE_INPUTPOLL = "OnInputPoll"; + + public const string EVENT_TYPE_LOADSTATE = "OnSavestateLoad"; + + public const string EVENT_TYPE_MEMEXEC = "OnMemoryExecute"; + + public const string EVENT_TYPE_MEMEXECANY = "OnMemoryExecuteAny"; + + public const string EVENT_TYPE_MEMREAD = "OnMemoryRead"; + + public const string EVENT_TYPE_MEMWRITE = "OnMemoryWrite"; + + public const string EVENT_TYPE_POSTFRAME = "OnFrameEnd"; + + public const string EVENT_TYPE_PREFRAME = "OnFrameStart"; + + public const string EVENT_TYPE_SAVESTATE = "OnSavestateSave"; + private readonly LuaFunction _function; public NamedLuaFunction(LuaFunction function, string theEvent, Action logCallback, LuaFile luaFile, string name = null) diff --git a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs index a56ba30d1e..7930ea69fe 100644 --- a/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs +++ b/src/BizHawk.Client.EmuHawk/tools/Lua/Win32LuaLibraries.cs @@ -186,7 +186,7 @@ namespace BizHawk.Client.EmuHawk { try { - foreach (var lf in RegisteredFunctions.Where(static l => l.Event == "OnSavestateSave").ToList()) + foreach (var lf in RegisteredFunctions.Where(static l => l.Event == NamedLuaFunction.EVENT_TYPE_SAVESTATE).ToList()) { lf.Call(name); } @@ -203,7 +203,7 @@ namespace BizHawk.Client.EmuHawk { try { - foreach (var lf in RegisteredFunctions.Where(static l => l.Event == "OnSavestateLoad").ToList()) + foreach (var lf in RegisteredFunctions.Where(static l => l.Event == NamedLuaFunction.EVENT_TYPE_LOADSTATE).ToList()) { lf.Call(name); } @@ -221,7 +221,7 @@ namespace BizHawk.Client.EmuHawk if (IsUpdateSupressed) return; try { - foreach (var lf in RegisteredFunctions.Where(static l => l.Event == "OnFrameStart").ToList()) + foreach (var lf in RegisteredFunctions.Where(static l => l.Event == NamedLuaFunction.EVENT_TYPE_PREFRAME).ToList()) { lf.Call(); } @@ -239,7 +239,7 @@ namespace BizHawk.Client.EmuHawk if (IsUpdateSupressed) return; try { - foreach (var lf in RegisteredFunctions.Where(static l => l.Event == "OnFrameEnd").ToList()) + foreach (var lf in RegisteredFunctions.Where(static l => l.Event == NamedLuaFunction.EVENT_TYPE_POSTFRAME).ToList()) { lf.Call(); } @@ -255,7 +255,8 @@ namespace BizHawk.Client.EmuHawk public void CallExitEvent(LuaFile lf) { foreach (var exitCallback in RegisteredFunctions - .Where(l => l.Event == "OnExit" && (l.LuaFile.Path == lf.Path || l.LuaFile.Thread == lf.Thread)) + .Where(l => l.Event == NamedLuaFunction.EVENT_TYPE_ENGINESTOP + && (l.LuaFile.Path == lf.Path || l.LuaFile.Thread == lf.Thread)) .ToList()) { exitCallback.Call(); @@ -266,7 +267,7 @@ namespace BizHawk.Client.EmuHawk public void Close() { foreach (var closeCallback in RegisteredFunctions - .Where(static l => l.Event == "OnConsoleClose") + .Where(static l => l.Event == NamedLuaFunction.EVENT_TYPE_CONSOLECLOSE) .ToList()) { closeCallback.Call();