Skip to content

Commit

Permalink
fix: fixing owner not being excluded when host
Browse files Browse the repository at this point in the history
  • Loading branch information
James-Frowen committed Dec 20, 2023
1 parent d5eb2ce commit 787e55a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 8 additions & 3 deletions Assets/Mirage/Runtime/RemoteCalls/ClientRpcSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private static void Validate(NetworkBehaviour behaviour, int index)
/// <param name="target"></param>
/// <param name="player">player used for RpcTarget.Player</param>
/// <returns></returns>
public static bool ShouldInvokeLocally(NetworkBehaviour behaviour, RpcTarget target, INetworkPlayer player)
public static bool ShouldInvokeLocally(NetworkBehaviour behaviour, RpcTarget target, INetworkPlayer player, bool excludeOwner)
{
// not server? error
if (!behaviour.IsServer)
Expand All @@ -117,7 +117,7 @@ public static bool ShouldInvokeLocally(NetworkBehaviour behaviour, RpcTarget tar
switch (target)
{
case RpcTarget.Observers:
return IsLocalPlayerObserver(behaviour);
return IsLocalPlayerObserver(behaviour, excludeOwner);
case RpcTarget.Owner:
return IsLocalPlayerTarget(behaviour, behaviour.Owner);
case RpcTarget.Player:
Expand All @@ -134,9 +134,14 @@ public static bool ShouldInvokeLocally(NetworkBehaviour behaviour, RpcTarget tar
/// </summary>
/// <param name="player"></param>
/// <returns></returns>
public static bool IsLocalPlayerObserver(NetworkBehaviour behaviour)
public static bool IsLocalPlayerObserver(NetworkBehaviour behaviour, bool excludeOwner)
{
var local = behaviour.Server.LocalPlayer;

// if local player is the owner, skip
if (excludeOwner && behaviour.Owner == local)
return false;

return behaviour.Identity.observers.Contains(local);
}

Expand Down
11 changes: 6 additions & 5 deletions Assets/Mirage/Weaver/Processors/ClientRpcProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private MethodDefinition GenerateStub(MethodDefinition md, CustomAttribute clien
// {
// call the body
// }
CallBody(worker, rpc, target);
CallBody(worker, rpc, target, excludeOwner);

// NetworkWriter writer = NetworkWriterPool.GetWriter()
var writer = md.AddLocal<PooledNetworkWriter>();
Expand Down Expand Up @@ -154,7 +154,7 @@ private static MethodReference GetSendMethod(MethodDefinition md, RpcTarget targ



private void InvokeLocally(ILProcessor worker, RpcTarget target, Action body)
private void InvokeLocally(ILProcessor worker, RpcTarget target, bool excludeOwner, Action body)
{
// if (IsLocalClient) {
var endif = worker.Create(OpCodes.Nop);
Expand All @@ -170,8 +170,9 @@ private void InvokeLocally(ILProcessor worker, RpcTarget target, Action body)
else
worker.Append(worker.Create(OpCodes.Ldnull));

worker.Append(worker.Create(excludeOwner.OpCode_Ldc()));
// call function
worker.Append(worker.Create(OpCodes.Call, () => ClientRpcSender.ShouldInvokeLocally(default, default, default)));
worker.Append(worker.Create(OpCodes.Call, () => ClientRpcSender.ShouldInvokeLocally(default, default, default, default)));
worker.Append(worker.Create(OpCodes.Brfalse, endif));

body();
Expand All @@ -181,9 +182,9 @@ private void InvokeLocally(ILProcessor worker, RpcTarget target, Action body)

}

private void CallBody(ILProcessor worker, MethodDefinition rpc, RpcTarget target)
private void CallBody(ILProcessor worker, MethodDefinition rpc, RpcTarget target, bool excludeOwner)
{
InvokeLocally(worker, target, () =>
InvokeLocally(worker, target, excludeOwner, () =>
{
InvokeBody(worker, rpc);
// if target is owner or player we can return after invoking locally
Expand Down

0 comments on commit 787e55a

Please sign in to comment.