< Summary

Class:MainScripts.DCL.Controllers.AssetManager.AssetBundles.SceneAB.AssetPromise_SceneAB
Assembly:SceneAssetBundle
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/SceneAB/AssetPromise_SceneAB.cs
Covered lines:0
Uncovered lines:45
Coverable lines:45
Total lines:120
Line coverage:0% (0 of 45)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:8
Method coverage:0% (0 of 8)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_SceneAB(...)0%2100%
GetEntityIdFromSceneId(...)0%12300%
OnCancelLoading()0%2100%
OnLoad(...)0%2100%
AsyncOnLoad()0%1101000%
IsEmptyScene()0%2100%
OnBeforeLoadOrReuse()0%2100%
OnAfterLoadOrReuse()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/SceneAB/AssetPromise_SceneAB.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Helpers;
 4using System;
 5using System.Text.RegularExpressions;
 6using System.Threading;
 7using UnityEngine;
 8
 9namespace 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
 029        public AssetPromise_SceneAB(string contentUrl, string sceneId) : base(contentUrl, sceneId)
 30        {
 031            cancellationTokenSource = new CancellationTokenSource();
 032            this.hash = GetEntityIdFromSceneId(sceneId);
 033        }
 34
 35        private string GetEntityIdFromSceneId(string sceneId)
 36        {
 037            if (sceneId == null)
 038                return null;
 39
 40            // This case happens when loading worlds
 041            if (sceneId.StartsWith(URN_PREFIX))
 42            {
 043                sceneId = sceneId.Replace(URN_PREFIX, "");
 044                sceneId = Regex.Replace(sceneId, "\\?.+", ""); // from "?" char onwards we delete everything
 45            }
 46
 047            return sceneId;
 48        }
 49
 50        protected override void OnCancelLoading()
 51        {
 052            cancellationTokenSource.Cancel();
 053            cancellationTokenSource.Dispose();
 054        }
 55
 56        protected override void OnLoad(Action OnSuccess, Action<Exception> _)
 57        {
 058            onSuccess = OnSuccess;
 059            AsyncOnLoad().Forget();
 060        }
 61
 62        private async UniTaskVoid AsyncOnLoad()
 63        {
 064            asset = new Asset_SceneAB
 65            {
 66                id = hash,
 67            };
 68
 069            if (string.IsNullOrEmpty(contentUrl))
 70            {
 071                onSuccess();
 072                return;
 73            }
 74
 075            if (string.IsNullOrEmpty(hash))
 76            {
 077                onSuccess();
 078                return;
 79            }
 80
 081            var finalUrl = $"{contentUrl}manifest/{hash}.json";
 82
 83            try
 84            {
 085                var result = await webRequestController.Ref.GetAsync(finalUrl, cancellationToken: cancellationTokenSourc
 86
 087                if (!string.IsNullOrEmpty(result.error))
 88                {
 089                    onSuccess();
 090                    return;
 91                }
 92
 093                string data = result.downloadHandler.text;
 094                var sceneAb = Utils.SafeFromJson<SceneAbDto>(data);
 095                asset.Setup(sceneAb, contentUrl);
 96
 97#if UNITY_EDITOR
 098                var version = int.Parse(sceneAb.version.Replace("v", ""));
 99                // increment this value if it gets old enough
 0100                if (version < 5)
 0101                    Debug.LogWarning($"Entity {hash} AB version v{version} is too old, last converted on {sceneAb.date}"
 102#endif
 0103            }
 0104            catch (OperationCanceledException) { }
 0105            catch (UnityWebRequestException)
 106            {
 0107                if (!IsEmptyScene())
 0108                    Debug.LogError("No Asset Bundles for scene " + finalUrl);
 0109            }
 0110            finally { onSuccess(); }
 0111        }
 112
 113        private bool IsEmptyScene() =>
 0114            hash.Contains(",");
 115
 0116        protected override void OnBeforeLoadOrReuse() { }
 117
 0118        protected override void OnAfterLoadOrReuse() { }
 119    }
 120}