< 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:47
Uncovered lines:44
Coverable lines:91
Total lines:251
Line coverage:51.6% (47 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
 17324        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 25
 026        public Rendereable loadedAsset { get; protected set; }
 27
 28        public bool isFinished
 29        {
 30            get
 31            {
 032                if (gltfPromise != null)
 033                    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;
 17374        float loadFinishTime = float.MaxValue;
 75#endif
 76
 17377        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl)
 78        {
 17379            this.contentProvider = contentProvider;
 17380            this.bundlesContentUrl = bundlesContentUrl;
 17381        }
 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        {
 17388            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 89#if UNITY_EDITOR
 17390            loadStartTime = Time.realtimeSinceStartup;
 91#endif
 92
 17393            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:
 84100                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 84101                    break;
 102                case LoadingType.DEFAULT:
 103                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 178104                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv
 105                    break;
 106            }
 89107        }
 108
 109        public void Unload()
 110        {
 419111            UnloadAB();
 419112            UnloadGLTF();
 419113        }
 114
 115        void UnloadAB()
 116        {
 419117            if ( abPromise != null )
 118            {
 0119                AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise);
 120            }
 419121        }
 122
 123        void UnloadGLTF()
 124        {
 419125            if ( gltfPromise != null )
 126            {
 419127                AssetPromiseKeeper_GLTF.i.Forget(gltfPromise);
 128            }
 419129        }
 130
 131        void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 132        {
 89133            if (abPromise != null)
 134            {
 0135                UnloadAB();
 0136                if (VERBOSE)
 0137                    Debug.Log("Forgetting not null promise..." + targetUrl);
 138            }
 139
 89140            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 141
 89142            if (string.IsNullOrEmpty(bundlesBaseUrl))
 143            {
 89144                OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"));
 89145                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                    materials = x.materials,
 166                    textures = x.textures,
 167                    meshToTriangleCount = x.meshToTriangleCount
 168                };
 169
 0170                OnSuccessWrapper(r, OnSuccess);
 0171            };
 172
 0173            abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception);
 174
 0175            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0176        }
 177
 178        void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 179        {
 173180            if (gltfPromise != null)
 181            {
 0182                UnloadGLTF();
 183
 0184                if (VERBOSE)
 0185                    Debug.Log("Forgetting not null promise... " + targetUrl);
 186            }
 187
 173188            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 189            {
 0190                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"));
 0191                return;
 192            }
 193
 173194            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 173195            gltfPromise.settings = settings;
 196
 173197            gltfPromise.OnSuccessEvent += (Asset_GLTF x) =>
 198            {
 76199                var r = new Rendereable
 200                {
 201                    container = x.container,
 202                    totalTriangleCount = x.totalTriangleCount,
 203                    meshes = x.meshes,
 204                    renderers = x.renderers,
 205                    materials = x.materials,
 206                    textures = x.textures,
 207                    meshToTriangleCount = x.meshToTriangleCount
 208                };
 209
 76210                OnSuccessWrapper(r, OnSuccess);
 76211            };
 176212            gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception);
 213
 173214            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 173215        }
 216
 217        private void OnFailWrapper(Action<Exception> OnFail, Exception exception)
 218        {
 219#if UNITY_EDITOR
 92220            loadFinishTime = Time.realtimeSinceStartup;
 221#endif
 222
 92223            OnFail?.Invoke(exception);
 92224            ClearEvents();
 92225        }
 226
 227        private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess)
 228        {
 229#if UNITY_EDITOR
 76230            loadFinishTime = Time.realtimeSinceStartup;
 231#endif
 76232            if (VERBOSE)
 233            {
 0234                if (gltfPromise != null)
 0235                    Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!");
 236                else
 0237                    Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!");
 238            }
 239
 76240            this.loadedAsset = loadedAsset;
 76241            OnSuccess?.Invoke(loadedAsset);
 76242            ClearEvents();
 76243        }
 244
 245        public void ClearEvents()
 246        {
 0247            OnSuccessEvent = null;
 0248            OnFailEvent = null;
 0249        }
 250    }
 251}