@@ -74,14 +74,38 @@ public BinaryCollector(int bufferSize)
74
74
collect = new object [ bufferSize ] ;
75
75
}
76
76
77
- public void Push < T > ( T b )
77
+ private void Push < T > ( T b )
78
78
{
79
79
if ( b is string || b . GetType ( ) . IsArray || IsSupportedType ( b . GetType ( ) ) )
80
80
collect [ collectCount ++ ] = b is string ? Encoding . UTF8 . GetBytes ( b as string ) : b as object ;
81
81
//else
82
82
// Debug.LogWarning("MLAPI: The type \"" + b.GetType() + "\" is not supported by the Binary Serializer. It will be ignored");
83
83
}
84
84
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
+
85
109
public byte [ ] ToArray ( )
86
110
{
87
111
long bitCount = 0 ;
@@ -135,18 +159,18 @@ private static void Serialize<T>(T t, byte[] writeTo, ref long bitOffset)
135
159
else result_holder . SetValue ( 0UL , 0 ) ;
136
160
type_holder . SetValue ( t , 0 ) ; // Insert the value to convert into the preallocated holder array
137
161
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 ) ;
139
162
140
163
// Since floating point flag bits are seemingly the highest bytes of the floating point values
141
164
// 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 ) ;
143
167
}
144
168
//bitOffset += offset;
145
169
}
146
170
else
147
171
{
148
172
bool signed = IsSigned ( t . GetType ( ) ) ;
149
- dynamic value ;
173
+ ulong value ;
150
174
if ( signed )
151
175
{
152
176
Type t1 = t . GetType ( ) ;
@@ -155,9 +179,12 @@ private static void Serialize<T>(T t, byte[] writeTo, ref long bitOffset)
155
179
else if ( t1 == typeof ( int ) ) value = ( uint ) ZigZagEncode ( t as int ? ?? 0 , 4 ) ;
156
180
else /*if (t1 == typeof(long))*/ value = ( ulong ) ZigZagEncode ( t as long ? ?? 0 , 8 ) ;
157
181
}
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 ;
159
186
160
- if ( value <= 240 ) WriteByte ( writeTo , value , bitOffset ) ;
187
+ if ( value <= 240 ) WriteByte ( writeTo , ( byte ) value , bitOffset ) ;
161
188
else if ( value <= 2287 )
162
189
{
163
190
WriteByte ( writeTo , ( value - 240 ) / 256 + 241 , bitOffset ) ;
@@ -246,7 +273,7 @@ private static long GetBitCount<T>(T t)
246
273
247
274
private static void WriteBit ( byte [ ] b , bool bit , long index )
248
275
=> 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 ) ;
250
277
private static void WriteByte ( byte [ ] b , byte value , long index )
251
278
{
252
279
int byteIndex = ( int ) ( index / 8 ) ;
0 commit comments