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