| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using JetBrains.Annotations; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Networking; |
| | 10 | | using Environment = DCL.Environment; |
| | 11 | |
|
| | 12 | | namespace MainScripts.DCL.Controllers.HotScenes |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// We have to keep this class to communicate with Kernel |
| | 16 | | /// </summary> |
| | 17 | | public class HotScenesController : MonoBehaviour, IHotScenesController |
| | 18 | | { |
| | 19 | | [Obsolete] |
| 1 | 20 | | public static HotScenesController i { get; private set; } |
| | 21 | |
|
| | 22 | | [Obsolete] |
| | 23 | | public event Action OnHotSceneListFinishUpdating; |
| | 24 | |
|
| | 25 | | [Obsolete] |
| | 26 | | public event Action OnHotSceneListChunkUpdated; |
| | 27 | |
|
| 3 | 28 | | public List<IHotScenesController.HotSceneInfo> hotScenesList => hotScenes; |
| | 29 | |
|
| | 30 | | [Obsolete] |
| 0 | 31 | | public float timeSinceLastUpdate => Time.realtimeSinceStartup - lastUpdateTime; |
| | 32 | |
|
| | 33 | | [Obsolete] |
| 1 | 34 | | private float lastUpdateTime = float.MinValue * .5f; |
| | 35 | |
|
| 1 | 36 | | private List<IHotScenesController.HotSceneInfo> hotScenes = new (); |
| | 37 | |
|
| | 38 | | [Serializable] |
| | 39 | | internal struct HotScenesUpdatePayload |
| | 40 | | { |
| | 41 | | public int chunkIndex; |
| | 42 | | public int chunksCount; |
| | 43 | | public IHotScenesController.HotSceneInfo[] scenesInfo; |
| | 44 | | } |
| | 45 | |
|
| 1 | 46 | | private readonly AutoResetUniTaskCompletionSource<IReadOnlyList<IHotScenesController.HotSceneInfo>> source |
| | 47 | | = AutoResetUniTaskCompletionSource<IReadOnlyList<IHotScenesController.HotSceneInfo>>.Create(); |
| | 48 | |
|
| | 49 | | private void Awake() |
| | 50 | | { |
| 1 | 51 | | i = this; |
| 1 | 52 | | } |
| | 53 | |
|
| | 54 | | public UniTask<IReadOnlyList<IHotScenesController.HotSceneInfo>> GetHotScenesListAsync(CancellationToken cancell |
| | 55 | | { |
| 0 | 56 | | WebInterface.FetchHotScenes(); |
| 0 | 57 | | return source.Task.AttachExternalCancellation(cancellationToken); |
| | 58 | | } |
| | 59 | |
|
| | 60 | | public async UniTask<IReadOnlyList<IHotScenesController.HotWorldInfo.WorldInfo>> GetHotWorldsListAsync(Cancellat |
| | 61 | | { |
| 0 | 62 | | UnityWebRequest result = await Environment.i.platform.webRequest.GetAsync("https://worlds-content-server.dec |
| | 63 | |
|
| 0 | 64 | | if (result.result != UnityWebRequest.Result.Success) |
| 0 | 65 | | throw new Exception($"Error fetching hot world info:\n{result.error}"); |
| | 66 | |
|
| 0 | 67 | | var response = Utils.SafeFromJson<IHotScenesController.HotWorldInfo>(result.downloadHandler.text); |
| | 68 | |
|
| 0 | 69 | | if (response == null) |
| 0 | 70 | | throw new Exception($"Error parsing hot world info:\n{result.downloadHandler.text}"); |
| | 71 | |
|
| 0 | 72 | | if (response.data == null) |
| 0 | 73 | | throw new Exception($"No worlds info retrieved:\n{result.downloadHandler.text}"); |
| | 74 | |
|
| 0 | 75 | | return response.data.perWorld != null ? response.data.perWorld : new List<IHotScenesController.HotWorldInfo. |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | [UsedImplicitly] |
| | 79 | | public void UpdateHotScenesList(string json) |
| | 80 | | { |
| 1 | 81 | | var updatePayload = Utils.SafeFromJson<HotScenesUpdatePayload>(json); |
| | 82 | |
|
| 2 | 83 | | if (updatePayload.chunkIndex == 0) { hotScenes.Clear(); } |
| | 84 | |
|
| 1 | 85 | | hotScenes.AddRange(updatePayload.scenesInfo); |
| 1 | 86 | | OnHotSceneListChunkUpdated?.Invoke(); |
| | 87 | |
|
| 1 | 88 | | if (updatePayload.chunkIndex >= updatePayload.chunksCount - 1) |
| | 89 | | { |
| 1 | 90 | | lastUpdateTime = Time.realtimeSinceStartup; |
| 1 | 91 | | OnHotSceneListFinishUpdating?.Invoke(); |
| 1 | 92 | | source.TrySetResult(hotScenesList); |
| | 93 | | } |
| 1 | 94 | | } |
| | 95 | |
|
| | 96 | | public void Dispose() |
| | 97 | | { |
| 0 | 98 | | hotScenes.Clear(); |
| 0 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | public void Initialize() { } |
| | 102 | | } |
| | 103 | | } |