33using System . Linq ;
44using System . Collections . Generic ;
55using System . Reflection ;
6+ using MLAPI . Logging ;
67using MLAPI . Messaging ;
78using MLAPI . Serialization ;
89using Mono . Cecil ;
@@ -74,12 +75,14 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
7475 return new ILPostProcessResult ( new InMemoryAssembly ( pe . ToArray ( ) , pdb . ToArray ( ) ) , m_Diagnostics ) ;
7576 }
7677
78+ private MethodReference Debug_LogWarning_MethodRef ;
7779 private TypeReference NetworkManager_TypeRef ;
7880 private MethodReference NetworkManager_getLocalClientId_MethodRef ;
7981 private MethodReference NetworkManager_getIsListening_MethodRef ;
8082 private MethodReference NetworkManager_getIsHost_MethodRef ;
8183 private MethodReference NetworkManager_getIsServer_MethodRef ;
8284 private MethodReference NetworkManager_getIsClient_MethodRef ;
85+ private FieldReference NetworkManager_LogLevel_FieldRef ;
8386 private FieldReference NetworkManager_ntable_FieldRef ;
8487 private MethodReference NetworkManager_ntable_Add_MethodRef ;
8588 private TypeReference NetworkBehaviour_TypeRef ;
@@ -142,11 +145,13 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
142145 private MethodReference NetworkSerializer_SerializeRayArray_MethodRef ;
143146 private MethodReference NetworkSerializer_SerializeRay2DArray_MethodRef ;
144147
148+ private const string k_Debug_LogWarning = nameof ( Debug . LogWarning ) ;
145149 private const string k_NetworkManager_LocalClientId = nameof ( NetworkManager . LocalClientId ) ;
146150 private const string k_NetworkManager_IsListening = nameof ( NetworkManager . IsListening ) ;
147151 private const string k_NetworkManager_IsHost = nameof ( NetworkManager . IsHost ) ;
148152 private const string k_NetworkManager_IsServer = nameof ( NetworkManager . IsServer ) ;
149153 private const string k_NetworkManager_IsClient = nameof ( NetworkManager . IsClient ) ;
154+ private const string k_NetworkManager_LogLevel = nameof ( NetworkManager . LogLevel ) ;
150155#pragma warning disable 618
151156 private const string k_NetworkManager_ntable = nameof ( NetworkManager . __ntable ) ;
152157
@@ -170,6 +175,17 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
170175
171176 private bool ImportReferences ( ModuleDefinition moduleDefinition )
172177 {
178+ var debugType = typeof ( Debug ) ;
179+ foreach ( var methodInfo in debugType . GetMethods ( ) )
180+ {
181+ switch ( methodInfo . Name )
182+ {
183+ case k_Debug_LogWarning :
184+ if ( methodInfo . GetParameters ( ) . Length == 1 ) Debug_LogWarning_MethodRef = moduleDefinition . ImportReference ( methodInfo ) ;
185+ break ;
186+ }
187+ }
188+
173189 var networkManagerType = typeof ( NetworkManager ) ;
174190 NetworkManager_TypeRef = moduleDefinition . ImportReference ( networkManagerType ) ;
175191 foreach ( var propertyInfo in networkManagerType . GetProperties ( ) )
@@ -198,6 +214,9 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
198214 {
199215 switch ( fieldInfo . Name )
200216 {
217+ case k_NetworkManager_LogLevel :
218+ NetworkManager_LogLevel_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
219+ break ;
201220 case k_NetworkManager_ntable :
202221 NetworkManager_ntable_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
203222 NetworkManager_ntable_Add_MethodRef = moduleDefinition . ImportReference ( fieldInfo . FieldType . GetMethod ( "Add" ) ) ;
@@ -598,7 +617,7 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA
598617 var roReturnInstr = processor . Create ( OpCodes . Ret ) ;
599618 var roLastInstr = processor . Create ( OpCodes . Nop ) ;
600619
601- // if (this.OwnerClientId != networkManager.LocalClientId) return;
620+ // if (this.OwnerClientId != networkManager.LocalClientId) { ... } return;
602621 instructions . Add ( processor . Create ( OpCodes . Ldarg_0 ) ) ;
603622 instructions . Add ( processor . Create ( OpCodes . Call , NetworkBehaviour_getOwnerClientId_MethodRef ) ) ;
604623 instructions . Add ( processor . Create ( OpCodes . Ldloc , netManLocIdx ) ) ;
@@ -608,6 +627,23 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA
608627 instructions . Add ( processor . Create ( OpCodes . Ceq ) ) ;
609628 instructions . Add ( processor . Create ( OpCodes . Brfalse , roLastInstr ) ) ;
610629
630+ var logNextInstr = processor . Create ( OpCodes . Nop ) ;
631+
632+ // if (LogLevel.Normal > networkManager.LogLevel)
633+ instructions . Add ( processor . Create ( OpCodes . Ldloc , netManLocIdx ) ) ;
634+ instructions . Add ( processor . Create ( OpCodes . Ldfld , NetworkManager_LogLevel_FieldRef ) ) ;
635+ instructions . Add ( processor . Create ( OpCodes . Ldc_I4 , ( int ) LogLevel . Normal ) ) ;
636+ instructions . Add ( processor . Create ( OpCodes . Cgt ) ) ;
637+ instructions . Add ( processor . Create ( OpCodes . Ldc_I4 , 0 ) ) ;
638+ instructions . Add ( processor . Create ( OpCodes . Ceq ) ) ;
639+ instructions . Add ( processor . Create ( OpCodes . Brfalse , logNextInstr ) ) ;
640+
641+ // Debug.LogWarning(...);
642+ instructions . Add ( processor . Create ( OpCodes . Ldstr , "Only the owner can invoke a ServerRpc that requires ownership!" ) ) ;
643+ instructions . Add ( processor . Create ( OpCodes . Call , Debug_LogWarning_MethodRef ) ) ;
644+
645+ instructions . Add ( logNextInstr ) ;
646+
611647 instructions . Add ( roReturnInstr ) ;
612648 instructions . Add ( roLastInstr ) ;
613649 }
@@ -1543,13 +1579,36 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
15431579 }
15441580
15451581 nhandler . Body . InitLocals = true ;
1582+ // NetworkManager networkManager;
1583+ nhandler . Body . Variables . Add ( new VariableDefinition ( NetworkManager_TypeRef ) ) ;
1584+ int netManLocIdx = nhandler . Body . Variables . Count - 1 ;
1585+
1586+ {
1587+ var returnInstr = processor . Create ( OpCodes . Ret ) ;
1588+ var lastInstr = processor . Create ( OpCodes . Nop ) ;
1589+
1590+ // networkManager = this.NetworkManager;
1591+ processor . Emit ( OpCodes . Ldarg_0 ) ;
1592+ processor . Emit ( OpCodes . Call , NetworkBehaviour_getNetworkManager_MethodRef ) ;
1593+ processor . Emit ( OpCodes . Stloc , netManLocIdx ) ;
1594+
1595+ // if (networkManager == null || !networkManager.IsListening) return;
1596+ processor . Emit ( OpCodes . Ldloc , netManLocIdx ) ;
1597+ processor . Emit ( OpCodes . Brfalse , returnInstr ) ;
1598+ processor . Emit ( OpCodes . Ldloc , netManLocIdx ) ;
1599+ processor . Emit ( OpCodes . Callvirt , NetworkManager_getIsListening_MethodRef ) ;
1600+ processor . Emit ( OpCodes . Brtrue , lastInstr ) ;
1601+
1602+ processor . Append ( returnInstr ) ;
1603+ processor . Append ( lastInstr ) ;
1604+ }
15461605
15471606 if ( isServerRpc && requireOwnership )
15481607 {
15491608 var roReturnInstr = processor . Create ( OpCodes . Ret ) ;
15501609 var roLastInstr = processor . Create ( OpCodes . Nop ) ;
15511610
1552- // if (rpcParams.Server.Receive.SenderClientId != target.OwnerClientId) return;
1611+ // if (rpcParams.Server.Receive.SenderClientId != target.OwnerClientId) { ... } return;
15531612 processor . Emit ( OpCodes . Ldarg_2 ) ;
15541613 processor . Emit ( OpCodes . Ldfld , RpcParams_Server_FieldRef ) ;
15551614 processor . Emit ( OpCodes . Ldfld , ServerRpcParams_Receive_FieldRef ) ;
@@ -1561,6 +1620,23 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
15611620 processor . Emit ( OpCodes . Ceq ) ;
15621621 processor . Emit ( OpCodes . Brfalse , roLastInstr ) ;
15631622
1623+ var logNextInstr = processor . Create ( OpCodes . Nop ) ;
1624+
1625+ // if (LogLevel.Normal > networkManager.LogLevel)
1626+ processor . Emit ( OpCodes . Ldloc , netManLocIdx ) ;
1627+ processor . Emit ( OpCodes . Ldfld , NetworkManager_LogLevel_FieldRef ) ;
1628+ processor . Emit ( OpCodes . Ldc_I4 , ( int ) LogLevel . Normal ) ;
1629+ processor . Emit ( OpCodes . Cgt ) ;
1630+ processor . Emit ( OpCodes . Ldc_I4 , 0 ) ;
1631+ processor . Emit ( OpCodes . Ceq ) ;
1632+ processor . Emit ( OpCodes . Brfalse , logNextInstr ) ;
1633+
1634+ // Debug.LogWarning(...);
1635+ processor . Emit ( OpCodes . Ldstr , "Only the owner can invoke a ServerRpc that requires ownership!" ) ;
1636+ processor . Emit ( OpCodes . Call , Debug_LogWarning_MethodRef ) ;
1637+
1638+ processor . Append ( logNextInstr ) ;
1639+
15641640 processor . Append ( roReturnInstr ) ;
15651641 processor . Append ( roLastInstr ) ;
15661642 }
0 commit comments