< Summary

Class:DCL.AssetPromise_GLTFast_Loader
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTFast/AssetPromise_GLTFast_Loader.cs
Covered lines:0
Uncovered lines:48
Coverable lines:48
Total lines:132
Line coverage:0% (0 of 48)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_GLTFast_Loader(...)0%6200%
OnBeforeLoadOrReuse()0%2100%
OnAfterLoadOrReuse()0%2100%
AddToLibrary()0%12300%
OnCancelLoading()0%2100%
OnLoad(...)0%2100%
Unload()0%2100%
ImportGltfAsync()0%72800%
FileToUrl(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTFast/AssetPromise_GLTFast_Loader.cs

#LineLine coverage
 1using System;
 2using System.Threading;
 3using Cysharp.Threading.Tasks;
 4using DCL.GLTFast.Wrappers;
 5using GLTFast;
 6using GLTFast.Logging;
 7using GLTFast.Materials;
 8using UnityEngine;
 9
 10// Disable async call not being awaited warning
 11#pragma warning disable CS4014
 12
 13namespace DCL
 14{
 15    /// <summary>
 16    /// Do not use this class directly to instantiate a new GLTF, use AssetPromise_GLTFast_Instance instead
 17    /// </summary>
 18    public class AssetPromise_GLTFast_Loader : AssetPromise_WithUrl<Asset_GLTFast_Loader>
 19    {
 20        private const string SHADER_DCL_LIT = "DCL/Universal Render Pipeline/Lit";
 21        private const string GLTFAST_THROTTLER_NAME = "GLTFastThrottler";
 22        private readonly ContentProvider contentProvider;
 23        private readonly string assetDirectoryPath;
 24        private readonly GltFastDownloadProvider gltFastDownloadProvider;
 25        private readonly CancellationTokenSource cancellationSource;
 26
 27        private readonly IMaterialGenerator gltFastMaterialGenerator;
 28        private readonly ConsoleLogger consoleLogger;
 29
 30        private static IDeferAgent staticDeferAgent;
 31
 32        public AssetPromise_GLTFast_Loader(string contentUrl, string hash, IWebRequestController requestController, Cont
 033            : base(contentUrl, hash)
 34        {
 035            this.contentProvider = contentProvider;
 036            assetDirectoryPath = URIHelper.GetDirectoryName(contentUrl);
 37
 038            if (staticDeferAgent == null)
 39            {
 040                var agentObject = new GameObject(GLTFAST_THROTTLER_NAME);
 041                staticDeferAgent = agentObject.AddComponent<GltFastDeferAgent>();
 42            }
 43
 044            gltFastDownloadProvider = new GltFastDownloadProvider(requestController, FileToUrl);
 045            cancellationSource = new CancellationTokenSource();
 046            gltFastMaterialGenerator = new DecentralandMaterialGenerator(SHADER_DCL_LIT);
 047            consoleLogger = new ConsoleLogger();
 048        }
 49
 050        protected override void OnBeforeLoadOrReuse() { }
 51
 052        protected override void OnAfterLoadOrReuse() { }
 53
 54        protected override bool AddToLibrary()
 55        {
 056            if (!library.Add(asset))
 57            {
 058                Debug.Log("add to library fail?");
 059                return false;
 60            }
 61
 062            if (asset == null)
 63            {
 064                Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}");
 065                return false;
 66            }
 67
 068            asset = library.Get(asset.id);
 069            return true;
 70        }
 71
 72        protected override void OnCancelLoading()
 73        {
 074            cancellationSource.Cancel();
 075        }
 76
 77        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 78        {
 079            ImportGltfAsync(onSuccess, onFail, cancellationSource.Token);
 080        }
 81
 82        internal override void Unload()
 83        {
 084            base.Unload();
 085            gltFastDownloadProvider.Dispose();
 086        }
 87
 88        private async UniTaskVoid ImportGltfAsync(Action onSuccess, Action<Exception> onFail, CancellationToken cancella
 89        {
 90            try
 91            {
 092                string url = contentProvider.baseUrl + hash;
 93
 094                var gltfImport = new GltfImport(gltFastDownloadProvider, staticDeferAgent, gltFastMaterialGenerator, con
 95
 096                var gltFastSettings = new ImportSettings
 97                {
 98                    generateMipMaps = false,
 99                    anisotropicFilterLevel = 3,
 100                    nodeNameMethod = ImportSettings.NameImportMethod.OriginalUnique
 101                };
 102
 0103                bool success = await gltfImport.Load(url, gltFastSettings, cancellationSourceToken).AsUniTask().AttachEx
 104
 0105                if (cancellationSourceToken.IsCancellationRequested)
 106                {
 0107                    gltfImport.Dispose();
 0108                    cancellationSourceToken.ThrowIfCancellationRequested();
 109                }
 110
 0111                if (!success)
 0112                    onFail?.Invoke(new Exception($"[GLTFast] Failed to load asset {url}"));
 113                else
 114                {
 0115                    asset.Setup(gltfImport);
 0116                    onSuccess.Invoke();
 117                }
 0118            }
 0119            catch (Exception e)
 120            {
 0121                if (e is OperationCanceledException)
 0122                    return;
 123
 0124                Debug.LogException(e);
 0125                onFail?.Invoke(e);
 0126            }
 0127        }
 128
 129        private bool FileToUrl(string fileName, out string fileHash) =>
 0130            contentProvider.TryGetContentsUrl(assetDirectoryPath + fileName, out fileHash);
 131    }
 132}