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