< 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    {
 15513        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)
 14235            : this(provider, url, hash, Environment.i.platform.webRequest)
 36        {
 14237        }
 38
 39        public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController)
 440            : this(provider, url, null, webRequestController)
 41        {
 442        }
 43
 15544        public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont
 45        {
 15546            this.provider = provider;
 15547            this.fileName = url.Substring(url.LastIndexOf('/') + 1);
 15548            this.id = hash ?? url;
 15549            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
 15552            assetDirectoryPath = URIHelper.GetDirectoryName(url);
 15553        }
 54
 55        protected override void OnBeforeLoadOrReuse()
 56        {
 57#if UNITY_EDITOR
 24458            asset.container.name = "GLTF: " + this.id;
 59#endif
 24460            settings.ApplyBeforeLoad(asset.container.transform);
 24461        }
 62
 63        protected override void OnAfterLoadOrReuse()
 64        {
 12765            if (asset?.container != null)
 66            {
 12767                settings.ApplyAfterLoad(asset.container.transform);
 68            }
 12769        }
 70
 95371        public override object GetId() { return id; }
 72
 73        protected override void OnLoad(Action OnSuccess, Action OnFail)
 74        {
 11575            gltfComponent = asset.container.AddComponent<GLTFComponent>();
 11576            gltfComponent.Initialize(webRequestController);
 77
 11578            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
 11587            gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string,
 88                false, tmpSettings, FileToHash);
 89
 11590            gltfComponent.sceneImporter.OnMeshCreated += MeshCreated;
 11591            gltfComponent.sceneImporter.OnRendererCreated += RendererCreated;
 92
 11593            gltfComponent.OnSuccess += () =>
 94            {
 10095                if ( asset != null )
 96                {
 10097                    asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriang
 98                }
 99
 100100                OnSuccess.Invoke();
 100101            };
 102
 115103            gltfComponent.OnFail += OnFail;
 104
 115105            asset.name = fileName;
 115106        }
 107
 108
 109        private void RendererCreated(Renderer r)
 110        {
 257111            Assert.IsTrue(r != null, "Renderer is null?");
 112
 113            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 257114            if ( asset == null )
 0115                return;
 116
 257117            asset.renderers.Add(r);
 257118        }
 119
 120        private void MeshCreated(Mesh mesh)
 121        {
 277122            Assert.IsTrue(mesh != null, "Mesh is null?");
 123
 124            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 277125            if ( asset == null )
 0126                return;
 127
 277128            asset.meshes.Add(mesh);
 277129            asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 277130        }
 131
 132        bool FileToHash(string fileName, out string hash)
 133        {
 295134            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        {
 100146            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.
 100153            if (settings.forceNewInstance)
 154            {
 2155                asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id);
 2156            }
 157            else
 158            {
 98159                asset = library.Get(asset.id);
 160            }
 161
 162            //NOTE(Brian): Call again this method because we are replacing the asset.
 100163            OnBeforeLoadOrReuse();
 100164            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}