| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Models; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Threading; |
| | 6 | | using UnityEngine; |
| | 7 | |
|
| | 8 | | namespace DCL.Controllers.LoadingScreenV2 |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// The SceneHintRequestSource class implements the IHintRequestSource interface for retrieving loading hints fr |
| | 12 | | /// It monitors the scene loading process, checking the loading status and extracting hints. |
| | 13 | | /// </summary> |
| | 14 | | public class SceneHintRequestSource : IHintRequestSource |
| | 15 | | { |
| | 16 | | private const int MAX_WAIT_FOR_SCENE = 1; |
| | 17 | |
|
| | 18 | | private readonly Vector2Int currentDestination; |
| | 19 | | private readonly ISceneController sceneController; |
| | 20 | | private LoadParcelScenesMessage.UnityParcelScene currentSceneBeingLoaded; |
| | 21 | | private UniTaskCompletionSource<bool> sceneLoadedCompletionSource; |
| | 22 | |
|
| 0 | 23 | | public SceneHintRequestSource(string sceneJson, SourceTag sourceTag, ISceneController sceneController, Vector2In |
| | 24 | | { |
| 0 | 25 | | Source = sceneJson; |
| 0 | 26 | | this.SourceTag = sourceTag; |
| 0 | 27 | | LoadingHints = new List<Hint>(); |
| | 28 | |
|
| 0 | 29 | | this.sceneController = sceneController; |
| 0 | 30 | | this.currentDestination = currentDestination; |
| | 31 | |
|
| 0 | 32 | | sceneLoadedCompletionSource = new UniTaskCompletionSource<bool>(); |
| 0 | 33 | | sceneController.OnNewSceneAdded += SceneController_OnNewSceneAdded; |
| 0 | 34 | | } |
| | 35 | |
|
| 0 | 36 | | public string Source { get; } |
| 0 | 37 | | public SourceTag SourceTag { get; } |
| 0 | 38 | | public List<Hint> LoadingHints { get; } |
| | 39 | |
|
| | 40 | | public async UniTask<List<Hint>> GetHintsAsync(CancellationToken ctx) |
| | 41 | | { |
| | 42 | | try |
| | 43 | | { |
| 0 | 44 | | await UniTask.WhenAny(sceneLoadedCompletionSource.Task, UniTask.Delay(TimeSpan.FromSeconds(MAX_WAIT_FOR_ |
| | 45 | |
|
| 0 | 46 | | if (ctx.IsCancellationRequested) |
| 0 | 47 | | return LoadingHints; |
| | 48 | |
|
| 0 | 49 | | bool sceneLoaded = sceneLoadedCompletionSource.Task.Status == UniTaskStatus.Succeeded; |
| | 50 | |
|
| 0 | 51 | | if (sceneLoaded && currentSceneBeingLoaded?.loadingScreenHints != null && currentSceneBeingLoaded.loadin |
| | 52 | | { |
| 0 | 53 | | foreach (Hint basehint in currentSceneBeingLoaded.loadingScreenHints) |
| | 54 | | { |
| 0 | 55 | | if (basehint is Hint hint) { LoadingHints.Add(hint); } |
| | 56 | | } |
| | 57 | | } |
| 0 | 58 | | } |
| 0 | 59 | | catch (Exception ex) { Debug.LogWarning($"Exception in SceneHintRequestSource.GetHintsAsync: {ex.Message}\n{ |
| | 60 | |
|
| | 61 | | // Returns an empty list if the scene is not loaded or the hints are not present |
| 0 | 62 | | return LoadingHints; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | public void Dispose() |
| | 66 | | { |
| 0 | 67 | | sceneController.OnNewSceneAdded -= SceneController_OnNewSceneAdded; |
| 0 | 68 | | LoadingHints.Clear(); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | private void SceneController_OnNewSceneAdded(IParcelScene scene) |
| | 72 | | { |
| 0 | 73 | | if (scene != null && CheckTargetSceneWithCoords(scene)) |
| | 74 | | { |
| 0 | 75 | | currentSceneBeingLoaded = scene.sceneData; |
| 0 | 76 | | sceneLoadedCompletionSource.TrySetResult(true); |
| | 77 | |
|
| | 78 | | // Recreate the UniTaskCompletionSource to avoid InvalidOperationException on next scene load |
| 0 | 79 | | sceneLoadedCompletionSource = new UniTaskCompletionSource<bool>(); |
| | 80 | | } |
| 0 | 81 | | } |
| | 82 | |
|
| | 83 | | public bool CheckTargetSceneWithCoords(IParcelScene scene) => |
| 0 | 84 | | Environment.i.world.state.GetSceneNumberByCoords(currentDestination).Equals(scene.sceneData.sceneNumber); |
| | 85 | | } |
| | 86 | | } |