Skip to content

Commit 82a80db

Browse files
committed
Restore and deprecate RequireOwnership in favor of RpcInvokePermission
1 parent 553f805 commit 82a80db

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

com.unity.netcode.gameobjects/Editor/CodeGen/NetworkBehaviourILPP.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
14411441
}
14421442

14431443
var invokePermission = RpcInvokePermission.Anyone;
1444+
14441445
foreach (var attrField in rpcAttribute.Fields)
14451446
{
14461447
switch (attrField.Name)
@@ -1537,6 +1538,7 @@ private void ProcessNetworkBehaviour(TypeDefinition typeDefinition, string[] ass
15371538
private CustomAttribute CheckAndGetRpcAttribute(MethodDefinition methodDefinition)
15381539
{
15391540
CustomAttribute rpcAttribute = null;
1541+
15401542
foreach (var customAttribute in methodDefinition.CustomAttributes)
15411543
{
15421544
var customAttributeType_FullName = customAttribute.AttributeType.FullName;
@@ -1620,6 +1622,30 @@ private CustomAttribute CheckAndGetRpcAttribute(MethodDefinition methodDefinitio
16201622

16211623
return null;
16221624
}
1625+
1626+
bool hasRequireOwnership = false, hasInvokePermission = false;
1627+
1628+
foreach (var argument in rpcAttribute.Fields)
1629+
{
1630+
switch (argument.Name)
1631+
{
1632+
case k_ServerRpcAttribute_RequireOwnership:
1633+
hasRequireOwnership = true;
1634+
break;
1635+
case k_RpcAttribute_InvokePermission:
1636+
hasInvokePermission = true;
1637+
break;
1638+
default:
1639+
break;
1640+
}
1641+
}
1642+
1643+
if (hasRequireOwnership && hasInvokePermission)
1644+
{
1645+
m_Diagnostics.AddError("Rpc attribute cannot declare both RequireOwnership and InvokePermission!");
1646+
return null;
1647+
}
1648+
16231649
// Checks for IsSerializable are moved to later as the check is now done by dynamically seeing if any valid
16241650
// serializer OR extension method exists for it.
16251651
return rpcAttribute;
@@ -2366,6 +2392,7 @@ private void InjectWriteAndCallBlocks(MethodDefinition methodDefinition, CustomA
23662392
m_Diagnostics.AddError($"{nameof(RpcAttribute)} contains field {field} which is not present in {nameof(RpcAttribute.RpcAttributeParams)}.");
23672393
}
23682394
}
2395+
23692396
instructions.Add(processor.Create(OpCodes.Ldloc, rpcAttributeParamsIdx));
23702397

23712398
// defaultTarget

com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,14 @@ internal FastBufferWriter __beginSendRpc(uint rpcMethodId, RpcParams rpcParams,
330330
{
331331
throw new RpcException("The NetworkBehaviour must be spawned before calling this method.");
332332
}
333-
if (attributeParams.InvokePermission == RpcInvokePermission.Owner && !IsOwner)
334-
{
335-
throw new RpcException("This RPC can only be sent by its owner.");
336-
} else if (attributeParams.InvokePermission == RpcInvokePermission.Server && !IsServer)
333+
else if (attributeParams.InvokePermission == RpcInvokePermission.Server && !IsServer)
337334
{
338335
throw new RpcException("This RPC can only be sent by the server.");
339336
}
337+
else if ((attributeParams.RequireOwnership || attributeParams.InvokePermission == RpcInvokePermission.Owner) && !IsOwner)
338+
{
339+
throw new RpcException("This RPC can only be sent by its owner.");
340+
}
340341
return new FastBufferWriter(k_RpcMessageDefaultSize, Allocator.Temp, k_RpcMessageMaximumSize);
341342
}
342343

com.unity.netcode.gameobjects/Runtime/Messaging/RpcAttributes.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public struct RpcAttributeParams
6767
/// When true, allows the RPC target to be overridden at runtime
6868
/// </summary>
6969
public bool AllowTargetOverride;
70+
71+
public bool RequireOwnership;
7072
}
7173

7274
// Must match the fields in RemoteAttributeParams
@@ -80,6 +82,15 @@ public struct RpcAttributeParams
8082
/// </summary>
8183
public RpcInvokePermission InvokePermission;
8284

85+
/// <summary>
86+
/// When true, only the owner of the object can execute this RPC
87+
/// </summary>
88+
/// <remarks>
89+
/// Deprecated in favor of <see cref="RpcInvokePermission"/>.
90+
/// </remarks>
91+
[Obsolete]
92+
public bool RequireOwnership;
93+
8394
/// <summary>
8495
/// When true, local execution of the RPC is deferred until the next network tick
8596
/// </summary>
@@ -116,7 +127,7 @@ public class ServerRpcAttribute : RpcAttribute
116127
/// When true, only the owner of the NetworkObject can invoke this ServerRpc.
117128
/// This property overrides the base RpcAttribute.RequireOwnership.
118129
/// </summary>
119-
public bool RequireOwnership;
130+
public new bool RequireOwnership;
120131

121132
/// <summary>
122133
/// Initializes a new instance of ServerRpcAttribute that targets the server
@@ -139,7 +150,7 @@ public class ClientRpcAttribute : RpcAttribute
139150
/// </summary>
140151
public ClientRpcAttribute() : base(SendTo.NotServer)
141152
{
142-
153+
InvokePermission = RpcInvokePermission.Server;
143154
}
144155
}
145156
}

0 commit comments

Comments
 (0)