< 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:73
Uncovered lines:9
Coverable lines:82
Total lines:219
Line coverage:89% (73 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%110100%
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.033085.71%
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    {
 16015        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 16        protected string assetDirectoryPath;
 17
 18        protected ContentProvider provider = null;
 39219        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)
 1133            : this(new ContentProvider_Dummy(), url, null, webRequestController)
 34        {
 1135        }
 36
 37        public AssetPromise_GLTF(ContentProvider provider, string url, string hash = null)
 14238            : this(provider, url, hash, Environment.i.platform.webRequest)
 39        {
 14240        }
 41
 42        public AssetPromise_GLTF(ContentProvider provider, string url, IWebRequestController webRequestController)
 743            : this(provider, url, null, webRequestController)
 44        {
 745        }
 46
 16047        public AssetPromise_GLTF(ContentProvider provider, string url, string hash, IWebRequestController webRequestCont
 48        {
 16049            this.provider = provider;
 16050            this.fileName = url.Substring(url.LastIndexOf('/') + 1);
 16051            this.id = hash ?? url;
 16052            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
 16055            assetDirectoryPath = URIHelper.GetDirectoryName(url);
 16056        }
 57
 58        protected override void OnBeforeLoadOrReuse()
 59        {
 25460            settings.ApplyBeforeLoad(asset.container.transform);
 25461        }
 62
 63        protected override void OnAfterLoadOrReuse()
 64        {
 13765            if (asset?.container != null)
 66            {
 13767                asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 13768                settings.ApplyAfterLoad(asset.renderers.ToList());
 69            }
 13770        }
 71
 96072        public override object GetId() { return id; }
 73
 74        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 75        {
 76            try
 77            {
 11678                gltfComponent = asset.container.AddComponent<GLTFComponent>();
 79
 11680                gltfComponent.Initialize(webRequestController, AssetPromiseKeeper_GLTF.i.throttlingCounter);
 11681                gltfComponent.RegisterCallbacks(MeshCreated, RendererCreated);
 82
 11683                GLTFComponent.Settings tmpSettings = new GLTFComponent.Settings
 84                {
 85                    useVisualFeedback = settings.visibleFlags ==
 86                                        AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION,
 87                    initialVisibility = settings.visibleFlags != AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE,
 88                    shaderOverride = settings.shaderOverride,
 89                    addMaterialsToPersistentCaching =
 90                        (settings.cachingFlags & MaterialCachingHelper.Mode.CACHE_MATERIALS) != 0,
 91                    forceGPUOnlyMesh = settings.forceGPUOnlyMesh
 92                };
 93
 11694                gltfComponent.OnSuccess += () =>
 95                {
 96#if UNITY_STANDALONE || UNITY_EDITOR
 10397                    if (DataStore.i.common.isApplicationQuitting.Get())
 098                        return;
 99#endif
 100
 103101                    if (asset != null)
 102                    {
 103103                        asset.totalTriangleCount =
 104                            MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCount);
 103105                        asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers);
 103106                        asset.textures = MeshesInfoUtils.ExtractUniqueTextures(asset.materials);
 103107                        asset.animationClipSize = gltfComponent.GetAnimationClipMemorySize();
 103108                        asset.meshDataSize = gltfComponent.GetMeshesMemorySize();
 103109                        var animations = MeshesInfoUtils.ExtractUniqueAnimations(asset.container);
 103110                        asset.animationClips = MeshesInfoUtils.ExtractUniqueAnimationClips(animations);
 111                    }
 112
 103113                    OnSuccess.Invoke();
 103114                };
 115
 116116                gltfComponent.OnFail += OnFail;
 117
 116118                gltfComponent.LoadAsset(provider.baseUrl ?? assetDirectoryPath, fileName, GetId() as string,
 119                    false, tmpSettings, FileToHash);
 120
 116121                asset.name = fileName;
 116122            }
 0123            catch (Exception error)
 124            {
 0125                OnFail?.Invoke(error);
 0126            }
 116127        }
 128
 129        private void RendererCreated(Renderer r)
 130        {
 302131            Assert.IsTrue(r != null, "Renderer is null?");
 132
 133            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 302134            if ( asset == null )
 0135                return;
 136
 302137            asset.renderers.Add(r);
 302138        }
 139
 140        private void MeshCreated(Mesh mesh)
 141        {
 326142            Assert.IsTrue(mesh != null, "Mesh is null?");
 143
 144            // TODO(Brian): SilentForget nulls this. Remove this line after fixing the GLTF cancellation.
 326145            if ( asset == null )
 0146                return;
 147
 326148            asset.meshes.Add(mesh);
 326149            asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 326150        }
 151
 281152        bool FileToHash(string fileName, out string hash) { return provider.TryGetContentHash(assetDirectoryPath + fileN
 153
 154        protected override void OnReuse(Action OnSuccess)
 155        {
 156            // Materials and textures are reused, so they are not extracted again
 35157            asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 158
 159            //NOTE(Brian):  Show the asset using the simple gradient feedback.
 35160            asset.Show(settings.visibleFlags == AssetPromiseSettings_Rendering.VisibleFlags.VISIBLE_WITH_TRANSITION, OnS
 35161        }
 162
 163        protected override bool AddToLibrary()
 164        {
 103165            if (!library.Add(asset))
 0166                return false;
 167
 168            //NOTE(Brian): If the asset did load "in world" add it to library and then Get it immediately
 169            //             So it keeps being there. As master gltfs can't be in the world.
 170            //
 171            //             ApplySettings will reposition the newly Get asset to the proper coordinates.
 103172            if (settings.forceNewInstance)
 173            {
 2174                asset = (library as AssetLibrary_GLTF).GetCopyFromOriginal(asset.id);
 175            }
 176            else
 177            {
 101178                asset = library.Get(asset.id);
 179            }
 180
 181            //NOTE(Brian): Call again this method because we are replacing the asset.
 103182            OnBeforeLoadOrReuse();
 103183            return true;
 184        }
 185
 186        protected override void OnCancelLoading()
 187        {
 14188            if (asset != null)
 189            {
 14190                asset.CancelShow();
 191            }
 14192        }
 193
 194        protected override Asset_GLTF GetAsset(object id)
 195        {
 35196            if (settings.forceNewInstance)
 197            {
 9198                return ((AssetLibrary_GLTF)library).GetCopyFromOriginal(id);
 199            }
 200            else
 201            {
 26202                return base.GetAsset(id);
 203            }
 204        }
 205
 206        // NOTE: master promise are silently forgotten. We should make sure that they are loaded anyway since
 207        // other promises are waiting for them
 208        internal void OnSilentForget()
 209        {
 3210            asset.Hide();
 3211            settings.parent = null;
 212
 3213            if (gltfComponent != null)
 214            {
 3215                gltfComponent.SetPrioritized();
 216            }
 3217        }
 218    }
 219}