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