@@ -609,6 +609,7 @@ private void CreateNetworkVariableTypeInitializers(AssemblyDefinition assembly,
609609 private const string k_NetworkVariableBase_Initialize = nameof ( NetworkVariableBase . Initialize ) ;
610610
611611 private const string k_RpcAttribute_Delivery = nameof ( RpcAttribute . Delivery ) ;
612+ private const string k_RpcAttribute_InvokePermission = nameof ( RpcAttribute . InvokePermission ) ;
612613 private const string k_ServerRpcAttribute_RequireOwnership = nameof ( ServerRpcAttribute . RequireOwnership ) ;
613614 private const string k_RpcParams_Server = nameof ( __RpcParams . Server ) ;
614615 private const string k_RpcParams_Client = nameof ( __RpcParams . Client ) ;
@@ -1311,7 +1312,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
13111312 return ;
13121313 }
13131314 }
1314- var rpcHandlers = new List < ( uint RpcMethodId , MethodDefinition RpcHandler , string RpcMethodName ) > ( ) ;
1315+ var rpcHandlers = new List < ( uint RpcMethodId , MethodDefinition RpcHandler , string RpcMethodName , CustomAttribute rpcAttribute ) > ( ) ;
13151316
13161317 bool isEditorOrDevelopment = assemblyDefines . Contains ( "UNITY_EDITOR" ) || assemblyDefines . Contains ( "DEVELOPMENT_BUILD" ) ;
13171318
@@ -1342,7 +1343,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
13421343
13431344 InjectWriteAndCallBlocks ( methodDefinition , rpcAttribute , rpcMethodId ) ;
13441345
1345- rpcHandlers . Add ( ( rpcMethodId , GenerateStaticHandler ( methodDefinition , rpcAttribute , rpcMethodId ) , methodDefinition . Name ) ) ;
1346+ rpcHandlers . Add ( ( rpcMethodId , GenerateStaticHandler ( methodDefinition , rpcAttribute , rpcMethodId ) , methodDefinition . Name , rpcAttribute ) ) ;
13461347 }
13471348
13481349 GenerateVariableInitialization ( typeDefinition ) ;
@@ -1424,7 +1425,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
14241425 var instructions = new List < Instruction > ( ) ;
14251426 var processor = initializeRpcsMethodDef . Body . GetILProcessor ( ) ;
14261427
1427- foreach ( var ( rpcMethodId , rpcHandler , rpcMethodName ) in rpcHandlers )
1428+ foreach ( var ( rpcMethodId , rpcHandler , rpcMethodName , rpcAttribute ) in rpcHandlers )
14281429 {
14291430 typeDefinition . Methods . Add ( rpcHandler ) ;
14301431
@@ -1439,12 +1440,25 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
14391440 callMethod = callMethod . MakeGeneric ( genericTypes . ToArray ( ) ) ;
14401441 }
14411442
1443+ RpcInvokePermission invokePermission = RpcInvokePermission . Anyone ;
1444+
1445+ foreach ( var attrField in rpcAttribute . Fields )
1446+ {
1447+ switch ( attrField . Name )
1448+ {
1449+ case k_RpcAttribute_InvokePermission :
1450+ invokePermission = ( RpcInvokePermission ) attrField . Argument . Value ;
1451+ break ;
1452+ }
1453+ }
1454+
14421455 // __registerRpc(RpcMethodId, HandleFunc, methodName);
14431456 instructions . Add ( processor . Create ( OpCodes . Ldarg_0 ) ) ;
14441457 instructions . Add ( processor . Create ( OpCodes . Ldc_I4 , unchecked ( ( int ) rpcMethodId ) ) ) ;
14451458 instructions . Add ( processor . Create ( OpCodes . Ldnull ) ) ;
14461459 instructions . Add ( processor . Create ( OpCodes . Ldftn , callMethod ) ) ;
14471460 instructions . Add ( processor . Create ( OpCodes . Newobj , m_NetworkHandlerDelegateCtor_MethodRef ) ) ;
1461+ instructions . Add ( processor . Create ( OpCodes . Ldc_I4 , ( int ) invokePermission ) ) ;
14481462 instructions . Add ( processor . Create ( OpCodes . Ldstr , rpcMethodName ) ) ;
14491463 instructions . Add ( processor . Create ( OpCodes . Call , m_NetworkBehaviour___registerRpc_MethodRef ) ) ;
14501464 }
@@ -2851,13 +2865,13 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
28512865 var isServerRpc = rpcAttribute. AttributeType. FullName == CodeGenHelpers. ServerRpcAttribute_FullName;
28522866 var isCientRpc = rpcAttribute. AttributeType. FullName == CodeGenHelpers. ClientRpcAttribute_FullName;
28532867 var isGenericRpc = rpcAttribute. AttributeType. FullName == CodeGenHelpers. RpcAttribute_FullName;
2854- var requireOwnership = true ; // default value MUST be == `ServerRpcAttribute.RequireOwnership`
2868+ var invokePermission = RpcInvokePermission . Anyone ; // default value MUST be == `ServerRpcAttribute.RequireOwnership`
28552869 foreach ( var attrField in rpcAttribute. Fields)
28562870 {
28572871 switch ( attrField. Name)
28582872 {
28592873 case k_ServerRpcAttribute_RequireOwnership:
2860- requireOwnership = attrField. Argument. Type == typeSystem. Boolean && ( bool ) attrField. Argument. Value;
2874+ invokePermission = ( attrField. Argument. Type == typeSystem. Boolean && ( bool ) attrField. Argument. Value) ? RpcInvokePermission . Owner : RpcInvokePermission . Anyone ;
28612875 break ;
28622876 }
28632877 }
@@ -2887,7 +2901,7 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
28872901 processor. Append( lastInstr) ;
28882902 }
28892903
2890- if ( isServerRpc && requireOwnership )
2904+ if ( isServerRpc && invokePermission == RpcInvokePermission . Owner )
28912905 {
28922906 var roReturnInstr = processor. Create( OpCodes. Ret) ;
28932907 var roLastInstr = processor. Create( OpCodes. Nop) ;
@@ -2921,6 +2935,42 @@ private MethodDefinition GenerateStaticHandler(MethodDefinition methodDefinition
29212935
29222936 processor. Append( logNextInstr) ;
29232937
2938+ processor. Append( roReturnInstr) ;
2939+ processor. Append( roLastInstr) ;
2940+ } else if ( invokePermission == RpcInvokePermission. Server)
2941+ {
2942+ var roReturnInstr = processor. Create( OpCodes. Ret) ;
2943+ var roLastInstr = processor. Create( OpCodes. Nop) ;
2944+
2945+ // if (rpcParams.Server.Receive.SenderClientId != NetworkManager.IsServer) { ... } return;
2946+ processor. Emit( OpCodes. Ldarg_2) ;
2947+ processor. Emit( OpCodes. Ldfld, m_RpcParams_Server_FieldRef) ;
2948+ processor. Emit( OpCodes. Ldfld, m_ServerRpcParams_Receive_FieldRef) ;
2949+ processor. Emit( OpCodes. Ldfld, m_ServerRpcParams_Receive_SenderClientId_FieldRef) ;
2950+ processor. Emit( OpCodes. Ldarg_0) ;
2951+ processor. Emit( OpCodes. Call, m_NetworkManager_getIsServer_MethodRef) ;
2952+ processor. Emit( OpCodes. Ceq) ;
2953+ processor. Emit( OpCodes. Ldc_I4, 0 ) ;
2954+ processor. Emit( OpCodes. Ceq) ;
2955+ processor. Emit( OpCodes. Brfalse, roLastInstr) ;
2956+
2957+ var logNextInstr = processor. Create( OpCodes. Nop) ;
2958+
2959+ // if (LogLevel.Normal > networkManager.LogLevel)
2960+ processor. Emit( OpCodes. Ldloc, netManLocIdx) ;
2961+ processor. Emit( OpCodes. Ldfld, m_NetworkManager_LogLevel_FieldRef) ;
2962+ processor. Emit( OpCodes. Ldc_I4, ( int ) LogLevel. Normal) ;
2963+ processor. Emit( OpCodes. Cgt) ;
2964+ processor. Emit( OpCodes. Ldc_I4, 0 ) ;
2965+ processor. Emit( OpCodes. Ceq) ;
2966+ processor. Emit( OpCodes. Brfalse, logNextInstr) ;
2967+
2968+ // Debug.LogError(...);
2969+ processor. Emit( OpCodes. Ldstr, "Only the server can invoke an Rpc with RpcInvokePermission. Server! ") ;
2970+ processor. Emit( OpCodes. Call, m_Debug_LogError_MethodRef) ;
2971+
2972+ processor. Append( logNextInstr) ;
2973+
29242974 processor. Append( roReturnInstr) ;
29252975 processor. Append( roLastInstr) ;
29262976 }
0 commit comments