11using MLAPI . Attributes ;
2+ using MLAPI . Data ;
23using System . Collections . Generic ;
3- using System . IO ;
44using System . Linq ;
55using System . Reflection ;
6- using System . Text ;
76using UnityEngine ;
87
98namespace MLAPI . NetworkingManagerComponents . Binary
@@ -41,90 +40,19 @@ public static byte[] Serialize<T>(T instance)
4140 cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
4241 }
4342
44- int outputSize = 0 ;
45- //Calculate output size
46- for ( int i = 0 ; i < sortedFields . Length ; i ++ )
47- {
48- if ( sortedFields [ i ] . FieldType == typeof ( bool ) )
49- outputSize += 1 ;
50- else if ( sortedFields [ i ] . FieldType == typeof ( byte ) )
51- outputSize += 1 ;
52- else if ( sortedFields [ i ] . FieldType == typeof ( char ) )
53- outputSize += 2 ;
54- else if ( sortedFields [ i ] . FieldType == typeof ( double ) )
55- outputSize += 8 ;
56- else if ( sortedFields [ i ] . FieldType == typeof ( float ) )
57- outputSize += 4 ;
58- else if ( sortedFields [ i ] . FieldType == typeof ( decimal ) )
59- outputSize += 16 ;
60- else if ( sortedFields [ i ] . FieldType == typeof ( int ) )
61- outputSize += 4 ;
62- else if ( sortedFields [ i ] . FieldType == typeof ( long ) )
63- outputSize += 8 ;
64- else if ( sortedFields [ i ] . FieldType == typeof ( sbyte ) )
65- outputSize += 1 ;
66- else if ( sortedFields [ i ] . FieldType == typeof ( short ) )
67- outputSize += 2 ;
68- else if ( sortedFields [ i ] . FieldType == typeof ( uint ) )
69- outputSize += 4 ;
70- else if ( sortedFields [ i ] . FieldType == typeof ( ulong ) )
71- outputSize += 8 ;
72- else if ( sortedFields [ i ] . FieldType == typeof ( ushort ) )
73- outputSize += 2 ;
74- else if ( sortedFields [ i ] . FieldType == typeof ( string ) )
75- outputSize += Encoding . UTF8 . GetByteCount ( ( string ) sortedFields [ i ] . GetValue ( instance ) ) + 2 ;
76- else if ( sortedFields [ i ] . FieldType == typeof ( byte [ ] ) )
77- outputSize += ( ( byte [ ] ) sortedFields [ i ] . GetValue ( instance ) ) . Length + 2 ; //Two bytes to specify the size
78- else
79- Debug . LogWarning ( "MLAPI: The type \" " + sortedFields [ i ] . FieldType . Name + "\" is not supported by the Binary Serializer. It will be ignored" ) ;
80- }
81-
82- //Write data
83- using ( MemoryStream stream = new MemoryStream ( outputSize ) )
43+ using ( BitWriter writer = new BitWriter ( ) )
8444 {
85- using ( BinaryWriter writer = new BinaryWriter ( stream ) )
45+ for ( int i = 0 ; i < sortedFields . Length ; i ++ )
8646 {
87- for ( int i = 0 ; i < sortedFields . Length ; i ++ )
47+ FieldType fieldType = FieldTypeHelper . GetFieldType ( sortedFields [ i ] . FieldType ) ;
48+ if ( fieldType == FieldType . Invalid )
8849 {
89- if ( sortedFields [ i ] . FieldType == typeof ( bool ) )
90- writer . Write ( ( bool ) sortedFields [ i ] . GetValue ( instance ) ) ;
91- else if ( sortedFields [ i ] . FieldType == typeof ( byte ) )
92- writer . Write ( ( byte ) sortedFields [ i ] . GetValue ( instance ) ) ;
93- else if ( sortedFields [ i ] . FieldType == typeof ( char ) )
94- writer . Write ( ( char ) sortedFields [ i ] . GetValue ( instance ) ) ;
95- else if ( sortedFields [ i ] . FieldType == typeof ( double ) )
96- writer . Write ( ( double ) sortedFields [ i ] . GetValue ( instance ) ) ;
97- else if ( sortedFields [ i ] . FieldType == typeof ( float ) )
98- writer . Write ( ( float ) sortedFields [ i ] . GetValue ( instance ) ) ;
99- else if ( sortedFields [ i ] . FieldType == typeof ( decimal ) )
100- writer . Write ( ( decimal ) sortedFields [ i ] . GetValue ( instance ) ) ;
101- else if ( sortedFields [ i ] . FieldType == typeof ( int ) )
102- writer . Write ( ( int ) sortedFields [ i ] . GetValue ( instance ) ) ;
103- else if ( sortedFields [ i ] . FieldType == typeof ( long ) )
104- writer . Write ( ( long ) sortedFields [ i ] . GetValue ( instance ) ) ;
105- else if ( sortedFields [ i ] . FieldType == typeof ( sbyte ) )
106- writer . Write ( ( sbyte ) sortedFields [ i ] . GetValue ( instance ) ) ;
107- else if ( sortedFields [ i ] . FieldType == typeof ( short ) )
108- writer . Write ( ( short ) sortedFields [ i ] . GetValue ( instance ) ) ;
109- else if ( sortedFields [ i ] . FieldType == typeof ( uint ) )
110- writer . Write ( ( uint ) sortedFields [ i ] . GetValue ( instance ) ) ;
111- else if ( sortedFields [ i ] . FieldType == typeof ( ulong ) )
112- writer . Write ( ( ulong ) sortedFields [ i ] . GetValue ( instance ) ) ;
113- else if ( sortedFields [ i ] . FieldType == typeof ( ushort ) )
114- writer . Write ( ( ushort ) sortedFields [ i ] . GetValue ( instance ) ) ;
115- else if ( sortedFields [ i ] . FieldType == typeof ( string ) )
116- {
117- writer . Write ( ( ushort ) Encoding . UTF8 . GetByteCount ( ( string ) sortedFields [ i ] . GetValue ( instance ) ) ) ; //Size of string in bytes
118- writer . Write ( Encoding . UTF8 . GetBytes ( ( string ) sortedFields [ i ] . GetValue ( instance ) ) ) ;
119- }
120- else if ( sortedFields [ i ] . FieldType == typeof ( byte [ ] ) )
121- {
122- writer . Write ( ( ushort ) ( ( byte [ ] ) sortedFields [ i ] . GetValue ( instance ) ) . Length ) ; //Size of byte array
123- writer . Write ( ( byte [ ] ) sortedFields [ i ] . GetValue ( instance ) ) ;
124- }
50+ Debug . LogWarning ( "MLAPI: The field " + sortedFields [ i ] . Name + " will not be serialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up." ) ;
51+ continue ;
12552 }
53+ FieldTypeHelper . WriteFieldType ( writer , sortedFields [ i ] . GetValue ( instance ) , fieldType ) ;
12654 }
127- return stream . ToArray ( ) ;
55+ return writer . Finalize ( ) ;
12856 }
12957 }
13058
@@ -148,52 +76,16 @@ public static byte[] Serialize<T>(T instance)
14876 cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
14977 }
15078
151- using ( MemoryStream stream = new MemoryStream ( binary ) )
79+ BitReader reader = new BitReader ( binary ) ;
80+ for ( int i = 0 ; i < sortedFields . Length ; i ++ )
15281 {
153- using ( BinaryReader reader = new BinaryReader ( stream ) )
82+ FieldType fieldType = FieldTypeHelper . GetFieldType ( sortedFields [ i ] . FieldType ) ;
83+ if ( fieldType == FieldType . Invalid )
15484 {
155- for ( int i = 0 ; i < sortedFields . Length ; i ++ )
156- {
157- if ( sortedFields [ i ] . FieldType == typeof ( bool ) )
158- sortedFields [ i ] . SetValue ( instance , reader . ReadBoolean ( ) ) ;
159- else if ( sortedFields [ i ] . FieldType == typeof ( byte ) )
160- sortedFields [ i ] . SetValue ( instance , reader . ReadByte ( ) ) ;
161- else if ( sortedFields [ i ] . FieldType == typeof ( char ) )
162- sortedFields [ i ] . SetValue ( instance , reader . ReadChar ( ) ) ;
163- else if ( sortedFields [ i ] . FieldType == typeof ( double ) )
164- sortedFields [ i ] . SetValue ( instance , reader . ReadDouble ( ) ) ;
165- else if ( sortedFields [ i ] . FieldType == typeof ( float ) )
166- sortedFields [ i ] . SetValue ( instance , reader . ReadSingle ( ) ) ;
167- else if ( sortedFields [ i ] . FieldType == typeof ( decimal ) )
168- sortedFields [ i ] . SetValue ( instance , reader . ReadDecimal ( ) ) ;
169- else if ( sortedFields [ i ] . FieldType == typeof ( int ) )
170- sortedFields [ i ] . SetValue ( instance , reader . ReadInt32 ( ) ) ;
171- else if ( sortedFields [ i ] . FieldType == typeof ( long ) )
172- sortedFields [ i ] . SetValue ( instance , reader . ReadInt64 ( ) ) ;
173- else if ( sortedFields [ i ] . FieldType == typeof ( sbyte ) )
174- sortedFields [ i ] . SetValue ( instance , reader . ReadSByte ( ) ) ;
175- else if ( sortedFields [ i ] . FieldType == typeof ( short ) )
176- sortedFields [ i ] . SetValue ( instance , reader . ReadInt16 ( ) ) ;
177- else if ( sortedFields [ i ] . FieldType == typeof ( uint ) )
178- sortedFields [ i ] . SetValue ( instance , reader . ReadUInt32 ( ) ) ;
179- else if ( sortedFields [ i ] . FieldType == typeof ( ulong ) )
180- sortedFields [ i ] . SetValue ( instance , reader . ReadUInt64 ( ) ) ;
181- else if ( sortedFields [ i ] . FieldType == typeof ( ushort ) )
182- sortedFields [ i ] . SetValue ( instance , reader . ReadUInt64 ( ) ) ;
183- else if ( sortedFields [ i ] . FieldType == typeof ( string ) )
184- {
185- ushort size = reader . ReadUInt16 ( ) ;
186- sortedFields [ i ] . SetValue ( instance , Encoding . UTF8 . GetString ( reader . ReadBytes ( size ) ) ) ;
187- }
188- else if ( sortedFields [ i ] . FieldType == typeof ( byte [ ] ) )
189- {
190- ushort size = reader . ReadUInt16 ( ) ;
191- sortedFields [ i ] . SetValue ( instance , reader . ReadBytes ( size ) ) ;
192- }
193- else
194- Debug . LogWarning ( "MLAPI: The type \" " + sortedFields [ i ] . FieldType . Name + "\" is not supported by the Binary Serializer. It will be ignored" ) ;
195- }
85+ Debug . LogWarning ( "MLAPI: The field " + sortedFields [ i ] . Name + " will not be deserialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up." ) ;
86+ continue ;
19687 }
88+ sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , fieldType ) ) ;
19789 }
19890 return instance ;
19991 }
0 commit comments