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