Skip to content

Commit d1f5f33

Browse files
Other changes?
¯\_(ツ)_/¯
1 parent e0e32c8 commit d1f5f33

File tree

11 files changed

+97
-60
lines changed

11 files changed

+97
-60
lines changed

MLAPI/Data/NetworkedCollections/NetworkedDictionary.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public string GetChannel()
7575
}
7676

7777
/// <inheritdoc />
78-
public void ReadDelta(BitReader reader)
78+
public void ReadDelta(BitReaderDeprecated reader)
7979
{
8080
ushort deltaCount = reader.ReadUShort();
8181
for (int i = 0; i < deltaCount; i++)
@@ -124,7 +124,7 @@ public void ReadDelta(BitReader reader)
124124
}
125125

126126
/// <inheritdoc />
127-
public void ReadField(BitReader reader)
127+
public void ReadField(BitReaderDeprecated reader)
128128
{
129129
dictionary.Clear();
130130
ushort entryCount = reader.ReadUShort();
@@ -149,7 +149,7 @@ public bool TryGetValue(TKey key, out TValue value)
149149
}
150150

151151
/// <inheritdoc />
152-
public void WriteDelta(BitWriter writer)
152+
public void WriteDelta(BitWriterDeprecated writer)
153153
{
154154
writer.WriteUShort((ushort)dirtyEvents.Count);
155155
for (int i = 0; i < dirtyEvents.Count; i++)
@@ -192,7 +192,7 @@ public void WriteDelta(BitWriter writer)
192192
}
193193

194194
/// <inheritdoc />
195-
public void WriteField(BitWriter writer)
195+
public void WriteField(BitWriterDeprecated writer)
196196
{
197197
writer.WriteUShort((ushort)dictionary.Count);
198198
foreach (KeyValuePair<TKey, TValue> pair in dictionary)

MLAPI/Data/NetworkedCollections/NetworkedList.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public bool CanClientRead(uint clientId)
126126
}
127127

128128
/// <inheritdoc />
129-
public void WriteDelta(BitWriter writer)
129+
public void WriteDelta(BitWriterDeprecated writer)
130130
{
131131
writer.WriteUShort((ushort)dirtyEvents.Count);
132132
for (int i = 0; i < dirtyEvents.Count; i++)
@@ -173,7 +173,7 @@ public void WriteDelta(BitWriter writer)
173173
}
174174

175175
/// <inheritdoc />
176-
public void WriteField(BitWriter writer)
176+
public void WriteField(BitWriterDeprecated writer)
177177
{
178178
writer.WriteUShort((ushort)list.Count);
179179
for (int i = 0; i < list.Count; i++)
@@ -183,7 +183,7 @@ public void WriteField(BitWriter writer)
183183
}
184184

185185
/// <inheritdoc />
186-
public void ReadField(BitReader reader)
186+
public void ReadField(BitReaderDeprecated reader)
187187
{
188188
list.Clear();
189189
ushort count = reader.ReadUShort();
@@ -194,7 +194,7 @@ public void ReadField(BitReader reader)
194194
}
195195

196196
/// <inheritdoc />
197-
public void ReadDelta(BitReader reader)
197+
public void ReadDelta(BitReaderDeprecated reader)
198198
{
199199
ushort deltaCount = reader.ReadUShort();
200200
for (int i = 0; i < deltaCount; i++)

MLAPI/Data/NetworkedVar.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public bool CanClientRead(uint clientId)
117117
/// Writes the variable to the writer
118118
/// </summary>
119119
/// <param name="writer">The writer to write the value to</param>
120-
public void WriteDelta(BitWriter writer) => WriteField(writer); //The NetworkedVar is built for simple data types and has no delta.
120+
public void WriteDelta(BitWriterDeprecated writer) => WriteField(writer); //The NetworkedVar is built for simple data types and has no delta.
121121

122122
/// <inheritdoc />
123123
public bool CanClientWrite(uint clientId)
@@ -144,7 +144,7 @@ public bool CanClientWrite(uint clientId)
144144
/// Reads value from the reader and applies it
145145
/// </summary>
146146
/// <param name="reader">The reader to read the value from</param>
147-
public void ReadDelta(BitReader reader) => ReadField(reader); //The NetworkedVar is built for simple data types and has no delta.
147+
public void ReadDelta(BitReaderDeprecated reader) => ReadField(reader); //The NetworkedVar is built for simple data types and has no delta.
148148

149149
/// <inheritdoc />
150150
public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
@@ -153,7 +153,7 @@ public void SetNetworkedBehaviour(NetworkedBehaviour behaviour)
153153
}
154154

155155
/// <inheritdoc />
156-
public void ReadField(BitReader reader)
156+
public void ReadField(BitReaderDeprecated reader)
157157
{
158158
T previousValue = InternalValue;
159159
InternalValue = reader.ReadValueTypeOrString<T>();
@@ -162,7 +162,7 @@ public void ReadField(BitReader reader)
162162
}
163163

164164
/// <inheritdoc />
165-
public void WriteField(BitWriter writer)
165+
public void WriteField(BitWriterDeprecated writer)
166166
{
167167
writer.WriteValueTypeOrString(InternalValue);
168168
}

MLAPI/Data/NetworkedVarMeta.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ public interface INetworkedVar
3939
/// Writes the dirty changes, that is, the changes since the variable was last dirty, to the writer
4040
/// </summary>
4141
/// <param name="writer">The writer to write the dirty changes to</param>
42-
void WriteDelta(BitWriter writer);
42+
void WriteDelta(BitWriterDeprecated writer);
4343
/// <summary>
4444
/// Writes the complete state of the variable to the writer
4545
/// </summary>
4646
/// <param name="writer">The writer to write the state to</param>
47-
void WriteField(BitWriter writer);
47+
void WriteField(BitWriterDeprecated writer);
4848
/// <summary>
4949
/// Reads the complete state from the reader and applies it
5050
/// </summary>
5151
/// <param name="reader">The reader to read the state from</param>
52-
void ReadField(BitReader reader);
52+
void ReadField(BitReaderDeprecated reader);
5353
/// <summary>
5454
/// Reads delta from the reader and applies them to the internal value
5555
/// </summary>
5656
/// <param name="reader">The reader to read the delta from</param>
57-
void ReadDelta(BitReader reader);
57+
void ReadDelta(BitReaderDeprecated reader);
5858
/// <summary>
5959
/// Sets NetworkedBehaviour the container belongs to.
6060
/// </summary>

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ internal void NetworkedVarUpdate()
700700
//This iterates over every "channel group".
701701
for (int j = 0; j < channelMappedVarIndexes.Count; j++)
702702
{
703-
using (BitWriter writer = BitWriter.Get())
703+
using (BitWriterDeprecated writer = BitWriterDeprecated.Get())
704704
{
705705
writer.WriteUInt(networkId);
706706
writer.WriteUShort(networkedObject.GetOrderIndex(this));
@@ -737,7 +737,7 @@ internal void NetworkedVarUpdate()
737737
}
738738
}
739739

740-
internal void HandleNetworkedVarDeltas(BitReader reader, uint clientId)
740+
internal void HandleNetworkedVarDeltas(BitReaderDeprecated reader, uint clientId)
741741
{
742742
for (int i = 0; i < networkedVarFields.Count; i++)
743743
{
@@ -761,7 +761,7 @@ internal void HandleNetworkedVarDeltas(BitReader reader, uint clientId)
761761
}
762762
}
763763

764-
internal void HandleNetworkedVarUpdate(BitReader reader, uint clientId)
764+
internal void HandleNetworkedVarUpdate(BitReaderDeprecated reader, uint clientId)
765765
{
766766
for (int i = 0; i < networkedVarFields.Count; i++)
767767
{

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId, uint
862862

863863
using (BitReaderDeprecated messageReader = readBuffer == null ? reader : BitReaderDeprecated.Get(readBuffer))
864864
#else
865-
using (BitReader messageReader = reader)
865+
using (BitReaderDeprecated messageReader = reader)
866866
#endif
867867
{
868868
if (isServer && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType))

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 10 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ public byte ReadNibble()
263263
);
264264
}
265265

266+
public sbyte ReadSByte() => (sbyte)ReadByte();
266267
/// <summary>
267268
/// Read an unsigned short (UInt16) from the stream.
268269
/// </summary>
@@ -821,25 +822,25 @@ public uint[] ReadUIntArrayDiff(uint[] readTo = null, long knownLength = -1)
821822
public ValueType ReadValueType<T>()
822823
{
823824
if (typeof(T) == typeof(float))
824-
return ReadFloat();
825+
return ReadSingle();
825826
else if (typeof(T) == typeof(double))
826827
return ReadDouble();
827828
else if (typeof(T) == typeof(byte))
828829
return ReadByte();
829830
else if (typeof(T) == typeof(sbyte))
830831
return ReadSByte();
831832
else if (typeof(T) == typeof(short))
832-
return ReadShort();
833+
return ReadInt16();
833834
else if (typeof(T) == typeof(ushort))
834-
return ReadUShort();
835+
return ReadUInt16();
835836
else if (typeof(T) == typeof(int))
836-
return ReadInt();
837+
return ReadInt32();
837838
else if (typeof(T) == typeof(uint))
838-
return ReadUInt();
839+
return ReadUInt32();
839840
else if (typeof(T) == typeof(long))
840-
return ReadLong();
841+
return ReadInt64();
841842
else if (typeof(T) == typeof(ulong))
842-
return ReadULong();
843+
return ReadUInt64();
843844

844845
return default(ValueType);
845846
}
@@ -860,36 +861,8 @@ public T ReadValueTypeOrString<T>()
860861
return default(T);
861862
}
862863
}
863-
864-
public bool ReadBool()
865-
{
866-
if (knownLength < 0) knownLength = (long)ReadUInt64Packed();
867-
uint[] writeTo = readTo == null || readTo.LongLength != knownLength ? new uint[knownLength] : readTo;
868-
ulong data = bitSource.BitPosition + (ulong)(readTo == null ? 0 : Math.Min(knownLength, readTo.LongLength));
869-
ulong rset;
870-
long readToLength = readTo == null ? 0 : readTo.LongLength;
871-
for (long i = 0; i < knownLength; ++i)
872-
{
873-
if (i >= readToLength || ReadBit())
874-
{
875-
#if ARRAY_WRITE_PREMAP
876-
// Move to data section
877-
rset = bitSource.BitPosition;
878-
bitSource.BitPosition = data;
879-
#endif
880-
// Read datum
881-
writeTo[i] = ReadUInt32Packed();
882-
#if ARRAY_WRITE_PREMAP
883-
// Return to mapping section
884-
data = bitSource.BitPosition;
885-
bitSource.BitPosition = rset;
886-
#endif
887-
}
888-
else if (i < readTo.LongLength) writeTo[i] = readTo[i];
889-
}
890-
bitSource.BitPosition = data;
891-
return writeTo;
892-
}
864+
865+
public bool ReadBool() => ReadBit();
893866

894867
public long[] ReadLongArray(long[] readTo = null, long knownLength = -1)
895868
{

MLAPI/NetworkingManagerComponents/Binary/BitReaderDeprecated.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace MLAPI.NetworkingManagerComponents.Binary
99
{
10+
[Obsolete]
1011
public class BitReaderDeprecated : IDisposable
1112
{
1213
private bool disposed;
@@ -64,6 +65,49 @@ public static BitReaderDeprecated Get(byte[] readFrom)
6465
}
6566
}
6667

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+
67111
public bool ReadBool()
68112
{
69113
bool result = (readFrom[bitCount / 8] & (byte)(1 << (int)(bitCount % 8))) != 0;

MLAPI/NetworkingManagerComponents/Binary/BitStream.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,19 @@ public void CopyFrom(Stream s, int count = -1)
357357
}
358358

359359
// TODO: Implement CopyFrom() for BitStream with bitCount parameter
360-
public void CopyFrom(BitStream s, int count, bool countBits)
360+
public void CopyFrom(BitStream s, int dataCount, bool copyBits)
361361
{
362-
362+
if (!copyBits)
363+
{
364+
CopyFrom(s, dataCount);
365+
}
366+
else
367+
{
368+
ulong count = dataCount < 0 ? s.BitLength : (ulong)dataCount;
369+
if (s.BitLength < count) throw new IndexOutOfRangeException("Attempted to read more data than is available");
370+
Write(s.GetBuffer(), 0, (int)(count >> 3));
371+
for (int i = (int)(count & 7); i >= 0; --i) WriteBit(s.ReadBit());
372+
}
363373
}
364374

365375
/// <summary>

MLAPI/NetworkingManagerComponents/Binary/BitWriterDeprecated.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ public void WriteGeneric<T>(T t)
133133
}
134134
*/
135135

136+
private void PushPreZigZag<T>(T b)
137+
{
138+
if (b is sbyte) Push(ZigZagEncode((sbyte)(object)b)); //BOX
139+
if (b is ushort) Push(ZigZagEncode((ushort)(object)b)); //BOX
140+
if (b is uint) Push(ZigZagEncode((uint)(object)b)); //BOX
141+
if (b is ulong) Push(ZigZagEncode((long)(ulong)(object)b)); //BOX
142+
else Push(b);
143+
}
144+
145+
public void WriteValueTypeOrString<T>(T t) => PushPreZigZag(t);
136146
public void WriteBool(bool b) => Push(b);
137147
public void WriteFloat(float f) => Push(f);
138148
public void WriteDouble(double d) => Push(d);

0 commit comments

Comments
 (0)