|
| 1 | +#if UNIFIED_NETCODE |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using Unity.Collections; |
| 5 | +using Unity.Netcode.Logging; |
| 6 | +using UnityEngine; |
| 7 | + |
| 8 | +namespace Unity.Netcode |
| 9 | +{ |
| 10 | + internal class GhostSpawnManager |
| 11 | + { |
| 12 | + private readonly NetworkManager m_NetworkManager; |
| 13 | + private readonly ContextualLogger m_Log; |
| 14 | + |
| 15 | + public GhostSpawnManager(NetworkManager networkManager) |
| 16 | + { |
| 17 | + m_NetworkManager = networkManager; |
| 18 | + m_Log = new ContextualLogger((Object)networkManager, networkManager); |
| 19 | + } |
| 20 | + |
| 21 | + private readonly Dictionary<ulong, NetworkObject> m_GhostsPendingSpawn = new(); |
| 22 | + |
| 23 | + // TODO: We might want to make this a mock interface but temporary solution to validate |
| 24 | + // the need to assure we are registering with the right NetworkManager instance when testing (everything |
| 25 | + // will use the singleton during Awake and Start when we need to register). |
| 26 | + internal delegate void RegisterPendingGhostDelegateHandler(NetworkObject networkObject, ulong networkObjectId); |
| 27 | + |
| 28 | + internal static RegisterPendingGhostDelegateHandler RegisterPendingGhost; |
| 29 | + |
| 30 | + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] |
| 31 | + private static void RuntimeInitializeOnLoad() => RegisterPendingGhost = null; |
| 32 | + |
| 33 | + internal void RegisterGhostBridge(ulong networkObjectId, NetworkObject networkObject) |
| 34 | + { |
| 35 | + m_Log.Info(new Context(LogLevel.Developer, $"Registering {nameof(NetworkObject)} for ghost bridge").AddInfo(nameof(NetworkObject.NetworkObjectId), networkObjectId)); |
| 36 | + |
| 37 | + // Set when running through integration tests in order to initially bypass the |
| 38 | + // normal registration. This is because at this point in the instantiation process, |
| 39 | + // NetworkObject's NetworkManager is pointing to the singleton which means all instances |
| 40 | + // (even if intended to be for a specific client) will end up registering with whichever |
| 41 | + // NetworkManager instance is being pointed to by the singleton. |
| 42 | + if (RegisterPendingGhost != null) |
| 43 | + { |
| 44 | + RegisterPendingGhost(networkObject, networkObjectId); |
| 45 | + } |
| 46 | + else if (!m_NetworkManager.IsServer) |
| 47 | + { |
| 48 | + RegisterGhostPendingSpawn(networkObject, networkObjectId); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + internal void RegisterGhostPendingSpawn(NetworkObject networkObject, ulong networkObjectId) |
| 53 | + { |
| 54 | + m_Log.Info(new Context(LogLevel.Developer, $"Registering {nameof(NetworkObject)} for ghost spawn").AddTag(networkObject.name).AddInfo(nameof(NetworkObject.NetworkObjectId), networkObjectId)); |
| 55 | + |
| 56 | + if (!m_GhostsPendingSpawn.TryAdd(networkObjectId, networkObject)) |
| 57 | + { |
| 58 | + m_Log.Error(new Context(LogLevel.Normal, $"{nameof(NetworkObject)} has already been registered as a pending ghost!").AddTag(networkObject.name).AddInfo(nameof(NetworkObject.NetworkObjectId), networkObjectId)); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // TODO-REVIEW-BELOW: *** This is very likely no longer an issue with the new connection sequence *** |
| 63 | + // TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn. |
| 64 | + // Edge-Case scenario: During initial client synchronization (i.e. !m_NetworkManager.IsConnectedClient). |
| 65 | + // |
| 66 | + // Description: A client can receive snapshots before finishing the NGO synchronization process. |
| 67 | + // This is when an edge case scenario can happen where the initial NGO synchronization information |
| 68 | + // can include new scenes to load. If one of those scenes is configured to load in SingleMode, then |
| 69 | + // any instantiated ghosts pending synchronization would be instantiated in whatever the currently |
| 70 | + // active scene was when the client was processing the synchronization data. If the ghosts pending |
| 71 | + // synchronization are in the currently active scene when the new scene is loaded in SingleMode, then |
| 72 | + // they would be destroyed. |
| 73 | + // |
| 74 | + // Current Fix: |
| 75 | + // If the client is not yet synchronized, then any ghost pending spawn get migrated into the DDOL. |
| 76 | + // |
| 77 | + // Further review: |
| 78 | + // We need to make sure that we are migrating NetworkObjects into their assigned scene (if scene |
| 79 | + // management is enabled). Currently, we assume all instances were in the DDOL and just migrate |
| 80 | + // them into the currently active scene upon spawn. |
| 81 | + if (!m_NetworkManager.IsConnectedClient && !m_GhostsPendingSynchronization.ContainsKey(networkObjectId)) |
| 82 | + { |
| 83 | + Object.DontDestroyOnLoad(networkObject.gameObject); |
| 84 | + } |
| 85 | + else // There is matching spawn data for this pending Ghost, process the pending spawn for this hybrid instance. |
| 86 | + { |
| 87 | + m_NetworkManager.DeferredMessageManager.ProcessTriggers(IDeferredNetworkMessageManager.TriggerType.OnGhostSpawned, networkObjectId); |
| 88 | + if (m_GhostsPendingSynchronization.ContainsKey(networkObjectId)) |
| 89 | + { |
| 90 | + ProcessGhostPendingSynchronization(networkObjectId); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + internal bool TryGetGhostNetworkObjectForSpawn(NetworkObject.SerializedObject serializedObject, [NotNullWhen(true)] out NetworkObject networkObject) |
| 96 | + { |
| 97 | + var networkObjectId = serializedObject.NetworkObjectId; |
| 98 | + |
| 99 | + // TODO-UNIFIED: Get this working somehow (or if not possible prevent this from happening prior to getting to this point) |
| 100 | + if (serializedObject.HasInstantiationData) |
| 101 | + { |
| 102 | + m_Log.Error(new Context(LogLevel.Error, $"{nameof(NetworkObject)} Pre-spawn instantiation data does not work in this version!").AddInfo(nameof(NetworkObject.NetworkObjectId), networkObjectId)); |
| 103 | + } |
| 104 | + if (!m_GhostsPendingSpawn.Remove(networkObjectId, out networkObject) || networkObject == null) |
| 105 | + { |
| 106 | + m_Log.Error(new Context(LogLevel.Error, $"Attempting to spawn {nameof(NetworkObject)} with no instance to spawn!").AddInfo(nameof(NetworkObject.NetworkObjectId), networkObjectId)); |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + // TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn. |
| 111 | + // NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene. |
| 112 | + UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene()); |
| 113 | + return true; |
| 114 | + } |
| 115 | + |
| 116 | + private bool m_GhostsArePendingSynchronization; |
| 117 | + private readonly Dictionary<ulong, PendingGhostSpawnEntry> m_GhostsPendingSynchronization = new(); |
| 118 | + internal void RegisterGhostPendingSynchronization(PendingGhostSpawnEntry pendingGhostSpawnEntry) |
| 119 | + { |
| 120 | + var networkObjectId = pendingGhostSpawnEntry.SerializedObject.NetworkObjectId; |
| 121 | + m_Log.Info(new Context(LogLevel.Developer, $"Registering {nameof(NetworkObject.SerializedObject)} for pending synchronization").AddInfo(nameof(NetworkObject.NetworkObjectId), networkObjectId)); |
| 122 | + |
| 123 | + m_GhostsPendingSynchronization.TryAdd(networkObjectId, pendingGhostSpawnEntry); |
| 124 | + m_GhostsArePendingSynchronization = true; |
| 125 | + } |
| 126 | + |
| 127 | + internal NetworkObject ProcessGhostPendingSynchronization(ulong networkObjectId, bool removeUponSpawn = true) |
| 128 | + { |
| 129 | + var ghostPendingSync = m_GhostsPendingSynchronization[networkObjectId]; |
| 130 | + var serializedObject = ghostPendingSync.SerializedObject; |
| 131 | + var reader = ghostPendingSync.Buffer; |
| 132 | + if (removeUponSpawn) |
| 133 | + { |
| 134 | + m_GhostsPendingSynchronization.Remove(networkObjectId); |
| 135 | + } |
| 136 | + |
| 137 | + if (serializedObject.IsSceneObject) |
| 138 | + { |
| 139 | + m_NetworkManager.SceneManager.SetTheSceneBeingSynchronized(serializedObject.NetworkSceneHandle); |
| 140 | + } |
| 141 | + var networkObject = NetworkObject.Deserialize(serializedObject, reader, m_NetworkManager); |
| 142 | + // TODO-UNIFIED: How do we handle the "all in-scene placed objects are spawned notification"? |
| 143 | + //if (serializedObject.IsSceneObject) |
| 144 | + //{ |
| 145 | + // networkObject.InternalInSceneNetworkObjectsSpawned(); |
| 146 | + //} |
| 147 | + if (removeUponSpawn) |
| 148 | + { |
| 149 | + m_GhostsPendingSynchronization.Remove(networkObjectId); |
| 150 | + m_GhostsArePendingSynchronization = m_GhostsPendingSynchronization.Count > 0; |
| 151 | + ghostPendingSync.Buffer.Dispose(); |
| 152 | + } |
| 153 | + return networkObject; |
| 154 | + } |
| 155 | + |
| 156 | + |
| 157 | + private readonly HashSet<ulong> m_GhostSynchronizationPendingRemoval = new(); |
| 158 | + |
| 159 | + internal void ProcessAllGhostsPendingSynchronization() |
| 160 | + { |
| 161 | + var spawnTimeout = m_NetworkManager.NetworkConfig.SpawnTimeout; |
| 162 | + if (!m_GhostsArePendingSynchronization) |
| 163 | + { |
| 164 | + return; |
| 165 | + } |
| 166 | + foreach (var ghost in m_GhostsPendingSynchronization) |
| 167 | + { |
| 168 | + var networkObjectId = ghost.Value.SerializedObject.NetworkObjectId; |
| 169 | + if (m_GhostsPendingSpawn.ContainsKey(networkObjectId)) |
| 170 | + { |
| 171 | + // Process it, but don't remove it as we handle that a little later |
| 172 | + ProcessGhostPendingSynchronization(ghost.Value.SerializedObject.NetworkObjectId, false); |
| 173 | + m_GhostSynchronizationPendingRemoval.Add(networkObjectId); |
| 174 | + } |
| 175 | + else |
| 176 | + if ((ghost.Value.RegistrationTime + spawnTimeout) < Time.realtimeSinceStartup) |
| 177 | + { |
| 178 | + m_Log.Info(new Context(LogLevel.Developer, $"Registering {nameof(NetworkObject.SerializedObject)} for pending synchronization").AddInfo(nameof(NetworkObject.NetworkObjectId), networkObjectId)); |
| 179 | + |
| 180 | + // Timed out entries are removed too |
| 181 | + m_GhostSynchronizationPendingRemoval.Add(ghost.Key); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + foreach (var networkObjectId in m_GhostSynchronizationPendingRemoval) |
| 186 | + { |
| 187 | + var entry = m_GhostsPendingSynchronization[networkObjectId]; |
| 188 | + m_GhostsPendingSynchronization.Remove(networkObjectId); |
| 189 | + entry.Buffer.Dispose(); |
| 190 | + } |
| 191 | + m_GhostSynchronizationPendingRemoval.Clear(); |
| 192 | + m_GhostsArePendingSynchronization = m_GhostsPendingSynchronization.Count > 0; |
| 193 | + } |
| 194 | + |
| 195 | + internal void MarkNetworkObjectAsDestroying(ulong networkObjectId) |
| 196 | + { |
| 197 | + m_GhostsPendingSpawn.Remove(networkObjectId); |
| 198 | + m_GhostsPendingSynchronization.Remove(networkObjectId); |
| 199 | + } |
| 200 | + |
| 201 | + /// <summary> |
| 202 | + /// Used in scene managment synchronization. |
| 203 | + /// Verifies if this <see cref="NetworkObject.SerializedObject"/> should defer its spawn until the associated GhostObject is created. |
| 204 | + /// </summary> |
| 205 | + /// <param name="serializedObject">The object to check</param> |
| 206 | + /// <param name="internalBuffer"><see cref="SceneEventData"/>'s <see cref="SceneEventData.InternalBuffer"/> to save the buffer with data for the deferred spawn.</param> |
| 207 | + /// <returns>True if the serialized object should should wait for the associated ghost object; false otherwise</returns> |
| 208 | + internal bool ShouldDeferGhostSceneObject(NetworkObject.SerializedObject serializedObject, FastBufferReader internalBuffer) |
| 209 | + { |
| 210 | + if (!m_GhostsPendingSpawn.TryGetValue(serializedObject.NetworkObjectId, out var existingObject)) |
| 211 | + { |
| 212 | + m_Log.Info(new Context(LogLevel.Developer, $"Deferring creation of InScenePlaced {nameof(NetworkObject)} to wait for Ghost.").AddTag("SynchronizeSceneNetworkObjects").AddInfo(nameof(NetworkObject.NetworkObjectId), serializedObject.NetworkObjectId)); |
| 213 | + |
| 214 | + var newEntry = new PendingGhostSpawnEntry() |
| 215 | + { |
| 216 | + RegistrationTime = Time.realtimeSinceStartup, |
| 217 | + SerializedObject = serializedObject, |
| 218 | + Buffer = new FastBufferReader(internalBuffer, Allocator.Persistent, serializedObject.SynchronizationDataSize, internalBuffer.Position) |
| 219 | + }; |
| 220 | + |
| 221 | + RegisterGhostPendingSynchronization(newEntry); |
| 222 | + internalBuffer.Seek(internalBuffer.Position + serializedObject.SynchronizationDataSize); |
| 223 | + return true; |
| 224 | + } |
| 225 | + |
| 226 | + if (existingObject == null) |
| 227 | + { |
| 228 | + m_Log.Info(new Context(LogLevel.Developer, $"Dropping creation of InScenePlaced {nameof(NetworkObject)} as it has an entry but no longer exists!").AddTag("SynchronizeSceneNetworkObjects").AddInfo(nameof(NetworkObject.NetworkObjectId), serializedObject.NetworkObjectId)); |
| 229 | + |
| 230 | + // If it no longer exists, then just remove the entry and skip it. |
| 231 | + internalBuffer.Seek(internalBuffer.Position + serializedObject.SynchronizationDataSize); |
| 232 | + m_GhostsPendingSpawn.Remove(serializedObject.NetworkObjectId); |
| 233 | + return true; |
| 234 | + } |
| 235 | + |
| 236 | + return false; |
| 237 | + } |
| 238 | + |
| 239 | + /// <summary> |
| 240 | + /// Finalizer that ensures proper cleanup of manager resources |
| 241 | + /// </summary> |
| 242 | + ~GhostSpawnManager() |
| 243 | + { |
| 244 | + Shutdown(); |
| 245 | + } |
| 246 | + |
| 247 | + internal void Shutdown() |
| 248 | + { |
| 249 | + m_GhostsPendingSpawn.Clear(); |
| 250 | + m_GhostsPendingSynchronization.Clear(); |
| 251 | + } |
| 252 | + } |
| 253 | +} |
| 254 | +#endif |
0 commit comments