@@ -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 ) ;
0 commit comments