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 term = require("term")
local timer = require("timer") local timer = require("timer")
local gpu = require("gpu")
local bootSleep = 2000 local bootSleep = 2000
local bg = 0x0 local bg = 0x0
@ -9,6 +10,9 @@ term.setForeground(fg)
term.setBackground(bg) term.setBackground(bg)
term.clear() term.clear()
term.setSize(51, 19)
gpu.setScale(2)
local function sleep(n) local function sleep(n)
local timerId = timer.start(n) local timerId = timer.start(n)
repeat repeat
@ -82,7 +86,7 @@ local function setupScreen()
selection = selection + 1 selection = selection + 1
elseif ev[3] == "enter" then elseif ev[3] == "enter" then
options[selection][2]() options[selection][2]()
elseif ev[3] == "esc" then elseif ev[3] == "escape" then
exit() exit()
end end
@ -118,9 +122,4 @@ end
bootScreen() bootScreen()
term.clear() 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 eventName = L.CheckString(1);
var nargs = L.GetTop(); var nargs = L.GetTop();
var parsState = L.NewThread();
L.Pop(1);
L.XMove(parsState, nargs - 1);
_game.LuaRuntime.QueueEvent(eventName, LK => _game.LuaRuntime.QueueEvent(eventName, LK =>
{ {
for (int i = 2; i <= nargs; i++) var nargs = parsState.GetTop();
{ parsState.XMove(LK, nargs);
L.PushCopy(i);
}
L.XMove(LK, nargs - 1); return nargs;
return nargs - 1;
}); });
return 0; return 0;

View file

@ -12,29 +12,15 @@ public class OS : IPlugin
_game = game; _game = game;
} }
public void LuaInit(Lua state) public void LuaInit(Lua state)
{ {
state.GetGlobal("os"); state.GetGlobal("os");
state.PushString("version");
state.PushCFunction(L_Version);
state.SetTable(-3);
state.PushString("shutdown"); state.PushString("shutdown");
state.PushCFunction(L_Shutdown); state.PushCFunction(L_Shutdown);
state.SetTable(-3); 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) private static int L_Shutdown(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);

View file

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

View file

@ -1,6 +1,8 @@
using Capy64.API; using Capy64.API;
using KeraLua; using KeraLua;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace Capy64.Runtime.Libraries; namespace Capy64.Runtime.Libraries;
@ -18,15 +20,26 @@ class Timer : IPlugin
}; };
private static IGame _game; 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) public Timer(IGame game)
{ {
_game = game; _game = game;
_timerId = 0;
} }
public void LuaInit(Lua state) 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); state.RequireF("timer", Open, false);
} }
@ -52,8 +65,12 @@ class Timer : IPlugin
Interval = delay, Interval = delay,
}; };
timers[timerId] = timer;
timer.Elapsed += (o, e) => timer.Elapsed += (o, e) =>
{ {
timers.TryRemove(timerId, out _);
_game.LuaRuntime.QueueEvent("timer", LK => _game.LuaRuntime.QueueEvent("timer", LK =>
{ {
LK.PushInteger(timerId); LK.PushInteger(timerId);
@ -65,13 +82,4 @@ class Timer : IPlugin
L.PushInteger(timerId); L.PushInteger(timerId);
return 1; 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 Capy64.API;
using KeraLua; using KeraLua;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -14,7 +15,7 @@ public class LuaState : IDisposable
public Lua Thread; public Lua Thread;
private Lua _parent; private Lua _parent;
private Queue<LuaEvent> _queue = new(); private ConcurrentQueue<LuaEvent> _queue = new();
private string[] _eventFilters = Array.Empty<string>(); private string[] _eventFilters = Array.Empty<string>();
@ -25,6 +26,9 @@ public class LuaState : IDisposable
Encoding = Encoding.UTF8, Encoding = Encoding.UTF8,
}; };
_parent.PushString("Capy64 " + Capy64.Version);
_parent.SetGlobal("_HOST");
Sandbox.OpenLibraries(_parent); Sandbox.OpenLibraries(_parent);
Sandbox.Patch(_parent); Sandbox.Patch(_parent);
@ -80,7 +84,8 @@ public class LuaState : IDisposable
if (_queue.Count == 0) if (_queue.Count == 0)
return false; return false;
var ev = _queue.Dequeue(); if (!_queue.TryDequeue(out var ev))
return false;
Thread.PushString(ev.Name); Thread.PushString(ev.Name);
npars = 1; npars = 1;