< 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:66
Uncovered lines:6
Coverable lines:72
Total lines:200
Line coverage:91.6% (66 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_GLTF(...)0%220100%
AssetPromise_GLTF(...)0%2100%
AssetPromise_GLTF(...)0%110100%
AssetPromise_GLTF(...)0%110100%
AssetPromise_GLTF(...)0%110100%
OnBeforeLoadOrReuse()0%330100%
OnAfterLoadOrReuse()0%440100%
GetId()0%110100%
OnLoad(...)0%220100%
RendererCreated(...)0%2.032080%
MeshCreated(...)0%2.022083.33%
FileToHash(...)0%110100%
OnReuse(...)0%110100%
AddToLibrary()0%3.023087.5%
OnCancelLoading()0%220100%
GetAsset(...)0%220100%
OnSilentForget()0%220100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Linq;
 3using DCL.Helpers;
 4using DCL.Models;
 5using UnityEngine;
 6using UnityEngine.Assertions;
 7using UnityGLTF;
 8
 9namespace DCL
 10{
 11    public class AssetPromise_GLTF : AssetPromise<Asset_GLTF>
 12    {
 15713        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 14        protected string assetDirectoryPath;
 15
 16        protected ContentProvider provider = null;
 017        public string fileName { get; private set; }
 18
 19        GLTFComponent gltfComponent = null;
 20        IWebRequestController webRequestController = null;
 21
 22        object id = null;
 23
 24        public AssetPromise_GLTF(string url)
 025            : this(new ContentProvider_Dummy(), url, null, Environment.i.platform.webRequest)
 26        {
 027        }
 28
 29        public AssetPromise_GLTF(string url, IWebRequestController webRequestController)
 930            : this(new ContentProvider_Dummy(), url, null, webRequestController)
 31        {
 932        }
 33
 34        public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null)
 14435            : this(provider, url, hash, Environment.i.platform.webRequest)
 36        {
 14437        }
 38
 39        public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController)
 440            : this(provider, url, null, webRequestController)
 41        {
 442        }
 43
 15744        public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont
 45        {
 15746            this.provider = provider;
 15747            this.fileName = url.Substring(url.LastIndexOf('/') + 1);
 15748            this.id = hash ?? url;
 15749            this.webRequestController = webRequestController;
 50            // We separate the directory path of the GLB and its file name, to be able to use the directory path when
 51            // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call
 15752            assetDirectoryPath = URIHelper.GetDirectoryName(url);
 15753        }
 54
 55        protected override void OnBeforeLoadOrReuse()
 56        {
 57#if UNITY_EDITOR
 24758            asset.container.name = "GLTF: " + this.id;
 59#endif
 24760            settings.ApplyBeforeLoad(asset.container.transform);
 24761        }
 62
 63        protected override void OnAfterLoadOrReuse()
 64        {
 12865            if (asset?.container != null)
 66            {
 12867                settings.ApplyAfterLoad(asset.container.transform);
 68            }
 12869        }
 70
 96771        public override object GetId() { return id; }
 72
 73        protected override void OnLoad(Action OnSuccess, Action OnFail)
 74        {
 11775            gltfComponent = asset.container.AddComponent<GLTFComponent>();
 11776            gltfComponent.Initialize(webRequestController);
 77
 11778            GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings()
 79            {
 80                useVisualFeedback = settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TR
 81                initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE,
 82                shaderOverride = settings.shaderOverride,
 83                addMaterialsToPersistentCaching = (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) !
 84                forceGPUOnlyMesh = settings.forceGPUOnlyMesh
 85            };
 86
 11787            gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string,
 88                false, tmpSettings, FileToHash);
 89
 11790            gltfComponent.sceneImporter.OnMeshCreated += MeshCreated;
 11791            gltfComponent.sceneImporter.OnRendererCreated += RendererCreated;
 92
 11793            gltfComponent.OnSuccess += () =>
 94            {
 10195                if ( asset != null )
 96                {
 10197                    asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriang
 98                }
 99
 101100                OnSuccess.Invoke();
 101101            };
 102
 117103            gltfComponent.OnFail += OnFail;
 104
 117105            asset.name = fileName;
 117106        }
 107
 108
 109        private void RendererCreated(Renderer r)
 110        {
 263111            Assert.IsTrue(r != null, "Renderer is null?");
 112
 113            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 263114            if ( asset == null )
 0115                return;
 116
 263117            asset.renderers.Add(r);
 263118        }
 119
 120        private void MeshCreated(Mesh mesh)
 121        {
 283122            Assert.IsTrue(mesh != null, "Mesh is null?");
 123
 124            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 283125            if ( asset == null )
 0126                return;
 127
 283128            asset.meshes.Add(mesh);
 283129            asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 283130        }
 131
 132        bool FileToHash(string fileName, out string hash)
 133        {
 297134            return provider.TryGetContentHash(assetDirectoryPath + fileName, out hash);
 135        }
 136
 137        protected override void OnReuse(Action OnSuccess)
 138        {
 29139            asset.renderers = asset.container.GetComponentsInChildren<Renderer>(true).ToList();
 140            //NOTE(Brian):  Show the asset using the simple gradient feedback.
 29141            asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS
 29142        }
 143
 144        protected override bool AddToLibrary()
 145        {
 101146            if (!library.Add(asset))
 0147                return false;
 148
 149            //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately
 150            //             So it keeps being there. As master gltfs can't be in the world.
 151            //
 152            //             ApplySettings will reposition the newly Get asset to the proper coordinates.
 101153            if (settings.forceNewInstance)
 154            {
 2155                asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id);
 2156            }
 157            else
 158            {
 99159                asset = library.Get(asset.id);
 160            }
 161
 162            //NOTE(Brian): Call again this method because we are replacing the asset.
 101163            OnBeforeLoadOrReuse();
 101164            return true;
 165        }
 166
 167        protected override void OnCancelLoading()
 168        {
 18169            if (asset != null)
 170            {
 18171                asset.CancelShow();
 172            }
 18173        }
 174
 175        protected override Asset_GLTF GetAsset(object id)
 176        {
 29177            if (settings.forceNewInstance)
 178            {
 9179                return ((AssetLibrary_GLTF)library).GetCopyFromOriginal(id);
 180            }
 181            else
 182            {
 20183                return base.GetAsset(id);
 184            }
 185        }
 186
 187        // NOTE: master promise are silently forgotten. We should make sure that they are loaded anyway since
 188        // other promises are waiting for them
 189        internal void OnSilentForget()
 190        {
 3191            asset.Hide();
 3192            settings.parent = null;
 193
 3194            if (gltfComponent != null)
 195            {
 2196                gltfComponent.SetPrioritized();
 197            }
 3198        }
 199    }
 200}