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 Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.API;

View file

@ -5,18 +5,10 @@ using Capy64.Eventing.Events;
using Capy64.LuaRuntime;
using Capy64.LuaRuntime.Libraries;
using KeraLua;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System;
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.IO;
namespace Capy64.BIOS;

View file

@ -1,12 +1,6 @@
using Capy64.Core;
using Capy64.LuaRuntime.Libraries;
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;
@ -30,8 +24,8 @@ public class PanicScreen
Term.Clear();
var title = " Capy64 ";
var halfX = Term.Width / 2 + 1;
Term.SetCursorPosition(halfX - title.Length / 2, 2);
var halfX = (Term.Width / 2) + 1;
Term.SetCursorPosition(halfX - (title.Length / 2), 2);
Term.ForegroundColor = BackgroundColor;
Term.BackgroundColor = ForegroundColor;
Term.Write(title);
@ -41,7 +35,8 @@ public class PanicScreen
Term.SetCursorPosition(1, 4);
Print(error + '\n');
if (details is not null) {
if (details is not null)
{
Print(details);
}
}

View file

@ -1,10 +1,5 @@
using Capy64.Eventing.Events;
using Capy64.Eventing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Capy64.Eventing;
using Capy64.Eventing.Events;
using Capy64.LuaRuntime;
using Microsoft.Xna.Framework.Input;
@ -103,7 +98,7 @@ internal class RuntimeInputEvents
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)
{

View file

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

View file

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

View file

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

View file

@ -1,7 +1,6 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using System;
using System.Threading.Channels;
namespace Capy64.Core;
@ -30,11 +29,12 @@ public class Audio
{
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 shortSample = (short)(floatSample * short.MaxValue);
int index = i * bytesPerSample + bytesPerSample;
int index = (i * bytesPerSample) + bytesPerSample;
if (!BitConverter.IsLittleEndian)
{

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,9 +2,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers;
@ -18,92 +16,58 @@ public class BinaryReadHandle : IHandle
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)
{
if (newTable)
L.NewTable();
L.PushString("nextByte");
L.PushCFunction(L_NextByte);
L.SetTable(-3);
L.PushString("nextShort");
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);
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);
}
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)
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -133,11 +97,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -154,11 +114,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -175,11 +131,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -196,11 +148,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -217,11 +165,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -238,11 +182,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -259,11 +199,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -280,11 +216,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -301,11 +233,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -322,11 +250,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -343,13 +267,10 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -379,13 +300,10 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var count = (int)L.OptNumber(2, 1);
L.ArgumentCheck(count >= 1, 2, "count must be a positive integer");
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -412,11 +330,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -433,10 +347,7 @@ public class BinaryReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<BinaryReadHandle>(-1, true);
var h = GetHandle(L, true);
if (h is null || h.IsClosed)
return 0;

View file

@ -2,131 +2,372 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers;
[Obsolete("Work in progress")]
public class BinaryWriteHandle : IHandle
{
private readonly BinaryWriter _stream;
private bool isClosed = false;
private readonly BinaryWriter Stream;
private bool IsClosed = false;
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)
{
throw new NotImplementedException();
}
/*public void Push(Lua L)
{
if (newTable)
L.NewTable();
L.PushString("readAll");
L.PushCFunction(L_ReadAll);
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);
foreach (var pair in functions)
{
L.PushString(pair.Key);
L.PushCFunction(pair.Value);
L.SetTable(-3);
}
private int L_ReadAll(IntPtr state)
L.PushString("_handle");
L.PushObject(this);
L.SetTable(-3);
}
private static BinaryWriteHandle GetHandle(Lua L, bool gc = true)
{
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
return L.ToObject<BinaryWriteHandle>(-1, gc);
}
private static int L_WriteByte(IntPtr state)
{
var L = Lua.FromIntPtr(state);
if (isClosed)
L.Error("handle is closed");
if (ended)
byte[] buffer;
if (L.IsInteger(2))
{
L.PushNil();
return 1;
}
//var content = _stream.read;
L.PushString(content);
return 1;
}
private int L_ReadLine(IntPtr state)
buffer = new[]
{
var L = Lua.FromIntPtr(state);
if (isClosed)
L.Error("handle is closed");
if (ended)
{
L.PushNil();
return 1;
(byte)L.ToInteger(2),
};
}
else if (L.IsTable(2))
{
var len = L.RawLen(2);
buffer = new byte[len];
var line = _stream.ReadLine();
if (line is null)
L.PushNil();
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;
}
}
else
L.PushString(line);
return 1;
{
L.ArgumentError(2, "integer or table expected, got " + L.Type(2));
return 0;
}
private int L_Read(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var count = (int)L.OptNumber(1, 1);
var h = GetHandle(L, false);
L.ArgumentCheck(count < 1, 1, "count must be a positive integer");
if (ended)
if (h is null || h.IsClosed)
L.Error("handle is closed");
if (_stream.EndOfStream)
{
L.PushNil();
return 1;
h.Stream.Write(buffer);
return 0;
}
var chunk = new char[count];
_stream.Read(chunk, 0, count);
L.PushString(new string(chunk));
return 1;
}
private int L_Close(IntPtr state)
private static int L_WriteShort(IntPtr state)
{
var L = Lua.FromIntPtr(state);
if (isClosed)
return 0;
var b = L.CheckInteger(2);
_stream.Close();
var h = GetHandle(L, false);
isClosed = true;
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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers;

View file

@ -2,10 +2,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers;
@ -18,40 +14,44 @@ public class ReadHandle : IHandle
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)
{
if (newTable)
L.NewTable();
L.PushString("readAll");
L.PushCFunction(L_ReadAll);
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);
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);
}
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)
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<ReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -72,10 +72,7 @@ public class ReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<ReadHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -94,16 +91,11 @@ public class ReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
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");
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -126,10 +118,7 @@ public class ReadHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<ReadHandle>(-1, true);
var h = GetHandle(L, true);
if (h is null || h.IsClosed)
return 0;

View file

@ -2,9 +2,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime.Handlers;
@ -22,47 +19,50 @@ public class WriteHandle : IHandle
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)
{
if (newTable)
L.NewTable();
L.PushString("write");
L.PushCFunction(L_Write);
L.SetTable(-3);
L.PushString("writeLine");
L.PushCFunction(L_WriteLine);
L.SetTable(-3);
L.PushString("flush");
L.PushCFunction(L_Flush);
L.SetTable(-3);
L.PushString("close");
L.PushCFunction(L_Close);
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);
}
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)
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var content = L.CheckString(2);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.Write(content);
return 0;
@ -72,17 +72,13 @@ public class WriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
var content = L.CheckString(2);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
h.Stream.WriteLine(content);
return 0;
@ -92,10 +88,7 @@ public class WriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, false);
var h = GetHandle(L, false);
if (h is null || h.IsClosed)
L.Error("handle is closed");
@ -109,10 +102,7 @@ public class WriteHandle : IHandle
{
var L = Lua.FromIntPtr(state);
L.CheckType(1, LuaType.Table);
L.PushString("_handle");
L.GetTable(1);
var h = L.ToObject<WriteHandle>(-1, true);
var h = GetHandle(L, true);
if (h is null || h.IsClosed)
return 0;

View file

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

View file

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

View file

@ -1,5 +1,4 @@
using Capy64.API;
using Capy64.Core;
using KeraLua;
using Microsoft.Xna.Framework;
using System;

View file

@ -5,7 +5,6 @@ using KeraLua;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;

View file

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

View file

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

View file

@ -173,7 +173,7 @@ internal class Term : IPlugin
if (!save)
return;
CharGrid[(int)pos.X + (int)pos.Y * Width] = new Char
CharGrid[(int)pos.X + ((int)pos.Y * Width)] = new Char
{
Character = ch,
Foreground = ForegroundColor,
@ -186,7 +186,7 @@ internal class Term : IPlugin
if (pos.X < 0 || pos.Y < 0 || pos.X >= Width || pos.Y >= Height)
return;
var ch = CharGrid[(int)pos.X + (int)pos.Y * Width] ??
var ch = CharGrid[(int)pos.X + ((int)pos.Y * Width)] ??
new Char
{
Character = ' ',
@ -244,7 +244,7 @@ internal class Term : IPlugin
if (_cursorPosition.X < 0 || _cursorPosition.Y < 0 || _cursorPosition.X >= Width || _cursorPosition.Y >= Height)
return;
var ch = CharGrid[(int)_cursorPosition.X + (int)_cursorPosition.Y * Width] ??
var ch = CharGrid[(int)_cursorPosition.X + ((int)_cursorPosition.Y * Width)] ??
new Char
{
Character = ' ',
@ -453,8 +453,8 @@ internal class Term : IPlugin
var x = (int)L.CheckNumber(1);
var y = (int)L.CheckNumber(2);
L.PushInteger(x * CharWidth - CharWidth + 1);
L.PushInteger(y * CharHeight - CharHeight + 1);
L.PushInteger((x * CharWidth) - CharWidth + 1);
L.PushInteger((y * CharHeight) - CharHeight + 1);
return 2;
}
@ -466,8 +466,8 @@ internal class Term : IPlugin
var x = (int)L.CheckNumber(1) - 1;
var y = (int)L.CheckNumber(2) - 1;
L.PushInteger(x / CharWidth + 1);
L.PushInteger(y / CharHeight + 1);
L.PushInteger((x / CharWidth) + 1);
L.PushInteger((y / CharHeight) + 1);
return 2;
}

View file

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

View file

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

View file

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

View file

@ -5,9 +5,7 @@ using System;
using System.Collections.Concurrent;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
namespace Capy64.LuaRuntime;
@ -79,7 +77,8 @@ public class Runtime
/// </param>
public void PushEvent(string name, Func<Lua, int> handler)
{
eventQueue.Enqueue(new LuaDelegateEvent {
eventQueue.Enqueue(new LuaDelegateEvent
{
Name = name,
Handler = handler
});

View file

@ -2,13 +2,9 @@
using Capy64.LuaRuntime.Libraries;
using KeraLua;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.LuaRuntime;

View file

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

View file

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

View file

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Capy64;
namespace Capy64;
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)
{
b = (byte)(packed & 0xff);
g = (byte)(packed >> 8 & 0xff);
r = (byte)(packed >> 16 & 0xff);
g = (byte)((packed >> 8) & 0xff);
r = (byte)((packed >> 16) & 0xff);
}
}

View file

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