1
1
using MLAPI . Attributes ;
2
+ using MLAPI . Data ;
2
3
using System . Collections . Generic ;
3
- using System . IO ;
4
4
using System . Linq ;
5
5
using System . Reflection ;
6
- using System . Text ;
7
6
using UnityEngine ;
8
7
9
8
namespace MLAPI . NetworkingManagerComponents . Binary
@@ -41,90 +40,19 @@ public static byte[] Serialize<T>(T instance)
41
40
cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
42
41
}
43
42
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 ( ) )
84
44
{
85
- using ( BinaryWriter writer = new BinaryWriter ( stream ) )
45
+ for ( int i = 0 ; i < sortedFields . Length ; i ++ )
86
46
{
87
- for ( int i = 0 ; i < sortedFields . Length ; i ++ )
47
+ FieldType fieldType = FieldTypeHelper . GetFieldType ( sortedFields [ i ] . FieldType ) ;
48
+ if ( fieldType == FieldType . Invalid )
88
49
{
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 ;
125
52
}
53
+ FieldTypeHelper . WriteFieldType ( writer , sortedFields [ i ] . GetValue ( instance ) , fieldType ) ;
126
54
}
127
- return stream . ToArray ( ) ;
55
+ return writer . Finalize ( ) ;
128
56
}
129
57
}
130
58
@@ -148,52 +76,16 @@ public static byte[] Serialize<T>(T instance)
148
76
cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
149
77
}
150
78
151
- using ( MemoryStream stream = new MemoryStream ( binary ) )
79
+ BitReader reader = new BitReader ( binary ) ;
80
+ for ( int i = 0 ; i < sortedFields . Length ; i ++ )
152
81
{
153
- using ( BinaryReader reader = new BinaryReader ( stream ) )
82
+ FieldType fieldType = FieldTypeHelper . GetFieldType ( sortedFields [ i ] . FieldType ) ;
83
+ if ( fieldType == FieldType . Invalid )
154
84
{
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 ;
196
87
}
88
+ sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , fieldType ) ) ;
197
89
}
198
90
return instance ;
199
91
}
0 commit comments