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