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