diff --git a/Capy64/LuaRuntime/ILuaEvent.cs b/Capy64/LuaRuntime/ILuaEvent.cs new file mode 100644 index 0000000..b26bfb4 --- /dev/null +++ b/Capy64/LuaRuntime/ILuaEvent.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Capy64.LuaRuntime; + +public interface ILuaEvent +{ + public string Name { get; set; } + public bool BypassFilter { get; set; } +} diff --git a/Capy64/LuaRuntime/LuaDelegateEvent.cs b/Capy64/LuaRuntime/LuaDelegateEvent.cs new file mode 100644 index 0000000..a74ce6d --- /dev/null +++ b/Capy64/LuaRuntime/LuaDelegateEvent.cs @@ -0,0 +1,15 @@ +using KeraLua; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Capy64.LuaRuntime; + +public class LuaDelegateEvent : ILuaEvent +{ + public string Name { get; set; } + public Func Handler { get; set; } + public bool BypassFilter { get; set; } = false; +} diff --git a/Capy64/LuaRuntime/LuaEvent.cs b/Capy64/LuaRuntime/LuaEvent.cs index caf1ea3..3cf8b65 100644 --- a/Capy64/LuaRuntime/LuaEvent.cs +++ b/Capy64/LuaRuntime/LuaEvent.cs @@ -1,8 +1,8 @@ namespace Capy64.LuaRuntime; -public struct LuaEvent +public class LuaEvent : ILuaEvent { public string Name { get; set; } public object[] Parameters { get; set; } - public bool BypassFilter { get; set; } + public bool BypassFilter { get; set; } = false; } diff --git a/Capy64/LuaRuntime/Runtime.cs b/Capy64/LuaRuntime/Runtime.cs index b60932c..d66723d 100644 --- a/Capy64/LuaRuntime/Runtime.cs +++ b/Capy64/LuaRuntime/Runtime.cs @@ -13,7 +13,7 @@ namespace Capy64.LuaRuntime; public class Runtime { - private readonly ConcurrentQueue eventQueue = new(new LuaEvent[] + private readonly ConcurrentQueue eventQueue = new(new LuaEvent[] { new() { @@ -62,7 +62,27 @@ public class Runtime public void PushEvent(string name, params object[] pars) { - eventQueue.Enqueue(new() { Name = name, Parameters = pars }); + eventQueue.Enqueue(new LuaEvent() { Name = name, Parameters = pars }); + } + + public void PushEvent(LuaDelegateEvent ev) + { + eventQueue.Enqueue(ev); + } + + /// + /// Push a new event to the event queue. + /// + /// Event name + /// Event handler. Push any needed parameter from here. Return n as amount of parameters pushed, minus event name. + /// For example: I push an event with 3 parameters, then I return 3. + /// + public void PushEvent(string name, Func handler) + { + eventQueue.Enqueue(new LuaDelegateEvent { + Name = name, + Handler = handler + }); } /// @@ -71,7 +91,7 @@ public class Runtime /// Whether it yielded public bool Resume() { - while (eventQueue.TryDequeue(out LuaEvent ev)) + while (eventQueue.TryDequeue(out ILuaEvent ev)) { if (!ResumeThread(ev)) return false; @@ -80,7 +100,7 @@ public class Runtime return true; } - private bool ResumeThread(LuaEvent ev) + private bool ResumeThread(ILuaEvent ev) { @@ -117,17 +137,32 @@ public class Runtime throw new LuaException($"Top thread exception:\n{error}\n{stacktrace}"); } - private int PushEventToStack(LuaEvent ev) + private int PushEventToStack(ILuaEvent ev) { Thread.PushString(ev.Name); - if (ev.Parameters != null) + + switch(ev) { - foreach (var par in ev.Parameters) - { - Thread.PushValue(par); - } + case LuaEvent e: + + if (e.Parameters != null) + { + foreach (var par in e.Parameters) + { + Thread.PushValue(par); + } + } + + return (e.Parameters?.Length ?? 0) + 1; + + case LuaDelegateEvent e: + + int n = e.Handler(Thread); + return n + 1; + + default: + throw new NotImplementedException(); } - return (ev.Parameters?.Length ?? 0) + 1; } public void Close()