Optimize ByteEncoder.cs
This commit is contained in:
@@ -47,55 +47,79 @@ namespace BoneSync.Data
|
||||
}
|
||||
internal class ByteEncoder
|
||||
{
|
||||
//public BinaryWriter writer;
|
||||
//public BinaryReader reader;
|
||||
private MemoryStream stream;
|
||||
private BinaryWriter writer;
|
||||
private BinaryReader reader;
|
||||
private bool isWriting;
|
||||
|
||||
public List<byte> Data;
|
||||
public ByteEncoder()
|
||||
{
|
||||
Data = new List<byte>();
|
||||
stream = new MemoryStream();
|
||||
writer = new BinaryWriter(stream);
|
||||
isWriting = true;
|
||||
}
|
||||
|
||||
public ByteEncoder(byte[] data)
|
||||
{
|
||||
Data = data.ToList();
|
||||
stream = new MemoryStream(data);
|
||||
reader = new BinaryReader(stream);
|
||||
isWriting = false;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
Data.Add(value);
|
||||
writer.Write(value);
|
||||
}
|
||||
|
||||
public void WriteBytes(byte[] value)
|
||||
{
|
||||
Data.AddRange(value);
|
||||
writer.Write(value);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public byte ReadByte()
|
||||
{
|
||||
byte value = Data[0];
|
||||
Data.RemoveAt(0);
|
||||
return value;
|
||||
return reader.ReadByte();
|
||||
}
|
||||
|
||||
public void WriteBool(bool value)
|
||||
@@ -110,50 +134,52 @@ namespace BoneSync.Data
|
||||
|
||||
public void WriteInt(int value)
|
||||
{
|
||||
WriteBytes(BitConverter.GetBytes(value));
|
||||
writer.Write(value);
|
||||
}
|
||||
|
||||
public int ReadInt()
|
||||
{
|
||||
return BitConverter.ToInt32(ReadBytes(sizeof(int)), 0);
|
||||
return reader.ReadInt32();
|
||||
}
|
||||
|
||||
public void WriteLong(long value)
|
||||
{
|
||||
WriteBytes(BitConverter.GetBytes(value));
|
||||
writer.Write(value);
|
||||
}
|
||||
|
||||
public long ReadLong()
|
||||
{
|
||||
return BitConverter.ToInt64(ReadBytes(sizeof(long)), 0);
|
||||
return reader.ReadInt64();
|
||||
}
|
||||
|
||||
public void WriteFloat(float value)
|
||||
{
|
||||
WriteBytes(BitConverter.GetBytes(value));
|
||||
writer.Write(value);
|
||||
}
|
||||
|
||||
public float ReadFloat()
|
||||
{
|
||||
return BitConverter.ToSingle(ReadBytes(sizeof(float)), 0);
|
||||
return reader.ReadSingle();
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
WriteBytes(BitConverter.GetBytes(value));
|
||||
writer.Write(value);
|
||||
}
|
||||
|
||||
public double ReadDouble()
|
||||
{
|
||||
return BitConverter.ToDouble(ReadBytes(sizeof(double)), 0);
|
||||
return reader.ReadDouble();
|
||||
}
|
||||
|
||||
public void WriteString(string value)
|
||||
@@ -171,12 +197,12 @@ namespace BoneSync.Data
|
||||
|
||||
public void WriteULong(ulong value)
|
||||
{
|
||||
WriteBytes(BitConverter.GetBytes(value));
|
||||
writer.Write(value);
|
||||
}
|
||||
|
||||
public ulong ReadULong()
|
||||
{
|
||||
return BitConverter.ToUInt64(ReadBytes(sizeof(ulong)), 0);
|
||||
return reader.ReadUInt64();
|
||||
}
|
||||
|
||||
public void WriteVector3(Vector3 value)
|
||||
@@ -261,7 +287,7 @@ namespace BoneSync.Data
|
||||
{
|
||||
byte length = ReadByte();
|
||||
byte[] bytes = ReadBytes(length);
|
||||
return BitPacking.UnpackBits(bytes, length);
|
||||
return BitPacking.UnpackBits(bytes, length * 8);
|
||||
}
|
||||
|
||||
public void WriteAmmoVariables(AmmoVariables ammoVariables)
|
||||
@@ -323,7 +349,6 @@ namespace BoneSync.Data
|
||||
|
||||
public void WriteCompressedFloat(float value)
|
||||
{
|
||||
// write a float in the range of 0-1 with 2 decimal places
|
||||
value = Mathf.Clamp01(value);
|
||||
int rounded = Mathf.FloorToInt(value * 255f);
|
||||
WriteByte((byte)rounded);
|
||||
@@ -356,4 +381,4 @@ namespace BoneSync.Data
|
||||
return fingerCurl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user