< 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:72
Uncovered lines:10
Coverable lines:82
Total lines:220
Line coverage:87.8% (72 of 82)
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.113076.92%
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.Collections.Generic;
 3using System.Linq;
 4using DCL.Helpers;
 5using DCL.Models;
 6using UnityEngine;
 7using UnityEngine.Assertions;
 8using UnityGLTF;
 9using UnityGLTF.Scripts;
 10
 11namespace DCL
 12{
 13    public class AssetPromise_GLTF : AssetPromise<Asset_GLTF>
 14    {
 24315        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 16        protected string assetDirectoryPath;
 17
 18        protected ContentProvider provider = null;
 019        public string fileName { get; private set; }
 20
 21        IGLTFComponent gltfComponent = null;
 22        IWebRequestController webRequestController = null;
 23
 24        object id = null;
 25        private List<int> texIdsCache;
 26
 27        public AssetPromise_GLTF(string url)
 028            : this(new ContentProvider_Dummy(), url, null, Environment.i.platform.webRequest)
 29        {
 030        }
 31
 32        public AssetPromise_GLTF(string url, IWebRequestController webRequestController)
 933            : this(new ContentProvider_Dummy(), url, null, webRequestController)
 34        {
 935        }
 36
 37        public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null)
 22738            : this(provider, url, hash, Environment.i.platform.webRequest)
 39        {
 22740        }
 41
 42        public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController)
 743            : this(provider, url, null, webRequestController)
 44        {
 745        }
 46
 24347        public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont
 48        {
 24349            this.provider = provider;
 24350            this.fileName = url.Substring(url.LastIndexOf('/') + 1);
 24351            this.id = hash ?? url;
 24352            this.webRequestController = webRequestController;
 53            // We separate the directory path of the GLB and its file name, to be able to use the directory path when
 54            // fetching relative assets like textures in the ParseGLTFWebRequestedFile() event call
 24355            assetDirectoryPath = URIHelper.GetDirectoryName(url);
 24356        }
 57
 58        protected override void OnBeforeLoadOrReuse()
 59        {
 60#if UNITY_EDITOR
 33261            asset.container.name = "GLTF: " + this.id;
 62#endif
 33263            settings.ApplyBeforeLoad(asset.container.transform);
 33264        }
 65
 66        protected override void OnAfterLoadOrReuse()
 67        {
 13068            if (asset?.container != null)
 69            {
 13070                asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 13071                settings.ApplyAfterLoad(asset.renderers.ToList());
 72            }
 13073        }
 74
 149575        public override object GetId() { return id; }
 76
 77        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 78        {
 79            try
 80            {
 20181                gltfComponent = asset.container.AddComponent<GLTFComponent>();
 82
 20183                gltfComponent.Initialize(webRequestController);
 20184                gltfComponent.RegisterCallbacks(MeshCreated, RendererCreated);
 85
 20186                GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings
 87                {
 88                    useVisualFeedback = settings.visibleFlags ==
 89                                        AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION,
 90                    initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE,
 91                    shaderOverride = settings.shaderOverride,
 92                    addMaterialsToPersistentCaching =
 93                        (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) != 0,
 94                    forceGPUOnlyMesh = settings.forceGPUOnlyMesh
 95                };
 96
 20197                gltfComponent.OnSuccess += () =>
 98                {
 99#if UNITY_STANDALONE || UNITY_EDITOR
 99100                    if (DataStore.i.common.isApplicationQuitting.Get())
 0101                        return;
 102#endif
 103
 99104                    if (asset != null)
 105                    {
 99106                        asset.totalTriangleCount =
 107                            MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCount);
 99108                        asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers);
 99109                        asset.textures = MeshesInfoUtils.ExtractUniqueTextures(asset.materials);
 99110                        asset.animationClipSize = gltfComponent.GetAnimationClipMemorySize();
 99111                        asset.meshDataSize = gltfComponent.GetMeshesMemorySize();
 112                    }
 113
 99114                    OnSuccess.Invoke();
 99115                };
 116
 201117                gltfComponent.OnFail += OnFail;
 118
 201119                gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string,
 120                    false, tmpSettings, FileToHash);
 121
 201122                asset.name = fileName;
 201123            }
 0124            catch (Exception error)
 125            {
 0126                OnFail?.Invoke(error);
 0127            }
 201128        }
 129
 130        private void RendererCreated(Renderer r)
 131        {
 279132            Assert.IsTrue(r != null, "Renderer is null?");
 133
 134            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 279135            if ( asset == null )
 0136                return;
 137
 279138            asset.renderers.Add(r);
 279139        }
 140
 141        private void MeshCreated(Mesh mesh)
 142        {
 291143            Assert.IsTrue(mesh != null, "Mesh is null?");
 144
 145            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 291146            if ( asset == null )
 0147                return;
 148
 291149            asset.meshes.Add(mesh);
 291150            asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 291151        }
 152
 271153        bool FileToHash(string fileName, out string hash) { return provider.TryGetContentHash(assetDirectoryPath + fileN
 154
 155        protected override void OnReuse(Action OnSuccess)
 156        {
 157            // Materials and textures are reused, so they are not extracted again
 32158            asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 159
 160            //NOTE(Brian):  Show the asset using the simple gradient feedback.
 32161            asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS
 32162        }
 163
 164        protected override bool AddToLibrary()
 165        {
 99166            if (!library.Add(asset))
 0167                return false;
 168
 169            //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately
 170            //             So it keeps being there. As master gltfs can't be in the world.
 171            //
 172            //             ApplySettings will reposition the newly Get asset to the proper coordinates.
 99173            if (settings.forceNewInstance)
 174            {
 2175                asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id);
 2176            }
 177            else
 178            {
 97179                asset = library.Get(asset.id);
 180            }
 181
 182            //NOTE(Brian): Call again this method because we are replacing the asset.
 99183            OnBeforeLoadOrReuse();
 99184            return true;
 185        }
 186
 187        protected override void OnCancelLoading()
 188        {
 103189            if (asset != null)
 190            {
 103191                asset.CancelShow();
 192            }
 103193        }
 194
 195        protected override Asset_GLTF GetAsset(object id)
 196        {
 32197            if (settings.forceNewInstance)
 198            {
 9199                return ((AssetLibrary_GLTF)library).GetCopyFromOriginal(id);
 200            }
 201            else
 202            {
 23203                return base.GetAsset(id);
 204            }
 205        }
 206
 207        // NOTE: master promise are silently forgotten. We should make sure that they are loaded anyway since
 208        // other promises are waiting for them
 209        internal void OnSilentForget()
 210        {
 3211            asset.Hide();
 3212            settings.parent = null;
 213
 3214            if (gltfComponent != null)
 215            {
 3216                gltfComponent.SetPrioritized();
 217            }
 3218        }
 219    }
 220}