Skip to content

Commit 829d117

Browse files
committed
Replace 2 ILuaLibraries methods with delegates (again)
see 0bc5861
1 parent 8f8d1b3 commit 829d117

File tree

5 files changed

+42
-27
lines changed

5 files changed

+42
-27
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
@@ -14,15 +12,6 @@ public interface ILuaLibraries
1412

1513
PathEntryCollection PathEntries { get; }
1614

17-
INamedLuaFunction CreateAndRegisterNamedFunction(
18-
LuaFunction function,
19-
string theEvent,
20-
Action<string> logCallback,
21-
LuaFile luaFile,
22-
string name = null);
23-
2415
NLuaTableHelper GetTableHelper();
25-
26-
bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate);
2716
}
2817
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace BizHawk.Client.Common
1313
[Description("Functions specific to Doom games (functions may not run when a Doom game is not loaded)")]
1414
public sealed class DoomLuaLibrary : LuaLibraryBase
1515
{
16+
public NLFAddCallback CreateAndRegisterNamedFunction { get; set; }
17+
1618
public DoomLuaLibrary(ILuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)
1719
: base(luaLibsImpl, apiContainer, logOutputCallback) { }
1820

@@ -41,7 +43,7 @@ public string OnPrandom(LuaFunction luaf, string name = null)
4143
}
4244

4345
var callbacks = dsda.RandomCallbacks;
44-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, "OnPrandom", LogOutputCallback, CurrentFile, name: name);
46+
var nlf = CreateAndRegisterNamedFunction(luaf, "OnPrandom", LogOutputCallback, CurrentFile, name: name);
4547
callbacks.Add(nlf.RandomCallback);
4648
nlf.OnRemove += () => callbacks.Remove(nlf.RandomCallback);
4749
return nlf.GuidStr;

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public sealed class EventsLuaLibrary : LuaLibraryBase
1313
{
1414
private static readonly string EMPTY_UUID_STR = Guid.Empty.ToString("D");
1515

16+
public NLFAddCallback CreateAndRegisterNamedFunction { get; set; }
17+
18+
public NLFRemoveCallback RemoveNamedFunctionMatching { get; set; }
19+
1620
[OptionalService]
1721
private IInputPollable InputPollableCore { get; set; }
1822

@@ -62,20 +66,20 @@ public bool CanUseCallbackParams(string subset = null)
6266
[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\" );")]
6367
[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")]
6468
public string OnFrameEnd(LuaFunction luaf, string name = null)
65-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_POSTFRAME, LogOutputCallback, CurrentFile, name)
69+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_POSTFRAME, LogOutputCallback, CurrentFile, name: name)
6670
.GuidStr;
6771

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

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

8185
if (InputPollableCore != null)
@@ -106,7 +110,7 @@ private void LogNotImplemented()
106110
[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\" );")]
107111
[LuaMethod("onloadstate", "Fires after a state is loaded. Your callback can have 1 parameter, which will be the name of the loaded state.")]
108112
public string OnLoadState(LuaFunction luaf, string name = null)
109-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_LOADSTATE, LogOutputCallback, CurrentFile, name)
113+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_LOADSTATE, LogOutputCallback, CurrentFile, name: name)
110114
.GuidStr;
111115

112116
[LuaDeprecatedMethod]
@@ -141,7 +145,7 @@ public string OnBusExec(
141145
return EMPTY_UUID_STR;
142146
}
143147

144-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXEC, LogOutputCallback, CurrentFile, name);
148+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXEC, LogOutputCallback, CurrentFile, name: name);
145149
AddMemCallbackOnCore(nlf, MemoryCallbackType.Execute, scope, address);
146150
return nlf.GuidStr;
147151
}
@@ -185,7 +189,7 @@ public string OnBusExecAny(
185189
return EMPTY_UUID_STR;
186190
}
187191

188-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXECANY, LogOutputCallback, CurrentFile, name);
192+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMEXECANY, LogOutputCallback, CurrentFile, name: name);
189193
AddMemCallbackOnCore(nlf, MemoryCallbackType.Execute, scope, address: null);
190194
return nlf.GuidStr;
191195
}
@@ -229,7 +233,7 @@ public string OnBusRead(
229233
return EMPTY_UUID_STR;
230234
}
231235

232-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMREAD, LogOutputCallback, CurrentFile, name);
236+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMREAD, LogOutputCallback, CurrentFile, name: name);
233237
AddMemCallbackOnCore(nlf, MemoryCallbackType.Read, scope, address);
234238
return nlf.GuidStr;
235239
}
@@ -274,7 +278,7 @@ public string OnBusWrite(
274278
return EMPTY_UUID_STR;
275279
}
276280

277-
var nlf = _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMWRITE, LogOutputCallback, CurrentFile, name);
281+
var nlf = CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_MEMWRITE, LogOutputCallback, CurrentFile, name: name);
278282
AddMemCallbackOnCore(nlf, MemoryCallbackType.Write, scope, address);
279283
return nlf.GuidStr;
280284
}
@@ -292,33 +296,33 @@ public string OnBusWrite(
292296
[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\" );")]
293297
[LuaMethod("onsavestate", "Fires after a state is saved. Your callback can have 1 parameter, which will be the name of the saved state.")]
294298
public string OnSaveState(LuaFunction luaf, string name = null)
295-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_SAVESTATE, LogOutputCallback, CurrentFile, name)
299+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_SAVESTATE, LogOutputCallback, CurrentFile, name: name)
296300
.GuidStr;
297301

298302
[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\" );")]
299303
[LuaMethod("onexit", "Fires after the calling script has stopped")]
300304
public string OnExit(LuaFunction luaf, string name = null)
301-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_ENGINESTOP, LogOutputCallback, CurrentFile, name)
305+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_ENGINESTOP, LogOutputCallback, CurrentFile, name: name)
302306
.GuidStr;
303307

304308
[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\" );")]
305309
[LuaMethod("onconsoleclose", "Fires when the emulator console closes")]
306310
public string OnConsoleClose(LuaFunction luaf, string name = null)
307-
=> _luaLibsImpl.CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_CONSOLECLOSE, LogOutputCallback, CurrentFile, name)
311+
=> CreateAndRegisterNamedFunction(luaf, NamedLuaFunction.EVENT_TYPE_CONSOLECLOSE, LogOutputCallback, CurrentFile, name: name)
308312
.GuidStr;
309313

310314
[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;")]
311315
[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.")]
312316
public bool UnregisterById(string guid)
313317
{
314318
Guid parsed = new(guid);
315-
return _luaLibsImpl.RemoveNamedFunctionMatching(nlf => nlf.Guid == parsed);
319+
return RemoveNamedFunctionMatching(nlf => nlf.Guid == parsed);
316320
}
317321

318322
[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;")]
319323
[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.")]
320324
public bool UnregisterByName(string name)
321-
=> _luaLibsImpl.RemoveNamedFunctionMatching(nlf => nlf.Name == name);
325+
=> RemoveNamedFunctionMatching(nlf => nlf.Name == name);
322326

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

src/BizHawk.Client.Common/lua/LuaLibraryBase.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22

33
using BizHawk.Emulation.Common;
44

5+
using NLua;
6+
57
namespace BizHawk.Client.Common
68
{
79
public abstract class LuaLibraryBase
810
{
11+
public delegate INamedLuaFunction NLFAddCallback(
12+
LuaFunction function,
13+
string theEvent,
14+
Action<string> logCallback,
15+
LuaFile luaFile,
16+
string name = null);
17+
18+
public delegate bool NLFRemoveCallback(Func<INamedLuaFunction, bool> predicate);
19+
920
public PathEntryCollection PathEntries { get; set; }
1021

1122
protected LuaLibraryBase(ILuaLibraries luaLibsImpl, ApiContainer apiContainer, Action<string> logOutputCallback)

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ void EnumerateLuaFunctions(string name, Type type, LuaLibraryBase instance)
9797
consoleLib.Tools = _mainForm.Tools;
9898
_logToLuaConsoleCallback = consoleLib.Log;
9999
}
100+
else if (instance is DoomLuaLibrary doomLib)
101+
{
102+
doomLib.CreateAndRegisterNamedFunction = CreateAndRegisterNamedFunction;
103+
}
104+
else if (instance is EventsLuaLibrary eventsLib)
105+
{
106+
eventsLib.CreateAndRegisterNamedFunction = CreateAndRegisterNamedFunction;
107+
eventsLib.RemoveNamedFunctionMatching = RemoveNamedFunctionMatching;
108+
}
100109
else if (instance is FormsLuaLibrary formsLib)
101110
{
102111
formsLib.MainForm = _mainForm;
@@ -304,7 +313,7 @@ public void Close()
304313
_lua = null;
305314
}
306315

307-
public INamedLuaFunction CreateAndRegisterNamedFunction(
316+
private INamedLuaFunction CreateAndRegisterNamedFunction(
308317
LuaFunction function,
309318
string theEvent,
310319
Action<string> logCallback,
@@ -316,7 +325,7 @@ public INamedLuaFunction CreateAndRegisterNamedFunction(
316325
return nlf;
317326
}
318327

319-
public bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate)
328+
private bool RemoveNamedFunctionMatching(Func<INamedLuaFunction, bool> predicate)
320329
{
321330
if (RegisteredFunctions.FirstOrDefault(predicate) is not NamedLuaFunction nlf) return false;
322331
RegisteredFunctions.Remove(nlf);

0 commit comments

Comments
 (0)