< Summary

Class:DCL.Components.RendereableAssetLoadHelper
Assembly:RendereableAssetLoadHelper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/RendereableAssetLoadHelper/RendereableAssetLoadHelper.cs
Covered lines:49
Uncovered lines:42
Coverable lines:91
Total lines:247
Line coverage:53.8% (49 of 91)
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%6.396077.78%
Unload()0%110100%
UnloadAB()0%2.152066.67%
UnloadGLTF()0%220100%
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/Helpers/RendereableAssetLoadHelper/RendereableAssetLoadHelper.cs

#LineLine coverage
 1using UnityEngine;
 2using UnityEngine.Assertions;
 3
 4namespace DCL.Components
 5{
 6    public class RendereableAssetLoadHelper
 7    {
 8        public enum LoadingType
 9        {
 10            ASSET_BUNDLE_WITH_GLTF_FALLBACK,
 11            ASSET_BUNDLE_ONLY,
 12            GLTF_ONLY,
 13            DEFAULT
 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 defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK;
 22
 9323        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 24
 025        public Rendereable loadedAsset { get; protected set; }
 26
 27        public bool isFinished
 28        {
 29            get
 30            {
 2131                if (gltfPromise != null)
 2132                    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;
 9373        float loadFinishTime = float.MaxValue;
 74#endif
 75
 9376        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl)
 77        {
 9378            this.contentProvider = contentProvider;
 9379            this.bundlesContentUrl = bundlesContentUrl;
 9380        }
 81
 82        public event System.Action<Rendereable> OnSuccessEvent;
 83        public event System.Action OnFailEvent;
 84
 85        public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT)
 86        {
 9387            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 88#if UNITY_EDITOR
 9389            loadStartTime = Time.realtimeSinceStartup;
 90#endif
 91
 9392            LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading
 93            switch (finalLoadingType)
 94            {
 95                case LoadingType.ASSET_BUNDLE_ONLY:
 096                    LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent);
 097                    break;
 98                case LoadingType.GLTF_ONLY:
 1499                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 14100                    break;
 101                case LoadingType.DEFAULT:
 102                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 158103                    LoadAssetBundle(targetUrl, OnSuccessEvent, () => LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent));
 104                    break;
 105            }
 79106        }
 107
 108        public void Unload()
 109        {
 86110            UnloadAB();
 86111            UnloadGLTF();
 86112        }
 113
 114        void UnloadAB()
 115        {
 86116            if ( abPromise != null )
 117            {
 0118                AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise);
 119            }
 86120        }
 121
 122        void UnloadGLTF()
 123        {
 86124            if ( gltfPromise != null )
 125            {
 86126                AssetPromiseKeeper_GLTF.i.Forget(gltfPromise);
 127            }
 86128        }
 129
 130        void LoadAssetBundle(string targetUrl, System.Action<Rendereable> OnSuccess, System.Action OnFail)
 131        {
 79132            if (abPromise != null)
 133            {
 0134                UnloadAB();
 0135                if (VERBOSE)
 0136                    Debug.Log("Forgetting not null promise..." + targetUrl);
 137            }
 138
 79139            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 140
 79141            if (string.IsNullOrEmpty(bundlesBaseUrl))
 142            {
 79143                OnFailWrapper(null, OnFail);
 79144                return;
 145            }
 146
 0147            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 148            {
 0149                OnFailWrapper(null, OnFail);
 0150                return;
 151            }
 152
 0153            abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash);
 0154            abPromise.settings = this.settings;
 155
 0156            abPromise.OnSuccessEvent += (x) =>
 157            {
 0158                var r = new Rendereable()
 159                {
 160                    container = x.container,
 161                    totalTriangleCount = x.totalTriangleCount,
 162                    meshes = x.meshes,
 163                    renderers = x.renderers,
 164                    meshToTriangleCount = x.meshToTriangleCount
 165                };
 166
 0167                OnSuccessWrapper(r, OnSuccess);
 0168            };
 169
 0170            abPromise.OnFailEvent += (x) => OnFailWrapper(x, OnFail);
 171
 0172            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0173        }
 174
 175        void LoadGltf(string targetUrl, System.Action<Rendereable> OnSuccess, System.Action OnFail)
 176        {
 93177            if (gltfPromise != null)
 178            {
 0179                UnloadGLTF();
 180
 0181                if (VERBOSE)
 0182                    Debug.Log("Forgetting not null promise... " + targetUrl);
 183            }
 184
 93185            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 186            {
 0187                OnFailWrapper(null, OnFail);
 0188                return;
 189            }
 190
 93191            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 93192            gltfPromise.settings = this.settings;
 193
 93194            gltfPromise.OnSuccessEvent += (x) =>
 195            {
 82196                var r = new Rendereable()
 197                {
 198                    container = x.container,
 199                    totalTriangleCount = x.totalTriangleCount,
 200                    meshes = x.meshes,
 201                    renderers = x.renderers,
 202                    meshToTriangleCount = x.meshToTriangleCount
 203                };
 204
 82205                OnSuccessWrapper(r, OnSuccess);
 82206            };
 97207            gltfPromise.OnFailEvent += (x) => OnFailWrapper(x, OnFail);
 208
 93209            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 93210        }
 211
 212        private void OnFailWrapper(Asset_WithPoolableContainer loadedAsset, System.Action OnFail)
 213        {
 214#if UNITY_EDITOR
 83215            loadFinishTime = Time.realtimeSinceStartup;
 216#endif
 217
 218
 83219            OnFail?.Invoke();
 83220            ClearEvents();
 83221        }
 222
 223        private void OnSuccessWrapper(Rendereable loadedAsset, System.Action<Rendereable> OnSuccess)
 224        {
 225#if UNITY_EDITOR
 82226            loadFinishTime = Time.realtimeSinceStartup;
 227#endif
 82228            if (VERBOSE)
 229            {
 0230                if (gltfPromise != null)
 0231                    Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!");
 232                else
 0233                    Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!");
 234            }
 235
 82236            this.loadedAsset = loadedAsset;
 82237            OnSuccess?.Invoke(loadedAsset);
 82238            ClearEvents();
 82239        }
 240
 241        public void ClearEvents()
 242        {
 0243            OnSuccessEvent = null;
 0244            OnFailEvent = null;
 0245        }
 246    }
 247}