< 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 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            DEFAULT
 15        }
 16
 17        public static bool VERBOSE = false;
 18
 19        public static bool useCustomContentServerUrl = false;
 20        public static string customContentServerUrl;
 21
 22        public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK;
 23
 9224        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 25
 026        public Rendereable loadedAsset { get; protected set; }
 27
 28        public bool isFinished
 29        {
 30            get
 31            {
 1832                if (gltfPromise != null)
 1833                    return gltfPromise.state == AssetPromiseState.FINISHED;
 34
 035                if (abPromise != null)
 036                    return abPromise.state == AssetPromiseState.FINISHED;
 37
 038                return true;
 39            }
 40        }
 41
 42        string bundlesContentUrl;
 43        ContentProvider contentProvider;
 44
 45        AssetPromise_GLTF gltfPromise;
 46        AssetPromise_AB_GameObject abPromise;
 47
 48#if UNITY_EDITOR
 49        public override string ToString()
 50        {
 051            float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime;
 52
 053            string result = "not loading";
 54
 055            if (gltfPromise != null)
 56            {
 057                result = $"GLTF -> promise state = {gltfPromise.state} ({loadTime} load time)... waiting promises = {Ass
 58
 059                if (gltfPromise.state == AssetPromiseState.WAITING)
 60                {
 061                    result += $"\nmaster promise state... is blocked... {AssetPromiseKeeper_GLTF.i.GetMasterState(gltfPr
 62                }
 63            }
 64
 065            if (abPromise != null)
 66            {
 067                result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load time)... waiting prom
 68            }
 69
 070            return result;
 71        }
 72
 73        float loadStartTime = 0;
 9274        float loadFinishTime = float.MaxValue;
 75#endif
 76
 9277        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl)
 78        {
 9279            this.contentProvider = contentProvider;
 9280            this.bundlesContentUrl = bundlesContentUrl;
 9281        }
 82
 83        public event Action<Rendereable> OnSuccessEvent;
 84        public event Action<Exception> OnFailEvent;
 85
 86        public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT)
 87        {
 9288            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 89#if UNITY_EDITOR
 9290            loadStartTime = Time.realtimeSinceStartup;
 91#endif
 92
 9293            LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading
 94            switch (finalLoadingType)
 95            {
 96                case LoadingType.ASSET_BUNDLE_ONLY:
 097                    LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent);
 098                    break;
 99                case LoadingType.GLTF_ONLY:
 13100                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 13101                    break;
 102                case LoadingType.DEFAULT:
 103                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 158104                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv
 105                    break;
 106            }
 79107        }
 108
 109        public void Unload()
 110        {
 79111            UnloadAB();
 79112            UnloadGLTF();
 79113        }
 114
 115        void UnloadAB()
 116        {
 79117            if ( abPromise != null )
 118            {
 0119                AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise);
 120            }
 79121        }
 122
 123        void UnloadGLTF()
 124        {
 79125            if ( gltfPromise != null )
 126            {
 79127                AssetPromiseKeeper_GLTF.i.Forget(gltfPromise);
 128            }
 79129        }
 130
 131        void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 132        {
 79133            if (abPromise != null)
 134            {
 0135                UnloadAB();
 0136                if (VERBOSE)
 0137                    Debug.Log("Forgetting not null promise..." + targetUrl);
 138            }
 139
 79140            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 141
 79142            if (string.IsNullOrEmpty(bundlesBaseUrl))
 143            {
 79144                OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"));
 79145                return;
 146            }
 147
 0148            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 149            {
 0150                OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}"));
 0151                return;
 152            }
 153
 0154            abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash);
 0155            abPromise.settings = this.settings;
 156
 0157            abPromise.OnSuccessEvent += (x) =>
 158            {
 0159                var r = new Rendereable()
 160                {
 161                    container = x.container,
 162                    totalTriangleCount = x.totalTriangleCount,
 163                    meshes = x.meshes,
 164                    renderers = x.renderers,
 165                    meshToTriangleCount = x.meshToTriangleCount
 166                };
 167
 0168                OnSuccessWrapper(r, OnSuccess);
 0169            };
 170
 0171            abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception);
 172
 0173            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0174        }
 175
 176        void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 177        {
 92178            if (gltfPromise != null)
 179            {
 0180                UnloadGLTF();
 181
 0182                if (VERBOSE)
 0183                    Debug.Log("Forgetting not null promise... " + targetUrl);
 184            }
 185
 92186            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 187            {
 0188                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"));
 0189                return;
 190            }
 191
 92192            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 92193            gltfPromise.settings = settings;
 194
 92195            gltfPromise.OnSuccessEvent += (x) =>
 196            {
 82197                var r = new Rendereable
 198                {
 199                    container = x.container,
 200                    totalTriangleCount = x.totalTriangleCount,
 201                    meshes = x.meshes,
 202                    renderers = x.renderers,
 203                    meshToTriangleCount = x.meshToTriangleCount
 204                };
 205
 82206                OnSuccessWrapper(r, OnSuccess);
 82207            };
 95208            gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception);
 209
 92210            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 92211        }
 212
 213        private void OnFailWrapper(Action<Exception> OnFail, Exception exception)
 214        {
 215#if UNITY_EDITOR
 82216            loadFinishTime = Time.realtimeSinceStartup;
 217#endif
 218
 82219            OnFail?.Invoke(exception);
 82220            ClearEvents();
 82221        }
 222
 223        private void OnSuccessWrapper(Rendereable loadedAsset, 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}