diff --git a/Capy64/Assets/bios.lua b/Capy64/Assets/bios.lua index 7e0b19e..6cce175 100644 --- a/Capy64/Assets/bios.lua +++ b/Capy64/Assets/bios.lua @@ -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 @@ -118,9 +122,4 @@ end bootScreen() -term.clear() -exit() - -while true do - coroutine.yield() -end \ No newline at end of file +term.clear() \ No newline at end of file diff --git a/Capy64/Runtime/Libraries/Event.cs b/Capy64/Runtime/Libraries/Event.cs index e08f091..5a5fe74 100644 --- a/Capy64/Runtime/Libraries/Event.cs +++ b/Capy64/Runtime/Libraries/Event.cs @@ -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; diff --git a/Capy64/Runtime/Libraries/OS.cs b/Capy64/Runtime/Libraries/OS.cs index 8a04778..5944521 100644 --- a/Capy64/Runtime/Libraries/OS.cs +++ b/Capy64/Runtime/Libraries/OS.cs @@ -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); diff --git a/Capy64/Runtime/Libraries/Term.cs b/Capy64/Runtime/Libraries/Term.cs index 6de70e0..974fe89 100644 --- a/Capy64/Runtime/Libraries/Term.cs +++ b/Capy64/Runtime/Libraries/Term.cs @@ -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 { diff --git a/Capy64/Runtime/Libraries/Timer.cs b/Capy64/Runtime/Libraries/Timer.cs index db2dd50..1469cc4 100644 --- a/Capy64/Runtime/Libraries/Timer.cs +++ b/Capy64/Runtime/Libraries/Timer.cs @@ -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 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; - } } diff --git a/Capy64/Runtime/LuaState.cs b/Capy64/Runtime/LuaState.cs index ceaea66..05870a8 100644 --- a/Capy64/Runtime/LuaState.cs +++ b/Capy64/Runtime/LuaState.cs @@ -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 _queue = new(); + private ConcurrentQueue _queue = new(); private string[] _eventFilters = Array.Empty(); @@ -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;