< 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:54
Uncovered lines:43
Coverable lines:97
Total lines:279
Line coverage:55.6% (54 of 97)
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.24076.92%
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        private const string FROM_ASSET_BUNDLE_TAG = "FromAssetBundle";
 18        private const string FROM_RAW_GLTF_TAG = "FromRawGLTF";
 19
 20        public static bool VERBOSE = false;
 21
 22        public static bool useCustomContentServerUrl = false;
 23        public static string customContentServerUrl;
 24
 25        public static LoadingType defaultLoadingType = LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK;
 26
 11127        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 28
 029        public Rendereable loadedAsset { get; protected set; }
 30
 31        public bool isFinished
 32        {
 33            get
 34            {
 7935                if (gltfPromise != null)
 7936                    return gltfPromise.state == AssetPromiseState.FINISHED;
 37
 038                if (abPromise != null)
 039                    return abPromise.state == AssetPromiseState.FINISHED;
 40
 041                return true;
 42            }
 43        }
 44
 45        string bundlesContentUrl;
 46        ContentProvider contentProvider;
 47
 48        AssetPromise_GLTF gltfPromise;
 49        AssetPromise_AB_GameObject abPromise;
 50
 51#if UNITY_EDITOR
 52        public override string ToString()
 53        {
 054            float loadTime = Mathf.Min(loadFinishTime, Time.realtimeSinceStartup) - loadStartTime;
 55
 056            string result = "not loading";
 57
 058            if (gltfPromise != null)
 59            {
 060                result = $"GLTF -> promise state = {gltfPromise.state} ({loadTime} load time)... waiting promises = {Ass
 61
 062                if (gltfPromise.state == AssetPromiseState.WAITING)
 63                {
 064                    result += $"\nmaster promise state... is blocked... {AssetPromiseKeeper_GLTF.i.GetMasterState(gltfPr
 65                }
 66            }
 67
 068            if (abPromise != null)
 69            {
 070                result = $"ASSET BUNDLE -> promise state = {abPromise.ToString()} ({loadTime} load time)... waiting prom
 71            }
 72
 073            return result;
 74        }
 75
 76        float loadStartTime = 0;
 11177        float loadFinishTime = float.MaxValue;
 78#endif
 79
 11180        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl)
 81        {
 11182            this.contentProvider = contentProvider;
 11183            this.bundlesContentUrl = bundlesContentUrl;
 11184        }
 85
 86        public event Action<Rendereable> OnSuccessEvent;
 87        public event Action<Exception> OnFailEvent;
 88
 89        public void Load(string targetUrl, LoadingType forcedLoadingType = LoadingType.DEFAULT)
 90        {
 11791            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 92#if UNITY_EDITOR
 11793            loadStartTime = Time.realtimeSinceStartup;
 94#endif
 95
 11796            LoadingType finalLoadingType = forcedLoadingType == LoadingType.DEFAULT ? defaultLoadingType : forcedLoading
 97            switch (finalLoadingType)
 98            {
 99                case LoadingType.ASSET_BUNDLE_ONLY:
 0100                    LoadAssetBundle(targetUrl, OnSuccessEvent, OnFailEvent);
 0101                    break;
 102                case LoadingType.GLTF_ONLY:
 8103                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 8104                    break;
 105                case LoadingType.DEFAULT:
 106                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 218107                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv
 108                    break;
 109            }
 109110        }
 111
 112        public void Unload()
 113        {
 98114            UnloadAB();
 98115            UnloadGLTF();
 98116        }
 117
 118        void UnloadAB()
 119        {
 98120            if ( abPromise != null )
 121            {
 0122                AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise);
 123            }
 98124        }
 125
 126        void UnloadGLTF()
 127        {
 104128            if ( gltfPromise != null )
 129            {
 98130                AssetPromiseKeeper_GLTF.i.Forget(gltfPromise);
 131            }
 104132        }
 133
 134        private const string AB_GO_NAME_PREFIX = "AB:";
 135        private const string GLTF_GO_NAME_PREFIX = "GLTF:";
 136
 137        void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 138        {
 109139            if (abPromise != null)
 140            {
 0141                UnloadAB();
 0142                if (VERBOSE)
 0143                    Debug.Log("Forgetting not null promise..." + targetUrl);
 144            }
 145
 109146            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 147
 109148            if (string.IsNullOrEmpty(bundlesBaseUrl))
 149            {
 109150                OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"));
 109151                return;
 152            }
 153
 0154            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 155            {
 0156                OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}"));
 0157                return;
 158            }
 159
 0160            abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash);
 0161            abPromise.settings = this.settings;
 162
 0163            abPromise.OnSuccessEvent += (x) =>
 164            {
 165#if UNITY_EDITOR
 0166                x.container.name = AB_GO_NAME_PREFIX + x.container.name;
 167#endif
 0168                var r = new Rendereable()
 169                {
 170                    container = x.container,
 171                    totalTriangleCount = x.totalTriangleCount,
 172                    meshes = x.meshes,
 173                    renderers = x.renderers,
 174                    materials = x.materials,
 175                    textures = x.textures,
 176                    meshToTriangleCount = x.meshToTriangleCount,
 177                    animationClipSize = x.animationClipSize,
 178                    animationClips = x.animationClips,
 179                    meshDataSize = x.meshDataSize
 180                };
 181
 0182                foreach (var someRenderer in r.renderers)
 183                {
 0184                    someRenderer.tag = FROM_ASSET_BUNDLE_TAG;
 185                }
 186
 0187                OnSuccessWrapper(r, OnSuccess);
 0188            };
 189
 0190            abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception);
 191
 0192            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0193        }
 194
 195        void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 196        {
 117197            if (gltfPromise != null)
 198            {
 6199                UnloadGLTF();
 200
 6201                if (VERBOSE)
 0202                    Debug.Log("Forgetting not null promise... " + targetUrl);
 203            }
 204
 117205            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 206            {
 0207                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"));
 0208                return;
 209            }
 210
 117211            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 117212            gltfPromise.settings = settings;
 213
 117214            gltfPromise.OnSuccessEvent += (Asset_GLTF x) =>
 215            {
 216#if UNITY_EDITOR
 95217                x.container.name = GLTF_GO_NAME_PREFIX + x.container.name;
 218#endif
 95219                var r = new Rendereable
 220                {
 221                    container = x.container,
 222                    totalTriangleCount = x.totalTriangleCount,
 223                    meshes = x.meshes,
 224                    renderers = x.renderers,
 225                    materials = x.materials,
 226                    textures = x.textures,
 227                    meshToTriangleCount = x.meshToTriangleCount,
 228                    animationClipSize = x.animationClipSize,
 229                    meshDataSize = x.meshDataSize,
 230                    animationClips = x.animationClips
 231                };
 232
 596233                foreach (var someRenderer in r.renderers)
 234                {
 203235                    someRenderer.tag = FROM_RAW_GLTF_TAG;
 236                }
 237
 95238                OnSuccessWrapper(r, OnSuccess);
 95239            };
 120240            gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception);
 241
 117242            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 117243        }
 244
 245        private void OnFailWrapper(Action<Exception> OnFail, Exception exception)
 246        {
 247#if UNITY_EDITOR
 112248            loadFinishTime = Time.realtimeSinceStartup;
 249#endif
 250
 112251            OnFail?.Invoke(exception);
 112252            ClearEvents();
 112253        }
 254
 255        private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess)
 256        {
 257#if UNITY_EDITOR
 95258            loadFinishTime = Time.realtimeSinceStartup;
 259#endif
 95260            if (VERBOSE)
 261            {
 0262                if (gltfPromise != null)
 0263                    Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!");
 264                else
 0265                    Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!");
 266            }
 267
 95268            this.loadedAsset = loadedAsset;
 95269            OnSuccess?.Invoke(loadedAsset);
 95270            ClearEvents();
 95271        }
 272
 273        public void ClearEvents()
 274        {
 0275            OnSuccessEvent = null;
 0276            OnFailEvent = null;
 0277        }
 278    }
 279}