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