From c27a9ad1bd1508fa16a2fb241f781bb8d91d1da5 Mon Sep 17 00:00:00 2001 From: vis2k Date: Mon, 17 Dec 2018 13:25:36 +0100 Subject: [PATCH] Removed NetworkScene, moved spawners to ClientScene --- Mirror/Runtime/ClientScene.cs | 155 ++++++++++++++++++++--- Mirror/Runtime/Mirror.Runtime.csproj | 1 - Mirror/Runtime/NetworkScene.cs | 182 --------------------------- 3 files changed, 140 insertions(+), 198 deletions(-) delete mode 100644 Mirror/Runtime/NetworkScene.cs diff --git a/Mirror/Runtime/ClientScene.cs b/Mirror/Runtime/ClientScene.cs index 876139a43..ee02bb451 100644 --- a/Mirror/Runtime/ClientScene.cs +++ b/Mirror/Runtime/ClientScene.cs @@ -14,7 +14,6 @@ public static class ClientScene static bool s_IsReady; static bool s_IsSpawnFinished; - static NetworkScene s_NetworkScene = new NetworkScene(); internal static void SetNotReady() { @@ -27,13 +26,18 @@ internal static void SetNotReady() public static bool ready { get { return s_IsReady; } } public static NetworkConnection readyConnection { get { return s_ReadyConnection; }} - public static Dictionary prefabs { get { return NetworkScene.guidToPrefab; } } + public static Dictionary prefabs = new Dictionary(); // scene id to NetworkIdentity public static Dictionary spawnableObjects; + // spawn handlers + internal static Dictionary spawnHandlers = new Dictionary(); + internal static Dictionary unspawnHandlers = new Dictionary(); + internal static void Shutdown() { - s_NetworkScene.Shutdown(); + NetworkIdentity.spawned.Clear(); + ClearSpawners(); s_PendingOwnerNetIds = new List(); spawnableObjects = null; s_ReadyConnection = null; @@ -229,46 +233,167 @@ internal static void RegisterSystemHandlers(NetworkClient client, bool localClie client.RegisterHandler(MsgType.AnimationTrigger, NetworkAnimator.OnAnimationTriggerClientMessage); } - // ------------------------ NetworkScene pass-throughs --------------------- + // spawn handlers and prefabs ////////////////////////////////////////// + internal static bool GetPrefab(Guid assetId, out GameObject prefab) + { + prefab = null; + if (assetId != Guid.Empty && prefabs.ContainsKey(assetId) && prefabs[assetId] != null) + { + prefab = prefabs[assetId]; + return true; + } + return false; + } + // this assigns the newAssetId to the prefab. This is for registering dynamically created game objects for already know assetIds. public static void RegisterPrefab(GameObject prefab, Guid newAssetId) { - NetworkScene.RegisterPrefab(prefab, newAssetId); + NetworkIdentity view = prefab.GetComponent(); + if (view) + { + view.SetDynamicAssetId(newAssetId); + + if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); } + prefabs[view.assetId] = prefab; + } + else + { + Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"); + } } public static void RegisterPrefab(GameObject prefab) { - NetworkScene.RegisterPrefab(prefab); + NetworkIdentity view = prefab.GetComponent(); + if (view) + { + if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); } + prefabs[view.assetId] = prefab; + + var uvs = prefab.GetComponentsInChildren(); + if (uvs.Length > 1) + { + Debug.LogWarning("The prefab '" + prefab.name + + "' has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object."); + } + } + else + { + Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"); + } } public static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) { - NetworkScene.RegisterPrefab(prefab, spawnHandler, unspawnHandler); + NetworkIdentity identity = prefab.GetComponent(); + if (identity == null) + { + Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"); + return; + } + + if (spawnHandler == null || unspawnHandler == null) + { + Debug.LogError("RegisterPrefab custom spawn function null for " + identity.assetId); + return; + } + + if (identity.assetId == Guid.Empty) + { + Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?"); + return; + } + + if (LogFilter.Debug) { Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); } + + spawnHandlers[identity.assetId] = spawnHandler; + unspawnHandlers[identity.assetId] = unspawnHandler; } public static void UnregisterPrefab(GameObject prefab) { - NetworkScene.UnregisterPrefab(prefab); + NetworkIdentity identity = prefab.GetComponent(); + if (identity == null) + { + Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component"); + return; + } + spawnHandlers.Remove(identity.assetId); + unspawnHandlers.Remove(identity.assetId); } public static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) { - NetworkScene.RegisterSpawnHandler(assetId, spawnHandler, unspawnHandler); + if (spawnHandler == null || unspawnHandler == null) + { + Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId); + return; + } + + if (LogFilter.Debug) { Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); } + + spawnHandlers[assetId] = spawnHandler; + unspawnHandlers[assetId] = unspawnHandler; } public static void UnregisterSpawnHandler(Guid assetId) { - NetworkScene.UnregisterSpawnHandler(assetId); + spawnHandlers.Remove(assetId); + unspawnHandlers.Remove(assetId); } public static void ClearSpawners() { - NetworkScene.ClearSpawners(); + prefabs.Clear(); + spawnHandlers.Clear(); + unspawnHandlers.Clear(); + } + + internal static bool GetSpawnHandler(Guid assetId, out SpawnDelegate handler) + { + if (spawnHandlers.ContainsKey(assetId)) + { + handler = spawnHandlers[assetId]; + return true; + } + handler = null; + return false; + } + + internal static bool InvokeUnSpawnHandler(Guid assetId, GameObject obj) + { + if (unspawnHandlers.ContainsKey(assetId) && unspawnHandlers[assetId] != null) + { + UnSpawnDelegate handler = unspawnHandlers[assetId]; + handler(obj); + return true; + } + return false; } public static void DestroyAllClientObjects() { - s_NetworkScene.DestroyAllClientObjects(); + foreach (var netId in NetworkIdentity.spawned.Keys) + { + NetworkIdentity uv = NetworkIdentity.spawned[netId]; + + if (uv != null && uv.gameObject != null) + { + if (!InvokeUnSpawnHandler(uv.assetId, uv.gameObject)) + { + if (uv.sceneId == 0) + { + Object.Destroy(uv.gameObject); + } + else + { + uv.MarkForReset(); + uv.gameObject.SetActive(false); + } + } + } + } + NetworkIdentity.spawned.Clear(); } public static GameObject FindLocalObject(uint netId) @@ -328,7 +453,7 @@ static void OnSpawnPrefab(NetworkMessage netMsg) GameObject prefab; SpawnDelegate handler; - if (NetworkScene.GetPrefab(msg.assetId, out prefab)) + if (GetPrefab(msg.assetId, out prefab)) { GameObject obj = Object.Instantiate(prefab, msg.position, msg.rotation); if (LogFilter.Debug) @@ -346,7 +471,7 @@ static void OnSpawnPrefab(NetworkMessage netMsg) ApplySpawnPayload(localObject, msg.position, msg.payload, msg.netId); } // lookup registered factory for type: - else if (NetworkScene.GetSpawnHandler(msg.assetId, out handler)) + else if (GetSpawnHandler(msg.assetId, out handler)) { GameObject obj = handler(msg.position, msg.assetId); if (obj == null) @@ -436,7 +561,7 @@ static void OnObjectDestroy(NetworkMessage netMsg) { localObject.OnNetworkDestroy(); - if (!NetworkScene.InvokeUnSpawnHandler(localObject.assetId, localObject.gameObject)) + if (!InvokeUnSpawnHandler(localObject.assetId, localObject.gameObject)) { // default handling if (localObject.sceneId == 0) diff --git a/Mirror/Runtime/Mirror.Runtime.csproj b/Mirror/Runtime/Mirror.Runtime.csproj index 38e101844..e7a8d0ba5 100644 --- a/Mirror/Runtime/Mirror.Runtime.csproj +++ b/Mirror/Runtime/Mirror.Runtime.csproj @@ -70,7 +70,6 @@ - diff --git a/Mirror/Runtime/NetworkScene.cs b/Mirror/Runtime/NetworkScene.cs deleted file mode 100644 index 62892427f..000000000 --- a/Mirror/Runtime/NetworkScene.cs +++ /dev/null @@ -1,182 +0,0 @@ -using System.Collections.Generic; -using UnityEngine; -using Guid = System.Guid; - -namespace Mirror -{ - // This is an internal class to allow the client and server to share scene-related functionality. - // This code (mostly) used to be in ClientScene. - internal class NetworkScene - { - internal static Dictionary guidToPrefab = new Dictionary(); - internal static Dictionary spawnHandlers = new Dictionary(); - internal static Dictionary unspawnHandlers = new Dictionary(); - - internal void Shutdown() - { - NetworkIdentity.spawned.Clear(); - ClearSpawners(); - } - - internal static void RegisterPrefab(GameObject prefab, Guid newAssetId) - { - NetworkIdentity view = prefab.GetComponent(); - if (view) - { - view.SetDynamicAssetId(newAssetId); - - if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); } - guidToPrefab[view.assetId] = prefab; - } - else - { - Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"); - } - } - - internal static void RegisterPrefab(GameObject prefab) - { - NetworkIdentity view = prefab.GetComponent(); - if (view) - { - if (LogFilter.Debug) { Debug.Log("Registering prefab '" + prefab.name + "' as asset:" + view.assetId); } - guidToPrefab[view.assetId] = prefab; - - var uvs = prefab.GetComponentsInChildren(); - if (uvs.Length > 1) - { - Debug.LogWarning("The prefab '" + prefab.name + - "' has multiple NetworkIdentity components. There can only be one NetworkIdentity on a prefab, and it must be on the root object."); - } - } - else - { - Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"); - } - } - - internal static bool GetPrefab(Guid assetId, out GameObject prefab) - { - prefab = null; - if (assetId != Guid.Empty && guidToPrefab.ContainsKey(assetId) && guidToPrefab[assetId] != null) - { - prefab = guidToPrefab[assetId]; - return true; - } - return false; - } - - internal static void ClearSpawners() - { - guidToPrefab.Clear(); - spawnHandlers.Clear(); - unspawnHandlers.Clear(); - } - - public static void UnregisterSpawnHandler(Guid assetId) - { - spawnHandlers.Remove(assetId); - unspawnHandlers.Remove(assetId); - } - - internal static void RegisterSpawnHandler(Guid assetId, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) - { - if (spawnHandler == null || unspawnHandler == null) - { - Debug.LogError("RegisterSpawnHandler custom spawn function null for " + assetId); - return; - } - - if (LogFilter.Debug) { Debug.Log("RegisterSpawnHandler asset '" + assetId + "' " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); } - - spawnHandlers[assetId] = spawnHandler; - unspawnHandlers[assetId] = unspawnHandler; - } - - internal static void UnregisterPrefab(GameObject prefab) - { - NetworkIdentity identity = prefab.GetComponent(); - if (identity == null) - { - Debug.LogError("Could not unregister '" + prefab.name + "' since it contains no NetworkIdentity component"); - return; - } - spawnHandlers.Remove(identity.assetId); - unspawnHandlers.Remove(identity.assetId); - } - - internal static void RegisterPrefab(GameObject prefab, SpawnDelegate spawnHandler, UnSpawnDelegate unspawnHandler) - { - NetworkIdentity identity = prefab.GetComponent(); - if (identity == null) - { - Debug.LogError("Could not register '" + prefab.name + "' since it contains no NetworkIdentity component"); - return; - } - - if (spawnHandler == null || unspawnHandler == null) - { - Debug.LogError("RegisterPrefab custom spawn function null for " + identity.assetId); - return; - } - - if (identity.assetId == Guid.Empty) - { - Debug.LogError("RegisterPrefab game object " + prefab.name + " has no prefab. Use RegisterSpawnHandler() instead?"); - return; - } - - if (LogFilter.Debug) { Debug.Log("Registering custom prefab '" + prefab.name + "' as asset:" + identity.assetId + " " + spawnHandler.GetMethodName() + "/" + unspawnHandler.GetMethodName()); } - - spawnHandlers[identity.assetId] = spawnHandler; - unspawnHandlers[identity.assetId] = unspawnHandler; - } - - internal static bool GetSpawnHandler(Guid assetId, out SpawnDelegate handler) - { - if (spawnHandlers.ContainsKey(assetId)) - { - handler = spawnHandlers[assetId]; - return true; - } - handler = null; - return false; - } - - internal static bool InvokeUnSpawnHandler(Guid assetId, GameObject obj) - { - if (unspawnHandlers.ContainsKey(assetId) && unspawnHandlers[assetId] != null) - { - UnSpawnDelegate handler = unspawnHandlers[assetId]; - handler(obj); - return true; - } - return false; - } - - internal void DestroyAllClientObjects() - { - foreach (var netId in NetworkIdentity.spawned.Keys) - { - NetworkIdentity uv = NetworkIdentity.spawned[netId]; - - if (uv != null && uv.gameObject != null) - { - if (!InvokeUnSpawnHandler(uv.assetId, uv.gameObject)) - { - if (uv.sceneId == 0) - { - Object.Destroy(uv.gameObject); - } - else - { - uv.MarkForReset(); - uv.gameObject.SetActive(false); - } - } - } - } - NetworkIdentity.spawned.Clear(); - } - } -}