Skip to content

Commit a85ef40

Browse files
committed
Replace 2 ILuaLibraries methods with delegates on EventsLuaLibrary
1 parent d4e10f0 commit a85ef40

File tree

3 files changed

+33
-26
lines changed

3 files changed

+33
-26
lines changed
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using NLua;
2-
31
namespace BizHawk.Client.Common
42
{
53
public interface ILuaLibraries
@@ -16,15 +14,6 @@ public interface ILuaLibraries
1614

1715
PathEntryCollection PathEntries { get; }
1816

19-
INamedLuaFunction CreateAndRegisterNamedFunction(
20-
LuaFunction function,
21-
string theEvent,
22-
Action<string> logCallback,
23-
LuaFile luaFile,
24-
string name = null);
25-
2617
NLuaTableHelper GetTableHelper();
27-
28-
bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate);
2918
}
3019
}

src/BizHawk.Client.Common/lua/LuaHelperLibs/EventsLuaLibrary.cs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,21 @@ namespace BizHawk.Client.Common
1111
[Description("A library for registering lua functions to emulator events.\n All events support multiple registered methods.\nAll registered event methods can be named and return a Guid when registered")]
1212
public sealed class EventsLuaLibrary : LuaLibraryBase
1313
{
14+
public delegate INamedLuaFunction NLFAddCallback(
15+
LuaFunction function,
16+
string theEvent,
17+
Action<string> logCallback,
18+
LuaFile luaFile,
19+
string name = null);
20+
21+
public delegate bool NLFRemoveCallback(Func<INamedLuaFunction, bool> predicate);
22+
1423
private static readonly string EMPTY_UUID_STR = Guid.Empty.ToString("D");
1524

25+
public NLFAddCallback CreateAndRegisterNamedFunction { get; set; }
26+
27+
public NLFRemoveCallback RemoveNamedFunctionMatching { get; set; }
28+
1629
[OptionalService]
1730
private IInputPollable InputPollableCore { get; set; }
1831

@@ -49,20 +62,20 @@ public bool CanUseCallbackParams(string subset = null)
4962
[LuaMethodExample("local steveonf = event.onframeend(\r\n\tfunction()\r\n\t\tconsole.log( \"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\" );\r\n\tend\r\n\t, \"Frame name\" );")]
5063
[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")]
5164
public string OnFrameEnd(LuaFunction luaf, string name = null)
52-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_POSTFRAME, LogOutputCallback, CurrentFile, name)
65+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_POSTFRAME, LogOutputCallback, CurrentFile, name: name)
5366
.GuidStr;
5467

5568
[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\" );")]
5669
[LuaMethod("onframestart", "Calls the given lua function at the beginning of each frame before any emulation and drawing occurs")]
5770
public string OnFrameStart(LuaFunction luaf, string name = null)
58-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_PREFRAME, LogOutputCallback, CurrentFile, name)
71+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_PREFRAME, LogOutputCallback, CurrentFile, name: name)
5972
.GuidStr;
6073

6174
[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\" );")]
6275
[LuaMethod("oninputpoll", "Calls the given lua function after each time the emulator core polls for input")]
6376
public string OnInputPoll(LuaFunction luaf, string name = null)
6477
{
65-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_INPUTPOLL, LogOutputCallback, CurrentFile, name);
78+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_INPUTPOLL, LogOutputCallback, CurrentFile, name: name);
6679
//TODO should we bother registering the function if the service isn't supported? none of the other events work this way --yoshi
6780

6881
if (InputPollableCore != null)
@@ -91,7 +104,7 @@ private void LogNotImplemented()
91104
[LuaMethodExample("local steveonl = event.onloadstate(\r\n\tfunction()\r\n\tconsole.log( \"Fires after a state is loaded. Receives a lua function name, and registers it to the event immediately following a successful savestate event\" );\r\nend\", \"Frame name\" );")]
92105
[LuaMethod("onloadstate", "Fires after a state is loaded. Your callback can have 1 parameter, which will be the name of the loaded state.")]
93106
public string OnLoadState(LuaFunction luaf, string name = null)
94-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_LOADSTATE, LogOutputCallback, CurrentFile, name)
107+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_LOADSTATE, LogOutputCallback, CurrentFile, name: name)
95108
.GuidStr;
96109

97110
[LuaDeprecatedMethod]
@@ -126,7 +139,7 @@ public string OnBusExec(
126139
return EMPTY_UUID_STR;
127140
}
128141

129-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXEC, LogOutputCallback, CurrentFile, name);
142+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXEC, LogOutputCallback, CurrentFile, name: name);
130143
DebuggableCore.MemoryCallbacks.Add(
131144
new MemoryCallback(ProcessScope(scope), MemoryCallbackType.Execute, "Lua Hook", nlf.MemCallback, address, null));
132145
return nlf.GuidStr;
@@ -171,7 +184,7 @@ public string OnBusExecAny(
171184
return EMPTY_UUID_STR;
172185
}
173186

174-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXECANY, LogOutputCallback, CurrentFile, name);
187+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXECANY, LogOutputCallback, CurrentFile, name: name);
175188
DebuggableCore.MemoryCallbacks.Add(new MemoryCallback(
176189
ProcessScope(scope),
177190
MemoryCallbackType.Execute,
@@ -222,7 +235,7 @@ public string OnBusRead(
222235
return EMPTY_UUID_STR;
223236
}
224237

225-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMREAD, LogOutputCallback, CurrentFile, name);
238+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMREAD, LogOutputCallback, CurrentFile, name: name);
226239
DebuggableCore.MemoryCallbacks.Add(
227240
new MemoryCallback(ProcessScope(scope), MemoryCallbackType.Read, "Lua Hook", nlf.MemCallback, address, null));
228241
return nlf.GuidStr;
@@ -268,7 +281,7 @@ public string OnBusWrite(
268281
return EMPTY_UUID_STR;
269282
}
270283

271-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMWRITE, LogOutputCallback, CurrentFile, name);
284+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMWRITE, LogOutputCallback, CurrentFile, name: name);
272285
DebuggableCore.MemoryCallbacks.Add(
273286
new MemoryCallback(ProcessScope(scope), MemoryCallbackType.Write, "Lua Hook", nlf.MemCallback, address, null));
274287
return nlf.GuidStr;
@@ -287,33 +300,33 @@ public string OnBusWrite(
287300
[LuaMethodExample("local steveons = event.onsavestate(\r\n\tfunction()\r\n\t\tconsole.log( \"Fires after a state is saved\" );\r\n\tend\r\n\t, \"Frame name\" );")]
288301
[LuaMethod("onsavestate", "Fires after a state is saved. Your callback can have 1 parameter, which will be the name of the saved state.")]
289302
public string OnSaveState(LuaFunction luaf, string name = null)
290-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_SAVESTATE, LogOutputCallback, CurrentFile, name)
303+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_SAVESTATE, LogOutputCallback, CurrentFile, name: name)
291304
.GuidStr;
292305

293306
[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\" );")]
294307
[LuaMethod("onexit", "Fires after the calling script has stopped")]
295308
public string OnExit(LuaFunction luaf, string name = null)
296-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_ENGINESTOP, LogOutputCallback, CurrentFile, name)
309+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_ENGINESTOP, LogOutputCallback, CurrentFile, name: name)
297310
.GuidStr;
298311

299312
[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\" );")]
300313
[LuaMethod("onconsoleclose", "Fires when the emulator console closes")]
301314
public string OnConsoleClose(LuaFunction luaf, string name = null)
302-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_CONSOLECLOSE, LogOutputCallback, CurrentFile, name)
315+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_CONSOLECLOSE, LogOutputCallback, CurrentFile, name: name)
303316
.GuidStr;
304317

305318
[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;")]
306319
[LuaMethod("unregisterbyid", "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.")]
307320
public bool UnregisterById(string guid)
308321
{
309322
Guid parsed = new(guid);
310-
return _luaLibsImpl.RemoveNamedFunctionMatching(nlf => nlf.Guid == parsed);
323+
return RemoveNamedFunctionMatching(nlf => nlf.Guid == parsed);
311324
}
312325

313326
[LuaMethodExample("if ( event.unregisterbyname( \"Function name\" ) ) then\r\n\tconsole.log( \"Removes the first registered function that matches Name.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;")]
314327
[LuaMethod("unregisterbyname", "Removes the first registered function that matches Name. If a function is found and remove the function will return true. If unable to find a match, the function will return false.")]
315328
public bool UnregisterByName(string name)
316-
=> _luaLibsImpl.RemoveNamedFunctionMatching(nlf => nlf.Name == name);
329+
=> RemoveNamedFunctionMatching(nlf => nlf.Name == name);
317330

318331
[LuaMethodExample("local scopes = event.availableScopes();")]
319332
[LuaMethod("availableScopes", "Lists the available scopes that can be specified for on_bus_* events")]

src/BizHawk.Client.EmuHawk/tools/Lua/LuaLibraries.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
9898
consoleLib.Tools = _mainForm.Tools;
9999
_logToLuaConsoleCallback = consoleLib.Log;
100100
}
101+
else if (instance is EventsLuaLibrary eventsLib)
102+
{
103+
eventsLib.CreateAndRegisterNamedFunction = CreateAndRegisterNamedFunction;
104+
eventsLib.RemoveNamedFunctionMatching = RemoveNamedFunctionMatching;
105+
}
101106
else if (instance is FormsLuaLibrary formsLib)
102107
{
103108
formsLib.MainForm = _mainForm;
@@ -305,7 +310,7 @@ public void Close()
305310
_lua = null;
306311
}
307312

308-
public INamedLuaFunction CreateAndRegisterNamedFunction(
313+
private INamedLuaFunction CreateAndRegisterNamedFunction(
309314
LuaFunction function,
310315
string theEvent,
311316
Action<string> logCallback,
@@ -317,7 +322,7 @@ public INamedLuaFunction CreateAndRegisterNamedFunction(
317322
return nlf;
318323
}
319324

320-
public bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate)
325+
private bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate)
321326
{
322327
var nlf = (NamedLuaFunction)RegisteredFunctions.FirstOrDefault(predicate);
323328
if (nlf == null) return false;

0 commit comments

Comments
 (0)