From 98dc229e98a4fabe63f5bc87d67c2d0e0309a3c8 Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 30 Dec 2018 18:56:43 +0100 Subject: [PATCH] NetworkIdentity.HandleRemoteCall function that is reused by HandleSyncEvent/Command/Rpc now --- Mirror/Runtime/NetworkIdentity.cs | 88 ++++++------------------------- 1 file changed, 17 insertions(+), 71 deletions(-) diff --git a/Mirror/Runtime/NetworkIdentity.cs b/Mirror/Runtime/NetworkIdentity.cs index 70c47cf93..49d430a30 100644 --- a/Mirror/Runtime/NetworkIdentity.cs +++ b/Mirror/Runtime/NetworkIdentity.cs @@ -555,27 +555,26 @@ internal void HandleClientAuthority(bool authority) ForceAuthority(authority); } - // happens on client - internal void HandleSyncEvent(int componentIndex, int cmdHash, NetworkReader reader) + // helper function to handle SyncEvent/Command/Rpc + internal void HandleRemoteCall(int componentIndex, int functionHash, UNetInvokeType invokeType, NetworkReader reader) { // this doesn't use NetworkBehaviour.InvokeSyncEvent function (anymore). this method of calling is faster. // The hash is only looked up once, insted of twice(!) per NetworkBehaviour on the object. if (gameObject == null) { - string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash); - Debug.LogWarning("SyncEvent [" + errorCmdName + "] received for deleted object [netId=" + netId + "]"); + string functionName = NetworkBehaviour.GetCmdHashHandlerName(functionHash); + Debug.LogWarning(invokeType + " [" + functionName + "] received for deleted object [netId=" + netId + "]"); return; } - // find the matching SyncEvent function and networkBehaviour class + // find the matching invoke function and NetworkBehaviour class NetworkBehaviour.CmdDelegate invokeFunction; - bool invokeFound = NetworkBehaviour.GetInvokerFunctionForHash(cmdHash, UNetInvokeType.SyncEvent, out invokeFunction); - if (!invokeFound) + if (!NetworkBehaviour.GetInvokerFunctionForHash(functionHash, invokeType, out invokeFunction)) { // We don't get a valid lookup of the command name when it doesn't exist... - string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash); - Debug.LogError("Found no receiver for incoming [" + errorCmdName + "] on " + gameObject + ", the server and client should have the same NetworkBehaviour instances [netId=" + netId + "]."); + string functionName = NetworkBehaviour.GetCmdHashHandlerName(functionHash); + Debug.LogError("Found no receiver for incoming [" + functionName + "] on " + gameObject + ", the server and client should have the same NetworkBehaviour instances [netId=" + netId + "]."); return; } @@ -586,78 +585,25 @@ internal void HandleSyncEvent(int componentIndex, int cmdHash, NetworkReader rea return; } NetworkBehaviour invokeComponent = m_NetworkBehaviours[componentIndex]; - invokeFunction(invokeComponent, reader); } + // happens on client + internal void HandleSyncEvent(int componentIndex, int eventHash, NetworkReader reader) + { + HandleRemoteCall(componentIndex, eventHash, UNetInvokeType.SyncEvent, reader); + } + // happens on server internal void HandleCommand(int componentIndex, int cmdHash, NetworkReader reader) { - // this doesn't use NetworkBehaviour.InvokeCommand function (anymore). this method of calling is faster. - // The hash is only looked up once, insted of twice(!) per NetworkBehaviour on the object. - - if (gameObject == null) - { - string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash); - Debug.LogWarning("Command [" + errorCmdName + "] received for deleted object [netId=" + netId + "]"); - return; - } - - // find the matching Command function and networkBehaviour class - NetworkBehaviour.CmdDelegate invokeFunction; - bool invokeFound = NetworkBehaviour.GetInvokerFunctionForHash(cmdHash, UNetInvokeType.Command, out invokeFunction); - if (!invokeFound) - { - // We don't get a valid lookup of the command name when it doesn't exist... - string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash); - Debug.LogError("Found no receiver for incoming [" + errorCmdName + "] on " + gameObject + ", the server and client should have the same NetworkBehaviour instances [netId=" + netId + "]."); - return; - } - - // find the right component to invoke the function on - if (componentIndex >= m_NetworkBehaviours.Length) - { - Debug.LogWarning("Component [" + componentIndex + "] not found for [netId=" + netId + "]"); - return; - } - NetworkBehaviour invokeComponent = m_NetworkBehaviours[componentIndex]; - - invokeFunction(invokeComponent, reader); + HandleRemoteCall(componentIndex, cmdHash, UNetInvokeType.Command, reader); } // happens on client - internal void HandleRPC(int componentIndex, int cmdHash, NetworkReader reader) + internal void HandleRPC(int componentIndex, int rpcHash, NetworkReader reader) { - // this doesn't use NetworkBehaviour.InvokeClientRpc function (anymore). this method of calling is faster. - // The hash is only looked up once, insted of twice(!) per NetworkBehaviour on the object. - - if (gameObject == null) - { - string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash); - Debug.LogWarning("ClientRpc [" + errorCmdName + "] received for deleted object [netId=" + netId + "]"); - return; - } - - // find the matching ClientRpc function and networkBehaviour class - NetworkBehaviour.CmdDelegate invokeFunction; - bool invokeFound = NetworkBehaviour.GetInvokerFunctionForHash(cmdHash, UNetInvokeType.ClientRpc, out invokeFunction); - if (!invokeFound) - { - // We don't get a valid lookup of the command name when it doesn't exist... - string errorCmdName = NetworkBehaviour.GetCmdHashHandlerName(cmdHash); - Debug.LogError("Found no receiver for incoming [" + errorCmdName + "] on " + gameObject + ", the server and client should have the same NetworkBehaviour instances [netId=" + netId + "]."); - return; - } - - // find the right component to invoke the function on - if (componentIndex >= m_NetworkBehaviours.Length) - { - Debug.LogWarning("Component [" + componentIndex + "] not found for [netId=" + netId + "]"); - return; - } - NetworkBehaviour invokeComponent = m_NetworkBehaviours[componentIndex]; - - invokeFunction(invokeComponent, reader); + HandleRemoteCall(componentIndex, rpcHash, UNetInvokeType.ClientRpc, reader); } // invoked by unity runtime immediately after the regular "Update()" function.