Optimize ByteEncoder.cs

This commit is contained in:
Aaro Varis
2026-02-17 14:37:29 +02:00
parent 671d8e4ad6
commit 6d7f9aa8c0

View File

@@ -47,55 +47,79 @@ namespace BoneSync.Data
} }
internal class ByteEncoder internal class ByteEncoder
{ {
//public BinaryWriter writer; private MemoryStream stream;
//public BinaryReader reader; private BinaryWriter writer;
private BinaryReader reader;
private bool isWriting;
public List<byte> Data;
public ByteEncoder() public ByteEncoder()
{ {
Data = new List<byte>(); stream = new MemoryStream();
writer = new BinaryWriter(stream);
isWriting = true;
} }
public ByteEncoder(byte[] data) public ByteEncoder(byte[] data)
{ {
Data = data.ToList(); stream = new MemoryStream(data);
reader = new BinaryReader(stream);
isWriting = false;
} }
public byte[] ToArray() public byte[] ToArray()
{ {
return Data.ToArray(); byte[] buf = stream.GetBuffer();
int start, len;
if (isWriting)
{
start = 0;
len = (int)stream.Length;
}
else
{
start = (int)stream.Position;
len = (int)stream.Length - start;
}
byte[] res = new byte[len];
Array.Copy(buf, start, res, 0, len);
return res;
} }
public void SetBytes(byte[] data) public void SetBytes(byte[] data)
{ {
Data = data.ToList(); if (stream != null)
{
stream.Dispose();
}
stream = new MemoryStream(data);
reader = new BinaryReader(stream);
writer = null;
isWriting = false;
} }
public void WriteByte(byte value) public void WriteByte(byte value)
{ {
Data.Add(value); writer.Write(value);
} }
public void WriteBytes(byte[] value) public void WriteBytes(byte[] value)
{ {
Data.AddRange(value); writer.Write(value);
} }
public byte[] ReadBytes(int count) public byte[] ReadBytes(int count)
{ {
if (Data.Count < count) byte[] value = reader.ReadBytes(count);
if (value.Length != count)
{ {
throw new Exception("Not enough data to read, expected " + count + " but only have " + Data.Count); throw new Exception("Not enough data to read, expected " + count + " but only have " + value.Length);
} }
byte[] value = Data.GetRange(0, count).ToArray();
Data.RemoveRange(0, count);
return value; return value;
} }
public byte ReadByte() public byte ReadByte()
{ {
byte value = Data[0]; return reader.ReadByte();
Data.RemoveAt(0);
return value;
} }
public void WriteBool(bool value) public void WriteBool(bool value)
@@ -110,50 +134,52 @@ namespace BoneSync.Data
public void WriteInt(int value) public void WriteInt(int value)
{ {
WriteBytes(BitConverter.GetBytes(value)); writer.Write(value);
} }
public int ReadInt() public int ReadInt()
{ {
return BitConverter.ToInt32(ReadBytes(sizeof(int)), 0); return reader.ReadInt32();
} }
public void WriteLong(long value) public void WriteLong(long value)
{ {
WriteBytes(BitConverter.GetBytes(value)); writer.Write(value);
} }
public long ReadLong() public long ReadLong()
{ {
return BitConverter.ToInt64(ReadBytes(sizeof(long)), 0); return reader.ReadInt64();
} }
public void WriteFloat(float value) public void WriteFloat(float value)
{ {
WriteBytes(BitConverter.GetBytes(value)); writer.Write(value);
} }
public float ReadFloat() public float ReadFloat()
{ {
return BitConverter.ToSingle(ReadBytes(sizeof(float)), 0); return reader.ReadSingle();
} }
public void WriteUShort(ushort value) public void WriteUShort(ushort value)
{ {
WriteBytes(BitConverter.GetBytes(value)); writer.Write(value);
} }
public ushort ReadUShort() {
return BitConverter.ToUInt16(ReadBytes(sizeof(ushort)), 0); public ushort ReadUShort()
{
return reader.ReadUInt16();
} }
public void WriteDouble(double value) public void WriteDouble(double value)
{ {
WriteBytes(BitConverter.GetBytes(value)); writer.Write(value);
} }
public double ReadDouble() public double ReadDouble()
{ {
return BitConverter.ToDouble(ReadBytes(sizeof(double)), 0); return reader.ReadDouble();
} }
public void WriteString(string value) public void WriteString(string value)
@@ -171,12 +197,12 @@ namespace BoneSync.Data
public void WriteULong(ulong value) public void WriteULong(ulong value)
{ {
WriteBytes(BitConverter.GetBytes(value)); writer.Write(value);
} }
public ulong ReadULong() public ulong ReadULong()
{ {
return BitConverter.ToUInt64(ReadBytes(sizeof(ulong)), 0); return reader.ReadUInt64();
} }
public void WriteVector3(Vector3 value) public void WriteVector3(Vector3 value)
@@ -261,7 +287,7 @@ namespace BoneSync.Data
{ {
byte length = ReadByte(); byte length = ReadByte();
byte[] bytes = ReadBytes(length); byte[] bytes = ReadBytes(length);
return BitPacking.UnpackBits(bytes, length); return BitPacking.UnpackBits(bytes, length * 8);
} }
public void WriteAmmoVariables(AmmoVariables ammoVariables) public void WriteAmmoVariables(AmmoVariables ammoVariables)
@@ -323,7 +349,6 @@ namespace BoneSync.Data
public void WriteCompressedFloat(float value) public void WriteCompressedFloat(float value)
{ {
// write a float in the range of 0-1 with 2 decimal places
value = Mathf.Clamp01(value); value = Mathf.Clamp01(value);
int rounded = Mathf.FloorToInt(value * 255f); int rounded = Mathf.FloorToInt(value * 255f);
WriteByte((byte)rounded); WriteByte((byte)rounded);