Skip to content

Commit 468c4f3

Browse files
committed
Merged master
2 parents 8a28e84 + 67eb0ef commit 468c4f3

File tree

9 files changed

+3016
-2838
lines changed

9 files changed

+3016
-2838
lines changed

MLAPI.Tests/NetworkingManagerComponents/Binary/BitStreamTest.cs

Lines changed: 146 additions & 100 deletions
Large diffs are not rendered by default.

MLAPI/MLAPI.csproj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,15 @@
116116
<Compile Include="MonoBehaviours\Prototyping\NetworkedAnimator.cs" />
117117
<Compile Include="MonoBehaviours\Prototyping\NetworkedNavMeshAgent.cs" />
118118
<Compile Include="NetworkingManagerComponents\Binary\Arithmetic.cs" />
119-
<Compile Include="NetworkingManagerComponents\Binary\BitStream.cs" />
120-
<Compile Include="NetworkingManagerComponents\Binary\BitWriter.cs" />
121119
<Compile Include="NetworkingManagerComponents\Binary\BitReader.cs" />
120+
<Compile Include="NetworkingManagerComponents\Binary\BitWriter.cs" />
121+
<Compile Include="NetworkingManagerComponents\Binary\BitStream.cs" />
122+
<Compile Include="NetworkingManagerComponents\Binary\BitWriterDeprecated.cs" />
123+
<Compile Include="NetworkingManagerComponents\Binary\BitReaderDeprecated.cs" />
122124
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
123125
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
126+
<Compile Include="NetworkingManagerComponents\Binary\ByteBool.cs" />
127+
<Compile Include="NetworkingManagerComponents\Binary\UIntFloat.cs" />
124128
<Compile Include="NetworkingManagerComponents\Core\LogHelper.cs" />
125129
<Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" />
126130
<Compile Include="NetworkingManagerComponents\Cryptography\DiffieHellman.cs" />

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 1085 additions & 159 deletions
Large diffs are not rendered by default.
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
2+
using MLAPI.NetworkingManagerComponents.Core;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
8+
namespace MLAPI.NetworkingManagerComponents.Binary
9+
{
10+
[Obsolete]
11+
public class BitReaderDeprecated : IDisposable
12+
{
13+
private bool disposed;
14+
private delegate T Getter<T>();
15+
private static readonly float[] holder_f = new float[1];
16+
private static readonly double[] holder_d = new double[1];
17+
private static readonly ulong[] holder_u = new ulong[1];
18+
private static readonly uint[] holder_i = new uint[1];
19+
20+
private byte[] readFrom;
21+
private long bitCount = 0;
22+
23+
private static int pools = 0;
24+
private static readonly Queue<BitReaderDeprecated> readerPool = new Queue<BitReaderDeprecated>();
25+
26+
public ulong Remaining
27+
{
28+
get
29+
{
30+
return BitLength - (ulong)bitCount;
31+
}
32+
}
33+
34+
public ulong BitLength
35+
{
36+
get
37+
{
38+
return (ulong)readFrom.Length * 8UL;
39+
}
40+
}
41+
42+
private BitReaderDeprecated(byte[] readFrom)
43+
{
44+
this.readFrom = readFrom;
45+
disposed = false;
46+
}
47+
48+
public static BitReaderDeprecated Get(byte[] readFrom)
49+
{
50+
if (readerPool.Count == 0)
51+
{
52+
if (pools > 10)
53+
if (LogHelper.CurrentLogLevel <= LogLevel.Normal) LogHelper.LogWarning("There are more than 10 BitReaders. Have you forgotten do dispose? (More readers hurt performance)");
54+
BitReaderDeprecated reader = new BitReaderDeprecated(readFrom);
55+
reader.disposed = false;
56+
pools++;
57+
return reader;
58+
}
59+
else
60+
{
61+
BitReaderDeprecated reader = readerPool.Dequeue();
62+
reader.disposed = false;
63+
reader.readFrom = readFrom;
64+
return reader;
65+
}
66+
}
67+
68+
public ValueType ReadValueType<T>()
69+
{
70+
if (typeof(T) == typeof(float))
71+
return ReadFloat();
72+
else if (typeof(T) == typeof(double))
73+
return ReadDouble();
74+
else if (typeof(T) == typeof(byte))
75+
return ReadByte();
76+
else if (typeof(T) == typeof(sbyte))
77+
return ReadSByte();
78+
else if (typeof(T) == typeof(short))
79+
return ReadShort();
80+
else if (typeof(T) == typeof(ushort))
81+
return ReadUShort();
82+
else if (typeof(T) == typeof(int))
83+
return ReadInt();
84+
else if (typeof(T) == typeof(uint))
85+
return ReadUInt();
86+
else if (typeof(T) == typeof(long))
87+
return ReadLong();
88+
else if (typeof(T) == typeof(ulong))
89+
return ReadULong();
90+
91+
return default(ValueType);
92+
}
93+
94+
public T ReadValueTypeOrString<T>()
95+
{
96+
if (typeof(T) == typeof(string))
97+
{
98+
return (T)(object)ReadString(); //BOX
99+
}
100+
else if (typeof(T).IsValueType)
101+
{
102+
ValueType type = ReadValueType<T>();
103+
return (T)(object)type; //BOX
104+
}
105+
else
106+
{
107+
return default(T);
108+
}
109+
}
110+
111+
public bool ReadBool()
112+
{
113+
bool result = (readFrom[bitCount / 8] & (byte)(1 << (int)(bitCount % 8))) != 0;
114+
++bitCount;
115+
return result;
116+
}
117+
118+
public float ReadFloat() => ReadFloating<float>();
119+
public double ReadDouble() => ReadFloating<double>();
120+
public byte ReadByte()
121+
{
122+
int shift = (int)(bitCount % 8);
123+
int index = (int)(bitCount / 8);
124+
byte lower_mask = (byte)(0xFF << shift);
125+
byte upper_mask = (byte)~lower_mask;
126+
byte result = (byte)(((readFrom[index] & lower_mask) >> shift) | (shift == 0 ? 0 : (readFrom[index + 1] & upper_mask) << (8 - shift)));
127+
bitCount += 8;
128+
return result;
129+
}
130+
public void SkipPadded() => bitCount += (8 - (bitCount % 8)) % 8;
131+
public ushort ReadUShort() => (ushort)ReadULong();
132+
public uint ReadUInt() => (uint)ReadULong();
133+
public sbyte ReadSByte() => (sbyte)ZigZagDecode(ReadULong());
134+
public short ReadShort() => (short)ZigZagDecode(ReadULong());
135+
public int ReadInt() => (int)ZigZagDecode(ReadULong());
136+
public long ReadLong() => ZigZagDecode(ReadULong());
137+
138+
public float[] ReadFloatArray(int known = -1) => ReadArray(ReadFloat, known);
139+
public uint ReadFloatArray(float[] buffer, int known = -1) => ReadArray(ReadFloat, buffer, known);
140+
public double[] ReadDoubleArray(int known = -1) => ReadArray(ReadDouble, known);
141+
public uint ReadDoubleArray(double[] buffer, int known = -1) => ReadArray(ReadDouble, buffer, known);
142+
public byte[] ReadByteArray(int known = -1) => ReadArray(ReadByte, known);
143+
public uint ReadByteArray(byte[] buffer, int known = -1) => ReadArray(ReadByte, buffer, known);
144+
public ushort[] ReadUShortArray(int known = -1) => ReadArray(ReadUShort, known);
145+
public uint ReadUShortArray(ushort[] buffer, int known = -1) => ReadArray(ReadUShort, buffer, known);
146+
public uint[] ReadUIntArray(int known = -1) => ReadArray(ReadUInt, known);
147+
public uint ReadUIntArray(uint[] buffer, int known = -1) => ReadArray(ReadUInt, buffer, known);
148+
public ulong[] ReadULongArray(int known = -1) => ReadArray(ReadULong, known);
149+
public uint ReadULongArray(ulong[] buffer, int known = -1) => ReadArray(ReadULong, buffer, known);
150+
public sbyte[] ReadSByteArray(int known = -1) => ReadArray(ReadSByte, known);
151+
public uint ReadSByteArray(sbyte[] buffer, int known = -1) => ReadArray(ReadSByte, buffer, known);
152+
public short[] ReadShortArray(int known = -1) => ReadArray(ReadShort, known);
153+
public uint ReadShortArray(short[] buffer, int known = -1) => ReadArray(ReadShort, buffer, known);
154+
public int[] ReadIntArray(int known = -1) => ReadArray(ReadInt, known);
155+
public uint ReadIntArray(int[] buffer, int known = -1) => ReadArray(ReadInt, buffer, known);
156+
public long[] ReadLongArray(int known = -1) => ReadArray(ReadLong, known);
157+
public uint ReadLongArray(long[] buffer, int known = -1) => ReadArray(ReadLong, buffer, known);
158+
public string ReadString() => Encoding.UTF8.GetString(ReadByteArray());
159+
public byte ReadBits(int bits)
160+
{
161+
byte b = 0;
162+
for (int i = 0; --bits >= 0; ++i)
163+
b |= (byte)((ReadBool() ? 1 : 0) << i);
164+
return b;
165+
}
166+
167+
public ulong ReadULong()
168+
{
169+
ulong header = ReadByte();
170+
if (header <= 240) return header;
171+
if (header <= 248) return 240 + 256 * (header - 241) + ReadByte();
172+
if (header == 249) return 2288 + 256UL * ReadByte() + ReadByte();
173+
ulong res = ReadByte() | ((ulong)ReadByte() << 8) | ((ulong)ReadByte() << 16);
174+
if(header > 250)
175+
{
176+
res |= (ulong) ReadByte() << 24;
177+
if(header > 251)
178+
{
179+
res |= (ulong)ReadByte() << 32;
180+
if(header > 252)
181+
{
182+
res |= (ulong)ReadByte() << 40;
183+
if (header > 253)
184+
{
185+
res |= (ulong)ReadByte() << 48;
186+
if (header > 254) res |= (ulong)ReadByte() << 56;
187+
}
188+
}
189+
}
190+
}
191+
return res;
192+
}
193+
194+
private T[] ReadArray<T>(Getter<T> g, int knownSize = -1)
195+
{
196+
T[] result = new T[knownSize > 0 ? (uint)knownSize : ReadUInt()];
197+
for (ushort s = 0; s < result.Length; ++s)
198+
result[s] = g();
199+
return result;
200+
}
201+
202+
private uint ReadArray<T>(Getter<T> g, T[] buffer, int knownSize = -1)
203+
{
204+
uint size = knownSize > 0 ? (uint)knownSize : ReadUInt();
205+
/*
206+
if (buffer.Length < size)
207+
throw new ArgumentException("Buffer size is too small");
208+
*/
209+
for (ushort s = 0; s < size; ++s)
210+
{
211+
if (s > buffer.Length)
212+
break; //The buffer is too small. We still give the correct size so it can be re-read
213+
buffer[s] = g();
214+
}
215+
return size;
216+
}
217+
218+
private T ReadFloating<T>()
219+
{
220+
int size = Marshal.SizeOf(typeof(T));
221+
Array type_holder = size == 4 ? holder_f as Array : holder_d as Array;
222+
Array result_holder = size == 4 ? holder_i as Array : holder_u as Array;
223+
T result;
224+
lock(result_holder)
225+
lock (type_holder)
226+
{
227+
if (size == 4) result_holder.SetValue(BinaryHelpers.SwapEndian(ReadUInt()), 0);
228+
else result_holder.SetValue(BinaryHelpers.SwapEndian(ReadULong()), 0);
229+
Buffer.BlockCopy(result_holder, 0, type_holder, 0, size);
230+
result = (T)type_holder.GetValue(0);
231+
}
232+
return result;
233+
}
234+
private static long ZigZagDecode(ulong d) => (long)(((d & 1) << 63) | (d >> 1));
235+
236+
public void Dispose()
237+
{
238+
readFrom = null; //Give to GC
239+
bitCount = 0;
240+
if (disposed) return; //this is already in the pool
241+
disposed = true;
242+
readerPool.Enqueue(this);
243+
}
244+
}
245+
}
246+
#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member

0 commit comments

Comments
 (0)