@@ -14,13 +14,17 @@ namespace MLAPI.NetworkingManagerComponents.Binary
14
14
public static class BinarySerializer
15
15
{
16
16
private static Dictionary < string , FieldInfo [ ] > cachedFields = new Dictionary < string , FieldInfo [ ] > ( ) ;
17
+ private static Dictionary < string , MethodInfo > preSerialize = new Dictionary < string , MethodInfo > ( ) ;
18
+ private static Dictionary < string , MethodInfo > postDeserialize = new Dictionary < string , MethodInfo > ( ) ;
17
19
18
20
/// <summary>
19
21
/// Clears the cache of the serializer
20
22
/// </summary>
21
23
public static void ClearCache ( )
22
24
{
23
25
cachedFields . Clear ( ) ;
26
+ preSerialize . Clear ( ) ;
27
+ postDeserialize . Clear ( ) ;
24
28
}
25
29
26
30
/// <summary>
@@ -32,6 +36,7 @@ public static void ClearCache()
32
36
public static byte [ ] Serialize < T > ( T instance )
33
37
{
34
38
FieldInfo [ ] sortedFields ;
39
+ MethodInfo preMethod ;
35
40
36
41
if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
37
42
sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -41,6 +46,17 @@ public static byte[] Serialize<T>(T instance)
41
46
cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
42
47
}
43
48
49
+ if ( preSerialize . ContainsKey ( instance . GetType ( ) . FullName ) )
50
+ preMethod = preSerialize [ instance . GetType ( ) . FullName ] ;
51
+ else
52
+ {
53
+ preMethod = instance . GetType ( ) . GetMethod ( "PreSerialize" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ;
54
+ preSerialize . Add ( instance . GetType ( ) . FullName , preMethod ) ;
55
+ }
56
+
57
+ if ( preMethod != null )
58
+ preMethod . Invoke ( instance , null ) ;
59
+
44
60
using ( BitWriter writer = BitWriter . Get ( ) )
45
61
{
46
62
for ( int i = 0 ; i < sortedFields . Length ; i ++ )
@@ -62,6 +78,7 @@ public static byte[] Serialize<T>(T instance)
62
78
T instance = new T ( ) ;
63
79
64
80
FieldInfo [ ] sortedFields ;
81
+ MethodInfo postMethod ;
65
82
66
83
if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
67
84
sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -71,10 +88,22 @@ public static byte[] Serialize<T>(T instance)
71
88
cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
72
89
}
73
90
91
+ if ( postDeserialize . ContainsKey ( instance . GetType ( ) . FullName ) )
92
+ postMethod = postDeserialize [ instance . GetType ( ) . FullName ] ;
93
+ else
94
+ {
95
+ postMethod = instance . GetType ( ) . GetMethod ( "PostDeserialize" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ;
96
+ postDeserialize . Add ( instance . GetType ( ) . FullName , postMethod ) ;
97
+ }
98
+
74
99
using ( BitReader reader = BitReader . Get ( binary ) )
75
100
{
76
101
for ( int i = 0 ; i < sortedFields . Length ; i ++ )
77
102
sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , sortedFields [ i ] . FieldType ) ) ;
103
+
104
+ if ( postMethod != null )
105
+ postMethod . Invoke ( instance , null ) ;
106
+
78
107
return instance ;
79
108
}
80
109
}
@@ -90,6 +119,7 @@ public static byte[] Serialize<T>(T instance)
90
119
T instance = new T ( ) ;
91
120
92
121
FieldInfo [ ] sortedFields ;
122
+ MethodInfo postMethod ;
93
123
94
124
if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
95
125
sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -99,16 +129,29 @@ public static byte[] Serialize<T>(T instance)
99
129
cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
100
130
}
101
131
132
+ if ( postDeserialize . ContainsKey ( instance . GetType ( ) . FullName ) )
133
+ postMethod = postDeserialize [ instance . GetType ( ) . FullName ] ;
134
+ else
135
+ {
136
+ postMethod = instance . GetType ( ) . GetMethod ( "PostDeserialize" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ;
137
+ postDeserialize . Add ( instance . GetType ( ) . FullName , postMethod ) ;
138
+ }
139
+
102
140
for ( int i = 0 ; i < sortedFields . Length ; i ++ )
103
141
{
104
142
sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , sortedFields [ i ] . FieldType ) ) ;
105
143
}
144
+
145
+ if ( postMethod != null )
146
+ postMethod . Invoke ( instance , null ) ;
147
+
106
148
return instance ;
107
149
}
108
150
109
151
internal static void Serialize ( object instance , BitWriter writer )
110
152
{
111
153
FieldInfo [ ] sortedFields ;
154
+ MethodInfo preMethod ;
112
155
113
156
if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
114
157
sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -117,6 +160,18 @@ internal static void Serialize(object instance, BitWriter writer)
117
160
sortedFields = instance . GetType ( ) . GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) . OrderBy ( x => x . Name ) . Where ( x => ! x . IsDefined ( typeof ( BinaryIgnore ) , true ) ) . ToArray ( ) ;
118
161
cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
119
162
}
163
+
164
+ if ( preSerialize . ContainsKey ( instance . GetType ( ) . FullName ) )
165
+ preMethod = preSerialize [ instance . GetType ( ) . FullName ] ;
166
+ else
167
+ {
168
+ preMethod = instance . GetType ( ) . GetMethod ( "PreSerialize" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ;
169
+ preSerialize . Add ( instance . GetType ( ) . FullName , preMethod ) ;
170
+ }
171
+
172
+ if ( preMethod != null )
173
+ preMethod . Invoke ( instance , null ) ;
174
+
120
175
for ( int i = 0 ; i < sortedFields . Length ; i ++ )
121
176
FieldTypeHelper . WriteFieldType ( writer , sortedFields [ i ] . GetValue ( instance ) ) ;
122
177
}
@@ -125,6 +180,7 @@ internal static object Deserialize(BitReader reader, Type type)
125
180
{
126
181
object instance = Activator . CreateInstance ( type ) ;
127
182
FieldInfo [ ] sortedFields ;
183
+ MethodInfo postMethod ;
128
184
129
185
if ( cachedFields . ContainsKey ( type . FullName ) )
130
186
sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -134,10 +190,22 @@ internal static object Deserialize(BitReader reader, Type type)
134
190
cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
135
191
}
136
192
193
+ if ( postDeserialize . ContainsKey ( instance . GetType ( ) . FullName ) )
194
+ postMethod = postDeserialize [ instance . GetType ( ) . FullName ] ;
195
+ else
196
+ {
197
+ postMethod = instance . GetType ( ) . GetMethod ( "PostDeserialize" , BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) ;
198
+ postDeserialize . Add ( instance . GetType ( ) . FullName , postMethod ) ;
199
+ }
200
+
137
201
for ( int i = 0 ; i < sortedFields . Length ; i ++ )
138
202
{
139
203
sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , sortedFields [ i ] . FieldType ) ) ;
140
204
}
205
+
206
+ if ( postMethod != null )
207
+ postMethod . Invoke ( instance , null ) ;
208
+
141
209
return instance ;
142
210
}
143
211
}
0 commit comments