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:
parent
725f6e7194
commit
8385337e71
|
@ -6,7 +6,7 @@ namespace BizHawk.Client.Common
|
|||
{
|
||||
public interface INamedLuaFunction
|
||||
{
|
||||
Action Callback { get; }
|
||||
Action InputCallback { get; }
|
||||
|
||||
Guid Guid { get; }
|
||||
|
||||
|
|
|
@ -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())
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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; }
|
||||
|
||||
|
|
Loading…
Reference in New Issue