< Summary

Class:DCL.AssetPromise_GLTF
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTF/AssetPromise_GLTF.cs
Covered lines:75
Uncovered lines:3
Coverable lines:78
Total lines:207
Line coverage:96.1% (75 of 78)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_GLTF(...)0%220100%
AssetPromise_GLTF(...)0%110100%
AssetPromise_GLTF(...)0%110100%
AssetPromise_GLTF(...)0%110100%
OnBeforeLoadOrReuse()0%330100%
OnAfterLoadOrReuse()0%440100%
GetId()0%110100%
Load()0%220100%
OnLoad(...)0%220100%
FileToHash(...)0%110100%
OnReuse(...)0%110100%
AddToLibrary()0%3.023087.5%
OnCancelLoading()0%220100%
GetAsset(...)0%220100%
Unload()0%44094.12%
OnSilentForget()0%220100%
OnLoadedAfterForget(...)0%330100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTF/AssetPromise_GLTF.cs

#LineLine coverage
 1using System;
 2using DCL.Helpers;
 3using UnityGLTF;
 4
 5namespace DCL
 6{
 7    public class AssetPromise_GLTF : AssetPromise<Asset_GLTF>
 8    {
 1489        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 10        protected string assetDirectoryPath;
 11
 12        protected ContentProvider provider = null;
 013        public string fileName { get; private set; }
 14
 15        GLTFComponent gltfComponent = null;
 16        IWebRequestController webRequestController = null;
 17
 18        object id = null;
 19
 20        private Action OnSuccess;
 21        private Action OnFail;
 22        private bool waitingAssetLoad = false;
 23
 24        public AssetPromise_GLTF(string url, IWebRequestController webRequestController)
 1825            : this(new ContentProvider_Dummy(), url, null, webRequestController) { }
 26
 27        public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null)
 27428            : this(provider, url, hash, Environment.i.platform.webRequest) { }
 29
 30        public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController)
 431            : this(provider, url, null, webRequestController) { }
 32
 14833        public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont
 34        {
 14835            this.provider = provider;
 14836            this.fileName = url.Substring(url.LastIndexOf('/') + 1);
 14837            this.id = hash ?? url;
 14838            this.webRequestController = webRequestController;
 39            // We separate the directory path of the GLB and its file name, to be able to use the directory path when
 40            // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call
 14841            assetDirectoryPath = URIHelper.GetDirectoryName(url);
 14842        }
 43
 44        protected override void OnBeforeLoadOrReuse()
 45        {
 46#if UNITY_EDITOR
 22347            asset.container.name = "GLTF: " + this.id;
 48#endif
 22349            settings.ApplyBeforeLoad(asset.container.transform);
 22350        }
 51
 52        protected override void OnAfterLoadOrReuse()
 53        {
 11054            if (asset?.container != null)
 55            {
 10956                settings.ApplyAfterLoad(asset.container.transform);
 57            }
 11058        }
 59
 90960        public override object GetId() { return id; }
 61
 62        internal override void Load()
 63        {
 13764            if (waitingAssetLoad)
 165                return;
 66
 13667            base.Load();
 13668        }
 69
 70        protected override void OnLoad(Action OnSuccess, Action OnFail)
 71        {
 11072            gltfComponent = asset.container.AddComponent<GLTFComponent>();
 11073            gltfComponent.Initialize(webRequestController);
 74
 11075            GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings()
 76            {
 77                useVisualFeedback = settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TR
 78                initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE,
 79                shaderOverride = settings.shaderOverride,
 80                addMaterialsToPersistentCaching = (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) !
 81            };
 82
 11083            gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string,
 84                false, tmpSettings, FileToHash);
 85
 11086            this.OnSuccess = OnSuccess;
 11087            this.OnFail = OnFail;
 88
 11089            gltfComponent.OnSuccess += this.OnSuccess;
 11090            gltfComponent.OnFail += this.OnFail;
 91
 11092            asset.name = fileName;
 11093        }
 94
 95        bool FileToHash(string fileName, out string hash)
 96        {
 26697            return provider.TryGetContentHash(assetDirectoryPath + fileName, out hash);
 98        }
 99
 100        protected override void OnReuse(Action OnSuccess)
 101        {
 102            //NOTE(Brian):  Show the asset using the simple gradient feedback.
 26103            asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS
 26104        }
 105
 106        protected override bool AddToLibrary()
 107        {
 87108            if (!library.Add(asset))
 0109                return false;
 110
 111            //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately
 112            //             So it keeps being there. As master gltfs can't be in the world.
 113            //
 114            //             ApplySettings will reposition the newly Get asset to the proper coordinates.
 87115            if (settings.forceNewInstance)
 116            {
 2117                asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id);
 2118            }
 119            else
 120            {
 85121                asset = library.Get(asset.id);
 122            }
 123
 124            //NOTE(Brian): Call again this method because we are replacing the asset.
 87125            OnBeforeLoadOrReuse();
 87126            return true;
 127        }
 128
 129        protected override void OnCancelLoading()
 130        {
 7131            if (asset != null)
 132            {
 7133                asset.CancelShow();
 134            }
 7135        }
 136
 137        protected override Asset_GLTF GetAsset(object id)
 138        {
 26139            if (settings.forceNewInstance)
 140            {
 9141                return ((AssetLibrary_GLTF)library).GetCopyFromOriginal(id);
 142            }
 143            else
 144            {
 17145                return base.GetAsset(id);
 146            }
 147        }
 148
 149        internal override void Unload()
 150        {
 116151            if (waitingAssetLoad)
 0152                return;
 153
 116154            settings.parent = null;
 155
 156            // NOTE: if Unload is called before finish loading we wait until gltf is loaded or failed before unloading i
 116157            if (state == AssetPromiseState.LOADING && gltfComponent != null)
 158            {
 20159                waitingAssetLoad = true;
 160
 20161                state = AssetPromiseState.IDLE_AND_EMPTY;
 162
 20163                var pendingAsset = asset;
 20164                asset.Hide();
 20165                asset = null;
 166
 20167                gltfComponent.OnSuccess -= OnSuccess;
 20168                gltfComponent.OnFail -= OnFail;
 169
 22170                gltfComponent.OnSuccess += () => OnLoadedAfterForget(true, pendingAsset);
 37171                gltfComponent.OnFail += () => OnLoadedAfterForget(false, pendingAsset);
 172
 20173                gltfComponent.CancelIfQueued();
 174
 20175                return;
 176            }
 177
 96178            base.Unload();
 96179        }
 180
 181        // NOTE: master promise are silently forgotten. We should make sure that they are loaded anyway since
 182        // other promises are waiting for them
 183        internal void OnSilentForget()
 184        {
 3185            asset.Hide();
 186
 3187            if (gltfComponent != null)
 188            {
 3189                gltfComponent.SetPrioritized();
 190            }
 3191        }
 192
 193        private void OnLoadedAfterForget(bool success, Asset_GLTF loadedAsset)
 194        {
 19195            asset = loadedAsset;
 196
 19197            if (success && asset != null)
 198            {
 2199                AddToLibrary();
 200            }
 201
 19202            ClearEvents();
 19203            CallAndClearEvents(success);
 19204            Cleanup();
 19205        }
 206    }
 207}