Allow frontend to pass arguments to Lua callback functions

for compatibility w/ older versions: `event.can_use_callback_params ~= nil`
This commit is contained in:
YoshiRulz 2022-01-10 15:20:33 +10:00
parent 725f6e7194
commit 8385337e71
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
4 changed files with 15 additions and 9 deletions

View File

@ -6,7 +6,7 @@ namespace BizHawk.Client.Common
{
public interface INamedLuaFunction
{
Action Callback { get; }
Action InputCallback { get; }
Guid Guid { get; }

View File

@ -28,7 +28,7 @@ namespace BizHawk.Client.Common
{
if (emulator.InputCallbacksAvailable())
{
emulator.AsInputPollable().InputCallbacks.Remove(function.Callback);
emulator.AsInputPollable().InputCallbacks.Remove(function.InputCallback);
}
if (emulator.MemoryCallbacksAvailable())
@ -64,7 +64,7 @@ namespace BizHawk.Client.Common
{
if (emulator.InputCallbacksAvailable())
{
emulator.AsInputPollable().InputCallbacks.RemoveAll(_functions.Select(w => w.Callback));
emulator.AsInputPollable().InputCallbacks.RemoveAll(_functions.Select(w => w.InputCallback));
}
if (emulator.MemoryCallbacksAvailable())

View File

@ -44,6 +44,10 @@ namespace BizHawk.Client.Common
Log($"{scope} is not an available scope for {Emulator.Attributes().CoreName}");
}
[LuaMethod("can_use_callback_params", "Returns true. Check this function exists to decide whether to use hacks for older versions w/o parameter support.")]
[LuaMethodExample("local mem_callback = event.can_use_callback_params ~= nil and mem_callback or mem_callback_pre_28;")]
public bool CanUseCallbackParams()
=> true;
[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\" );")]
[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")]
@ -68,7 +72,7 @@ namespace BizHawk.Client.Common
{
try
{
InputPollableCore.InputCallbacks.Add(nlf.Callback);
InputPollableCore.InputCallbacks.Add(nlf.InputCallback);
return nlf.Guid.ToString();
}
catch (NotImplementedException)

View File

@ -32,19 +32,19 @@ namespace BizHawk.Client.Common
Guid = Guid.NewGuid();
Callback = () =>
Callback = args =>
{
try
{
_function.Call();
_function.Call(args);
}
catch (Exception ex)
{
logCallback($"error running function attached by the event {Event}\nError message: {ex.Message}");
}
};
MemCallback = (address, value, flags) => Callback();
InputCallback = () => Callback(Array.Empty<object>());
MemCallback = (addr, val, flags) => Callback(new object[] { addr, val, flags });
}
public void DetachFromScript()
@ -66,7 +66,9 @@ namespace BizHawk.Client.Common
public string Event { get; }
public Action Callback { get; }
private Action<object[]> Callback { get; }
public Action InputCallback { get; }
public MemoryCallbackDelegate MemCallback { get; }