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