@@ -22,10 +22,9 @@ internal class NetworkBehaviourProcessor : CodegenBase
2222 #endregion
2323
2424 #region Const.
25- internal const string EARLY_INITIALIZED_NAME = "NetworkInitializeEarly_" ;
26- internal const string LATE_INITIALIZED_NAME = "NetworkInitializeLate_" ;
2725 internal const string NETWORKINITIALIZE_EARLY_INTERNAL_NAME = "NetworkInitialize___Early" ;
28- internal const string NETWORKINITIALIZE_LATE_INTERNAL_NAME = "NetworkInitialize__Late" ;
26+ internal const string NETWORKINITIALIZE_LATE_INTERNAL_NAME = "NetworkInitialize___Late" ;
27+ private readonly string [ ] _networkInitializeMethodNames = new string [ ] { NETWORKINITIALIZE_EARLY_INTERNAL_NAME , NETWORKINITIALIZE_LATE_INTERNAL_NAME } ;
2928 #endregion
3029
3130 internal bool ProcessLocal ( TypeDefinition typeDef )
@@ -65,10 +64,10 @@ internal bool ProcessLocal(TypeDefinition typeDef)
6564 * multiple times. EG: with this solution if parent had 1 sync type
6665 * and childA had 2 the parent would be index 0, and childA would have 1 and 2.
6766 * But also, if childB inherited childA it would have 3+.
68- *
67+ *
6968 * Going in reverse also gaurantees the awake method will already be created
70- * or modified in any class a child inherits. This lets us call it appropriately
71- * as well error if the awake does not exist, such as could not be created. */
69+ * or modified in any class a child inherits. This lets us call it appropriately
70+ * as well error if the awake does not exist, such as could not be created. */
7271 typeDefs . Reverse ( ) ;
7372
7473 foreach ( TypeDefinition td in typeDefs )
@@ -77,7 +76,7 @@ internal bool ProcessLocal(TypeDefinition typeDef)
7776 * can use it. */
7877 MethodDefinition networkInitializeIfDisabledMd ;
7978 CreateNetworkInitializeMethods ( td , out networkInitializeIfDisabledMd ) ;
80- CallNetworkInitializesFromNetworkInitializeIfDisabled ( networkInitializeIfDisabledMd ) ;
79+ CallNetworkInitialize ( networkInitializeIfDisabledMd ) ;
8180
8281
8382
@@ -111,7 +110,7 @@ internal bool ProcessLocal(TypeDefinition typeDef)
111110 //Copy user logic from awake into a new method.
112111 CopyAwakeUserLogic ( td ) ;
113112 /* Create awake method or if already exist make
114- * it public virtual. */
113+ * it public virtual. */
115114 if ( ! ModifyAwakeMethod ( td , out bool awakeCreated ) )
116115 {
117116 //This is a hard fail and will break the solution so throw here.
@@ -146,7 +145,6 @@ internal bool ProcessLocal(TypeDefinition typeDef)
146145 /// </summary>
147146 internal string GetAwakeUserLogicMethodDefinition ( TypeDefinition td ) => $ "Awake_UserLogic_{ td . FullName } _{ base . Module . Name } ";
148147
149-
150148 /// <summary>
151149 /// Returns if a class has been processed.
152150 /// </summary>
@@ -207,7 +205,7 @@ internal bool NonNetworkBehaviourHasInvalidAttributes(Collection<TypeDefinition>
207205 private void CallBaseAwake ( TypeDefinition td )
208206 {
209207 /* If base is not a class which can be processed then there
210- * is no need to continue. */
208+ * is no need to continue. */
211209 if ( ! td . CanProcessBaseType ( base . Session ) )
212210 return ;
213211
@@ -221,7 +219,6 @@ private void CallBaseAwake(TypeDefinition td)
221219 processor . Emit ( OpCodes . Call , baseAwakeMr ) ;
222220 }
223221
224-
225222 /// <summary>
226223 /// Calls the next awake method if the nested awake was created by codegen.
227224 /// </summary>
@@ -241,7 +238,6 @@ private void CallAwakeUserLogic(TypeDefinition td)
241238 base . GetClass < GeneralHelper > ( ) . CallCopiedMethod ( awakeMd , userLogicMd ) ;
242239 }
243240
244-
245241 /// <summary>
246242 /// Adds a check to NetworkInitialize to see if it has already run.
247243 /// </summary>
@@ -287,23 +283,22 @@ void AddCheck(string methodName)
287283 /// </summary>
288284 private void CallBaseOnNetworkInitializeMethods ( TypeDefinition typeDef )
289285 {
290- //If base class cannot have a networkinitialize no reason to continue .
291- if ( ! typeDef . CanProcessBaseType ( base . Session ) )
286+ //If base is null cannot call base .
287+ if ( typeDef . BaseType == null )
292288 return ;
293289
294- string [ ] initializeMethodNames = new string [ ] { NETWORKINITIALIZE_EARLY_INTERNAL_NAME , NETWORKINITIALIZE_LATE_INTERNAL_NAME } ;
295- foreach ( string mdName in initializeMethodNames )
290+ foreach ( string mdName in _networkInitializeMethodNames )
296291 {
297- /* Awake will always exist because it was added previously.
298- * Get awake for copy and base of copy. */
299- MethodDefinition thisMd = typeDef . GetMethod ( mdName ) ;
300- ILProcessor processor = thisMd . Body . GetILProcessor ( ) ;
301-
302- /* Awake will always exist because it was added previously.
303- * Get awake for copy and base of copy. */
304292 MethodReference baseMr = typeDef . GetMethodReferenceInBase ( base . Session , mdName ) ;
293+ //Not found in base.
294+ if ( baseMr == null )
295+ continue ;
296+
305297 MethodDefinition baseMd = baseMr . CachedResolve ( base . Session ) ;
306298
299+ MethodDefinition thisMd = typeDef . GetMethod ( mdName ) ;
300+ ILProcessor processor = thisMd . Body . GetILProcessor ( ) ;
301+
307302 bool alreadyHasBaseCall = false ;
308303 //Check if already calls baseAwake.
309304 foreach ( Instruction item in thisMd . Body . Instructions )
@@ -328,11 +323,12 @@ private void CallBaseOnNetworkInitializeMethods(TypeDefinition typeDef)
328323 //Create instructions for base call.
329324 List < Instruction > instructions = new ( )
330325 {
331- processor . Create ( OpCodes . Ldarg_0 ) , //this.
332- processor . Create ( OpCodes . Call , baseMr )
333- } ;
326+ processor . Create ( OpCodes . Ldarg_0 ) , //this.
327+ processor . Create ( OpCodes . Call , baseMr )
328+ } ;
334329 processor . InsertFirst ( instructions ) ;
335330 }
331+
336332 }
337333 }
338334
@@ -345,8 +341,7 @@ private void AddReturnToAwake(TypeDefinition td)
345341 MethodDefinition awakeMd = td . GetMethod ( NetworkBehaviourHelper . AWAKE_METHOD_NAME ) ;
346342 ILProcessor processor = awakeMd . Body . GetILProcessor ( ) ;
347343 //If no instructions or the last instruction isnt ret.
348- if ( processor . Body . Instructions . Count == 0
349- || processor . Body . Instructions [ processor . Body . Instructions . Count - 1 ] . OpCode != OpCodes . Ret )
344+ if ( processor . Body . Instructions . Count == 0 || processor . Body . Instructions [ processor . Body . Instructions . Count - 1 ] . OpCode != OpCodes . Ret )
350345 {
351346 processor . Emit ( OpCodes . Ret ) ;
352347 }
@@ -392,22 +387,21 @@ MethodDefinition CreateMethod(string name, MethodDefinition copied = null)
392387 }
393388 }
394389
395-
396390 /// <summary>
397391 /// Creates an 'NetworkInitialize' method which is called by the childmost class to initialize scripts on Awake.
398392 /// </summary>
399- private void CallNetworkInitializesFromNetworkInitializeIfDisabled ( MethodDefinition networkInitializeIfDisabledMd )
393+ private void CallNetworkInitialize ( MethodDefinition networkIntializeMd )
400394 {
401- ILProcessor processor = networkInitializeIfDisabledMd . Body . GetILProcessor ( ) ;
395+ ILProcessor processor = networkIntializeMd . Body . GetILProcessor ( ) ;
402396
403- networkInitializeIfDisabledMd . Body . Instructions . Clear ( ) ;
397+ networkIntializeMd . Body . Instructions . Clear ( ) ;
404398 CallMethod ( NETWORKINITIALIZE_EARLY_INTERNAL_NAME ) ;
405399 CallMethod ( NETWORKINITIALIZE_LATE_INTERNAL_NAME ) ;
406400 processor . Emit ( OpCodes . Ret ) ;
407401
408402 void CallMethod ( string name )
409403 {
410- MethodReference initIfDisabledMr = networkInitializeIfDisabledMd . DeclaringType . GetMethodReference ( base . Session , name ) ;
404+ MethodReference initIfDisabledMr = networkIntializeMd . DeclaringType . GetMethodReference ( base . Session , name ) ;
411405 processor . Emit ( OpCodes . Ldarg_0 ) ;
412406 processor . Emit ( initIfDisabledMr . GetCallOpCode ( base . Session ) , initIfDisabledMr ) ;
413407 }
@@ -473,8 +467,7 @@ internal void CreateFirstNetworkInitializeCall(TypeDefinition typeDef, MethodDef
473467 {
474468 created = true ;
475469
476- thisAwakeMethodDef = new ( NetworkBehaviourHelper . AWAKE_METHOD_NAME , MethodDefinitionExtensions . PUBLIC_VIRTUAL_ATTRIBUTES ,
477- typeDef . Module . TypeSystem . Void ) ;
470+ thisAwakeMethodDef = new ( NetworkBehaviourHelper . AWAKE_METHOD_NAME , MethodDefinitionExtensions . PUBLIC_VIRTUAL_ATTRIBUTES , typeDef . Module . TypeSystem . Void ) ;
478471 thisAwakeMethodDef . Body . InitLocals = true ;
479472 typeDef . Methods . Add ( thisAwakeMethodDef ) ;
480473
@@ -500,14 +493,11 @@ internal void CreateFirstNetworkInitializeCall(TypeDefinition typeDef, MethodDef
500493 if ( created && firstUserAwakeMethodDef != null )
501494 {
502495 MethodReference baseAwakeMethodRef = typeDef . Module . ImportReference ( firstUserAwakeMethodDef ) ;
503- instructions . Add ( processor . Create ( OpCodes . Ldarg_0 ) ) ; //this.
496+ instructions . Add ( processor . Create ( OpCodes . Ldarg_0 ) ) ; //this.
504497 instructions . Add ( processor . Create ( OpCodes . Call , baseAwakeMethodRef ) ) ;
505498 }
506499
507500 processor . InsertFirst ( instructions ) ;
508501 }
509-
510-
511-
512502 }
513503}
0 commit comments