Capy64/Capy64/LuaRuntime/Libraries/Timer.cs
Alessandro Proto 927a2e253a Added BinaryWriteHandle
Clean up of all the code
2023-01-14 18:16:35 +01:00

72 lines
1.3 KiB
C#

using Capy64.API;
using KeraLua;
using System;
namespace Capy64.LuaRuntime.Libraries;
class Timer : IPlugin
{
private LuaRegister[] TimerLib = new LuaRegister[]
{
new()
{
name = "start",
function = L_StartTimer,
},
new(),
};
private static IGame _game;
private static uint _timerId;
public Timer(IGame game)
{
_game = game;
_timerId = 0;
}
public void LuaInit(Lua state)
{
state.RequireF("timer", Open, false);
}
private int Open(IntPtr state)
{
var l = Lua.FromIntPtr(state);
l.NewLib(TimerLib);
return 1;
}
private static int L_StartTimer(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var delay = L.CheckInteger(1);
L.ArgumentCheck(delay > 0, 1, "delay must be greater than 0");
var timerId = _timerId++;
var timer = new System.Timers.Timer
{
AutoReset = false,
Enabled = true,
Interval = delay,
};
timer.Elapsed += (o, e) =>
{
_game.LuaRuntime.PushEvent("timer", timerId);
};
L.PushInteger(timerId);
return 1;
}
private static int L_Sleep(IntPtr state)
{
var L = Lua.FromIntPtr(state);
return 0;
}
}