Added BinaryWriteHandle

Clean up of all the code
This commit is contained in:
Alessandro Proto 2023-01-14 18:16:35 +01:00
parent b5f044223a
commit 927a2e253a
37 changed files with 497 additions and 477 deletions

View file

@ -1,10 +1,5 @@
using KeraLua; using KeraLua;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.API; namespace Capy64.API;

View file

@ -5,18 +5,10 @@ using Capy64.Eventing.Events;
using Capy64.LuaRuntime; using Capy64.LuaRuntime;
using Capy64.LuaRuntime.Libraries; using Capy64.LuaRuntime.Libraries;
using KeraLua; using KeraLua;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Xml.Linq;
using System.Diagnostics; using System.Diagnostics;
using System.IO;
namespace Capy64.BIOS; namespace Capy64.BIOS;

View file

@ -1,12 +1,6 @@
using Capy64.Core; using Capy64.Core;
using Capy64.LuaRuntime.Libraries; using Capy64.LuaRuntime.Libraries;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.BIOS; namespace Capy64.BIOS;
@ -30,8 +24,8 @@ public class PanicScreen
Term.Clear(); Term.Clear();
var title = " Capy64 "; var title = " Capy64 ";
var halfX = Term.Width / 2 + 1; var halfX = (Term.Width / 2) + 1;
Term.SetCursorPosition(halfX - title.Length / 2, 2); Term.SetCursorPosition(halfX - (title.Length / 2), 2);
Term.ForegroundColor = BackgroundColor; Term.ForegroundColor = BackgroundColor;
Term.BackgroundColor = ForegroundColor; Term.BackgroundColor = ForegroundColor;
Term.Write(title); Term.Write(title);
@ -41,7 +35,8 @@ public class PanicScreen
Term.SetCursorPosition(1, 4); Term.SetCursorPosition(1, 4);
Print(error + '\n'); Print(error + '\n');
if (details is not null) { if (details is not null)
{
Print(details); Print(details);
} }
} }
@ -51,7 +46,7 @@ public class PanicScreen
foreach (var ch in txt) foreach (var ch in txt)
{ {
Term.Write(ch.ToString()); Term.Write(ch.ToString());
if(Term.CursorPosition.X >= Term.Width || ch == '\n') if (Term.CursorPosition.X >= Term.Width || ch == '\n')
{ {
Term.SetCursorPosition(1, (int)Term.CursorPosition.Y + 1); Term.SetCursorPosition(1, (int)Term.CursorPosition.Y + 1);
} }

View file

@ -1,10 +1,5 @@
using Capy64.Eventing.Events; using Capy64.Eventing;
using Capy64.Eventing; using Capy64.Eventing.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Capy64.LuaRuntime; using Capy64.LuaRuntime;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
@ -103,7 +98,7 @@ internal class RuntimeInputEvents
e.IsHeld, e.IsHeld,
}); });
if (e.Mods.HasFlag(InputManager.Modifiers.LCtrl) || e.Mods.HasFlag(InputManager.Modifiers.RCtrl) && !e.IsHeld) if (e.Mods.HasFlag(InputManager.Modifiers.LCtrl) || (e.Mods.HasFlag(InputManager.Modifiers.RCtrl) && !e.IsHeld))
{ {
if (e.Key == Keys.C) if (e.Key == Keys.C)
{ {

View file

@ -2,20 +2,16 @@
using Capy64.Core; using Capy64.Core;
using Capy64.Eventing; using Capy64.Eventing;
using Capy64.Extensions; using Capy64.Extensions;
using Capy64.Eventing.Events;
using Capy64.LuaRuntime; using Capy64.LuaRuntime;
using Capy64.PluginManager; using Capy64.PluginManager;
using KeraLua;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using static Capy64.Utils;
using System.IO; using System.IO;
using System.Linq;
using static Capy64.Utils;
namespace Capy64; namespace Capy64;

View file

@ -1,10 +1,4 @@
using System; namespace Capy64.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.Configuration;
class HTTP class HTTP
{ {

View file

@ -1,11 +1,4 @@
using System; namespace Capy64.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.Configuration;
class WebSockets class WebSockets
{ {

View file

@ -1,7 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Audio;
using System; using System;
using System.Threading.Channels;
namespace Capy64.Core; namespace Capy64.Core;
@ -30,11 +29,12 @@ public class Audio
{ {
var outBuffer = new byte[SamplesPerBuffer * bytesPerSample]; var outBuffer = new byte[SamplesPerBuffer * bytesPerSample];
for (int i = 0; i < from.Length; i++) { for (int i = 0; i < from.Length; i++)
{
var floatSample = MathHelper.Clamp(from[i], -1.0f, 1.0f); var floatSample = MathHelper.Clamp(from[i], -1.0f, 1.0f);
var shortSample = (short)(floatSample * short.MaxValue); var shortSample = (short)(floatSample * short.MaxValue);
int index = i * bytesPerSample + bytesPerSample; int index = (i * bytesPerSample) + bytesPerSample;
if (!BitConverter.IsLittleEndian) if (!BitConverter.IsLittleEndian)
{ {

View file

@ -81,7 +81,7 @@ public class Drawing : IDisposable
if (point.X < 0 || point.Y < 0) return; if (point.X < 0 || point.Y < 0) return;
if (point.X >= _canvas.Width || point.Y >= _canvas.Height) return; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) return;
grid[point.X + point.Y * _canvas.Width] = color; grid[point.X + (point.Y * _canvas.Width)] = color;
_canvas.SetData(grid); _canvas.SetData(grid);
} }
@ -94,7 +94,7 @@ public class Drawing : IDisposable
{ {
if (point.X < 0 || point.Y < 0) continue; if (point.X < 0 || point.Y < 0) continue;
if (point.X >= _canvas.Width || point.Y >= _canvas.Height) continue; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) continue;
grid[point.X + point.Y * _canvas.Width] = color; grid[point.X + (point.Y * _canvas.Width)] = color;
} }
_canvas.SetData(grid); _canvas.SetData(grid);
} }
@ -103,7 +103,7 @@ public class Drawing : IDisposable
{ {
var grid = new Color[_canvas.Width * _canvas.Height]; var grid = new Color[_canvas.Width * _canvas.Height];
_canvas.GetData(grid); _canvas.GetData(grid);
grid[point.X + point.Y * _canvas.Width] = color; grid[point.X + (point.Y * _canvas.Width)] = color;
_canvas.SetData(grid); _canvas.SetData(grid);
} }
@ -113,7 +113,7 @@ public class Drawing : IDisposable
_canvas.GetData(grid); _canvas.GetData(grid);
foreach (var point in points) foreach (var point in points)
{ {
grid[point.X + point.Y * _canvas.Width] = color; grid[point.X + (point.Y * _canvas.Width)] = color;
} }
_canvas.SetData(grid); _canvas.SetData(grid);
} }

View file

@ -85,7 +85,7 @@ public class EventEmitter
public void RaiseTick(TickEvent ev) public void RaiseTick(TickEvent ev)
{ {
if(OnTick is not null) if (OnTick is not null)
{ {
OnTick(this, ev); OnTick(this, ev);
} }
@ -93,7 +93,7 @@ public class EventEmitter
public void RaiseInit() public void RaiseInit()
{ {
if(OnInit is not null) if (OnInit is not null)
{ {
OnInit(this, EventArgs.Empty); OnInit(this, EventArgs.Empty);
} }
@ -101,7 +101,7 @@ public class EventEmitter
public void RaiseScreenSizeChange() public void RaiseScreenSizeChange()
{ {
if(OnScreenSizeChange is not null) if (OnScreenSizeChange is not null)
{ {
OnScreenSizeChange(this, EventArgs.Empty); OnScreenSizeChange(this, EventArgs.Empty);
} }

View file

@ -1,9 +1,5 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.Eventing.Events; namespace Capy64.Eventing.Events;

View file

@ -1,9 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.Extensions.Bindings; namespace Capy64.Extensions.Bindings;

View file

@ -1,11 +1,6 @@
using Capy64.Extensions.Bindings; using Capy64.Extensions.Bindings;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.Extensions; namespace Capy64.Extensions;
@ -29,7 +24,7 @@ public static class GameWindowExtensions
SDL_WINDOW_INPUT_FOCUS = 0x00000200, SDL_WINDOW_INPUT_FOCUS = 0x00000200,
SDL_WINDOW_MOUSE_FOCUS = 0x00000400, SDL_WINDOW_MOUSE_FOCUS = 0x00000400,
SDL_WINDOW_FULLSCREEN_DESKTOP = SDL_WINDOW_FULLSCREEN_DESKTOP =
(SDL_WINDOW_FULLSCREEN | 0x00001000), SDL_WINDOW_FULLSCREEN | 0x00001000,
SDL_WINDOW_FOREIGN = 0x00000800, SDL_WINDOW_FOREIGN = 0x00000800,
SDL_WINDOW_ALLOW_HIGHDPI = 0x00002000, SDL_WINDOW_ALLOW_HIGHDPI = 0x00002000,
SDL_WINDOW_MOUSE_CAPTURE = 0x00004000, SDL_WINDOW_MOUSE_CAPTURE = 0x00004000,

View file

@ -20,7 +20,7 @@ public interface IGame
EventEmitter EventEmitter { get; } EventEmitter EventEmitter { get; }
void ConfigureServices(IServiceProvider serviceProvider); void ConfigureServices(IServiceProvider serviceProvider);
int Width { get; set; } int Width { get; set; }
int Height { get; set; } int Height { get; set; }
float Scale { get; set; } float Scale { get; set; }
void UpdateSize(); void UpdateSize();

View file

@ -1,10 +1,4 @@
using System; namespace Capy64.LuaRuntime;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime;
public class Constants public class Constants
{ {

View file

@ -1,12 +1,8 @@
using KeraLua; using KeraLua;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Extensions; namespace Capy64.LuaRuntime.Extensions;
public static class Utils public static class Utils

View file

@ -2,9 +2,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers; namespace Capy64.LuaRuntime.Handlers;
@ -18,92 +16,58 @@ public class BinaryReadHandle : IHandle
Stream = new BinaryReader(stream, Encoding.ASCII); Stream = new BinaryReader(stream, Encoding.ASCII);
} }
public BinaryReadHandle(BinaryReader stream) private static readonly Dictionary<string, LuaFunction> functions = new()
{ {
Stream = stream; ["nextByte"] = L_NextByte,
} ["nextShort"] = L_NextShort,
["nextInt"] = L_NextInt,
["nextLong"] = L_NextLong,
["nextSByte"] = L_NextSByte,
["nextUShort"] = L_NextUShort,
["nextUInt"] = L_NextUInt,
["nextULong"] = L_NextULong,
["nextHalf"] = L_NextHalf,
["nextFloat"] = L_NextFloat,
["nextDouble"] = L_NextDouble,
["nextChar"] = L_NextChar,
["nextString"] = L_NextString,
["nextBoolean"] = L_NextBoolean,
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true) public void Push(Lua L, bool newTable = true)
{ {
if (newTable) if (newTable)
L.NewTable(); L.NewTable();
L.PushString("nextByte"); foreach (var pair in functions)
L.PushCFunction(L_NextByte); {
L.SetTable(-3); L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.PushString("nextShort"); L.SetTable(-3);
L.PushCFunction(L_NextShort); }
L.SetTable(-3);
L.PushString("nextInt");
L.PushCFunction(L_NextInt);
L.SetTable(-3);
L.PushString("nextLong");
L.PushCFunction(L_NextLong);
L.SetTable(-3);
L.PushString("nextSByte");
L.PushCFunction(L_NextSByte);
L.SetTable(-3);
L.PushString("nextUShort");
L.PushCFunction(L_NextUShort);
L.SetTable(-3);
L.PushString("nextUInt");
L.PushCFunction(L_NextUInt);
L.SetTable(-3);
L.PushString("nextULong");
L.PushCFunction(L_NextULong);
L.SetTable(-3);
L.PushString("nextHalf");
L.PushCFunction(L_NextHalf);
L.SetTable(-3);
L.PushString("nextFloat");
L.PushCFunction(L_NextFloat);
L.SetTable(-3);
L.PushString("nextDouble");
L.PushCFunction(L_NextDouble);
L.SetTable(-3);
L.PushString("nextChar");
L.PushCFunction(L_NextChar);
L.SetTable(-3);
L.PushString("nextString");
L.PushCFunction(L_NextString);
L.SetTable(-3);
L.PushString("nextBoolean");
L.PushCFunction(L_NextBoolean);
L.SetTable(-3);
L.PushString("close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("_handle"); L.PushString("_handle");
L.PushObject(this); L.PushObject(this);
L.SetTable(-3); L.SetTable(-3);
} }
private static BinaryReadHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<BinaryReadHandle>(-1, gc);
}
private static int L_NextByte(IntPtr state) private static int L_NextByte(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var count = (int)L.OptNumber(2, 1); var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer"); L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
L.PushString("_handle"); var h = GetHandle(L, false);
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -133,11 +97,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -154,11 +114,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -175,11 +131,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -196,11 +148,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -217,11 +165,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -238,11 +182,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -259,11 +199,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -280,11 +216,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -301,11 +233,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -322,11 +250,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -343,13 +267,10 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var count = (int)L.OptNumber(2, 1); var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer"); L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
L.PushString("_handle"); var h = GetHandle(L, false);
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -379,13 +300,10 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var count = (int)L.OptNumber(2, 1); var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer"); L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
L.PushString("_handle"); var h = GetHandle(L, false);
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -412,11 +330,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -433,10 +347,7 @@ public class BinaryReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, true);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, true);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
return 0; return 0;

View file

@ -2,131 +2,372 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers; namespace Capy64.LuaRuntime.Handlers;
[Obsolete("Work in progress")]
public class BinaryWriteHandle : IHandle public class BinaryWriteHandle : IHandle
{ {
private readonly BinaryWriter _stream; private readonly BinaryWriter Stream;
private bool isClosed = false; private bool IsClosed = false;
public BinaryWriteHandle(Stream stream) public BinaryWriteHandle(Stream stream)
{ {
_stream = new BinaryWriter(stream); Stream = new BinaryWriter(stream, Encoding.ASCII);
} }
public BinaryWriteHandle(BinaryWriter stream) private static readonly Dictionary<string, LuaFunction> functions = new()
{ {
_stream = stream; ["writeByte"] = L_WriteByte,
} ["writeShort"] = L_WriteShort,
["writeInt"] = L_WriteInt,
["writeLong"] = L_WriteLong,
["writeSByte"] = L_WriteSByte,
["writeUShort"] = L_WriteUShort,
["writeUInt"] = L_WriteUInt,
["writeULong"] = L_WriteULong,
["writeHalf"] = L_WriteHalf,
["writeFloat"] = L_WriteFloat,
["writeDouble"] = L_WriteDouble,
["writeChar"] = L_WriteChar,
["writeString"] = L_WriteString,
["writeBoolean"] = L_WriteBoolean,
["flush"] = L_Flush,
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true) public void Push(Lua L, bool newTable = true)
{ {
throw new NotImplementedException(); if (newTable)
L.NewTable();
foreach (var pair in functions)
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.SetTable(-3);
}
L.PushString("_handle");
L.PushObject(this);
L.SetTable(-3);
} }
/*public void Push(Lua L) private static BinaryWriteHandle GetHandle(Lua L, bool gc = true)
{ {
L.NewTable(); L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.PushString("readAll"); L.GetTable(1);
L.PushCFunction(L_ReadAll); return L.ToObject<BinaryWriteHandle>(-1, gc);
L.SetTable(-3);
L.PushString("readLine");
L.PushCFunction(L_ReadLine);
L.SetTable(-3);
L.PushString("read");
L.PushCFunction(L_Read);
L.SetTable(-3);
L.PushString("close");
L.PushCFunction(L_Close);
L.SetTable(-3);
} }
private int L_ReadAll(IntPtr state) private static int L_WriteByte(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
if (isClosed) byte[] buffer;
L.Error("handle is closed"); if (L.IsInteger(2))
if (ended)
{ {
L.PushNil(); buffer = new[]
return 1; {
(byte)L.ToInteger(2),
};
} }
else if (L.IsTable(2))
//var content = _stream.read;
L.PushString(content);
return 1;
}
private int L_ReadLine(IntPtr state)
{
var L = Lua.FromIntPtr(state);
if (isClosed)
L.Error("handle is closed");
if (ended)
{ {
L.PushNil(); var len = L.RawLen(2);
return 1; buffer = new byte[len];
for (int i = 1; i <= len; i++)
{
L.PushInteger(i);
L.GetTable(2);
var b = L.CheckInteger(-1);
L.Pop(1);
buffer[i - 1] = (byte)b;
}
} }
var line = _stream.ReadLine();
if (line is null)
L.PushNil();
else else
L.PushString(line);
return 1;
}
private int L_Read(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var count = (int)L.OptNumber(1, 1);
L.ArgumentCheck(count < 1, 1, "count must be a positive integer");
if (ended)
L.Error("handle is closed");
if (_stream.EndOfStream)
{ {
L.PushNil(); L.ArgumentError(2, "integer or table expected, got " + L.Type(2));
return 1; return 0;
} }
var chunk = new char[count]; var h = GetHandle(L, false);
_stream.Read(chunk, 0, count); if (h is null || h.IsClosed)
L.Error("handle is closed");
L.PushString(new string(chunk)); h.Stream.Write(buffer);
return 1;
}
private int L_Close(IntPtr state)
{
var L = Lua.FromIntPtr(state);
if (isClosed)
return 0;
_stream.Close();
isClosed = true;
return 0; return 0;
}*/ }
private static int L_WriteShort(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((short)b);
return 0;
}
private static int L_WriteInt(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((int)b);
return 0;
}
private static int L_WriteLong(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write(b);
return 0;
}
private static int L_WriteSByte(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((sbyte)b);
return 0;
}
private static int L_WriteUShort(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((ushort)b);
return 0;
}
private static int L_WriteUInt(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((uint)b);
return 0;
}
private static int L_WriteULong(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckInteger(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((ulong)b);
return 0;
}
private static int L_WriteHalf(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckNumber(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((Half)b);
return 0;
}
private static int L_WriteFloat(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckNumber(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((float)b);
return 0;
}
private static int L_WriteDouble(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckNumber(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write((double)b);
return 0;
}
private static int L_WriteChar(IntPtr state)
{
var L = Lua.FromIntPtr(state);
byte[] buffer;
if (L.IsString(2))
{
var str = L.ToString(2);
buffer = Encoding.ASCII.GetBytes(str);
}
else if (L.IsTable(2))
{
var len = L.RawLen(2);
var tmpBuffer = new List<byte>();
for (int i = 1; i <= len; i++)
{
L.PushInteger(i);
L.GetTable(2);
var b = L.CheckString(-1);
L.Pop(1);
var chunk = Encoding.ASCII.GetBytes(b);
foreach (var c in chunk)
{
tmpBuffer.Add(c);
}
}
buffer = tmpBuffer.ToArray();
}
else
{
L.ArgumentError(2, "integer or table expected, got " + L.Type(2));
return 0;
}
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write(buffer);
return 0;
}
private static int L_WriteString(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var b = L.CheckString(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write(b);
return 0;
}
private static int L_WriteBoolean(IntPtr state)
{
var L = Lua.FromIntPtr(state);
L.CheckType(2, LuaType.Boolean);
var b = L.ToBoolean(2);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write(b);
return 0;
}
private static int L_Flush(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Flush();
return 0;
}
private static int L_Close(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var h = GetHandle(L, true);
if (h is null || h.IsClosed)
return 0;
h.Stream.Close();
h.IsClosed = true;
return 0;
}
} }

View file

@ -1,9 +1,4 @@
using KeraLua; using KeraLua;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers; namespace Capy64.LuaRuntime.Handlers;

View file

@ -2,10 +2,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers; namespace Capy64.LuaRuntime.Handlers;
@ -18,40 +14,44 @@ public class ReadHandle : IHandle
Stream = new StreamReader(stream); Stream = new StreamReader(stream);
} }
private static readonly Dictionary<string, LuaFunction> functions = new()
{
["readAll"] = L_ReadAll,
["readLine"] = L_ReadLine,
["read"] = L_Read,
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true) public void Push(Lua L, bool newTable = true)
{ {
if (newTable) if (newTable)
L.NewTable(); L.NewTable();
L.PushString("readAll"); foreach (var pair in functions)
L.PushCFunction(L_ReadAll); {
L.SetTable(-3); L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.PushString("readLine"); L.SetTable(-3);
L.PushCFunction(L_ReadLine); }
L.SetTable(-3);
L.PushString("read");
L.PushCFunction(L_Read);
L.SetTable(-3);
L.PushString("close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("_handle"); L.PushString("_handle");
L.PushObject(this); L.PushObject(this);
L.SetTable(-3); L.SetTable(-3);
} }
private static ReadHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<ReadHandle>(-1, gc);
}
private static int L_ReadAll(IntPtr state) private static int L_ReadAll(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<ReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -72,10 +72,7 @@ public class ReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<ReadHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -94,16 +91,11 @@ public class ReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var count = (int)L.OptNumber(2, 1); var count = (int)L.OptNumber(2, 1);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<ReadHandle>(-1, false);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer"); L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
var h = GetHandle(L, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -126,10 +118,7 @@ public class ReadHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, true);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<ReadHandle>(-1, true);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
return 0; return 0;

View file

@ -2,9 +2,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers; namespace Capy64.LuaRuntime.Handlers;
@ -22,47 +19,50 @@ public class WriteHandle : IHandle
Stream = stream; Stream = stream;
} }
private static readonly Dictionary<string, LuaFunction> functions = new()
{
["write"] = L_Write,
["writeLine"] = L_WriteLine,
["flush"] = L_Flush,
["close"] = L_Close,
};
public void Push(Lua L, bool newTable = true) public void Push(Lua L, bool newTable = true)
{ {
if (newTable) if (newTable)
L.NewTable(); L.NewTable();
L.PushString("write"); foreach (var pair in functions)
L.PushCFunction(L_Write); {
L.SetTable(-3); L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.PushString("writeLine"); L.SetTable(-3);
L.PushCFunction(L_WriteLine); }
L.SetTable(-3);
L.PushString("flush");
L.PushCFunction(L_Flush);
L.SetTable(-3);
L.PushString("close");
L.PushCFunction(L_Close);
L.SetTable(-3);
L.PushString("_handle"); L.PushString("_handle");
L.PushObject(this); L.PushObject(this);
L.SetTable(-3); L.SetTable(-3);
} }
private static WriteHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<WriteHandle>(-1, gc);
}
private static int L_Write(IntPtr state) private static int L_Write(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var content = L.CheckString(2); var content = L.CheckString(2);
L.PushString("_handle"); var h = GetHandle(L, false);
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
h.Stream.Write(content); h.Stream.Write(content);
return 0; return 0;
@ -72,17 +72,13 @@ public class WriteHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var content = L.CheckString(2); var content = L.CheckString(2);
L.PushString("_handle"); var h = GetHandle(L, false);
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
h.Stream.WriteLine(content); h.Stream.WriteLine(content);
return 0; return 0;
@ -92,10 +88,7 @@ public class WriteHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, false);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, false);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
L.Error("handle is closed"); L.Error("handle is closed");
@ -109,10 +102,7 @@ public class WriteHandle : IHandle
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table); var h = GetHandle(L, true);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, true);
if (h is null || h.IsClosed) if (h is null || h.IsClosed)
return 0; return 0;

View file

@ -1,10 +1,4 @@
using System; namespace Capy64.LuaRuntime;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime;
public interface ILuaEvent public interface ILuaEvent
{ {

View file

@ -6,8 +6,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading;
namespace Capy64.LuaRuntime.Libraries; namespace Capy64.LuaRuntime.Libraries;
@ -487,7 +485,7 @@ public class FileSystem : IPlugin
fileAccess = FileAccess.Read; fileAccess = FileAccess.Read;
break; break;
case 'w': case 'w':
fileMode = FileMode.CreateNew; fileMode = FileMode.Create;
fileAccess = FileAccess.Write; fileAccess = FileAccess.Write;
break; break;
case 'a': case 'a':
@ -515,17 +513,27 @@ public class FileSystem : IPlugin
var fileStream = File.Open(path, fileMode, fileAccess, FileShare.ReadWrite); var fileStream = File.Open(path, fileMode, fileAccess, FileShare.ReadWrite);
// todo: add binary mode IHandle handle;
if (fileAccess == FileAccess.Read) if (fileAccess == FileAccess.Read)
{ {
var handle = new ReadHandle(fileStream); if (binaryMode)
handle.Push(L); handle = new BinaryReadHandle(fileStream);
else
handle = new ReadHandle(fileStream);
} }
else if (fileAccess == FileAccess.Write) else if (fileAccess == FileAccess.Write)
{ {
var handle = new WriteHandle(fileStream); if (binaryMode)
handle.Push(L); handle = new BinaryWriteHandle(fileStream);
else
handle = new WriteHandle(fileStream);
} }
else
{
throw new InvalidOperationException();
}
handle.Push(L);
return 1; return 1;
} }

View file

@ -1,5 +1,4 @@
using Capy64.API; using Capy64.API;
using Capy64.Core;
using KeraLua; using KeraLua;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using System; using System;
@ -81,7 +80,7 @@ public class Graphics : IPlugin
L.ArgumentCheck(L.IsNumber(-1), 1, "expected number at index " + (i + 1)); L.ArgumentCheck(L.IsNumber(-1), 1, "expected number at index " + (i + 1));
var y = (int)L.ToNumber(-1) - 1; var y = (int)L.ToNumber(-1) - 1;
L.Pop(1); L.Pop(1);
pts.Add(new Point(x, y)); pts.Add(new Point(x, y));
} }

View file

@ -5,7 +5,6 @@ using KeraLua;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net.Http; using System.Net.Http;
@ -155,7 +154,7 @@ public class HTTP : IPlugin
reqTask.ContinueWith(async (task) => reqTask.ContinueWith(async (task) =>
{ {
if(task.IsFaulted || task.IsCanceled) if (task.IsFaulted || task.IsCanceled)
{ {
_game.LuaRuntime.PushEvent("http_failure", requestId, task.Exception?.Message); _game.LuaRuntime.PushEvent("http_failure", requestId, task.Exception?.Message);
return; return;

View file

@ -1,10 +1,6 @@
using Capy64.API; using Capy64.API;
using KeraLua; using KeraLua;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Libraries; namespace Capy64.LuaRuntime.Libraries;

View file

@ -1,12 +1,6 @@
using Capy64.API; using Capy64.API;
using Capy64.Core;
using KeraLua; using KeraLua;
using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Libraries; namespace Capy64.LuaRuntime.Libraries;

View file

@ -173,7 +173,7 @@ internal class Term : IPlugin
if (!save) if (!save)
return; return;
CharGrid[(int)pos.X + (int)pos.Y * Width] = new Char CharGrid[(int)pos.X + ((int)pos.Y * Width)] = new Char
{ {
Character = ch, Character = ch,
Foreground = ForegroundColor, Foreground = ForegroundColor,
@ -186,7 +186,7 @@ internal class Term : IPlugin
if (pos.X < 0 || pos.Y < 0 || pos.X >= Width || pos.Y >= Height) if (pos.X < 0 || pos.Y < 0 || pos.X >= Width || pos.Y >= Height)
return; return;
var ch = CharGrid[(int)pos.X + (int)pos.Y * Width] ?? var ch = CharGrid[(int)pos.X + ((int)pos.Y * Width)] ??
new Char new Char
{ {
Character = ' ', Character = ' ',
@ -199,9 +199,9 @@ internal class Term : IPlugin
public static void RedrawAll() public static void RedrawAll()
{ {
for(int y = 0; y < Height; y++) for (int y = 0; y < Height; y++)
{ {
for(int x = 0; x < Width; x++) for (int x = 0; x < Width; x++)
{ {
RedrawPos(new(x, y)); RedrawPos(new(x, y));
} }
@ -210,7 +210,7 @@ internal class Term : IPlugin
public static void DumpScreen(bool clear = false) public static void DumpScreen(bool clear = false)
{ {
if(clear) if (clear)
Console.Clear(); Console.Clear();
Console.WriteLine("\n /{0}", new string('-', Width)); Console.WriteLine("\n /{0}", new string('-', Width));
for (int i = 0; i < CharGrid.Length; i++) for (int i = 0; i < CharGrid.Length; i++)
@ -244,7 +244,7 @@ internal class Term : IPlugin
if (_cursorPosition.X < 0 || _cursorPosition.Y < 0 || _cursorPosition.X >= Width || _cursorPosition.Y >= Height) if (_cursorPosition.X < 0 || _cursorPosition.Y < 0 || _cursorPosition.X >= Width || _cursorPosition.Y >= Height)
return; return;
var ch = CharGrid[(int)_cursorPosition.X + (int)_cursorPosition.Y * Width] ?? var ch = CharGrid[(int)_cursorPosition.X + ((int)_cursorPosition.Y * Width)] ??
new Char new Char
{ {
Character = ' ', Character = ' ',
@ -453,8 +453,8 @@ internal class Term : IPlugin
var x = (int)L.CheckNumber(1); var x = (int)L.CheckNumber(1);
var y = (int)L.CheckNumber(2); var y = (int)L.CheckNumber(2);
L.PushInteger(x * CharWidth - CharWidth + 1); L.PushInteger((x * CharWidth) - CharWidth + 1);
L.PushInteger(y * CharHeight - CharHeight + 1); L.PushInteger((y * CharHeight) - CharHeight + 1);
return 2; return 2;
} }
@ -466,8 +466,8 @@ internal class Term : IPlugin
var x = (int)L.CheckNumber(1) - 1; var x = (int)L.CheckNumber(1) - 1;
var y = (int)L.CheckNumber(2) - 1; var y = (int)L.CheckNumber(2) - 1;
L.PushInteger(x / CharWidth + 1); L.PushInteger((x / CharWidth) + 1);
L.PushInteger(y / CharHeight + 1); L.PushInteger((y / CharHeight) + 1);
return 2; return 2;
} }

View file

@ -1,11 +1,6 @@
using Capy64.API; using Capy64.API;
using KeraLua; using KeraLua;
using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Libraries; namespace Capy64.LuaRuntime.Libraries;

View file

@ -1,9 +1,5 @@
using KeraLua; using KeraLua;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime; namespace Capy64.LuaRuntime;

View file

@ -1,9 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime; namespace Capy64.LuaRuntime;

View file

@ -5,9 +5,7 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection;
using System.Text; using System.Text;
using System.Threading;
namespace Capy64.LuaRuntime; namespace Capy64.LuaRuntime;
@ -79,7 +77,8 @@ public class Runtime
/// </param> /// </param>
public void PushEvent(string name, Func<Lua, int> handler) public void PushEvent(string name, Func<Lua, int> handler)
{ {
eventQueue.Enqueue(new LuaDelegateEvent { eventQueue.Enqueue(new LuaDelegateEvent
{
Name = name, Name = name,
Handler = handler Handler = handler
}); });
@ -102,7 +101,7 @@ public class Runtime
private bool ResumeThread(ILuaEvent ev) private bool ResumeThread(ILuaEvent ev)
{ {
if (filters.Length > 0 && !filters.Contains(ev.Name)) if (filters.Length > 0 && !filters.Contains(ev.Name))
{ {
@ -119,7 +118,7 @@ public class Runtime
var status = Thread.Resume(null, evpars, out int pars); var status = Thread.Resume(null, evpars, out int pars);
if (status is LuaStatus.OK or LuaStatus.Yield) if (status is LuaStatus.OK or LuaStatus.Yield)
{ {
if(Disposing) if (Disposing)
return false; return false;
filters = new string[pars]; filters = new string[pars];
for (int i = 0; i < pars; i++) for (int i = 0; i < pars; i++)
@ -141,7 +140,7 @@ public class Runtime
{ {
Thread.PushString(ev.Name); Thread.PushString(ev.Name);
switch(ev) switch (ev)
{ {
case LuaEvent e: case LuaEvent e:

View file

@ -2,13 +2,9 @@
using Capy64.LuaRuntime.Libraries; using Capy64.LuaRuntime.Libraries;
using KeraLua; using KeraLua;
using System; using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime; namespace Capy64.LuaRuntime;
@ -278,7 +274,7 @@ internal class Sandbox
L.Error(L.ToString(-1)); L.Error(L.ToString(-1));
return 0; return 0;
} }
L.PushCopy(-2); L.PushCopy(-2);
L.Insert(1); L.Insert(1);
L.SetTop(1); L.SetTop(1);

View file

@ -1,10 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.Loader; using System.Runtime.Loader;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.PluginManager namespace Capy64.PluginManager
{ {

View file

@ -2,7 +2,6 @@
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using System;
using System.IO; using System.IO;
using var game = new Capy64.Capy64(); using var game = new Capy64.Capy64();

View file

@ -1,10 +1,4 @@
using System; namespace Capy64;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64;
public static class Utils public static class Utils
{ {
@ -15,7 +9,7 @@ public static class Utils
public static void UnpackRGB(uint packed, out byte r, out byte g, out byte b) public static void UnpackRGB(uint packed, out byte r, out byte g, out byte b)
{ {
b = (byte)(packed & 0xff); b = (byte)(packed & 0xff);
g = (byte)(packed >> 8 & 0xff); g = (byte)((packed >> 8) & 0xff);
r = (byte)(packed >> 16 & 0xff); r = (byte)((packed >> 16) & 0xff);
} }
} }

View file

@ -1,9 +1,5 @@
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Xna.Framework;
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;