Bugfixes, improvements and API changes

This commit is contained in:
Alessandro Proto 2023-01-27 18:35:40 +01:00
parent 8636bf4ab1
commit 2644477847
6 changed files with 44 additions and 51 deletions

View file

@ -1,5 +1,6 @@
local term = require("term")
local timer = require("timer")
local gpu = require("gpu")
local bootSleep = 2000
local bg = 0x0
@ -9,6 +10,9 @@ term.setForeground(fg)
term.setBackground(bg)
term.clear()
term.setSize(51, 19)
gpu.setScale(2)
local function sleep(n)
local timerId = timer.start(n)
repeat
@ -82,7 +86,7 @@ local function setupScreen()
selection = selection + 1
elseif ev[3] == "enter" then
options[selection][2]()
elseif ev[3] == "esc" then
elseif ev[3] == "escape" then
exit()
end
@ -119,8 +123,3 @@ end
bootScreen()
term.clear()
exit()
while true do
coroutine.yield()
end

View file

@ -99,17 +99,16 @@ public class Event : IPlugin
var eventName = L.CheckString(1);
var nargs = L.GetTop();
var parsState = L.NewThread();
L.Pop(1);
L.XMove(parsState, nargs - 1);
_game.LuaRuntime.QueueEvent(eventName, LK =>
{
for (int i = 2; i <= nargs; i++)
{
L.PushCopy(i);
}
var nargs = parsState.GetTop();
parsState.XMove(LK, nargs);
L.XMove(LK, nargs - 1);
return nargs - 1;
return nargs;
});
return 0;

View file

@ -12,29 +12,15 @@ public class OS : IPlugin
_game = game;
}
public void LuaInit(Lua state)
{
state.GetGlobal("os");
state.PushString("version");
state.PushCFunction(L_Version);
state.SetTable(-3);
state.PushString("shutdown");
state.PushCFunction(L_Shutdown);
state.SetTable(-3);
}
private static int L_Version(IntPtr state)
{
var L = Lua.FromIntPtr(state);
L.PushString("Capy64 " + Capy64.Version);
return 1;
}
private static int L_Shutdown(IntPtr state)
{
var L = Lua.FromIntPtr(state);

View file

@ -360,11 +360,9 @@ internal class Term : IPlugin
{
var L = Lua.FromIntPtr(state);
L.PushInteger(ForegroundColor.R);
L.PushInteger(ForegroundColor.G);
L.PushInteger(ForegroundColor.B);
L.PushInteger(PackRGB(ForegroundColor));
return 3;
return 1;
}
private static int L_SetForegroundColor(IntPtr state)
@ -385,7 +383,7 @@ internal class Term : IPlugin
else if (argsn == 1)
{
var c = (uint)L.CheckInteger(1);
Utils.UnpackRGB(c, out r, out g, out b);
UnpackRGB(c, out r, out g, out b);
}
else
{
@ -402,11 +400,9 @@ internal class Term : IPlugin
{
var L = Lua.FromIntPtr(state);
L.PushInteger(BackgroundColor.R);
L.PushInteger(BackgroundColor.G);
L.PushInteger(BackgroundColor.B);
L.PushInteger(PackRGB(BackgroundColor));
return 3;
return 1;
}
private static int L_SetBackgroundColor(IntPtr state)
@ -427,7 +423,7 @@ internal class Term : IPlugin
else if (argsn == 1)
{
var c = (uint)L.CheckInteger(1);
Utils.UnpackRGB(c, out r, out g, out b);
UnpackRGB(c, out r, out g, out b);
}
else
{

View file

@ -1,6 +1,8 @@
using Capy64.API;
using KeraLua;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Capy64.Runtime.Libraries;
@ -18,15 +20,26 @@ class Timer : IPlugin
};
private static IGame _game;
private static uint _timerId;
private static uint _timerId = 0;
private static ConcurrentDictionary<uint, System.Timers.Timer> timers = new();
public Timer(IGame game)
{
_game = game;
_timerId = 0;
}
public void LuaInit(Lua state)
{
_timerId = 0;
foreach (var pair in timers)
{
pair.Value.Stop();
pair.Value.Dispose();
}
timers.Clear();
state.RequireF("timer", Open, false);
}
@ -52,8 +65,12 @@ class Timer : IPlugin
Interval = delay,
};
timers[timerId] = timer;
timer.Elapsed += (o, e) =>
{
timers.TryRemove(timerId, out _);
_game.LuaRuntime.QueueEvent("timer", LK =>
{
LK.PushInteger(timerId);
@ -65,13 +82,4 @@ class Timer : IPlugin
L.PushInteger(timerId);
return 1;
}
private static int L_Sleep(IntPtr state)
{
var L = Lua.FromIntPtr(state);
return 0;
}
}

View file

@ -1,6 +1,7 @@
using Capy64.API;
using KeraLua;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
@ -14,7 +15,7 @@ public class LuaState : IDisposable
public Lua Thread;
private Lua _parent;
private Queue<LuaEvent> _queue = new();
private ConcurrentQueue<LuaEvent> _queue = new();
private string[] _eventFilters = Array.Empty<string>();
@ -25,6 +26,9 @@ public class LuaState : IDisposable
Encoding = Encoding.UTF8,
};
_parent.PushString("Capy64 " + Capy64.Version);
_parent.SetGlobal("_HOST");
Sandbox.OpenLibraries(_parent);
Sandbox.Patch(_parent);
@ -80,7 +84,8 @@ public class LuaState : IDisposable
if (_queue.Count == 0)
return false;
var ev = _queue.Dequeue();
if (!_queue.TryDequeue(out var ev))
return false;
Thread.PushString(ev.Name);
npars = 1;