| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using System; |
| | 5 | | using System.Text.RegularExpressions; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace MainScripts.DCL.Controllers.AssetManager.AssetBundles.SceneAB |
| | 10 | | { |
| | 11 | | // this datatype is defined by https://github.com/decentraland/asset-bundle-converter |
| | 12 | | [Serializable] |
| | 13 | | public class SceneAbDto |
| | 14 | | { |
| | 15 | | public string version; |
| | 16 | | public string[] files; |
| | 17 | | public int exitCode; |
| | 18 | | public string date; |
| | 19 | | } |
| | 20 | |
|
| | 21 | | public class AssetPromise_SceneAB : AssetPromise_WithUrl<Asset_SceneAB> |
| | 22 | | { |
| | 23 | | private const string URN_PREFIX = "urn:decentraland:entity:"; |
| | 24 | | private readonly CancellationTokenSource cancellationTokenSource; |
| | 25 | | private Service<IWebRequestController> webRequestController; |
| | 26 | |
|
| | 27 | | private Action onSuccess; |
| | 28 | |
|
| 0 | 29 | | public AssetPromise_SceneAB(string contentUrl, string sceneId) : base(contentUrl, sceneId) |
| | 30 | | { |
| 0 | 31 | | cancellationTokenSource = new CancellationTokenSource(); |
| 0 | 32 | | this.hash = GetEntityIdFromSceneId(sceneId); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | private string GetEntityIdFromSceneId(string sceneId) |
| | 36 | | { |
| 0 | 37 | | if (sceneId == null) |
| 0 | 38 | | return null; |
| | 39 | |
|
| | 40 | | // This case happens when loading worlds |
| 0 | 41 | | if (sceneId.StartsWith(URN_PREFIX)) |
| | 42 | | { |
| 0 | 43 | | sceneId = sceneId.Replace(URN_PREFIX, ""); |
| 0 | 44 | | sceneId = Regex.Replace(sceneId, "\\?.+", ""); // from "?" char onwards we delete everything |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | return sceneId; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | protected override void OnCancelLoading() |
| | 51 | | { |
| 0 | 52 | | cancellationTokenSource.Cancel(); |
| 0 | 53 | | cancellationTokenSource.Dispose(); |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | protected override void OnLoad(Action OnSuccess, Action<Exception> _) |
| | 57 | | { |
| 0 | 58 | | onSuccess = OnSuccess; |
| 0 | 59 | | AsyncOnLoad().Forget(); |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | private async UniTaskVoid AsyncOnLoad() |
| | 63 | | { |
| 0 | 64 | | asset = new Asset_SceneAB |
| | 65 | | { |
| | 66 | | id = hash, |
| | 67 | | }; |
| | 68 | |
|
| 0 | 69 | | if (string.IsNullOrEmpty(contentUrl)) |
| | 70 | | { |
| 0 | 71 | | onSuccess(); |
| 0 | 72 | | return; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | if (string.IsNullOrEmpty(hash)) |
| | 76 | | { |
| 0 | 77 | | onSuccess(); |
| 0 | 78 | | return; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | var finalUrl = $"{contentUrl}manifest/{hash}.json"; |
| | 82 | |
|
| | 83 | | try |
| | 84 | | { |
| 0 | 85 | | var result = await webRequestController.Ref.GetAsync(finalUrl, cancellationToken: cancellationTokenSourc |
| | 86 | |
|
| 0 | 87 | | if (!string.IsNullOrEmpty(result.error)) |
| | 88 | | { |
| 0 | 89 | | onSuccess(); |
| 0 | 90 | | return; |
| | 91 | | } |
| | 92 | |
|
| 0 | 93 | | string data = result.downloadHandler.text; |
| 0 | 94 | | var sceneAb = Utils.SafeFromJson<SceneAbDto>(data); |
| 0 | 95 | | asset.Setup(sceneAb, contentUrl); |
| | 96 | |
|
| | 97 | | #if UNITY_EDITOR |
| 0 | 98 | | var version = int.Parse(sceneAb.version.Replace("v", "")); |
| | 99 | | // increment this value if it gets old enough |
| 0 | 100 | | if (version < 5) |
| 0 | 101 | | Debug.LogWarning($"Entity {hash} AB version v{version} is too old, last converted on {sceneAb.date}" |
| | 102 | | #endif |
| 0 | 103 | | } |
| 0 | 104 | | catch (OperationCanceledException) { } |
| 0 | 105 | | catch (UnityWebRequestException) |
| | 106 | | { |
| 0 | 107 | | if (!IsEmptyScene()) |
| 0 | 108 | | Debug.LogError("No Asset Bundles for scene " + finalUrl); |
| 0 | 109 | | } |
| 0 | 110 | | finally { onSuccess(); } |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | private bool IsEmptyScene() => |
| 0 | 114 | | hash.Contains(","); |
| | 115 | |
|
| 0 | 116 | | protected override void OnBeforeLoadOrReuse() { } |
| | 117 | |
|
| 0 | 118 | | protected override void OnAfterLoadOrReuse() { } |
| | 119 | | } |
| | 120 | | } |