| | 1 | | using DCL.Controllers; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using DCL.Models; |
| | 5 | | using DCL.Configuration; |
| | 6 | | using System; |
| | 7 | | using System.Collections; |
| | 8 | | using System.Collections.Generic; |
| | 9 | | using System.Linq; |
| | 10 | | using DCL.Components; |
| | 11 | | using Newtonsoft.Json; |
| | 12 | | using UnityEngine; |
| | 13 | |
|
| | 14 | | namespace DCL |
| | 15 | | { |
| | 16 | | public class SceneController : ISceneController |
| | 17 | | { |
| | 18 | | public static bool VERBOSE = false; |
| | 19 | |
|
| 718 | 20 | | public bool enabled { get; set; } = true; |
| | 21 | |
|
| | 22 | | private Coroutine deferredDecodingCoroutine; |
| | 23 | |
|
| | 24 | | public void Initialize() |
| | 25 | | { |
| 682 | 26 | | sceneSortDirty = true; |
| 682 | 27 | | positionDirty = true; |
| 682 | 28 | | lastSortFrame = 0; |
| 682 | 29 | | enabled = true; |
| | 30 | |
|
| 682 | 31 | | Environment.i.platform.debugController.OnDebugModeSet += OnDebugModeSet; |
| | 32 | |
|
| | 33 | | // We trigger the Decentraland logic once SceneController has been instanced and is ready to act. |
| 682 | 34 | | WebInterface.StartDecentraland(); |
| | 35 | |
|
| 682 | 36 | | if (deferredMessagesDecoding) // We should be able to delete this code |
| 0 | 37 | | deferredDecodingCoroutine = CoroutineStarter.Start(DeferredDecoding()); // |
| | 38 | |
|
| 682 | 39 | | DCLCharacterController.OnCharacterMoved += SetPositionDirty; |
| | 40 | |
|
| 682 | 41 | | CommonScriptableObjects.sceneID.OnChange += OnCurrentSceneIdChange; |
| | 42 | |
|
| | 43 | | //TODO(Brian): Move those subscriptions elsewhere. |
| 682 | 44 | | PoolManager.i.OnGet -= Environment.i.platform.physicsSyncController.MarkDirty; |
| 682 | 45 | | PoolManager.i.OnGet += Environment.i.platform.physicsSyncController.MarkDirty; |
| | 46 | |
|
| 682 | 47 | | PoolManager.i.OnGet -= Environment.i.platform.cullingController.objectsTracker.MarkDirty; |
| 682 | 48 | | PoolManager.i.OnGet += Environment.i.platform.cullingController.objectsTracker.MarkDirty; |
| 682 | 49 | | } |
| | 50 | |
|
| 20 | 51 | | private void OnDebugModeSet() { Environment.i.world.sceneBoundsChecker.SetFeedbackStyle(new SceneBoundsFeedbackS |
| | 52 | |
|
| | 53 | | public void Start() |
| | 54 | | { |
| 123 | 55 | | if (prewarmSceneMessagesPool) |
| | 56 | | { |
| 0 | 57 | | for (int i = 0; i < 100000; i++) |
| | 58 | | { |
| 0 | 59 | | sceneMessagesPool.Enqueue(new QueuedSceneMessage_Scene()); |
| | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| 123 | 63 | | if (prewarmEntitiesPool) |
| | 64 | | { |
| 0 | 65 | | EnsureEntityPool(); |
| | 66 | | } |
| | 67 | |
|
| | 68 | | // Warmup some shader variants |
| 123 | 69 | | Resources.Load<ShaderVariantCollection>("ShaderVariantCollections/shaderVariants-selected").WarmUp(); |
| 123 | 70 | | } |
| | 71 | |
|
| | 72 | | public void Dispose() |
| | 73 | | { |
| 702 | 74 | | PoolManager.i.OnGet -= Environment.i.platform.physicsSyncController.MarkDirty; |
| 702 | 75 | | PoolManager.i.OnGet -= Environment.i.platform.cullingController.objectsTracker.MarkDirty; |
| 702 | 76 | | DCLCharacterController.OnCharacterMoved -= SetPositionDirty; |
| 702 | 77 | | Environment.i.platform.debugController.OnDebugModeSet -= OnDebugModeSet; |
| | 78 | |
|
| 702 | 79 | | UnloadAllScenes(includePersistent: true); |
| | 80 | |
|
| 702 | 81 | | if (deferredDecodingCoroutine != null) |
| 0 | 82 | | CoroutineStarter.Stop(deferredDecodingCoroutine); |
| 702 | 83 | | } |
| | 84 | |
|
| | 85 | | public void Update() |
| | 86 | | { |
| 17343 | 87 | | if (!enabled) |
| 52 | 88 | | return; |
| | 89 | |
|
| 17291 | 90 | | InputController_Legacy.i.Update(); |
| | 91 | |
|
| 17291 | 92 | | Environment.i.world.pointerEventsController.Update(); |
| | 93 | |
|
| 17291 | 94 | | if (lastSortFrame != Time.frameCount && sceneSortDirty) |
| | 95 | | { |
| 827 | 96 | | lastSortFrame = Time.frameCount; |
| 827 | 97 | | sceneSortDirty = false; |
| 827 | 98 | | SortScenesByDistance(); |
| | 99 | | } |
| 17291 | 100 | | } |
| | 101 | |
|
| | 102 | | public void LateUpdate() |
| | 103 | | { |
| 17337 | 104 | | if (!enabled) |
| 52 | 105 | | return; |
| | 106 | |
|
| 17285 | 107 | | Environment.i.platform.physicsSyncController.Sync(); |
| 17285 | 108 | | } |
| | 109 | |
|
| | 110 | | public void EnsureEntityPool() // TODO: Move to PoolManagerFactory |
| | 111 | | { |
| 542 | 112 | | if (PoolManager.i.ContainsPool(EMPTY_GO_POOL_NAME)) |
| 275 | 113 | | return; |
| | 114 | |
|
| 267 | 115 | | GameObject go = new GameObject(); |
| 267 | 116 | | Pool pool = PoolManager.i.AddPool(EMPTY_GO_POOL_NAME, go, maxPrewarmCount: 2000, isPersistent: true); |
| | 117 | |
|
| 267 | 118 | | if (prewarmEntitiesPool) |
| 14 | 119 | | pool.ForcePrewarm(); |
| 267 | 120 | | } |
| | 121 | |
|
| | 122 | | //====================================================================== |
| | 123 | |
|
| | 124 | | #region MESSAGES_HANDLING |
| | 125 | |
|
| | 126 | | //====================================================================== |
| | 127 | |
|
| | 128 | | #if UNITY_EDITOR |
| | 129 | | public delegate void ProcessDelegate(string sceneId, string method); |
| | 130 | |
|
| | 131 | | public event ProcessDelegate OnMessageProcessInfoStart; |
| | 132 | | public event ProcessDelegate OnMessageProcessInfoEnds; |
| | 133 | | #endif |
| 657 | 134 | | public bool deferredMessagesDecoding { get; set; } = false; |
| | 135 | |
|
| 682 | 136 | | Queue<string> payloadsToDecode = new Queue<string>(); |
| | 137 | | const float MAX_TIME_FOR_DECODE = 0.005f; |
| | 138 | |
|
| | 139 | | public bool ProcessMessage(QueuedSceneMessage_Scene msgObject, out CustomYieldInstruction yieldInstruction) |
| | 140 | | { |
| 0 | 141 | | string sceneId = msgObject.sceneId; |
| 0 | 142 | | string method = msgObject.method; |
| | 143 | |
|
| 0 | 144 | | yieldInstruction = null; |
| | 145 | |
|
| | 146 | | IParcelScene scene; |
| 0 | 147 | | bool res = false; |
| 0 | 148 | | IWorldState worldState = Environment.i.world.state; |
| 0 | 149 | | DebugConfig debugConfig = DataStore.i.debugConfig; |
| | 150 | |
|
| 0 | 151 | | if (worldState.loadedScenes.TryGetValue(sceneId, out scene)) |
| | 152 | | { |
| | 153 | | #if UNITY_EDITOR |
| 0 | 154 | | if (debugConfig.soloScene && scene is GlobalScene && debugConfig.ignoreGlobalScenes) |
| | 155 | | { |
| 0 | 156 | | return false; |
| | 157 | | } |
| | 158 | | #endif |
| 0 | 159 | | if (!scene.GetSceneTransform().gameObject.activeInHierarchy) |
| | 160 | | { |
| 0 | 161 | | return true; |
| | 162 | | } |
| | 163 | |
|
| | 164 | | #if UNITY_EDITOR |
| 0 | 165 | | OnMessageProcessInfoStart?.Invoke(sceneId, method); |
| | 166 | | #endif |
| 0 | 167 | | ProfilingEvents.OnMessageProcessStart?.Invoke(method); |
| | 168 | |
|
| 0 | 169 | | ProcessMessage(scene as ParcelScene, method, msgObject.payload, out yieldInstruction); |
| | 170 | |
|
| 0 | 171 | | ProfilingEvents.OnMessageProcessEnds?.Invoke(method); |
| | 172 | |
|
| | 173 | | #if UNITY_EDITOR |
| 0 | 174 | | OnMessageProcessInfoEnds?.Invoke(sceneId, method); |
| | 175 | | #endif |
| | 176 | |
|
| 0 | 177 | | res = true; |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | else |
| | 181 | | { |
| 0 | 182 | | res = false; |
| | 183 | | } |
| | 184 | |
|
| 0 | 185 | | sceneMessagesPool.Enqueue(msgObject); |
| | 186 | |
|
| 0 | 187 | | return res; |
| | 188 | | } |
| | 189 | |
|
| | 190 | | private void ProcessMessage(ParcelScene scene, string method, object msgPayload, |
| | 191 | | out CustomYieldInstruction yieldInstruction) |
| | 192 | | { |
| 0 | 193 | | yieldInstruction = null; |
| 0 | 194 | | IDelayedComponent delayedComponent = null; |
| | 195 | |
|
| | 196 | | try |
| | 197 | | { |
| | 198 | | switch (method) |
| | 199 | | { |
| | 200 | | case MessagingTypes.ENTITY_CREATE: |
| | 201 | | { |
| 0 | 202 | | if (msgPayload is Protocol.CreateEntity payload) |
| 0 | 203 | | scene.CreateEntity(payload.entityId); |
| | 204 | |
|
| 0 | 205 | | break; |
| | 206 | | } |
| | 207 | | case MessagingTypes.ENTITY_REPARENT: |
| | 208 | | { |
| 0 | 209 | | if (msgPayload is Protocol.SetEntityParent payload) |
| 0 | 210 | | scene.SetEntityParent(payload.entityId, payload.parentId); |
| | 211 | |
|
| 0 | 212 | | break; |
| | 213 | | } |
| | 214 | |
|
| | 215 | | case MessagingTypes.ENTITY_COMPONENT_CREATE_OR_UPDATE: |
| | 216 | | { |
| 0 | 217 | | if (msgPayload is Protocol.EntityComponentCreateOrUpdate payload) |
| | 218 | | { |
| 0 | 219 | | delayedComponent = scene.EntityComponentCreateOrUpdate(payload.entityId, |
| | 220 | | (CLASS_ID_COMPONENT) payload.classId, payload.json) as IDelayedComponent; |
| | 221 | | } |
| | 222 | |
|
| 0 | 223 | | break; |
| | 224 | | } |
| | 225 | |
|
| | 226 | | case MessagingTypes.ENTITY_COMPONENT_DESTROY: |
| | 227 | | { |
| 0 | 228 | | if (msgPayload is Protocol.EntityComponentDestroy payload) |
| 0 | 229 | | scene.EntityComponentRemove(payload.entityId, payload.name); |
| | 230 | |
|
| 0 | 231 | | break; |
| | 232 | | } |
| | 233 | |
|
| | 234 | | case MessagingTypes.SHARED_COMPONENT_ATTACH: |
| | 235 | | { |
| 0 | 236 | | if (msgPayload is Protocol.SharedComponentAttach payload) |
| 0 | 237 | | scene.SharedComponentAttach(payload.entityId, payload.id); |
| | 238 | |
|
| 0 | 239 | | break; |
| | 240 | | } |
| | 241 | |
|
| | 242 | | case MessagingTypes.SHARED_COMPONENT_CREATE: |
| | 243 | | { |
| 0 | 244 | | if (msgPayload is Protocol.SharedComponentCreate payload) |
| 0 | 245 | | scene.SharedComponentCreate(payload.id, payload.classId); |
| | 246 | |
|
| 0 | 247 | | break; |
| | 248 | | } |
| | 249 | |
|
| | 250 | | case MessagingTypes.SHARED_COMPONENT_DISPOSE: |
| | 251 | | { |
| 0 | 252 | | if (msgPayload is Protocol.SharedComponentDispose payload) |
| 0 | 253 | | scene.SharedComponentDispose(payload.id); |
| 0 | 254 | | break; |
| | 255 | | } |
| | 256 | |
|
| | 257 | | case MessagingTypes.SHARED_COMPONENT_UPDATE: |
| | 258 | | { |
| 0 | 259 | | if (msgPayload is Protocol.SharedComponentUpdate payload) |
| 0 | 260 | | delayedComponent = scene.SharedComponentUpdate(payload.componentId, payload.json) as IDe |
| | 261 | |
|
| 0 | 262 | | break; |
| | 263 | | } |
| | 264 | |
|
| | 265 | | case MessagingTypes.ENTITY_DESTROY: |
| | 266 | | { |
| 0 | 267 | | if (msgPayload is Protocol.RemoveEntity payload) |
| 0 | 268 | | scene.RemoveEntity(payload.entityId); |
| 0 | 269 | | break; |
| | 270 | | } |
| | 271 | |
|
| | 272 | | case MessagingTypes.INIT_DONE: |
| | 273 | | { |
| 0 | 274 | | scene.sceneLifecycleHandler.SetInitMessagesDone(); |
| 0 | 275 | | break; |
| | 276 | | } |
| | 277 | |
|
| | 278 | | case MessagingTypes.QUERY: |
| | 279 | | { |
| 0 | 280 | | if (msgPayload is QueryMessage queryMessage) |
| 0 | 281 | | ParseQuery(queryMessage.payload, scene.sceneData.id); |
| 0 | 282 | | break; |
| | 283 | | } |
| | 284 | |
|
| | 285 | | case MessagingTypes.OPEN_EXTERNAL_URL: |
| | 286 | | { |
| 0 | 287 | | if (msgPayload is Protocol.OpenExternalUrl payload) |
| 0 | 288 | | OnOpenExternalUrlRequest?.Invoke(scene, payload.url); |
| 0 | 289 | | break; |
| | 290 | | } |
| | 291 | |
|
| | 292 | | case MessagingTypes.OPEN_NFT_DIALOG: |
| | 293 | | { |
| 0 | 294 | | if (msgPayload is Protocol.OpenNftDialog payload) |
| 0 | 295 | | DataStore.i.onOpenNFTPrompt.Set(new NFTPromptModel(payload.contactAddress, payload.token |
| | 296 | | payload.comment), true); |
| 0 | 297 | | break; |
| | 298 | | } |
| | 299 | |
|
| | 300 | | default: |
| 0 | 301 | | Debug.LogError($"Unknown method {method}"); |
| | 302 | | break; |
| | 303 | | } |
| 0 | 304 | | } |
| 0 | 305 | | catch (Exception e) |
| | 306 | | { |
| 0 | 307 | | throw new Exception( |
| | 308 | | $"Scene message error. scene: {scene.sceneData.id} method: {method} payload: {JsonUtility.ToJson(msg |
| | 309 | | } |
| | 310 | |
|
| 0 | 311 | | if (delayedComponent != null) |
| | 312 | | { |
| 0 | 313 | | if (delayedComponent.isRoutineRunning) |
| 0 | 314 | | yieldInstruction = delayedComponent.yieldInstruction; |
| | 315 | | } |
| 0 | 316 | | } |
| | 317 | |
|
| | 318 | | public void ParseQuery(object payload, string sceneId) |
| | 319 | | { |
| 0 | 320 | | IParcelScene scene = Environment.i.world.state.loadedScenes[sceneId]; |
| | 321 | |
|
| 0 | 322 | | if (!(payload is RaycastQuery raycastQuery)) |
| 0 | 323 | | return; |
| | 324 | |
|
| 0 | 325 | | Vector3 worldOrigin = raycastQuery.ray.origin + Utils.GridToWorldPosition(scene.sceneData.basePosition.x, sc |
| | 326 | |
|
| 0 | 327 | | raycastQuery.ray.unityOrigin = PositionUtils.WorldToUnityPosition(worldOrigin); |
| 0 | 328 | | raycastQuery.sceneId = sceneId; |
| 0 | 329 | | PhysicsCast.i.Query(raycastQuery); |
| 0 | 330 | | } |
| | 331 | |
|
| 0 | 332 | | public void SendSceneMessage(string payload) { SendSceneMessage(payload, deferredMessagesDecoding); } |
| | 333 | |
|
| | 334 | | private void SendSceneMessage(string payload, bool enqueue) |
| | 335 | | { |
| 0 | 336 | | string[] chunks = payload.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries); |
| 0 | 337 | | int count = chunks.Length; |
| | 338 | |
|
| 0 | 339 | | for (int i = 0; i < count; i++) |
| | 340 | | { |
| 0 | 341 | | if (CommonScriptableObjects.rendererState.Get() && enqueue) |
| | 342 | | { |
| 0 | 343 | | payloadsToDecode.Enqueue(chunks[i]); |
| 0 | 344 | | } |
| | 345 | | else |
| | 346 | | { |
| 0 | 347 | | DecodeAndEnqueue(chunks[i]); |
| | 348 | | } |
| | 349 | | } |
| 0 | 350 | | } |
| | 351 | |
|
| | 352 | | private void DecodeAndEnqueue(string payload) |
| | 353 | | { |
| 0 | 354 | | ProfilingEvents.OnMessageDecodeStart?.Invoke("Misc"); |
| | 355 | |
|
| | 356 | | string sceneId; |
| | 357 | | string message; |
| | 358 | | string messageTag; |
| | 359 | | PB_SendSceneMessage sendSceneMessage; |
| | 360 | |
|
| 0 | 361 | | if (!MessageDecoder.DecodePayloadChunk(payload, out sceneId, out message, out messageTag, out sendSceneMessa |
| | 362 | | { |
| 0 | 363 | | return; |
| | 364 | | } |
| | 365 | |
|
| | 366 | | QueuedSceneMessage_Scene queuedMessage; |
| | 367 | |
|
| 0 | 368 | | if (sceneMessagesPool.Count > 0) |
| 0 | 369 | | queuedMessage = sceneMessagesPool.Dequeue(); |
| | 370 | | else |
| 0 | 371 | | queuedMessage = new QueuedSceneMessage_Scene(); |
| | 372 | |
|
| 0 | 373 | | MessageDecoder.DecodeSceneMessage(sceneId, message, messageTag, sendSceneMessage, ref queuedMessage); |
| | 374 | |
|
| 0 | 375 | | EnqueueSceneMessage(queuedMessage); |
| | 376 | |
|
| 0 | 377 | | ProfilingEvents.OnMessageDecodeEnds?.Invoke("Misc"); |
| 0 | 378 | | } |
| | 379 | |
|
| | 380 | | private IEnumerator DeferredDecoding() |
| | 381 | | { |
| 0 | 382 | | float start = Time.realtimeSinceStartup; |
| | 383 | | float maxTimeForDecode; |
| | 384 | |
|
| 0 | 385 | | while (true) |
| | 386 | | { |
| 0 | 387 | | maxTimeForDecode = CommonScriptableObjects.rendererState.Get() ? MAX_TIME_FOR_DECODE : float.MaxValue; |
| | 388 | |
|
| 0 | 389 | | if (payloadsToDecode.Count > 0) |
| | 390 | | { |
| 0 | 391 | | string payload = payloadsToDecode.Dequeue(); |
| | 392 | |
|
| 0 | 393 | | DecodeAndEnqueue(payload); |
| | 394 | |
|
| 0 | 395 | | if (Time.realtimeSinceStartup - start < maxTimeForDecode) |
| | 396 | | continue; |
| | 397 | | } |
| | 398 | |
|
| 0 | 399 | | yield return null; |
| 0 | 400 | | start = Time.unscaledTime; |
| | 401 | | } |
| | 402 | | } |
| | 403 | |
|
| | 404 | | public void EnqueueSceneMessage(QueuedSceneMessage_Scene message) |
| | 405 | | { |
| 0 | 406 | | bool isGlobalScene = WorldStateUtils.IsGlobalScene(message.sceneId); |
| 0 | 407 | | Environment.i.messaging.manager.AddControllerIfNotExists(this, message.sceneId); |
| 0 | 408 | | Environment.i.messaging.manager.Enqueue(isGlobalScene, message); |
| 0 | 409 | | } |
| | 410 | |
|
| | 411 | | //====================================================================== |
| | 412 | |
|
| | 413 | | #endregion |
| | 414 | |
|
| | 415 | | //====================================================================== |
| | 416 | |
|
| | 417 | | //====================================================================== |
| | 418 | |
|
| | 419 | | #region SCENES_MANAGEMENT |
| | 420 | |
|
| | 421 | | //====================================================================== |
| | 422 | | public event Action<string> OnReadyScene; |
| | 423 | |
|
| | 424 | | public IParcelScene CreateTestScene(LoadParcelScenesMessage.UnityParcelScene data = null) |
| | 425 | | { |
| 682 | 426 | | if (data == null) |
| | 427 | | { |
| 674 | 428 | | data = new LoadParcelScenesMessage.UnityParcelScene(); |
| | 429 | | } |
| | 430 | |
|
| 682 | 431 | | if (data.parcels == null) |
| | 432 | | { |
| 674 | 433 | | data.parcels = new Vector2Int[] { data.basePosition }; |
| | 434 | | } |
| | 435 | |
|
| 682 | 436 | | if (string.IsNullOrEmpty(data.id)) |
| | 437 | | { |
| 674 | 438 | | data.id = $"(test):{data.basePosition.x},{data.basePosition.y}"; |
| | 439 | | } |
| | 440 | |
|
| 682 | 441 | | if (Environment.i.world.state.loadedScenes.ContainsKey(data.id)) |
| | 442 | | { |
| 12 | 443 | | Debug.LogWarning($"Scene {data.id} is already loaded."); |
| 12 | 444 | | return Environment.i.world.state.loadedScenes[data.id]; |
| | 445 | | } |
| | 446 | |
|
| 670 | 447 | | var go = new GameObject(); |
| 670 | 448 | | var newScene = go.AddComponent<ParcelScene>(); |
| 670 | 449 | | newScene.ownerController = this; |
| 670 | 450 | | newScene.isTestScene = true; |
| 670 | 451 | | newScene.isPersistent = true; |
| 670 | 452 | | newScene.SetData(data); |
| | 453 | |
|
| 670 | 454 | | if (DCLCharacterController.i != null) |
| 670 | 455 | | newScene.InitializeDebugPlane(); |
| | 456 | |
|
| 670 | 457 | | Environment.i.world.state.scenesSortedByDistance.Add(newScene); |
| | 458 | |
|
| 670 | 459 | | Environment.i.messaging.manager.AddControllerIfNotExists(this, data.id); |
| | 460 | |
|
| 670 | 461 | | Environment.i.world.state.loadedScenes.Add(data.id, newScene); |
| 670 | 462 | | OnNewSceneAdded?.Invoke(newScene); |
| 670 | 463 | | return newScene; |
| | 464 | | } |
| | 465 | |
|
| | 466 | | public void SendSceneReady(string sceneId) |
| | 467 | | { |
| 678 | 468 | | Environment.i.world.state.readyScenes.Add(sceneId); |
| | 469 | |
|
| 678 | 470 | | Environment.i.messaging.manager.SetSceneReady(sceneId); |
| | 471 | |
|
| 678 | 472 | | WebInterface.ReportControlEvent(new WebInterface.SceneReady(sceneId)); |
| | 473 | |
|
| 678 | 474 | | Environment.i.world.blockersController.SetupWorldBlockers(); |
| | 475 | |
|
| 678 | 476 | | OnReadyScene?.Invoke(sceneId); |
| 7 | 477 | | } |
| | 478 | |
|
| 14 | 479 | | public void ActivateBuilderInWorldEditScene() { Environment.i.world.sceneBoundsChecker.SetFeedbackStyle(new Scen |
| | 480 | |
|
| 8 | 481 | | public void DeactivateBuilderInWorldEditScene() { Environment.i.world.sceneBoundsChecker.SetFeedbackStyle(new Sc |
| | 482 | |
|
| | 483 | | private void SetPositionDirty(DCLCharacterPosition character) |
| | 484 | | { |
| 1751 | 485 | | var currentX = (int) Math.Floor(character.worldPosition.x / ParcelSettings.PARCEL_SIZE); |
| 1751 | 486 | | var currentY = (int) Math.Floor(character.worldPosition.z / ParcelSettings.PARCEL_SIZE); |
| | 487 | |
|
| 1751 | 488 | | positionDirty = currentX != currentGridSceneCoordinate.x || currentY != currentGridSceneCoordinate.y; |
| | 489 | |
|
| 1751 | 490 | | if (positionDirty) |
| | 491 | | { |
| 357 | 492 | | sceneSortDirty = true; |
| 357 | 493 | | currentGridSceneCoordinate.x = currentX; |
| 357 | 494 | | currentGridSceneCoordinate.y = currentY; |
| | 495 | |
|
| | 496 | | // Since the first position for the character is not sent from Kernel until just-before calling |
| | 497 | | // the rendering activation from Kernel, we need to sort the scenes to get the current scene id |
| | 498 | | // to lock the rendering accordingly... |
| 357 | 499 | | if (!CommonScriptableObjects.rendererState.Get()) |
| | 500 | | { |
| 1 | 501 | | SortScenesByDistance(); |
| | 502 | | } |
| | 503 | | } |
| 1751 | 504 | | } |
| | 505 | |
|
| | 506 | | public void SortScenesByDistance() |
| | 507 | | { |
| 831 | 508 | | if (DCLCharacterController.i == null) |
| 0 | 509 | | return; |
| | 510 | |
|
| 831 | 511 | | IWorldState worldState = Environment.i.world.state; |
| | 512 | |
|
| 831 | 513 | | worldState.currentSceneId = null; |
| 831 | 514 | | worldState.scenesSortedByDistance.Sort(SortScenesByDistanceMethod); |
| | 515 | |
|
| 831 | 516 | | using (var iterator = Environment.i.world.state.scenesSortedByDistance.GetEnumerator()) |
| | 517 | | { |
| | 518 | | IParcelScene scene; |
| | 519 | | bool characterIsInsideScene; |
| | 520 | |
|
| 1472 | 521 | | while (iterator.MoveNext()) |
| | 522 | | { |
| 799 | 523 | | scene = iterator.Current; |
| | 524 | |
|
| 799 | 525 | | if (scene == null) |
| | 526 | | continue; |
| | 527 | |
|
| 799 | 528 | | characterIsInsideScene = WorldStateUtils.IsCharacterInsideScene(scene); |
| | 529 | |
|
| 799 | 530 | | if (!worldState.globalSceneIds.Contains(scene.sceneData.id) && characterIsInsideScene) |
| | 531 | | { |
| 158 | 532 | | worldState.currentSceneId = scene.sceneData.id; |
| 158 | 533 | | break; |
| | 534 | | } |
| | 535 | | } |
| 673 | 536 | | } |
| | 537 | |
|
| 831 | 538 | | if (!DataStore.i.debugConfig.isDebugMode && string.IsNullOrEmpty(worldState.currentSceneId)) |
| | 539 | | { |
| | 540 | | // When we don't know the current scene yet, we must lock the rendering from enabling until it is set |
| 645 | 541 | | CommonScriptableObjects.rendererState.AddLock(this); |
| 645 | 542 | | } |
| | 543 | | else |
| | 544 | | { |
| | 545 | | // 1. Set current scene id |
| 186 | 546 | | CommonScriptableObjects.sceneID.Set(worldState.currentSceneId); |
| | 547 | |
|
| | 548 | | // 2. Attempt to remove SceneController's lock on rendering |
| 186 | 549 | | CommonScriptableObjects.rendererState.RemoveLock(this); |
| | 550 | | } |
| | 551 | |
|
| 831 | 552 | | OnSortScenes?.Invoke(); |
| 815 | 553 | | } |
| | 554 | |
|
| | 555 | | private int SortScenesByDistanceMethod(IParcelScene sceneA, IParcelScene sceneB) |
| | 556 | | { |
| 60 | 557 | | sortAuxiliaryVector = sceneA.sceneData.basePosition - currentGridSceneCoordinate; |
| 60 | 558 | | int dist1 = sortAuxiliaryVector.sqrMagnitude; |
| | 559 | |
|
| 60 | 560 | | sortAuxiliaryVector = sceneB.sceneData.basePosition - currentGridSceneCoordinate; |
| 60 | 561 | | int dist2 = sortAuxiliaryVector.sqrMagnitude; |
| | 562 | |
|
| 60 | 563 | | return dist1 - dist2; |
| | 564 | | } |
| | 565 | |
|
| | 566 | | private void OnCurrentSceneIdChange(string newSceneId, string prevSceneId) |
| | 567 | | { |
| 7277 | 568 | | if (Environment.i.world.state.TryGetScene(newSceneId, out IParcelScene newCurrentScene) |
| | 569 | | && !(newCurrentScene as ParcelScene).sceneLifecycleHandler.isReady) |
| | 570 | | { |
| 0 | 571 | | CommonScriptableObjects.rendererState.AddLock(newCurrentScene); |
| | 572 | |
|
| 0 | 573 | | (newCurrentScene as ParcelScene).sceneLifecycleHandler.OnSceneReady += (readyScene) => { CommonScriptabl |
| | 574 | | } |
| 7277 | 575 | | } |
| | 576 | |
|
| | 577 | | public void LoadParcelScenesExecute(string scenePayload) |
| | 578 | | { |
| | 579 | | LoadParcelScenesMessage.UnityParcelScene scene; |
| | 580 | |
|
| 27 | 581 | | ProfilingEvents.OnMessageDecodeStart?.Invoke(MessagingTypes.SCENE_LOAD); |
| 27 | 582 | | scene = Utils.SafeFromJson<LoadParcelScenesMessage.UnityParcelScene>(scenePayload); |
| 27 | 583 | | ProfilingEvents.OnMessageDecodeEnds?.Invoke(MessagingTypes.SCENE_LOAD); |
| | 584 | |
|
| 27 | 585 | | if (scene == null || scene.id == null) |
| 0 | 586 | | return; |
| | 587 | |
|
| 27 | 588 | | var sceneToLoad = scene; |
| | 589 | |
|
| | 590 | |
|
| 27 | 591 | | DebugConfig debugConfig = DataStore.i.debugConfig; |
| | 592 | | #if UNITY_EDITOR |
| 27 | 593 | | if (debugConfig.soloScene && sceneToLoad.basePosition.ToString() != debugConfig.soloSceneCoords.ToString()) |
| | 594 | | { |
| 0 | 595 | | SendSceneReady(sceneToLoad.id); |
| 0 | 596 | | return; |
| | 597 | | } |
| | 598 | | #endif |
| | 599 | |
|
| 27 | 600 | | ProfilingEvents.OnMessageProcessStart?.Invoke(MessagingTypes.SCENE_LOAD); |
| | 601 | |
|
| 27 | 602 | | IWorldState worldState = Environment.i.world.state; |
| | 603 | |
|
| 27 | 604 | | if (!worldState.loadedScenes.ContainsKey(sceneToLoad.id)) |
| | 605 | | { |
| 27 | 606 | | var newGameObject = new GameObject("New Scene"); |
| | 607 | |
|
| 27 | 608 | | var newScene = newGameObject.AddComponent<ParcelScene>(); |
| 27 | 609 | | newScene.SetData(sceneToLoad); |
| | 610 | |
|
| 27 | 611 | | if (debugConfig.isDebugMode) |
| | 612 | | { |
| 27 | 613 | | newScene.InitializeDebugPlane(); |
| | 614 | | } |
| | 615 | |
|
| 27 | 616 | | newScene.ownerController = this; |
| 27 | 617 | | worldState.loadedScenes.Add(sceneToLoad.id, newScene); |
| 27 | 618 | | worldState.scenesSortedByDistance.Add(newScene); |
| | 619 | |
|
| 27 | 620 | | sceneSortDirty = true; |
| | 621 | |
|
| 27 | 622 | | OnNewSceneAdded?.Invoke(newScene); |
| | 623 | |
|
| 27 | 624 | | Environment.i.messaging.manager.AddControllerIfNotExists(this, newScene.sceneData.id); |
| | 625 | |
|
| 27 | 626 | | if (VERBOSE) |
| 0 | 627 | | Debug.Log($"{Time.frameCount} : Load parcel scene {newScene.sceneData.basePosition}"); |
| | 628 | | } |
| | 629 | |
|
| 27 | 630 | | ProfilingEvents.OnMessageProcessEnds?.Invoke(MessagingTypes.SCENE_LOAD); |
| 0 | 631 | | } |
| | 632 | |
|
| | 633 | | public void UpdateParcelScenesExecute(string sceneId) |
| | 634 | | { |
| | 635 | | LoadParcelScenesMessage.UnityParcelScene sceneData; |
| | 636 | |
|
| 0 | 637 | | ProfilingEvents.OnMessageDecodeStart?.Invoke(MessagingTypes.SCENE_UPDATE); |
| 0 | 638 | | sceneData = Utils.SafeFromJson<LoadParcelScenesMessage.UnityParcelScene>(sceneId); |
| 0 | 639 | | ProfilingEvents.OnMessageDecodeEnds?.Invoke(MessagingTypes.SCENE_UPDATE); |
| | 640 | |
|
| 0 | 641 | | IWorldState worldState = Environment.i.world.state; |
| | 642 | |
|
| 0 | 643 | | if (worldState.TryGetScene(sceneData.id, out IParcelScene sceneInterface)) |
| | 644 | | { |
| 0 | 645 | | ParcelScene scene = sceneInterface as ParcelScene; |
| 0 | 646 | | scene.SetUpdateData(sceneData); |
| 0 | 647 | | } |
| | 648 | | else |
| | 649 | | { |
| 0 | 650 | | LoadParcelScenesExecute(sceneId); |
| | 651 | | } |
| 0 | 652 | | } |
| | 653 | |
|
| | 654 | | public void UpdateParcelScenesExecute(LoadParcelScenesMessage.UnityParcelScene scene) |
| | 655 | | { |
| 16 | 656 | | if (scene == null || scene.id == null) |
| 0 | 657 | | return; |
| | 658 | |
|
| 16 | 659 | | var sceneToLoad = scene; |
| | 660 | |
|
| 16 | 661 | | ProfilingEvents.OnMessageProcessStart?.Invoke(MessagingTypes.SCENE_UPDATE); |
| | 662 | |
|
| 16 | 663 | | ParcelScene parcelScene = Environment.i.world.state.GetScene(sceneToLoad.id) as ParcelScene; |
| | 664 | |
|
| 16 | 665 | | if (parcelScene != null) |
| 16 | 666 | | parcelScene.SetUpdateData(sceneToLoad); |
| | 667 | |
|
| 16 | 668 | | ProfilingEvents.OnMessageProcessEnds?.Invoke(MessagingTypes.SCENE_UPDATE); |
| 0 | 669 | | } |
| | 670 | |
|
| | 671 | | public void UnloadScene(string sceneKey) |
| | 672 | | { |
| 1 | 673 | | var queuedMessage = new QueuedSceneMessage() |
| | 674 | | { type = QueuedSceneMessage.Type.UNLOAD_PARCEL, message = sceneKey }; |
| | 675 | |
|
| 1 | 676 | | ProfilingEvents.OnMessageWillQueue?.Invoke(MessagingTypes.SCENE_DESTROY); |
| | 677 | |
|
| 1 | 678 | | Environment.i.messaging.manager.ForceEnqueueToGlobal(MessagingBusType.INIT, queuedMessage); |
| | 679 | |
|
| 1 | 680 | | Environment.i.messaging.manager.RemoveController(sceneKey); |
| | 681 | |
|
| 1 | 682 | | IWorldState worldState = Environment.i.world.state; |
| | 683 | |
|
| 1 | 684 | | if (worldState.loadedScenes.ContainsKey(sceneKey)) |
| | 685 | | { |
| 1 | 686 | | ParcelScene sceneToUnload = worldState.GetScene(sceneKey) as ParcelScene; |
| 1 | 687 | | sceneToUnload.isPersistent = false; |
| | 688 | |
|
| 1 | 689 | | if (sceneToUnload is GlobalScene globalScene && globalScene.isPortableExperience) |
| 0 | 690 | | OnNewPortableExperienceSceneRemoved?.Invoke(sceneKey); |
| | 691 | | } |
| 1 | 692 | | } |
| | 693 | |
|
| | 694 | | public void UnloadParcelSceneExecute(string sceneId) |
| | 695 | | { |
| 719 | 696 | | ProfilingEvents.OnMessageProcessStart?.Invoke(MessagingTypes.SCENE_DESTROY); |
| | 697 | |
|
| 719 | 698 | | IWorldState worldState = Environment.i.world.state; |
| | 699 | |
|
| 719 | 700 | | if (!worldState.Contains(sceneId) || worldState.loadedScenes[sceneId].isPersistent) |
| | 701 | | { |
| 1 | 702 | | return; |
| | 703 | | } |
| | 704 | |
|
| 718 | 705 | | var scene = worldState.loadedScenes[sceneId] as ParcelScene; |
| | 706 | |
|
| 718 | 707 | | if (scene == null) |
| 0 | 708 | | return; |
| | 709 | |
|
| 718 | 710 | | worldState.loadedScenes.Remove(sceneId); |
| 718 | 711 | | worldState.globalSceneIds.Remove(sceneId); |
| | 712 | |
|
| | 713 | | // Remove the scene id from the msg. priorities list |
| 718 | 714 | | worldState.scenesSortedByDistance.Remove(scene); |
| | 715 | |
|
| | 716 | | // Remove messaging controller for unloaded scene |
| 718 | 717 | | Environment.i.messaging.manager.RemoveController(scene.sceneData.id); |
| | 718 | |
|
| 718 | 719 | | scene.Cleanup(!CommonScriptableObjects.rendererState.Get()); |
| | 720 | |
|
| 718 | 721 | | if (VERBOSE) |
| | 722 | | { |
| 0 | 723 | | Debug.Log($"{Time.frameCount} : Destroying scene {scene.sceneData.basePosition}"); |
| | 724 | | } |
| | 725 | |
|
| 718 | 726 | | Environment.i.world.blockersController.SetupWorldBlockers(); |
| | 727 | |
|
| 718 | 728 | | ProfilingEvents.OnMessageProcessEnds?.Invoke(MessagingTypes.SCENE_DESTROY); |
| 0 | 729 | | } |
| | 730 | |
|
| | 731 | | public void UnloadAllScenes(bool includePersistent = false) |
| | 732 | | { |
| 705 | 733 | | var worldState = Environment.i.world.state; |
| | 734 | |
|
| 705 | 735 | | if (includePersistent) |
| | 736 | | { |
| 1420 | 737 | | var persistentScenes = worldState.loadedScenes.Where(x => x.Value.isPersistent); |
| | 738 | |
|
| 2790 | 739 | | foreach (var kvp in persistentScenes) |
| | 740 | | { |
| 691 | 741 | | if (kvp.Value is ParcelScene scene) |
| | 742 | | { |
| 691 | 743 | | scene.isPersistent = false; |
| | 744 | | } |
| | 745 | | } |
| | 746 | | } |
| | 747 | |
|
| 705 | 748 | | var list = worldState.loadedScenes.ToArray(); |
| | 749 | |
|
| 2846 | 750 | | for (int i = 0; i < list.Length; i++) |
| | 751 | | { |
| 718 | 752 | | UnloadParcelSceneExecute(list[i].Key); |
| | 753 | | } |
| 705 | 754 | | } |
| | 755 | |
|
| | 756 | | public void LoadParcelScenes(string decentralandSceneJSON) |
| | 757 | | { |
| 27 | 758 | | var queuedMessage = new QueuedSceneMessage() |
| | 759 | | { |
| | 760 | | type = QueuedSceneMessage.Type.LOAD_PARCEL, |
| | 761 | | message = decentralandSceneJSON |
| | 762 | | }; |
| | 763 | |
|
| 27 | 764 | | ProfilingEvents.OnMessageWillQueue?.Invoke(MessagingTypes.SCENE_LOAD); |
| | 765 | |
|
| 27 | 766 | | Environment.i.messaging.manager.ForceEnqueueToGlobal(MessagingBusType.INIT, queuedMessage); |
| | 767 | |
|
| 27 | 768 | | if (VERBOSE) |
| 0 | 769 | | Debug.Log($"{Time.frameCount} : Load parcel scene queue {decentralandSceneJSON}"); |
| 27 | 770 | | } |
| | 771 | |
|
| | 772 | | public void UpdateParcelScenes(string decentralandSceneJSON) |
| | 773 | | { |
| 0 | 774 | | var queuedMessage = new QueuedSceneMessage() |
| | 775 | | { type = QueuedSceneMessage.Type.UPDATE_PARCEL, message = decentralandSceneJSON }; |
| | 776 | |
|
| 0 | 777 | | ProfilingEvents.OnMessageWillQueue?.Invoke(MessagingTypes.SCENE_UPDATE); |
| | 778 | |
|
| 0 | 779 | | Environment.i.messaging.manager.ForceEnqueueToGlobal(MessagingBusType.INIT, queuedMessage); |
| 0 | 780 | | } |
| | 781 | |
|
| | 782 | | public void UnloadAllScenesQueued() |
| | 783 | | { |
| 0 | 784 | | var queuedMessage = new QueuedSceneMessage() { type = QueuedSceneMessage.Type.UNLOAD_SCENES }; |
| | 785 | |
|
| 0 | 786 | | ProfilingEvents.OnMessageWillQueue?.Invoke(MessagingTypes.SCENE_DESTROY); |
| | 787 | |
|
| 0 | 788 | | Environment.i.messaging.manager.ForceEnqueueToGlobal(MessagingBusType.INIT, queuedMessage); |
| 0 | 789 | | } |
| | 790 | |
|
| | 791 | | public void CreateGlobalScene(string json) |
| | 792 | | { |
| | 793 | | #if UNITY_EDITOR |
| 21 | 794 | | DebugConfig debugConfig = DataStore.i.debugConfig; |
| | 795 | |
|
| 21 | 796 | | if (debugConfig.soloScene && debugConfig.ignoreGlobalScenes) |
| 0 | 797 | | return; |
| | 798 | | #endif |
| 21 | 799 | | CreateGlobalSceneMessage globalScene = Utils.SafeFromJson<CreateGlobalSceneMessage>(json); |
| | 800 | |
|
| 21 | 801 | | string newGlobalSceneId = globalScene.id; |
| | 802 | |
|
| 21 | 803 | | IWorldState worldState = Environment.i.world.state; |
| | 804 | |
|
| 21 | 805 | | if (worldState.loadedScenes.ContainsKey(newGlobalSceneId)) |
| 0 | 806 | | return; |
| | 807 | |
|
| 21 | 808 | | var newGameObject = new GameObject("Global Scene - " + newGlobalSceneId); |
| | 809 | |
|
| 21 | 810 | | var newScene = newGameObject.AddComponent<GlobalScene>(); |
| 21 | 811 | | newScene.ownerController = this; |
| 21 | 812 | | newScene.unloadWithDistance = false; |
| 21 | 813 | | newScene.isPersistent = true; |
| 21 | 814 | | newScene.sceneName = globalScene.name; |
| 21 | 815 | | newScene.isPortableExperience = globalScene.isPortableExperience; |
| | 816 | |
|
| 21 | 817 | | LoadParcelScenesMessage.UnityParcelScene data = new LoadParcelScenesMessage.UnityParcelScene |
| | 818 | | { |
| | 819 | | id = newGlobalSceneId, |
| | 820 | | basePosition = new Vector2Int(0, 0), |
| | 821 | | baseUrl = globalScene.baseUrl, |
| | 822 | | contents = globalScene.contents |
| | 823 | | }; |
| | 824 | |
|
| 21 | 825 | | newScene.SetData(data); |
| | 826 | |
|
| 21 | 827 | | if (!string.IsNullOrEmpty(globalScene.icon)) |
| 0 | 828 | | newScene.iconUrl = newScene.contentProvider.GetContentsUrl(globalScene.icon); |
| | 829 | |
|
| 21 | 830 | | worldState.loadedScenes.Add(newGlobalSceneId, newScene); |
| 21 | 831 | | OnNewSceneAdded?.Invoke(newScene); |
| | 832 | |
|
| 21 | 833 | | if (newScene.isPortableExperience) |
| 0 | 834 | | OnNewPortableExperienceSceneAdded?.Invoke(newScene); |
| | 835 | |
|
| 21 | 836 | | worldState.globalSceneIds.Add(newGlobalSceneId); |
| | 837 | |
|
| 21 | 838 | | Environment.i.messaging.manager.AddControllerIfNotExists(this, newGlobalSceneId, isGlobal: true); |
| | 839 | |
|
| 21 | 840 | | if (VERBOSE) |
| | 841 | | { |
| 0 | 842 | | Debug.Log($"Creating Global scene {newGlobalSceneId}"); |
| | 843 | | } |
| 21 | 844 | | } |
| | 845 | |
|
| | 846 | | public void IsolateScene(IParcelScene sceneToActive) |
| | 847 | | { |
| 196 | 848 | | foreach (IParcelScene scene in Environment.i.world.state.scenesSortedByDistance) |
| | 849 | | { |
| 49 | 850 | | if (scene != sceneToActive) |
| 0 | 851 | | scene.GetSceneTransform().gameObject.SetActive(false); |
| | 852 | | } |
| 49 | 853 | | } |
| | 854 | |
|
| | 855 | | public void ReIntegrateIsolatedScene() |
| | 856 | | { |
| 28 | 857 | | foreach (IParcelScene scene in Environment.i.world.state.scenesSortedByDistance) |
| | 858 | | { |
| 7 | 859 | | scene.GetSceneTransform().gameObject.SetActive(true); |
| | 860 | | } |
| 7 | 861 | | } |
| | 862 | |
|
| | 863 | | //====================================================================== |
| | 864 | |
|
| | 865 | | #endregion |
| | 866 | |
|
| | 867 | | //====================================================================== |
| | 868 | |
|
| 682 | 869 | | public Queue<QueuedSceneMessage_Scene> sceneMessagesPool { get; } = new Queue<QueuedSceneMessage_Scene>(); |
| | 870 | |
|
| 1339 | 871 | | public bool prewarmSceneMessagesPool { get; set; } = true; |
| 1339 | 872 | | public bool prewarmEntitiesPool { get; set; } = true; |
| | 873 | |
|
| | 874 | | private bool sceneSortDirty = false; |
| 682 | 875 | | private bool positionDirty = true; |
| | 876 | | private int lastSortFrame = 0; |
| | 877 | |
|
| | 878 | | public event Action OnSortScenes; |
| | 879 | | public event Action<IParcelScene, string> OnOpenExternalUrlRequest; |
| | 880 | | public event Action<IParcelScene> OnNewSceneAdded; |
| | 881 | | public event Action<IParcelScene> OnNewPortableExperienceSceneAdded; |
| | 882 | | public event Action<string> OnNewPortableExperienceSceneRemoved; |
| | 883 | |
|
| 682 | 884 | | private Vector2Int currentGridSceneCoordinate = new Vector2Int(EnvironmentSettings.MORDOR_SCALAR, EnvironmentSet |
| 682 | 885 | | private Vector2Int sortAuxiliaryVector = new Vector2Int(EnvironmentSettings.MORDOR_SCALAR, EnvironmentSettings.M |
| | 886 | |
|
| | 887 | | public const string EMPTY_GO_POOL_NAME = "Empty"; |
| | 888 | | } |
| | 889 | | } |