Skip to content

Commit b797201

Browse files
committed
Merge branch 'master' of https://github.com/TwoTenPvP/MLAPI
2 parents c8b6c52 + 87be243 commit b797201

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,25 @@ public byte ReadByte()
7272
public int ReadInt() => (int)ZigZagDecode(ReadUInt(), 4);
7373
public long ReadLong() => ZigZagDecode(ReadULong(), 8);
7474
public float[] ReadFloatArray(int known = -1) => ReadArray(ReadFloat, known);
75+
public uint ReadFloatArray(float[] buffer, int known = -1) => ReadArray(ReadFloat, buffer, known);
7576
public double[] ReadDoubleArray(int known = -1) => ReadArray(ReadDouble, known);
77+
public uint ReadDoubleArray(double[] buffer, int known = -1) => ReadArray(ReadDouble, buffer, known);
7678
public byte[] ReadByteArray(int known = -1) => ReadArray(ReadByte, known);
79+
public uint ReadByteArray(byte[] buffer, int known = -1) => ReadArray(ReadByte, buffer, known);
7780
public ushort[] ReadUShortArray(int known = -1) => ReadArray(ReadUShort, known);
81+
public uint ReadUShortArray(ushort[] buffer, int known = -1) => ReadArray(ReadUShort, buffer, known);
7882
public uint[] ReadUIntArray(int known = -1) => ReadArray(ReadUInt, known);
83+
public uint ReadUIntArray(uint[] buffer, int known = -1) => ReadArray(ReadUInt, buffer, known);
7984
public ulong[] ReadULongArray(int known = -1) => ReadArray(ReadULong, known);
85+
public uint ReadULongArray(ulong[] buffer, int known = -1) => ReadArray(ReadULong, buffer, known);
8086
public sbyte[] ReadSByteArray(int known = -1) => ReadArray(ReadSByte, known);
87+
public uint ReadSByteArray(sbyte[] buffer, int known = -1) => ReadArray(ReadSByte, buffer, known);
8188
public short[] ReadShortArray(int known = -1) => ReadArray(ReadShort, known);
89+
public uint ReadShortArray(short[] buffer, int known = -1) => ReadArray(ReadShort, buffer, known);
8290
public int[] ReadIntArray(int known = -1) => ReadArray(ReadInt, known);
91+
public uint ReadIntArray(int[] buffer, int known = -1) => ReadArray(ReadInt, buffer, known);
8392
public long[] ReadLongArray(int known = -1) => ReadArray(ReadLong, known);
93+
public uint ReadLongArray(long[] buffer, int known = -1) => ReadArray(ReadLong, buffer, known);
8494
public string ReadString() => Encoding.UTF8.GetString(ReadByteArray());
8595
public byte ReadBits(int bits)
8696
{
@@ -116,6 +126,7 @@ public ulong ReadULong()
116126
}
117127
return res;
118128
}
129+
119130
private T[] ReadArray<T>(Getter<T> g, int knownSize = -1)
120131
{
121132
T[] result = new T[knownSize > 0 ? (uint)knownSize : ReadUInt()];
@@ -124,6 +135,22 @@ private T[] ReadArray<T>(Getter<T> g, int knownSize = -1)
124135
return result;
125136
}
126137

138+
private uint ReadArray<T>(Getter<T> g, T[] buffer, int knownSize = -1)
139+
{
140+
uint size = knownSize > 0 ? (uint)knownSize : ReadUInt();
141+
/*
142+
if (buffer.Length < size)
143+
throw new ArgumentException("Buffer size is too small");
144+
*/
145+
for (ushort s = 0; s < size; ++s)
146+
{
147+
if (s > buffer.Length)
148+
break; //The buffer is too small. We still give the correct size so it can be re-read
149+
buffer[s] = g();
150+
}
151+
return size;
152+
}
153+
127154
private T ReadFloating<T>()
128155
{
129156
int size = Marshal.SizeOf(typeof(T));
@@ -144,6 +171,7 @@ private T ReadFloating<T>()
144171

145172
public void Dispose()
146173
{
174+
readFrom = null; //Give to GC
147175
bitCount = 0;
148176
readerPool.Enqueue(this);
149177
}

MLAPI/NetworkingManagerComponents/Binary/BitWriter.cs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,26 @@ private void Push<T>(T b)
117117
public void WriteLong(long l) => Push(ZigZagEncode(l, 8));
118118
public void WriteString(string s) => Push(s);
119119
public void WriteAlignBits() => Push<object>(null);
120-
public void WriteFloatArray(float[] f, bool known = false) => PushArray(f, known);
121-
public void WriteDoubleArray(double[] d, bool known = false) => PushArray(d, known);
122-
public void WriteByteArray(byte[] b, bool known = false) => PushArray(b, known);
123-
public void WriteUShortArray(ushort[] s, bool known = false) => PushArray(s, known);
124-
public void WriteUIntArray(uint[] i, bool known = false) => PushArray(i, known);
125-
public void WriteULongArray(ulong[] l, bool known = false) => PushArray(l, known);
126-
public void WriteSByteArray(sbyte[] b, bool known = false) => PushArray(b, known);
127-
public void WriteShortArray(short[] s, bool known = false) => PushArray(s, known);
128-
public void WriteIntArray(int[] i, bool known = false) => PushArray(i, known);
129-
public void WriteLongArray(long[] l, bool known = false) => PushArray(l, known);
120+
public void WriteFloatArray(float[] f, bool known = false) => PushArray(f, known);
121+
public void WriteFloatArray(float[] f, int startIndex, int length, bool known = false) => PushArray(f, startIndex, length, known);
122+
public void WriteDoubleArray(double[] d, bool known = false) => PushArray(d, known);
123+
public void WriteDoubleArray(double[] d, int startIndex, int length, bool known = false) => PushArray(d, startIndex, length, known);
124+
public void WriteByteArray(byte[] b, bool known = false) => PushArray(b, known);
125+
public void WriteByteArray(byte[] b, int startIndex, int length, bool known = false) => PushArray(b, startIndex, length, known);
126+
public void WriteUShortArray(ushort[] s, bool known = false) => PushArray(s, known);
127+
public void WriteUShortArray(ushort[] s, int startIndex, int length, bool known = false) => PushArray(s, startIndex, length, known);
128+
public void WriteUIntArray(uint[] i, bool known = false) => PushArray(i, known);
129+
public void WriteUIntArray(uint[] i, int startIndex, int length, bool known = false) => PushArray(i, startIndex, length, known);
130+
public void WriteULongArray(ulong[] l, bool known = false) => PushArray(l, known);
131+
public void WriteULongArray(ulong[] l, int startIndex, int length, bool known = false) => PushArray(l, startIndex, length, known);
132+
public void WriteSByteArray(sbyte[] b, bool known = false) => PushArray(b, known);
133+
public void WriteSByteArray(sbyte[] b, int startIndex, int length, bool known = false) => PushArray(b, startIndex, length, known);
134+
public void WriteShortArray(short[] s, bool known = false) => PushArray(s, known);
135+
public void WriteShortArray(short[] s, int startIndex, int length, bool known = false) => PushArray(s, startIndex, length, known);
136+
public void WriteIntArray(int[] i, bool known = false) => PushArray(i, known);
137+
public void WriteIntArray(int[] i, int startIndex, int length, bool known = false) => PushArray(i, startIndex, length, known);
138+
public void WriteLongArray(long[] l, bool known = false) => PushArray(l, known);
139+
public void WriteLongArray(long[] l, int startIndex, int length, bool known = false) => PushArray(l, startIndex, length, known);
130140
public void WriteBits(byte value, int bits) => Push(new Partial(ReadNBits(value, 0, bits % 8), (byte)(bits%8))); // Suggestion: store (bits % 8) result?
131141
public void WriteWriter(BitWriter writer)
132142
{
@@ -144,6 +154,14 @@ private void PushArray<T>(T[] t, bool knownSize = false)
144154
foreach (T t1 in t) Push(signed ? (object)ZigZagEncode(t1 as long? ?? t1 as int? ?? t1 as short? ?? t1 as sbyte? ?? 0, size) : (object)t1);
145155
}
146156

157+
private void PushArray<T>(T[] t, int startIndex, int length, bool knownSize = false)
158+
{
159+
if (!knownSize) Push((uint)t.Length);
160+
bool signed = IsSigned(t.GetType().GetElementType());
161+
int size = Marshal.SizeOf(t.GetType().GetElementType());
162+
for (int i = startIndex; i < length; i++) Push(signed ? (object)ZigZagEncode(t[i] as long? ?? t[i] as int? ?? t[i] as short? ?? t[i] as sbyte? ?? 0, size) : (object)t[i]);
163+
}
164+
147165
/// <summary>
148166
/// Serializes data, allocates an array and returns it
149167
/// </summary>

0 commit comments

Comments
 (0)