@@ -14,13 +14,17 @@ namespace MLAPI.NetworkingManagerComponents.Binary
1414 public static class BinarySerializer
1515 {
1616 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 > ( ) ;
1719
1820 /// <summary>
1921 /// Clears the cache of the serializer
2022 /// </summary>
2123 public static void ClearCache ( )
2224 {
2325 cachedFields . Clear ( ) ;
26+ preSerialize . Clear ( ) ;
27+ postDeserialize . Clear ( ) ;
2428 }
2529
2630 /// <summary>
@@ -32,6 +36,7 @@ public static void ClearCache()
3236 public static byte [ ] Serialize < T > ( T instance )
3337 {
3438 FieldInfo [ ] sortedFields ;
39+ MethodInfo preMethod ;
3540
3641 if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
3742 sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -41,6 +46,17 @@ public static byte[] Serialize<T>(T instance)
4146 cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
4247 }
4348
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+
4460 using ( BitWriter writer = BitWriter . Get ( ) )
4561 {
4662 for ( int i = 0 ; i < sortedFields . Length ; i ++ )
@@ -62,6 +78,7 @@ public static byte[] Serialize<T>(T instance)
6278 T instance = new T ( ) ;
6379
6480 FieldInfo [ ] sortedFields ;
81+ MethodInfo postMethod ;
6582
6683 if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
6784 sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -71,10 +88,22 @@ public static byte[] Serialize<T>(T instance)
7188 cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
7289 }
7390
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+
7499 using ( BitReader reader = BitReader . Get ( binary ) )
75100 {
76101 for ( int i = 0 ; i < sortedFields . Length ; i ++ )
77102 sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , sortedFields [ i ] . FieldType ) ) ;
103+
104+ if ( postMethod != null )
105+ postMethod . Invoke ( instance , null ) ;
106+
78107 return instance ;
79108 }
80109 }
@@ -90,6 +119,7 @@ public static byte[] Serialize<T>(T instance)
90119 T instance = new T ( ) ;
91120
92121 FieldInfo [ ] sortedFields ;
122+ MethodInfo postMethod ;
93123
94124 if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
95125 sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -99,16 +129,29 @@ public static byte[] Serialize<T>(T instance)
99129 cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
100130 }
101131
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+
102140 for ( int i = 0 ; i < sortedFields . Length ; i ++ )
103141 {
104142 sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , sortedFields [ i ] . FieldType ) ) ;
105143 }
144+
145+ if ( postMethod != null )
146+ postMethod . Invoke ( instance , null ) ;
147+
106148 return instance ;
107149 }
108150
109151 internal static void Serialize ( object instance , BitWriter writer )
110152 {
111153 FieldInfo [ ] sortedFields ;
154+ MethodInfo preMethod ;
112155
113156 if ( cachedFields . ContainsKey ( instance . GetType ( ) . FullName ) )
114157 sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -117,6 +160,18 @@ internal static void Serialize(object instance, BitWriter writer)
117160 sortedFields = instance . GetType ( ) . GetFields ( BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ) . OrderBy ( x => x . Name ) . Where ( x => ! x . IsDefined ( typeof ( BinaryIgnore ) , true ) ) . ToArray ( ) ;
118161 cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
119162 }
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+
120175 for ( int i = 0 ; i < sortedFields . Length ; i ++ )
121176 FieldTypeHelper . WriteFieldType ( writer , sortedFields [ i ] . GetValue ( instance ) ) ;
122177 }
@@ -125,6 +180,7 @@ internal static object Deserialize(BitReader reader, Type type)
125180 {
126181 object instance = Activator . CreateInstance ( type ) ;
127182 FieldInfo [ ] sortedFields ;
183+ MethodInfo postMethod ;
128184
129185 if ( cachedFields . ContainsKey ( type . FullName ) )
130186 sortedFields = cachedFields [ instance . GetType ( ) . FullName ] ;
@@ -134,10 +190,22 @@ internal static object Deserialize(BitReader reader, Type type)
134190 cachedFields . Add ( instance . GetType ( ) . FullName , sortedFields ) ;
135191 }
136192
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+
137201 for ( int i = 0 ; i < sortedFields . Length ; i ++ )
138202 {
139203 sortedFields [ i ] . SetValue ( instance , FieldTypeHelper . ReadFieldType ( reader , sortedFields [ i ] . FieldType ) ) ;
140204 }
205+
206+ if ( postMethod != null )
207+ postMethod . Invoke ( instance , null ) ;
208+
141209 return instance ;
142210 }
143211 }
0 commit comments