< 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:63
Uncovered lines:62
Coverable lines:125
Total lines:327
Line coverage:50.4% (63 of 125)
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%14.298053.85%
ProxyLoadGltf(...)0%2.062075%
Unload()0%110100%
UnloadAB()0%2.152066.67%
UnloadGLTF()0%220100%
UnloadGLTFast()0%2.152066.67%
LoadAssetBundle(...)0%24.247029.41%
LoadGltf(...)0%4.24076.92%
LoadGLTFast(...)0%20400%
OnFailWrapper(...)0%220100%
OnSuccessWrapper(...)0%4.594066.67%
ClearEvents()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Helpers/RendereableAssetLoadHelper/RendereableAssetLoadHelper.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using UnityEngine.Assertions;
 5
 6namespace DCL.Components
 7{
 8    public class RendereableAssetLoadHelper
 9    {
 10        public event Action<Rendereable> OnSuccessEvent;
 11        public event Action<Exception> OnFailEvent;
 12
 13        public enum LoadingType
 14        {
 15            ASSET_BUNDLE_WITH_GLTF_FALLBACK,
 16            ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK,
 17            ASSET_BUNDLE_ONLY,
 18            GLTF_ONLY,
 19            OLD_GLTF,
 20            DEFAULT
 21        }
 22
 23        private const string AB_GO_NAME_PREFIX = "AB:";
 24        private const string GLTF_GO_NAME_PREFIX = "GLTF:";
 25        private const string GLTFAST_GO_NAME_PREFIX = "GLTFast:";
 26        private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle";
 27        private const string FROM_RAW_GLTF_TAG = "FromRawGLTF";
 28
 29        public static bool VERBOSE = false;
 30        public static bool useCustomContentServerUrl = false;
 31        public static string customContentServerUrl;
 32        public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK;
 33
 8234        public AssetPromiseSettings_Rendering settings = new ();
 29835        public Rendereable loadedAsset { get; protected set; }
 36
 37        private readonly string bundlesContentUrl;
 38        private readonly Func<bool> IsGltFastEnabled;
 39        private readonly ContentProvider contentProvider;
 40        private AssetPromise_GLTF gltfPromise;
 41        private AssetPromise_GLTFast_Instance gltfastPromise;
 42        private AssetPromise_AB_GameObject abPromise;
 43
 44        public bool isFinished
 45        {
 46            get
 47            {
 7348                if (gltfPromise != null)
 7349                    return gltfPromise.state == AssetPromiseState.FINISHED;
 50
 051                if (abPromise != null)
 052                    return abPromise.state == AssetPromiseState.FINISHED;
 53
 054                return true;
 55            }
 56        }
 57
 58#if UNITY_EDITOR
 59        public override string ToString()
 60        {
 061            float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime;
 62
 063            string result = "not loading";
 64
 065            if (gltfPromise != null)
 66            {
 067                result = $"GLTF -> promise state = {gltfPromise.state} ({loadTime} load time)... waiting promises = {Ass
 68
 069                if (gltfPromise.state == AssetPromiseState.WAITING) { result += $"\nmaster promise state... is blocked..
 70            }
 71
 072            if (abPromise != null) { result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load 
 73
 074            return result;
 75        }
 76
 77        float loadStartTime = 0;
 8278        float loadFinishTime = float.MaxValue;
 79#endif
 80
 8281        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl, Func<bool> isGltFas
 82        {
 8283            this.contentProvider = contentProvider;
 8284            this.bundlesContentUrl = bundlesContentUrl;
 8285            this.IsGltFastEnabled = isGltFastEnabled;
 8286        }
 87
 88        public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT)
 89        {
 8890            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 91#if UNITY_EDITOR
 8892            loadStartTime = Time.realtimeSinceStartup;
 93#endif
 94
 8895            LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading
 96
 97            switch (finalLoadingType)
 98            {
 99                case LoadingType.ASSET_BUNDLE_ONLY:
 0100                    LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent);
 0101                    break;
 102                case LoadingType.GLTF_ONLY:
 8103                    ProxyLoadGltf(targetUrl);
 8104                    break;
 105                case LoadingType.OLD_GLTF:
 0106                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 0107                    break;
 108                case LoadingType.DEFAULT:
 109                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 160110                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => ProxyLoadGltf(targetUrl));
 80111                    break;
 112                case LoadingType.ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK:
 0113                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv
 114                    break;
 115            }
 0116        }
 117
 118        private void ProxyLoadGltf(string targetUrl)
 119        {
 88120            if (IsGltFastEnabled())
 0121                LoadGLTFast(targetUrl, OnSuccessEvent, _ =>
 122                {
 0123                    Debug.LogError($"GLTFast failed to load for {targetUrl} so we are going to fallback into old gltf");
 0124                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 0125                });
 126            else
 88127                LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 88128        }
 129
 130        public void Unload()
 131        {
 70132            UnloadAB();
 70133            UnloadGLTF();
 70134            UnloadGLTFast();
 70135        }
 136
 137        void UnloadAB()
 138        {
 70139            if (abPromise != null) { AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise); }
 70140        }
 141
 142        void UnloadGLTF()
 143        {
 146144            if (gltfPromise != null) { AssetPromiseKeeper_GLTF.i.Forget(gltfPromise); }
 76145        }
 146
 147        void UnloadGLTFast()
 148        {
 70149            if (gltfastPromise != null) { AssetPromiseKeeper_GLTFast_Instance.i.Forget(gltfastPromise); }
 70150        }
 151
 152        void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 153        {
 80154            if (abPromise != null)
 155            {
 0156                UnloadAB();
 157
 0158                if (VERBOSE)
 0159                    Debug.Log("Forgetting not null promise..." + targetUrl);
 160            }
 161
 80162            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 163
 80164            if (string.IsNullOrEmpty(bundlesBaseUrl))
 165            {
 80166                OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"));
 80167                return;
 168            }
 169
 0170            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 171            {
 0172                OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}"));
 0173                return;
 174            }
 175
 0176            abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash);
 0177            abPromise.settings = this.settings;
 178
 0179            abPromise.OnSuccessEvent += (x) =>
 180            {
 181#if UNITY_EDITOR
 0182                x.container.name = AB_GO_NAME_PREFIX + x.container.name;
 183#endif
 0184                var r = new Rendereable()
 185                {
 186                    container = x.container,
 187                    totalTriangleCount = x.totalTriangleCount,
 188                    meshes = x.meshes,
 189                    renderers = x.renderers,
 190                    materials = x.materials,
 191                    textures = x.textures,
 192                    meshToTriangleCount = x.meshToTriangleCount,
 193                    animationClipSize = x.animationClipSize,
 194                    animationClips = x.animationClips,
 195                    meshDataSize = x.meshDataSize
 196                };
 197
 0198                foreach (var someRenderer in r.renderers) { someRenderer.tag = FROM_ASSET_BUNDLE_TAG; }
 199
 0200                OnSuccessWrapper(r, OnSuccess);
 0201            };
 202
 0203            abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception);
 204
 0205            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0206        }
 207
 208        void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 209        {
 88210            if (gltfPromise != null)
 211            {
 6212                UnloadGLTF();
 213
 6214                if (VERBOSE)
 0215                    Debug.Log("Forgetting not null promise... " + targetUrl);
 216            }
 217
 88218            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 219            {
 0220                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"));
 0221                return;
 222            }
 223
 88224            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 88225            gltfPromise.settings = settings;
 226
 88227            gltfPromise.OnSuccessEvent += (Asset_GLTF x) =>
 228            {
 229#if UNITY_EDITOR
 81230                x.container.name = GLTF_GO_NAME_PREFIX + x.container.name;
 231#endif
 81232                var r = new Rendereable
 233                {
 234                    container = x.container,
 235                    totalTriangleCount = x.totalTriangleCount,
 236                    meshes = x.meshes,
 237                    renderers = x.renderers,
 238                    materials = x.materials,
 239                    textures = x.textures,
 240                    meshToTriangleCount = x.meshToTriangleCount,
 241                    animationClipSize = x.animationClipSize,
 242                    meshDataSize = x.meshDataSize,
 243                    animationClips = x.animationClips
 244                };
 245
 723246                foreach (var someRenderer in r.renderers) { someRenderer.tag = FROM_RAW_GLTF_TAG; }
 247
 81248                OnSuccessWrapper(r, OnSuccess);
 81249            };
 250
 91251            gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception);
 252
 88253            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 88254        }
 255
 256        private void LoadGLTFast(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 257        {
 0258            if (gltfastPromise != null)
 259            {
 0260                UnloadGLTFast();
 261
 0262                if (VERBOSE)
 0263                    Debug.Log("Forgetting not null promise... " + targetUrl);
 264            }
 265
 0266            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 267            {
 0268                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"));
 0269                return;
 270            }
 271
 0272            gltfastPromise = new AssetPromise_GLTFast_Instance(targetUrl, hash, Environment.i.platform.webRequest, conte
 273
 0274            gltfastPromise.OnSuccessEvent += (Asset_GLTFast_Instance x) =>
 275            {
 276#if UNITY_EDITOR
 0277                x.container.name = GLTFAST_GO_NAME_PREFIX + x.container.name;
 278#endif
 0279                Rendereable r = x.ToRendereable();
 280
 0281                OnSuccessWrapper(r, OnSuccess);
 0282            };
 283
 0284            gltfastPromise.OnFailEvent += (asset, exception) =>
 285            {
 0286                Debug.LogException(exception);
 0287                OnFailWrapper(OnFail, exception);
 0288            };
 289
 0290            AssetPromiseKeeper_GLTFast_Instance.i.Keep(gltfastPromise);
 0291        }
 292
 293        private void OnFailWrapper(Action<Exception> OnFail, Exception exception)
 294        {
 295#if UNITY_EDITOR
 83296            loadFinishTime = Time.realtimeSinceStartup;
 297#endif
 298
 83299            OnFail?.Invoke(exception);
 83300            ClearEvents();
 83301        }
 302
 303        private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess)
 304        {
 305#if UNITY_EDITOR
 81306            loadFinishTime = Time.realtimeSinceStartup;
 307#endif
 81308            if (VERBOSE)
 309            {
 0310                if (gltfPromise != null)
 0311                    Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!");
 312                else
 0313                    Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!");
 314            }
 315
 81316            this.loadedAsset = loadedAsset;
 81317            OnSuccess?.Invoke(loadedAsset);
 81318            ClearEvents();
 81319        }
 320
 321        public void ClearEvents()
 322        {
 192323            OnSuccessEvent = null;
 192324            OnFailEvent = null;
 192325        }
 326    }
 327}