From e09234e55ce5aacea144962b6e7545cd73565a35 Mon Sep 17 00:00:00 2001 From: "Marcus.Silva" Date: Thu, 9 Mar 2023 18:59:02 -0300 Subject: [PATCH] refactor(NetworkPrefabHandler): reduce effort cognitive for understand this. - Reduce nested if. - Reduce complexity cyclomatic. - Add Guard clause. resume: simplify the class. --- .../Runtime/Spawning/NetworkPrefabHandler.cs | 113 ++++++++---------- 1 file changed, 52 insertions(+), 61 deletions(-) diff --git a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkPrefabHandler.cs b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkPrefabHandler.cs index a123dc2170..cabcb29287 100644 --- a/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkPrefabHandler.cs +++ b/com.unity.netcode.gameobjects/Runtime/Spawning/NetworkPrefabHandler.cs @@ -90,13 +90,13 @@ public bool AddHandler(NetworkObject prefabAssetNetworkObject, INetworkPrefabIns /// public bool AddHandler(uint globalObjectIdHash, INetworkPrefabInstanceHandler instanceHandler) { - if (!m_PrefabAssetToPrefabHandler.ContainsKey(globalObjectIdHash)) + if (m_PrefabAssetToPrefabHandler.ContainsKey(globalObjectIdHash)) { - m_PrefabAssetToPrefabHandler.Add(globalObjectIdHash, instanceHandler); - return true; + return false; } - return false; + m_PrefabAssetToPrefabHandler.Add(globalObjectIdHash, instanceHandler); + return true; } /// @@ -108,47 +108,38 @@ public bool AddHandler(uint globalObjectIdHash, INetworkPrefabInstanceHandler in /// one or more NetworkPrefabs could be used to override the source NetworkPrefab public void RegisterHostGlobalObjectIdHashValues(GameObject sourceNetworkPrefab, List networkPrefabOverrides) { - if (NetworkManager.Singleton.IsListening) + if (!NetworkManager.Singleton.IsListening) { - if (NetworkManager.Singleton.IsHost) + throw new System.Exception($"You can only call {nameof(RegisterHostGlobalObjectIdHashValues)} once NetworkManager is listening!"); + } + + if (!NetworkManager.Singleton.IsHost) + { + throw new System.Exception($"You should only call {nameof(RegisterHostGlobalObjectIdHashValues)} as a Host!"); + } + + var sourceNetworkObject = sourceNetworkPrefab.GetComponent(); + + if (sourceNetworkPrefab == null) + { + throw new System.Exception($"{sourceNetworkPrefab.name} does not have a {nameof(NetworkObject)} component!"); + } + + var sourceGlobalObjectIdHash = sourceNetworkObject.GlobalObjectIdHash; + // Now we register all + foreach (var gameObject in networkPrefabOverrides) + { + if (!gameObject.TryGetComponent(out var targetNetworkObject)) { - var sourceNetworkObject = sourceNetworkPrefab.GetComponent(); - if (sourceNetworkPrefab != null) - { - var sourceGlobalObjectIdHash = sourceNetworkObject.GlobalObjectIdHash; - // Now we register all - foreach (var gameObject in networkPrefabOverrides) - { - if (gameObject.TryGetComponent(out var targetNetworkObject)) - { - if (!m_PrefabInstanceToPrefabAsset.ContainsKey(targetNetworkObject.GlobalObjectIdHash)) - { - m_PrefabInstanceToPrefabAsset.Add(targetNetworkObject.GlobalObjectIdHash, sourceGlobalObjectIdHash); - } - else - { - Debug.LogWarning($"{targetNetworkObject.name} appears to be a duplicate entry!"); - } - } - else - { - throw new System.Exception($"{targetNetworkObject.name} does not have a {nameof(NetworkObject)} component!"); - } - } - } - else - { - throw new System.Exception($"{sourceNetworkPrefab.name} does not have a {nameof(NetworkObject)} component!"); - } + throw new System.Exception($"{targetNetworkObject.name} does not have a {nameof(NetworkObject)} component!"); } - else + + if (m_PrefabInstanceToPrefabAsset.ContainsKey(targetNetworkObject.GlobalObjectIdHash)) { - throw new System.Exception($"You should only call {nameof(RegisterHostGlobalObjectIdHashValues)} as a Host!"); + Debug.LogWarning($"{targetNetworkObject.name} appears to be a duplicate entry!"); } - } - else - { - throw new System.Exception($"You can only call {nameof(RegisterHostGlobalObjectIdHashValues)} once NetworkManager is listening!"); + + m_PrefabInstanceToPrefabAsset.Add(targetNetworkObject.GlobalObjectIdHash, sourceGlobalObjectIdHash); } } @@ -179,21 +170,21 @@ public bool RemoveHandler(NetworkObject networkObject) /// true (success) or false (failure) public bool RemoveHandler(uint globalObjectIdHash) { - if (m_PrefabInstanceToPrefabAsset.ContainsValue(globalObjectIdHash)) + if (!m_PrefabInstanceToPrefabAsset.ContainsValue(globalObjectIdHash)) + { + return m_PrefabAssetToPrefabHandler.Remove(globalObjectIdHash); + } + + uint networkPrefabHashKey = 0; + foreach (var kvp in m_PrefabInstanceToPrefabAsset) { - uint networkPrefabHashKey = 0; - foreach (var kvp in m_PrefabInstanceToPrefabAsset) + if (kvp.Value == globalObjectIdHash) { - if (kvp.Value == globalObjectIdHash) - { - networkPrefabHashKey = kvp.Key; - break; - } + networkPrefabHashKey = kvp.Key; + break; } - m_PrefabInstanceToPrefabAsset.Remove(networkPrefabHashKey); } - - return m_PrefabAssetToPrefabHandler.Remove(globalObjectIdHash); + m_PrefabInstanceToPrefabAsset.Remove(networkPrefabHashKey); } /// @@ -256,21 +247,21 @@ internal uint GetSourceGlobalObjectIdHash(uint networkPrefabHash) /// internal NetworkObject HandleNetworkPrefabSpawn(uint networkPrefabAssetHash, ulong ownerClientId, Vector3 position, Quaternion rotation) { - if (m_PrefabAssetToPrefabHandler.ContainsKey(networkPrefabAssetHash)) + if (!m_PrefabAssetToPrefabHandler.ContainsKey(networkPrefabAssetHash)) { - var networkObjectInstance = m_PrefabAssetToPrefabHandler[networkPrefabAssetHash].Instantiate(ownerClientId, position, rotation); + return null; + } - //Now we must make sure this alternate PrefabAsset spawned in place of the prefab asset with the networkPrefabAssetHash (GlobalObjectIdHash) - //is registered and linked to the networkPrefabAssetHash so during the HandleNetworkPrefabDestroy process we can identify the alternate prefab asset. - if (networkObjectInstance != null && !m_PrefabInstanceToPrefabAsset.ContainsKey(networkObjectInstance.GlobalObjectIdHash)) - { - m_PrefabInstanceToPrefabAsset.Add(networkObjectInstance.GlobalObjectIdHash, networkPrefabAssetHash); - } + var networkObjectInstance = m_PrefabAssetToPrefabHandler[networkPrefabAssetHash].Instantiate(ownerClientId, position, rotation); - return networkObjectInstance; + //Now we must make sure this alternate PrefabAsset spawned in place of the prefab asset with the networkPrefabAssetHash (GlobalObjectIdHash) + //is registered and linked to the networkPrefabAssetHash so during the HandleNetworkPrefabDestroy process we can identify the alternate prefab asset. + if (networkObjectInstance != null && !m_PrefabInstanceToPrefabAsset.ContainsKey(networkObjectInstance.GlobalObjectIdHash)) + { + m_PrefabInstanceToPrefabAsset.Add(networkObjectInstance.GlobalObjectIdHash, networkPrefabAssetHash); } - return null; + return networkObjectInstance; } ///