< Summary

Class:DCL.Components.RendereableAssetLoadHelper
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/LoadWrapper/RendereableAssetLoadHelper.cs
Covered lines:41
Uncovered lines:38
Coverable lines:79
Total lines:204
Line coverage:51.8% (41 of 79)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
RendereableAssetLoadHelper(...)0%110100%
ToString()0%20400%
Load(...)0%4.184077.78%
Unload()0%110100%
LoadAssetBundle(...)0%24.247029.41%
LoadGltf(...)0%4.914061.54%
OnFailWrapper(...)0%220100%
OnSuccessWrapper(...)0%4.594066.67%
ClearEvents()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/LoadableShapes/LoadWrapper/RendereableAssetLoadHelper.cs

#LineLine coverage
 1using System;
 2using UnityEngine;
 3using UnityEngine.Assertions;
 4
 5namespace DCL.Components
 6{
 7    public class RendereableAssetLoadHelper
 8    {
 9        public enum LoadingType
 10        {
 11            ASSET_BUNDLE_WITH_GLTF_FALLBACK,
 12            ASSET_BUNDLE_ONLY,
 13            GLTF_ONLY
 14        }
 15
 16        public static bool VERBOSE = false;
 17
 18        public static bool useCustomContentServerUrl = false;
 19        public static string customContentServerUrl;
 20
 21        public static LoadingType loadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK;
 22
 8823        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 24
 025        public GameObject loadedAsset { get; protected set; }
 26
 27        public bool isFinished
 28        {
 29            get
 30            {
 1831                if (gltfPromise != null)
 1832                    return gltfPromise.state == AssetPromiseState.FINISHED;
 33
 034                if (abPromise != null)
 035                    return abPromise.state == AssetPromiseState.FINISHED;
 36
 037                return true;
 38            }
 39        }
 40
 41        string bundlesContentUrl;
 42        ContentProvider contentProvider;
 43
 44        AssetPromise_GLTF gltfPromise;
 45        AssetPromise_AB_GameObject abPromise;
 46
 47#if UNITY_EDITOR
 48        public override string ToString()
 49        {
 050            float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime;
 51
 052            string result = "not loading";
 53
 054            if (gltfPromise != null)
 55            {
 056                result = $"GLTF -> promise state = {gltfPromise.state} ({loadTime} load time)... waiting promises = {Ass
 57
 058                if (gltfPromise.state == AssetPromiseState.WAITING)
 59                {
 060                    result += $"\nmaster promise state... is blocked... {AssetPromiseKeeper_GLTF.i.GetMasterState(gltfPr
 61                }
 62            }
 63
 064            if (abPromise != null)
 65            {
 066                result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load time)... waiting prom
 67            }
 68
 069            return result;
 70        }
 71
 72        float loadStartTime = 0;
 8873        float loadFinishTime = float.MaxValue;
 74#endif
 75
 8876        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl)
 77        {
 8878            this.contentProvider = contentProvider;
 8879            this.bundlesContentUrl = bundlesContentUrl;
 8880        }
 81
 82        public event Action<GameObject> OnSuccessEvent;
 83        public event Action OnFailEvent;
 84
 85        public void Load(string targetUrl)
 86        {
 8887            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 88#if UNITY_EDITOR
 8889            loadStartTime = Time.realtimeSinceStartup;
 90#endif
 8891            switch (loadingType)
 92            {
 93                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 17494                    LoadAssetBundle(targetUrl, OnSuccessEvent, () => LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent));
 8795                    break;
 96                case LoadingType.ASSET_BUNDLE_ONLY:
 197                    LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent);
 198                    break;
 99                case LoadingType.GLTF_ONLY:
 0100                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 101                    break;
 102            }
 0103        }
 104
 105        public void Unload()
 106        {
 81107            AssetPromiseKeeper_GLTF.i.Forget(gltfPromise);
 81108            AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise);
 81109        }
 110
 111        void LoadAssetBundle(string targetUrl, Action<GameObject> OnSuccess, Action OnFail)
 112        {
 88113            if (abPromise != null)
 114            {
 0115                AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise);
 116
 0117                if (VERBOSE)
 0118                    Debug.Log("Forgetting not null promise..." + targetUrl);
 119            }
 120
 88121            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 122
 88123            if (string.IsNullOrEmpty(bundlesBaseUrl))
 124            {
 88125                OnFailWrapper(null, OnFail);
 88126                return;
 127            }
 128
 0129            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 130            {
 0131                OnFailWrapper(null, OnFail);
 0132                return;
 133            }
 134
 0135            abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash);
 0136            abPromise.settings = this.settings;
 137
 0138            abPromise.OnSuccessEvent += (x) => OnSuccessWrapper(x, OnSuccess);
 0139            abPromise.OnFailEvent += (x) => OnFailWrapper(x, OnFail);
 140
 0141            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0142        }
 143
 144        void LoadGltf(string targetUrl, Action<GameObject> OnSuccess, Action OnFail)
 145        {
 87146            if (gltfPromise != null)
 147            {
 0148                AssetPromiseKeeper_GLTF.i.Forget(gltfPromise);
 149
 0150                if (VERBOSE)
 0151                    Debug.Log("Forgetting not null promise... " + targetUrl);
 152            }
 153
 87154            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 155            {
 0156                OnFailWrapper(null, OnFail);
 0157                return;
 158            }
 159
 87160            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 87161            gltfPromise.settings = this.settings;
 162
 152163            gltfPromise.OnSuccessEvent += (x) => OnSuccessWrapper(x, OnSuccess);
 90164            gltfPromise.OnFailEvent += (x) => OnFailWrapper(x, OnFail);
 165
 87166            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 87167        }
 168
 169        private void OnFailWrapper(Asset_WithPoolableContainer loadedAsset, Action OnFail)
 170        {
 171#if UNITY_EDITOR
 91172            loadFinishTime = Time.realtimeSinceStartup;
 173#endif
 174
 175
 91176            OnFail?.Invoke();
 91177            ClearEvents();
 91178        }
 179
 180        private void OnSuccessWrapper(Asset_WithPoolableContainer loadedAsset, Action<GameObject> OnSuccess)
 181        {
 182#if UNITY_EDITOR
 65183            loadFinishTime = Time.realtimeSinceStartup;
 184#endif
 65185            if (VERBOSE)
 186            {
 0187                if (gltfPromise != null)
 0188                    Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!");
 189                else
 0190                    Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!");
 191            }
 192
 65193            this.loadedAsset = loadedAsset.container;
 65194            OnSuccess?.Invoke(loadedAsset.container);
 65195            ClearEvents();
 65196        }
 197
 198        public void ClearEvents()
 199        {
 0200            OnSuccessEvent = null;
 0201            OnFailEvent = null;
 0202        }
 203    }
 204}