3
3
using System . Linq ;
4
4
using System . Collections . Generic ;
5
5
using System . Reflection ;
6
+ using MLAPI . Logging ;
6
7
using MLAPI . Messaging ;
7
8
using MLAPI . Serialization ;
8
9
using Mono . Cecil ;
@@ -74,12 +75,14 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
74
75
return new ILPostProcessResult ( new InMemoryAssembly ( pe . ToArray ( ) , pdb . ToArray ( ) ) , m_Diagnostics ) ;
75
76
}
76
77
78
+ private MethodReference Debug_LogWarning_MethodRef ;
77
79
private TypeReference NetworkManager_TypeRef ;
78
80
private MethodReference NetworkManager_getLocalClientId_MethodRef ;
79
81
private MethodReference NetworkManager_getIsListening_MethodRef ;
80
82
private MethodReference NetworkManager_getIsHost_MethodRef ;
81
83
private MethodReference NetworkManager_getIsServer_MethodRef ;
82
84
private MethodReference NetworkManager_getIsClient_MethodRef ;
85
+ private FieldReference NetworkManager_LogLevel_FieldRef ;
83
86
private FieldReference NetworkManager_ntable_FieldRef ;
84
87
private MethodReference NetworkManager_ntable_Add_MethodRef ;
85
88
private TypeReference NetworkBehaviour_TypeRef ;
@@ -142,11 +145,13 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
142
145
private MethodReference NetworkSerializer_SerializeRayArray_MethodRef ;
143
146
private MethodReference NetworkSerializer_SerializeRay2DArray_MethodRef ;
144
147
148
+ private const string k_Debug_LogWarning = nameof ( Debug . LogWarning ) ;
145
149
private const string k_NetworkManager_LocalClientId = nameof ( NetworkManager . LocalClientId ) ;
146
150
private const string k_NetworkManager_IsListening = nameof ( NetworkManager . IsListening ) ;
147
151
private const string k_NetworkManager_IsHost = nameof ( NetworkManager . IsHost ) ;
148
152
private const string k_NetworkManager_IsServer = nameof ( NetworkManager . IsServer ) ;
149
153
private const string k_NetworkManager_IsClient = nameof ( NetworkManager . IsClient ) ;
154
+ private const string k_NetworkManager_LogLevel = nameof ( NetworkManager . LogLevel ) ;
150
155
#pragma warning disable 618
151
156
private const string k_NetworkManager_ntable = nameof ( NetworkManager . __ntable ) ;
152
157
@@ -170,6 +175,17 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
170
175
171
176
private bool ImportReferences ( ModuleDefinition moduleDefinition )
172
177
{
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
+
173
189
var networkManagerType = typeof ( NetworkManager ) ;
174
190
NetworkManager_TypeRef = moduleDefinition . ImportReference ( networkManagerType ) ;
175
191
foreach ( var propertyInfo in networkManagerType . GetProperties ( ) )
@@ -198,6 +214,9 @@ private bool ImportReferences(ModuleDefinition moduleDefinition)
198
214
{
199
215
switch ( fieldInfo . Name )
200
216
{
217
+ case k_NetworkManager_LogLevel :
218
+ NetworkManager_LogLevel_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
219
+ break ;
201
220
case k_NetworkManager_ntable :
202
221
NetworkManager_ntable_FieldRef = moduleDefinition . ImportReference ( fieldInfo ) ;
203
222
NetworkManager_ntable_Add_MethodRef = moduleDefinition . ImportReference ( fieldInfo . FieldType . GetMethod ( "Add" ) ) ;
@@ -598,7 +617,7 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA
598
617
var roReturnInstr = processor . Create ( OpCodes . Ret ) ;
599
618
var roLastInstr = processor . Create ( OpCodes . Nop ) ;
600
619
601
- // if (this.OwnerClientId != networkManager.LocalClientId) return;
620
+ // if (this.OwnerClientId != networkManager.LocalClientId) { ... } return;
602
621
instructions . Add ( processor . Create ( OpCodes . Ldarg_0 ) ) ;
603
622
instructions . Add ( processor . Create ( OpCodes . Call , NetworkBehaviour_getOwnerClientId_MethodRef ) ) ;
604
623
instructions . Add ( processor . Create ( OpCodes . Ldloc , netManLocIdx ) ) ;
@@ -608,6 +627,23 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA
608
627
instructions . Add ( processor . Create ( OpCodes . Ceq ) ) ;
609
628
instructions . Add ( processor . Create ( OpCodes . Brfalse , roLastInstr ) ) ;
610
629
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
+
611
647
instructions . Add ( roReturnInstr ) ;
612
648
instructions . Add ( roLastInstr ) ;
613
649
}
@@ -1543,13 +1579,36 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
1543
1579
}
1544
1580
1545
1581
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
+ }
1546
1605
1547
1606
if ( isServerRpc && requireOwnership )
1548
1607
{
1549
1608
var roReturnInstr = processor . Create ( OpCodes . Ret ) ;
1550
1609
var roLastInstr = processor . Create ( OpCodes . Nop ) ;
1551
1610
1552
- // if (rpcParams.Server.Receive.SenderClientId != target.OwnerClientId) return;
1611
+ // if (rpcParams.Server.Receive.SenderClientId != target.OwnerClientId) { ... } return;
1553
1612
processor . Emit ( OpCodes . Ldarg_2 ) ;
1554
1613
processor . Emit ( OpCodes . Ldfld , RpcParams_Server_FieldRef ) ;
1555
1614
processor . Emit ( OpCodes . Ldfld , ServerRpcParams_Receive_FieldRef ) ;
@@ -1561,6 +1620,23 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
1561
1620
processor . Emit ( OpCodes . Ceq ) ;
1562
1621
processor . Emit ( OpCodes . Brfalse , roLastInstr ) ;
1563
1622
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
+
1564
1640
processor . Append ( roReturnInstr ) ;
1565
1641
processor . Append ( roLastInstr ) ;
1566
1642
}
0 commit comments