From 5de28f78d2e7126605b8c6121820cf6311ba7095 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 20 Jan 2023 10:51:22 +0100 Subject: [PATCH] Ported event library to C#. Added event.push --- Capy64/Assets/Lua/lib/event.lua | 15 ---- Capy64/LuaRuntime/Libraries/Event.cs | 121 +++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 15 deletions(-) delete mode 100644 Capy64/Assets/Lua/lib/event.lua create mode 100644 Capy64/LuaRuntime/Libraries/Event.cs diff --git a/Capy64/Assets/Lua/lib/event.lua b/Capy64/Assets/Lua/lib/event.lua deleted file mode 100644 index b15af13..0000000 --- a/Capy64/Assets/Lua/lib/event.lua +++ /dev/null @@ -1,15 +0,0 @@ -local event = {} - -function event.pull(...) - local pars = table.pack(event.pullRaw(...)) - if pars[1] == "interrupt" then - error("Interrupted", 0) - end - return table.unpack(pars) -end - -function event.pullRaw(...) - return coroutine.yield(...) -end - -return event \ No newline at end of file diff --git a/Capy64/LuaRuntime/Libraries/Event.cs b/Capy64/LuaRuntime/Libraries/Event.cs new file mode 100644 index 0000000..7859586 --- /dev/null +++ b/Capy64/LuaRuntime/Libraries/Event.cs @@ -0,0 +1,121 @@ +using Capy64.API; +using KeraLua; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Capy64.LuaRuntime.Libraries; + +public class Event : IPlugin +{ + private static IGame _game; + public Event(IGame game) + { + _game = game; + } + + private static LuaRegister[] EventLib = new LuaRegister[] + { + new() + { + name = "pull", + function = L_Pull, + }, + new() + { + name = "pullRaw", + function = L_PullRaw + }, + new() + { + name = "push", + function = L_Push, + }, + new(), + }; + + public void LuaInit(Lua L) + { + L.RequireF("event", OpenLib, false); + } + + private static int OpenLib(IntPtr state) + { + var L = Lua.FromIntPtr(state); + L.NewLib(EventLib); + return 1; + } + + private static int LK_Pull(IntPtr state, int status, IntPtr ctx) + { + var L = Lua.FromIntPtr(state); + + if(L.ToString(1) == "interrupt") + { + L.Error("interrupt"); + } + + var nargs = L.GetTop(); + + return nargs; + } + + private static int L_Pull(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var nargs = L.GetTop(); + for(int i = 1; i <= nargs; i++) + { + L.CheckString(i); + } + + L.YieldK(nargs, 0, LK_Pull); + + return 0; + } + + private static int L_PullRaw(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var nargs = L.GetTop(); + for (int i = 1; i <= nargs; i++) + { + L.CheckString(i); + } + + L.Yield(nargs); + + return 0; + } + + private static int L_Push(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var eventName = L.CheckString(1); + + var nargs = L.GetTop(); + + var evParsState = L.NewThread(); + + for(int i = 2; i <= nargs; i++) + { + L.PushCopy(i); + } + + L.XMove(evParsState, nargs - 1); + + _game.LuaRuntime.PushEvent(eventName, LK => + { + evParsState.XMove(LK, nargs - 1); + + return nargs - 1; + }); + + return 0; + } +}