< 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:48
Uncovered lines:45
Coverable lines:93
Total lines:266
Line coverage:51.6% (48 of 93)
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
 10424        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;
 10474        float loadFinishTime = float.MaxValue;
 75#endif
 76
 10477        public RendereableAssetLoadHelper(ContentProvider contentProvider, string bundlesContentUrl)
 78        {
 10479            this.contentProvider = contentProvider;
 10480            this.bundlesContentUrl = bundlesContentUrl;
 10481        }
 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        {
 10488            Assert.IsFalse(string.IsNullOrEmpty(targetUrl), "url is null!!");
 89#if UNITY_EDITOR
 10490            loadStartTime = Time.realtimeSinceStartup;
 91#endif
 92
 10493            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:
 8100                    LoadGltf(targetUrl, OnSuccessEvent, OnFailEvent);
 8101                    break;
 102                case LoadingType.DEFAULT:
 103                case LoadingType.ASSET_BUNDLE_WITH_GLTF_FALLBACK:
 192104                    LoadAssetBundle(targetUrl, OnSuccessEvent, exception => LoadGltf(targetUrl, OnSuccessEvent, OnFailEv
 105                    break;
 106            }
 96107        }
 108
 109        public void Unload()
 110        {
 77111            UnloadAB();
 77112            UnloadGLTF();
 77113        }
 114
 115        void UnloadAB()
 116        {
 77117            if ( abPromise != null )
 118            {
 0119                AssetPromiseKeeper_AB_GameObject.i.Forget(abPromise);
 120            }
 77121        }
 122
 123        void UnloadGLTF()
 124        {
 77125            if ( gltfPromise != null )
 126            {
 77127                AssetPromiseKeeper_GLTF.i.Forget(gltfPromise);
 128            }
 77129        }
 130
 131        private const string AB_GO_NAME_PREFIX = "AB:";
 132        private const string GLTF_GO_NAME_PREFIX = "GLTF:";
 133
 134        void LoadAssetBundle(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 135        {
 96136            if (abPromise != null)
 137            {
 0138                UnloadAB();
 0139                if (VERBOSE)
 0140                    Debug.Log("Forgetting not null promise..." + targetUrl);
 141            }
 142
 96143            string bundlesBaseUrl = useCustomContentServerUrl ? customContentServerUrl : bundlesContentUrl;
 144
 96145            if (string.IsNullOrEmpty(bundlesBaseUrl))
 146            {
 96147                OnFailWrapper(OnFail, new Exception("bundlesBaseUrl is null"));
 96148                return;
 149            }
 150
 0151            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 152            {
 0153                OnFailWrapper(OnFail, new Exception($"Content url does not contains {targetUrl}"));
 0154                return;
 155            }
 156
 0157            abPromise = new AssetPromise_AB_GameObject(bundlesBaseUrl, hash);
 0158            abPromise.settings = this.settings;
 159
 0160            abPromise.OnSuccessEvent += (x) =>
 161            {
 162#if UNITY_EDITOR
 0163                x.container.name = AB_GO_NAME_PREFIX + x.container.name;
 164#endif
 0165                var r = new Rendereable()
 166                {
 167                    container = x.container,
 168                    totalTriangleCount = x.totalTriangleCount,
 169                    meshes = x.meshes,
 170                    renderers = x.renderers,
 171                    materials = x.materials,
 172                    textures = x.textures,
 173                    meshToTriangleCount = x.meshToTriangleCount,
 174                    animationClipSize = x.animationClipSize,
 175                    animationClips = x.animationClips,
 176                    meshDataSize = x.meshDataSize
 177                };
 178
 0179                OnSuccessWrapper(r, OnSuccess);
 0180            };
 181
 0182            abPromise.OnFailEvent += (x, exception) => OnFailWrapper(OnFail, exception);
 183
 0184            AssetPromiseKeeper_AB_GameObject.i.Keep(abPromise);
 0185        }
 186
 187        void LoadGltf(string targetUrl, Action<Rendereable> OnSuccess, Action<Exception> OnFail)
 188        {
 104189            if (gltfPromise != null)
 190            {
 0191                UnloadGLTF();
 192
 0193                if (VERBOSE)
 0194                    Debug.Log("Forgetting not null promise... " + targetUrl);
 195            }
 196
 104197            if (!contentProvider.TryGetContentsUrl_Raw(targetUrl, out string hash))
 198            {
 0199                OnFailWrapper(OnFail, new Exception($"Content provider does not contains url {targetUrl}"));
 0200                return;
 201            }
 202
 104203            gltfPromise = new AssetPromise_GLTF(contentProvider, targetUrl, hash);
 104204            gltfPromise.settings = settings;
 205
 104206            gltfPromise.OnSuccessEvent += (Asset_GLTF x) =>
 207            {
 208#if UNITY_EDITOR
 83209                x.container.name = GLTF_GO_NAME_PREFIX + x.container.name;
 210#endif
 83211                var r = new Rendereable
 212                {
 213                    container = x.container,
 214                    totalTriangleCount = x.totalTriangleCount,
 215                    meshes = x.meshes,
 216                    renderers = x.renderers,
 217                    materials = x.materials,
 218                    textures = x.textures,
 219                    meshToTriangleCount = x.meshToTriangleCount,
 220                    animationClipSize = x.animationClipSize,
 221                    meshDataSize = x.meshDataSize,
 222                    animationClips = x.animationClips
 223                };
 224
 83225                OnSuccessWrapper(r, OnSuccess);
 83226            };
 107227            gltfPromise.OnFailEvent += (asset, exception) => OnFailWrapper(OnFail, exception);
 228
 104229            AssetPromiseKeeper_GLTF.i.Keep(gltfPromise);
 104230        }
 231
 232        private void OnFailWrapper(Action<Exception> OnFail, Exception exception)
 233        {
 234#if UNITY_EDITOR
 99235            loadFinishTime = Time.realtimeSinceStartup;
 236#endif
 237
 99238            OnFail?.Invoke(exception);
 99239            ClearEvents();
 99240        }
 241
 242        private void OnSuccessWrapper(Rendereable loadedAsset, Action<Rendereable> OnSuccess)
 243        {
 244#if UNITY_EDITOR
 83245            loadFinishTime = Time.realtimeSinceStartup;
 246#endif
 83247            if (VERBOSE)
 248            {
 0249                if (gltfPromise != null)
 0250                    Debug.Log($"GLTF Load(): target URL -> {gltfPromise.GetId()}. Success!");
 251                else
 0252                    Debug.Log($"AB Load(): target URL -> {abPromise.hash}. Success!");
 253            }
 254
 83255            this.loadedAsset = loadedAsset;
 83256            OnSuccess?.Invoke(loadedAsset);
 83257            ClearEvents();
 83258        }
 259
 260        public void ClearEvents()
 261        {
 0262            OnSuccessEvent = null;
 0263            OnFailEvent = null;
 0264        }
 265    }
 266}