Skip to content

Commit 06fdaa9

Browse files
Completely removed dynamic
Made specific setters and getters for serialization and deserialization respectively
1 parent 13470ef commit 06fdaa9

File tree

2 files changed

+68
-34
lines changed

2 files changed

+68
-34
lines changed

MLAPI/NetworkingManagerComponents/Binary/BinaryCollector.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,38 @@ public BinaryCollector(int bufferSize)
7474
collect = new object[bufferSize];
7575
}
7676

77-
public void Push<T>(T b)
77+
private void Push<T>(T b)
7878
{
7979
if (b is string || b.GetType().IsArray || IsSupportedType(b.GetType()))
8080
collect[collectCount++] = b is string ? Encoding.UTF8.GetBytes(b as string) : b as object;
8181
//else
8282
// Debug.LogWarning("MLAPI: The type \"" + b.GetType() + "\" is not supported by the Binary Serializer. It will be ignored");
8383
}
8484

85+
86+
public void WriteBool(bool b) => Push(b);
87+
public void WriteFloat(float f) => Push(f);
88+
public void WriteDouble(double d) => Push(d);
89+
public void WriteByte(byte b) => Push(b);
90+
public void WriteUShort(ushort s) => Push(s);
91+
public void WriteUInt(uint i) => Push(i);
92+
public void WriteULong(ulong l) => Push(l);
93+
public void WriteSByte(sbyte b) => Push(b);
94+
public void WriteShort(short s) => Push(s);
95+
public void WriteInt(int i) => Push(i);
96+
public void WriteLong(long l) => Push(l);
97+
public void WriteFloatArray(float[] f) => Push(f);
98+
public void WriteDoubleArray(double[] d) => Push(d);
99+
public void WriteByteArray(byte[] b) => Push(b);
100+
public void WriteUShortArray(ushort[] s) => Push(s);
101+
public void WriteUIntArray(uint[] i) => Push(i);
102+
public void WriteULongArray(ulong[] l) => Push(l);
103+
public void WriteSByteArray(sbyte[] b) => Push(b);
104+
public void WriteShortArray(short[] s) => Push(s);
105+
public void WriteIntArray(int[] i) => Push(i);
106+
public void WriteLongArray(long[] l) => Push(l);
107+
public void WriteString(string s) => Push(s);
108+
85109
public byte[] ToArray()
86110
{
87111
long bitCount = 0;
@@ -135,18 +159,18 @@ private static void Serialize<T>(T t, byte[] writeTo, ref long bitOffset)
135159
else result_holder.SetValue(0UL, 0);
136160
type_holder.SetValue(t, 0); // Insert the value to convert into the preallocated holder array
137161
Buffer.BlockCopy(type_holder, 0, result_holder, 0, bytes); // Perform an internal copy to the byte-based holder
138-
dynamic d = result_holder.GetValue(0);
139162

140163
// Since floating point flag bits are seemingly the highest bytes of the floating point values
141164
// and even very small values have them, we swap the endianness in the hopes of reducing the size
142-
Serialize(BinaryHelpers.SwapEndian(d), writeTo, ref bitOffset);
165+
if(size) Serialize(BinaryHelpers.SwapEndian((uint)result_holder.GetValue(0)), writeTo, ref bitOffset);
166+
else Serialize(BinaryHelpers.SwapEndian((ulong)result_holder.GetValue(0)), writeTo, ref bitOffset);
143167
}
144168
//bitOffset += offset;
145169
}
146170
else
147171
{
148172
bool signed = IsSigned(t.GetType());
149-
dynamic value;
173+
ulong value;
150174
if (signed)
151175
{
152176
Type t1 = t.GetType();
@@ -155,9 +179,12 @@ private static void Serialize<T>(T t, byte[] writeTo, ref long bitOffset)
155179
else if (t1 == typeof(int)) value = (uint)ZigZagEncode(t as int? ?? 0, 4);
156180
else /*if (t1 == typeof(long))*/ value = (ulong)ZigZagEncode(t as long? ?? 0, 8);
157181
}
158-
else value = t;
182+
else if (t is byte) value = t as byte? ?? 0;
183+
else if (t is ushort) value = t as ushort? ?? 0;
184+
else if (t is uint) value = t as uint? ?? 0;
185+
else /*if (t is ulong)*/ value = t as ulong? ?? 0;
159186

160-
if (value <= 240) WriteByte(writeTo, value, bitOffset);
187+
if (value <= 240) WriteByte(writeTo, (byte)value, bitOffset);
161188
else if (value <= 2287)
162189
{
163190
WriteByte(writeTo, (value - 240) / 256 + 241, bitOffset);
@@ -246,7 +273,7 @@ private static long GetBitCount<T>(T t)
246273

247274
private static void WriteBit(byte[] b, bool bit, long index)
248275
=> b[index / 8] = (byte)((b[index / 8] & ~(1 << (int)(index % 8))) | (bit ? 1 << (int)(index % 8) : 0));
249-
//private static void WriteByte(byte[] b, dynamic value, long index) => WriteByte(b, (byte)value, index);
276+
private static void WriteByte(byte[] b, ulong value, long index) => WriteByte(b, (byte)value, index);
250277
private static void WriteByte(byte[] b, byte value, long index)
251278
{
252279
int byteIndex = (int)(index / 8);

MLAPI/NetworkingManagerComponents/Binary/BinaryDistributor.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Tofvesson.Common
99
{
1010
public class BinaryDistributor
1111
{
12+
private delegate T Getter<T>();
1213
private static readonly float[] holder_f = new float[1];
1314
private static readonly double[] holder_d = new double[1];
1415
private static readonly ulong[] holder_u = new ulong[1];
@@ -18,13 +19,15 @@ public class BinaryDistributor
1819
private long bitCount = 0;
1920
public BinaryDistributor(byte[] readFrom) => this.readFrom = readFrom;
2021

21-
public bool ReadBit()
22+
public bool ReadBool()
2223
{
2324
bool result = (readFrom[bitCount / 8] & (byte)(1 << (int)(bitCount % 8))) != 0;
2425
++bitCount;
2526
return result;
2627
}
2728

29+
public float ReadFloat() => ReadFloating<float>();
30+
public double ReadDouble() => ReadFloating<double>();
2831
public byte ReadByte()
2932
{
3033
int shift = (int)(bitCount % 8);
@@ -35,51 +38,55 @@ public byte ReadByte()
3538
bitCount += 8;
3639
return result;
3740
}
38-
39-
public float ReadFloat() => ReadFloating<float>();
40-
public double ReadDouble() => ReadFloating<double>();
41-
public float[] ReadFloatArray() => ReadFloatingArray<float>();
42-
public double[] ReadDoubleArray() => ReadFloatingArray<double>();
43-
public ushort ReadUShort() => ReadUnsigned<ushort>();
44-
public uint ReadUInt() => ReadUnsigned<uint>();
45-
public ulong ReadULong() => ReadUnsigned<ulong>();
41+
public ushort ReadUShort() => (ushort)ReadULong();
42+
public uint ReadUInt() => (uint)ReadULong();
4643
public sbyte ReadSByte() => (sbyte)ZigZagDecode(ReadByte(), 1);
4744
public short ReadShort() => (short)ZigZagDecode(ReadUShort(), 2);
4845
public int ReadInt() => (int)ZigZagDecode(ReadUInt(), 4);
49-
public long ReadLong() => (long)ZigZagDecode(ReadULong(), 8);
46+
public long ReadLong() => ZigZagDecode(ReadULong(), 8);
47+
public float[] ReadFloatArray() => ReadArray(ReadFloat);
48+
public double[] ReadDoubleArray() => ReadArray(ReadDouble);
49+
public byte[] ReadByteArray() => ReadArray(ReadByte);
50+
public ushort[] ReadUShortArray() => ReadArray(ReadUShort);
51+
public uint[] ReadUIntArray() => ReadArray(ReadUInt);
52+
public ulong[] ReadULongArray() => ReadArray(ReadULong);
53+
public sbyte[] ReadSByteArray() => ReadArray(ReadSByte);
54+
public short[] ReadShortArray() => ReadArray(ReadShort);
55+
public int[] ReadIntArray() => ReadArray(ReadInt);
56+
public long[] ReadLongArray() => ReadArray(ReadLong);
57+
public string ReadString() => Encoding.UTF8.GetString(ReadByteArray());
5058

51-
private T ReadUnsigned<T>()
59+
private ulong ReadULong()
5260
{
53-
dynamic header = ReadByte();
54-
if (header <= 240) return (T) header;
55-
if (header <= 248) return (T) (240 + 256 * (header - 241) + ReadByte());
56-
if (header == 249) return (T) (header = 2288 + 256 * ReadByte() + ReadByte());
57-
dynamic res = ReadByte() | ((long)ReadByte() << 8) | ((long)ReadByte() << 16);
61+
ulong header = ReadByte();
62+
if (header <= 240) return header;
63+
if (header <= 248) return 240 + 256 * (header - 241) + ReadByte();
64+
if (header == 249) return 2288 + 256UL * ReadByte() + ReadByte();
65+
ulong res = ReadByte() | ((ulong)ReadByte() << 8) | ((ulong)ReadByte() << 16);
5866
if(header > 250)
5967
{
60-
res |= (long) ReadByte() << 24;
68+
res |= (ulong) ReadByte() << 24;
6169
if(header > 251)
6270
{
63-
res |= (long)ReadByte() << 32;
71+
res |= (ulong)ReadByte() << 32;
6472
if(header > 252)
6573
{
66-
res |= (long)ReadByte() << 40;
74+
res |= (ulong)ReadByte() << 40;
6775
if (header > 253)
6876
{
69-
res |= (long)ReadByte() << 48;
70-
if (header > 254) res |= (long)ReadByte() << 56;
77+
res |= (ulong)ReadByte() << 48;
78+
if (header > 254) res |= (ulong)ReadByte() << 56;
7179
}
7280
}
7381
}
7482
}
75-
return (T) res;
83+
return res;
7684
}
77-
private T[] ReadFloatingArray<T>()
85+
private T[] ReadArray<T>(Getter<T> g)
7886
{
79-
ushort size = ReadUShort();
80-
T[] result = new T[size];
81-
for (short s = 0; s < size; ++s)
82-
result[s] = ReadFloating<T>();
87+
T[] result = new T[ReadUShort()];
88+
for (ushort s = 0; s < result.Length; ++s)
89+
result[s] = g();
8390
return result;
8491
}
8592

0 commit comments

Comments
 (0)