Add file:seek to BinaryReadHandle and BinaryWriteHandle

This commit is contained in:
Alessandro Proto 2023-01-28 10:32:56 +01:00
parent 162ebc3dc2
commit 8c592a960f
2 changed files with 128 additions and 29 deletions

View file

@ -13,6 +13,7 @@ public class BinaryReadHandle
private static readonly Dictionary<string, LuaFunction> functions = new() private static readonly Dictionary<string, LuaFunction> functions = new()
{ {
["readAll"] = L_ReadAll, ["readAll"] = L_ReadAll,
["seek"] = L_Seek,
["nextByte"] = L_NextByte, ["nextByte"] = L_NextByte,
["nextShort"] = L_NextShort, ["nextShort"] = L_NextShort,
["nextInt"] = L_NextInt, ["nextInt"] = L_NextInt,
@ -66,8 +67,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position >= stream.BaseStream.Length)
{
L.PushNil();
return 0; return 0;
}
var chars = stream.ReadChars((int)stream.BaseStream.Length); var chars = stream.ReadChars((int)stream.BaseStream.Length);
var buffer = Encoding.ASCII.GetBytes(chars); var buffer = Encoding.ASCII.GetBytes(chars);
@ -76,6 +80,32 @@ public class BinaryReadHandle
return 1; return 1;
} }
private static int L_Seek(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var stream = L.CheckObject<BinaryReader>(1, ObjectType, false);
if (stream is null)
L.Error("handle is closed");
var whence = L.CheckOption(2, "cur", new[]
{
"set", // begin 0
"cur", // current 1
"end", // end 2
null,
});
var offset = L.OptInteger(3, 0);
var newPosition = stream.BaseStream.Seek(offset, (SeekOrigin)whence);
L.PushInteger(newPosition);
return 1;
}
private static int L_NextByte(IntPtr state) private static int L_NextByte(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
@ -88,8 +118,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position >= stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
if (count == 1) if (count == 1)
{ {
@ -118,8 +151,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position >= stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadInt16(); var b = stream.ReadInt16();
L.PushInteger(b); L.PushInteger(b);
@ -135,8 +171,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadInt32(); var b = stream.ReadInt32();
L.PushInteger(b); L.PushInteger(b);
@ -152,8 +191,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadInt64(); var b = stream.ReadInt64();
L.PushInteger(b); L.PushInteger(b);
@ -169,8 +211,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadSByte(); var b = stream.ReadSByte();
L.PushInteger(b); L.PushInteger(b);
@ -186,8 +231,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadUInt16(); var b = stream.ReadUInt16();
L.PushInteger(b); L.PushInteger(b);
@ -203,8 +251,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadUInt32(); var b = stream.ReadUInt32();
L.PushInteger(b); L.PushInteger(b);
@ -220,8 +271,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadUInt64(); var b = stream.ReadUInt64();
L.PushInteger((long)b); L.PushInteger((long)b);
@ -237,8 +291,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadHalf(); var b = stream.ReadHalf();
L.PushNumber((double)b); L.PushNumber((double)b);
@ -254,8 +311,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadSingle(); var b = stream.ReadSingle();
L.PushNumber((double)b); L.PushNumber((double)b);
@ -271,8 +331,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadDouble(); var b = stream.ReadDouble();
L.PushNumber((double)b); L.PushNumber((double)b);
@ -291,8 +354,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position >= stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
if (count == 1) if (count == 1)
{ {
@ -324,8 +390,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position >= stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
if (count == 1) if (count == 1)
{ {
@ -351,8 +420,11 @@ public class BinaryReadHandle
if (stream is null) if (stream is null)
L.Error("handle is closed"); L.Error("handle is closed");
if (stream.BaseStream.Position == stream.BaseStream.Length) if (stream.BaseStream.Position > stream.BaseStream.Length)
return 0; {
L.PushNil();
return 1;
}
var b = stream.ReadBoolean(); var b = stream.ReadBoolean();
L.PushBoolean(b); L.PushBoolean(b);

View file

@ -12,6 +12,7 @@ public class BinaryWriteHandle
private static readonly Dictionary<string, LuaFunction> functions = new() private static readonly Dictionary<string, LuaFunction> functions = new()
{ {
["seek"] = L_Seek,
["writeByte"] = L_WriteByte, ["writeByte"] = L_WriteByte,
["writeShort"] = L_WriteShort, ["writeShort"] = L_WriteShort,
["writeInt"] = L_WriteInt, ["writeInt"] = L_WriteInt,
@ -57,6 +58,32 @@ public class BinaryWriteHandle
L.SetMetaTable(-2); L.SetMetaTable(-2);
} }
private static int L_Seek(IntPtr state)
{
var L = Lua.FromIntPtr(state);
var stream = L.CheckObject<BinaryWriter>(1, ObjectType, false);
if (stream is null)
L.Error("handle is closed");
var whence = L.CheckOption(2, "cur", new[]
{
"set", // begin 0
"cur", // current 1
"end", // end 2
null,
});
var offset = L.OptInteger(3, 0);
var newPosition = stream.BaseStream.Seek(offset, (SeekOrigin)whence);
L.PushInteger(newPosition);
return 1;
}
private static int L_WriteByte(IntPtr state) private static int L_WriteByte(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);