< 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:8
Coverable lines:74
Total lines:200
Line coverage:89.1% (66 of 74)
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%3.073080%
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
 27        public AssetPromise_GLTF(string url, IWebRequestController webRequestController)
 1828            : this(new ContentProvider_Dummy(), url, null, webRequestController) { }
 29
 30        public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null)
 28631            : this(provider, url, hash, Environment.i.platform.webRequest) { }
 32
 33        public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController)
 1034            : this(provider, url, null, webRequestController) { }
 35
 15736        public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont
 37        {
 15738            this.provider = provider;
 15739            this.fileName = url.Substring(url.LastIndexOf('/') + 1);
 15740            this.id = hash ?? url;
 15741            this.webRequestController = webRequestController;
 42            // We separate the directory path of the GLB and its file name, to be able to use the directory path when
 43            // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call
 15744            assetDirectoryPath = URIHelper.GetDirectoryName(url);
 15745        }
 46
 47        protected override void OnBeforeLoadOrReuse()
 48        {
 49#if UNITY_EDITOR
 24850            asset.container.name = "GLTF: " + this.id;
 51#endif
 24852            settings.ApplyBeforeLoad(asset.container.transform);
 24853        }
 54
 55        protected override void OnAfterLoadOrReuse()
 56        {
 12957            if (asset?.container != null)
 58            {
 12959                asset.renderers = asset.container.GetComponentsInChildren<Renderer>(true).ToList();
 12960                settings.ApplyAfterLoad(asset.container.transform);
 61            }
 12962        }
 63
 95964        public override object GetId() { return id; }
 65
 66        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 67        {
 68            try
 69            {
 11770                gltfComponent = asset.container.AddComponent<GLTFComponent>();
 11771                gltfComponent.throttlingCounter = AssetPromiseKeeper_GLTF.i.throttlingCounter;
 11772                gltfComponent.Initialize(webRequestController);
 73
 11774                GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings()
 75                {
 76                    useVisualFeedback = settings.visibleFlags ==
 77                                        AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION,
 78                    initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE,
 79                    shaderOverride = settings.shaderOverride,
 80                    addMaterialsToPersistentCaching =
 81                        (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) != 0,
 82                    forceGPUOnlyMesh = settings.forceGPUOnlyMesh
 83                };
 84
 11785                gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string,
 86                    false, tmpSettings, FileToHash);
 87
 11788                gltfComponent.sceneImporter.OnMeshCreated += MeshCreated;
 11789                gltfComponent.sceneImporter.OnRendererCreated += RendererCreated;
 90
 11791                gltfComponent.OnSuccess += () =>
 92                {
 10293                    if (asset != null)
 94                    {
 10295                        asset.totalTriangleCount =
 96                            MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCount);
 97                    }
 98
 10299                    OnSuccess.Invoke();
 102100                };
 101
 117102                gltfComponent.OnFail += OnFail;
 103
 117104                asset.name = fileName;
 117105            }
 0106            catch (Exception error)
 107            {
 0108                OnFail?.Invoke(error);
 0109            }
 117110        }
 111
 112        private void RendererCreated(Renderer r)
 113        {
 263114            Assert.IsTrue(r != null, "Renderer is null?");
 115
 116            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 263117            if ( asset == null )
 0118                return;
 119
 263120            asset.renderers.Add(r);
 263121        }
 122
 123        private void MeshCreated(Mesh mesh)
 124        {
 283125            Assert.IsTrue(mesh != null, "Mesh is null?");
 126
 127            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 283128            if ( asset == null )
 0129                return;
 130
 283131            asset.meshes.Add(mesh);
 283132            asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 283133        }
 134
 291135        bool FileToHash(string fileName, out string hash) { return provider.TryGetContentHash(assetDirectoryPath + fileN
 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        {
 102146            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.
 102153            if (settings.forceNewInstance)
 154            {
 2155                asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id);
 2156            }
 157            else
 158            {
 100159                asset = library.Get(asset.id);
 160            }
 161
 162            //NOTE(Brian): Call again this method because we are replacing the asset.
 102163            OnBeforeLoadOrReuse();
 102164            return true;
 165        }
 166
 167        protected override void OnCancelLoading()
 168        {
 17169            if (asset != null)
 170            {
 17171                asset.CancelShow();
 172            }
 17173        }
 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}