Added support for fixed size arrays

This commit is contained in:
Albin Corén 2018-04-12 16:12:57 +01:00
parent b124f5ed3e
commit 902fb77ed4
2 changed files with 29 additions and 23 deletions

View File

@ -41,16 +41,16 @@ namespace MLAPI.NetworkingManagerComponents.Binary
public short ReadShort() => (short)ZigZagDecode(ReadUShort(), 2);
public int ReadInt() => (int)ZigZagDecode(ReadUInt(), 4);
public long ReadLong() => ZigZagDecode(ReadULong(), 8);
public float[] ReadFloatArray() => ReadArray(ReadFloat);
public double[] ReadDoubleArray() => ReadArray(ReadDouble);
public byte[] ReadByteArray() => ReadArray(ReadByte);
public ushort[] ReadUShortArray() => ReadArray(ReadUShort);
public uint[] ReadUIntArray() => ReadArray(ReadUInt);
public ulong[] ReadULongArray() => ReadArray(ReadULong);
public sbyte[] ReadSByteArray() => ReadArray(ReadSByte);
public short[] ReadShortArray() => ReadArray(ReadShort);
public int[] ReadIntArray() => ReadArray(ReadInt);
public long[] ReadLongArray() => ReadArray(ReadLong);
public float[] ReadFloatArray(int known = -1) => ReadArray(ReadFloat, known);
public double[] ReadDoubleArray(int known = -1) => ReadArray(ReadDouble, known);
public byte[] ReadByteArray(int known = -1) => ReadArray(ReadByte, known);
public ushort[] ReadUShortArray(int known = -1) => ReadArray(ReadUShort, known);
public uint[] ReadUIntArray(int known = -1) => ReadArray(ReadUInt, known);
public ulong[] ReadULongArray(int known = -1) => ReadArray(ReadULong, known);
public sbyte[] ReadSByteArray(int known = -1) => ReadArray(ReadSByte, known);
public short[] ReadShortArray(int known = -1) => ReadArray(ReadShort, known);
public int[] ReadIntArray(int known = -1) => ReadArray(ReadInt, known);
public long[] ReadLongArray(int known = -1) => ReadArray(ReadLong, known);
public string ReadString() => Encoding.UTF8.GetString(ReadByteArray());
private ulong ReadULong()
@ -79,9 +79,9 @@ namespace MLAPI.NetworkingManagerComponents.Binary
}
return res;
}
private T[] ReadArray<T>(Getter<T> g)
private T[] ReadArray<T>(Getter<T> g, int knownSize = -1)
{
T[] result = new T[ReadUShort()];
T[] result = new T[knownSize > 0 ? (uint)knownSize : ReadUInt()];
for (ushort s = 0; s < result.Length; ++s)
result[s] = g();
return result;

View File

@ -91,17 +91,23 @@ namespace MLAPI.NetworkingManagerComponents.Binary
public void WriteShort(short s) => Push(s);
public void WriteInt(int i) => Push(i);
public void WriteLong(long l) => Push(l);
public void WriteFloatArray(float[] f) => Push(f);
public void WriteDoubleArray(double[] d) => Push(d);
public void WriteByteArray(byte[] b) => Push(b);
public void WriteUShortArray(ushort[] s) => Push(s);
public void WriteUIntArray(uint[] i) => Push(i);
public void WriteULongArray(ulong[] l) => Push(l);
public void WriteSByteArray(sbyte[] b) => Push(b);
public void WriteShortArray(short[] s) => Push(s);
public void WriteIntArray(int[] i) => Push(i);
public void WriteLongArray(long[] l) => Push(l);
public void WriteString(string s) => Push(s);
public void WriteFloatArray(float[] f, bool known = false) => PushArray(f, known);
public void WriteDoubleArray(double[] d, bool known = false) => PushArray(d, known);
public void WriteByteArray(byte[] b, bool known = false) => PushArray(b, known);
public void WriteUShortArray(ushort[] s, bool known = false) => PushArray(s, known);
public void WriteUIntArray(uint[] i, bool known = false) => PushArray(i, known);
public void WriteULongArray(ulong[] l, bool known = false) => PushArray(l, known);
public void WriteSByteArray(sbyte[] b, bool known = false) => PushArray(b, known);
public void WriteShortArray(short[] s, bool known = false) => PushArray(s, known);
public void WriteIntArray(int[] i, bool known = false) => PushArray(i, known);
public void WriteLongArray(long[] l, bool known = false) => PushArray(l, known);
private void PushArray<T>(T[] t, bool knownSize = false)
{
if (!knownSize) Push(t);
else foreach (T t1 in t) Push(t1);
}
public long Finalize(ref byte[] buffer)
{
@ -139,7 +145,7 @@ namespace MLAPI.NetworkingManagerComponents.Binary
if (type.IsArray)
{
var array = t as Array;
Serialize((ushort)array.Length, writeTo, ref bitOffset);
Serialize((uint)array.Length, writeTo, ref bitOffset);
foreach (var element in array)
Serialize(element, writeTo, ref bitOffset);
}