< 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:70
Uncovered lines:10
Coverable lines:80
Total lines:218
Line coverage:87.5% (70 of 80)
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    {
 24115        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)
 543            : this(provider, url, null, webRequestController)
 44        {
 545        }
 46
 24147        public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont
 48        {
 24149            this.provider = provider;
 24150            this.fileName = url.Substring(url.LastIndexOf('/') + 1);
 24151            this.id = hash ?? url;
 24152            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
 24155            assetDirectoryPath = URIHelper.GetDirectoryName(url);
 24156        }
 57
 58        protected override void OnBeforeLoadOrReuse()
 59        {
 60#if UNITY_EDITOR
 32861            asset.container.name = "GLTF: " + this.id;
 62#endif
 32863            settings.ApplyBeforeLoad(asset.container.transform);
 32864        }
 65
 66        protected override void OnAfterLoadOrReuse()
 67        {
 12868            if (asset?.container != null)
 69            {
 12870                asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 12871                settings.ApplyAfterLoad(asset.renderers.ToList());
 72            }
 12873        }
 74
 148375        public override object GetId() { return id; }
 76
 77        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 78        {
 79            try
 80            {
 19981                gltfComponent = asset.container.AddComponent<GLTFComponent>();
 82
 19983                gltfComponent.Initialize(webRequestController, AssetPromiseKeeper_GLTF.i.throttlingCounter);
 19984                gltfComponent.RegisterCallbacks(MeshCreated, RendererCreated);
 85
 19986                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
 19997                gltfComponent.OnSuccess += () =>
 98                {
 99#if UNITY_STANDALONE || UNITY_EDITOR
 97100                    if (DataStore.i.common.isApplicationQuitting.Get())
 0101                        return;
 102#endif
 103
 97104                    if (asset != null)
 105                    {
 97106                        asset.totalTriangleCount =
 107                            MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCount);
 97108                        asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers);
 97109                        asset.textures = MeshesInfoUtils.ExtractUniqueTextures(asset.materials);
 110                    }
 111
 97112                    OnSuccess.Invoke();
 97113                };
 114
 199115                gltfComponent.OnFail += OnFail;
 116
 199117                gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string,
 118                    false, tmpSettings, FileToHash);
 119
 199120                asset.name = fileName;
 199121            }
 0122            catch (Exception error)
 123            {
 0124                OnFail?.Invoke(error);
 0125            }
 199126        }
 127
 128        private void RendererCreated(Renderer r)
 129        {
 271130            Assert.IsTrue(r != null, "Renderer is null?");
 131
 132            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 271133            if ( asset == null )
 0134                return;
 135
 271136            asset.renderers.Add(r);
 271137        }
 138
 139        private void MeshCreated(Mesh mesh)
 140        {
 283141            Assert.IsTrue(mesh != null, "Mesh is null?");
 142
 143            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 283144            if ( asset == null )
 0145                return;
 146
 283147            asset.meshes.Add(mesh);
 283148            asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 283149        }
 150
 269151        bool FileToHash(string fileName, out string hash) { return provider.TryGetContentHash(assetDirectoryPath + fileN
 152
 153        protected override void OnReuse(Action OnSuccess)
 154        {
 155            // Materials and textures are reused, so they are not extracted again
 32156            asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 157
 158            //NOTE(Brian):  Show the asset using the simple gradient feedback.
 32159            asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS
 32160        }
 161
 162        protected override bool AddToLibrary()
 163        {
 97164            if (!library.Add(asset))
 0165                return false;
 166
 167            //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately
 168            //             So it keeps being there. As master gltfs can't be in the world.
 169            //
 170            //             ApplySettings will reposition the newly Get asset to the proper coordinates.
 97171            if (settings.forceNewInstance)
 172            {
 2173                asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id);
 2174            }
 175            else
 176            {
 95177                asset = library.Get(asset.id);
 178            }
 179
 180            //NOTE(Brian): Call again this method because we are replacing the asset.
 97181            OnBeforeLoadOrReuse();
 97182            return true;
 183        }
 184
 185        protected override void OnCancelLoading()
 186        {
 103187            if (asset != null)
 188            {
 103189                asset.CancelShow();
 190            }
 103191        }
 192
 193        protected override Asset_GLTF GetAsset(object id)
 194        {
 32195            if (settings.forceNewInstance)
 196            {
 9197                return ((AssetLibrary_GLTF)library).GetCopyFromOriginal(id);
 198            }
 199            else
 200            {
 23201                return base.GetAsset(id);
 202            }
 203        }
 204
 205        // NOTE: master promise are silently forgotten. We should make sure that they are loaded anyway since
 206        // other promises are waiting for them
 207        internal void OnSilentForget()
 208        {
 3209            asset.Hide();
 3210            settings.parent = null;
 211
 3212            if (gltfComponent != null)
 213            {
 3214                gltfComponent.SetPrioritized();
 215            }
 3216        }
 217    }
 218}