< 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:70
Uncovered lines:72
Coverable lines:142
Total lines:353
Line coverage:49.2% (70 of 142)
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%21.527033.33%
LoadGltf(...)0%4.164078.57%
LoadGLTFast(...)0%20400%
OnFailWrapper(...)0%5.275077.78%
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 UnityEngine;
 3using UnityEngine.Assertions;
 4
 5namespace DCL.Components
 6{
 7    public class RendereableAssetLoadHelper
 8    {
 9        public event Action<Rendereable> OnSuccessEvent;
 10        public event Action<Exception> OnFailEvent;
 11
 12        public enum LoadingType
 13        {
 14            ASSET_BUNDLE_WITH_GLTF_FALLBACK,
 15            ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK,
 16            ASSET_BUNDLE_ONLY,
 17            GLTF_ONLY,
 18            OLD_GLTF,
 19            DEFAULT
 20        }
 21
 22        private const string AB_GO_NAME_PREFIX = "AB:";
 23        private const string GLTF_GO_NAME_PREFIX = "GLTF:";
 24        private const string GLTFAST_GO_NAME_PREFIX = "GLTFast:";
 25        private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle";
 26        private const string FROM_RAW_GLTF_TAG = "FromRawGLTF";
 27
 28        public static bool VERBOSE = false;
 29        public static bool useCustomContentServerUrl = false;
 30        public static string customContentServerUrl;
 31        public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK;
 32
 8233        public AssetPromiseSettings_Rendering settings = new();
 29834        public Rendereable loadedAsset { get; protected set; }
 35
 36        private readonly string bundlesContentUrl;
 37        private readonly Func<bool> IsGltFastEnabled;
 38        private readonly ContentProvider contentProvider;
 39        private AssetPromise_GLTF gltfPromise;
 40        private AssetPromise_GLTFast_Instance gltfastPromise;
 41        private AssetPromise_AB_GameObject abPromise;
 42        private string currentLoadingSystem;
 43
 44        public bool isFinished
 45        {
 46            get
 47            {
 7148                if (gltfPromise != null)
 7149                    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)
 070                { result += $"\nmaster promise state... is blocked... {AssetPromiseKeeper_GLTF.i.GetMasterState(gltfProm
 71            }
 72
 073            if (abPromise != null)
 074            { result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load time)... waiting promis
 75
 076            return result;
 77        }
 78
 79        float loadStartTime = 0;
 8280        float loadFinishTime = float.MaxValue;
 81#endif
 82
 8283        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl, Func<bool> isGltFas
 84        {
 8285            this.contentProvider = contentProvider;
 8286            this.bundlesContentUrl = bundlesContentUrl;
 8287            this.IsGltFastEnabled = isGltFastEnabled;
 8288        }
 89
 90        public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT)
 91        {
 8892            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 93#if UNITY_EDITOR
 8894            loadStartTime = Time.realtimeSinceStartup;
 95#endif
 8896            LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading
 97
 98            switch (finalLoadingType)
 99            {
 100                case LoadingType.ASSET_BUNDLE_ONLY:
 0101                    LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent, false);
 0102                    break;
 103                case LoadingType.GLTF_ONLY:
 8104                    ProxyLoadGltf(targetUrl, false);
 8105                    break;
 106                case LoadingType.OLD_GLTF:
 0107                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent, false);
 0108                    break;
 109                case LoadingType.DEFAULT:
 110                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 160111                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => ProxyLoadGltf(targetUrl, false), true);
 80112                    break;
 113                case LoadingType.ASSET_BUNDLE_WITH_OLD_GLTF_FALLBACK:
 0114                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv
 115                    break;
 116            }
 0117        }
 118
 119        private void ProxyLoadGltf(string targetUrl, bool hasFallback)
 120        {
 88121            if (IsGltFastEnabled())
 0122                LoadGLTFast(targetUrl, OnSuccessEvent, _ =>
 123                {
 0124                    if (VERBOSE)
 0125                        Debug.Log($"GLTFast failed to load for {targetUrl} so we are going to fallback into old gltf");
 0126                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent, hasFallback);
 0127                }, true);
 128            else
 88129                LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent, hasFallback);
 88130        }
 131
 132        public void Unload()
 133        {
 70134            UnloadAB();
 70135            UnloadGLTF();
 70136            UnloadGLTFast();
 70137        }
 138
 139        void UnloadAB()
 140        {
 70141            if (abPromise != null)
 0142            { AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise); }
 70143        }
 144
 145        void UnloadGLTF()
 146        {
 76147            if (gltfPromise != null)
 70148            { AssetPromiseKeeper_GLTF.i.Forget(gltfPromise); }
 76149        }
 150
 151        void UnloadGLTFast()
 152        {
 70153            if (gltfastPromise != null)
 0154            { AssetPromiseKeeper_GLTFast_Instance.i.Forget(gltfastPromise); }
 70155        }
 156
 157        void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFallback
 158        {
 80159            currentLoadingSystem = AB_GO_NAME_PREFIX;
 160
 80161            if (abPromise != null)
 162            {
 0163                UnloadAB();
 164
 0165                if (VERBOSE)
 0166                    Debug.Log("Forgetting not null promise..." + targetUrl);
 167            }
 168
 80169            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 170
 80171            if (string.IsNullOrEmpty(bundlesBaseUrl))
 172            {
 80173                OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"), hasFallback);
 80174                return;
 175            }
 176
 0177            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 178            {
 0179                OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}"), hasFallback);
 0180                return;
 181            }
 182
 0183            abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash);
 0184            abPromise.settings = this.settings;
 185
 0186            abPromise.OnSuccessEvent += (x) =>
 187            {
 188#if UNITY_EDITOR
 0189                x.container.name = AB_GO_NAME_PREFIX + x.container.name;
 190#endif
 0191                var r = new Rendereable()
 192                {
 193                    container = x.container,
 194                    totalTriangleCount = x.totalTriangleCount,
 195                    meshes = x.meshes,
 196                    renderers = x.renderers,
 197                    materials = x.materials,
 198                    textures = x.textures,
 199                    meshToTriangleCount = x.meshToTriangleCount,
 200                    animationClipSize = x.animationClipSize,
 201                    animationClips = x.animationClips,
 202                    meshDataSize = x.meshDataSize
 203                };
 204
 0205                foreach (var someRenderer in r.renderers)
 0206                    someRenderer.tag = FROM_ASSET_BUNDLE_TAG;
 207
 0208                OnSuccessWrapper(r, OnSuccess);
 0209            };
 210
 0211            abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception, hasFallback);
 212
 0213            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0214        }
 215
 216        void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFallback)
 217        {
 88218            currentLoadingSystem = GLTF_GO_NAME_PREFIX;
 219
 88220            if (gltfPromise != null)
 221            {
 6222                UnloadGLTF();
 223
 6224                if (VERBOSE)
 0225                    Debug.Log("Forgetting not null promise... " + targetUrl);
 226            }
 227
 88228            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 229            {
 0230                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"), hasFallback)
 0231                return;
 232            }
 233
 88234            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 88235            gltfPromise.settings = settings;
 236
 88237            gltfPromise.OnSuccessEvent += (Asset_GLTF x) =>
 238            {
 239#if UNITY_EDITOR
 81240                x.container.name = GLTF_GO_NAME_PREFIX + x.container.name;
 241#endif
 81242                var r = new Rendereable
 243                {
 244                    container = x.container,
 245                    totalTriangleCount = x.totalTriangleCount,
 246                    meshes = x.meshes,
 247                    renderers = x.renderers,
 248                    materials = x.materials,
 249                    textures = x.textures,
 250                    meshToTriangleCount = x.meshToTriangleCount,
 251                    animationClipSize = x.animationClipSize,
 252                    meshDataSize = x.meshDataSize,
 253                    animationClips = x.animationClips
 254                };
 255
 536256                foreach (var someRenderer in r.renderers)
 187257                    someRenderer.tag = FROM_RAW_GLTF_TAG;
 258
 81259                OnSuccessWrapper(r, OnSuccess);
 81260            };
 261
 91262            gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception, hasFallback);
 263
 88264            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 88265        }
 266
 267        private void LoadGLTFast(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail, bool hasFall
 268        {
 0269            currentLoadingSystem = GLTFAST_GO_NAME_PREFIX;
 270
 0271            if (gltfastPromise != null)
 272            {
 0273                UnloadGLTFast();
 274
 0275                if (VERBOSE)
 0276                    Debug.Log("Forgetting not null promise... " + targetUrl);
 277            }
 278
 0279            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 280            {
 0281                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"), hasFallback)
 0282                return;
 283            }
 284
 0285            gltfastPromise = new AssetPromise_GLTFast_Instance(targetUrl, hash,
 286                Environment.i.platform.webRequest, contentProvider, settings);
 287
 0288            gltfastPromise.OnSuccessEvent += (Asset_GLTFast_Instance x) =>
 289            {
 290#if UNITY_EDITOR
 0291                x.container.name = GLTFAST_GO_NAME_PREFIX + x.container.name;
 292#endif
 0293                Rendereable r = x.ToRendereable();
 0294                foreach (var someRenderer in r.renderers)
 0295                    someRenderer.tag = FROM_RAW_GLTF_TAG;
 296
 0297                OnSuccessWrapper(r, OnSuccess);
 0298            };
 299
 0300            gltfastPromise.OnFailEvent += (asset, exception) =>
 301            {
 0302                OnFailWrapper(OnFail, exception, hasFallback);
 0303            };
 304
 0305            AssetPromiseKeeper_GLTFast_Instance.i.Keep(gltfastPromise);
 0306        }
 307
 308        private void OnFailWrapper(Action<Exception> OnFail, Exception exception, bool hasFallback)
 309        {
 310#if UNITY_EDITOR
 83311            loadFinishTime = Time.realtimeSinceStartup;
 312#endif
 313
 314            // If the entity is destroyed while loading, the exception is expected to be null and no error should be thr
 83315            if (exception != null)
 316            {
 80317                if (!hasFallback)
 0318                    Debug.LogException(exception);
 80319                else if (VERBOSE)
 320                {
 0321                    Debug.Log($"Load Fail Detected, trying to use a fallback, " +
 322                        $"loading type was: {currentLoadingSystem} and error was: {exception.Message}");
 323                }
 324            }
 83325            OnFail?.Invoke(exception);
 83326            ClearEvents();
 83327        }
 328
 329        private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess)
 330        {
 331#if UNITY_EDITOR
 81332            loadFinishTime = Time.realtimeSinceStartup;
 333#endif
 81334            if (VERBOSE)
 335            {
 0336                if (gltfPromise != null)
 0337                    Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!");
 338                else
 0339                    Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!");
 340            }
 341
 81342            this.loadedAsset = loadedAsset;
 81343            OnSuccess?.Invoke(loadedAsset);
 81344            ClearEvents();
 81345        }
 346
 347        public void ClearEvents()
 348        {
 192349            OnSuccessEvent = null;
 192350            OnFailEvent = null;
 192351        }
 352    }
 353}