mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-01-18 18:46:43 +00:00
Ported event library to C#. Added event.push
This commit is contained in:
parent
5636cb0b0d
commit
5de28f78d2
2 changed files with 121 additions and 15 deletions
|
@ -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
|
121
Capy64/LuaRuntime/Libraries/Event.cs
Normal file
121
Capy64/LuaRuntime/Libraries/Event.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue