< 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:49
Coverable lines:49
Total lines:133
Line coverage:0% (0 of 49)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_GLTFast_Loader(...)0%20400%
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            string baseUrl = contentProvider is null ? string.Empty : contentProvider.baseUrl;
 045            gltFastDownloadProvider = new GltFastDownloadProvider(baseUrl, requestController, FileToUrl);
 046            cancellationSource = new CancellationTokenSource();
 047            gltFastMaterialGenerator = new DecentralandMaterialGenerator(SHADER_DCL_LIT);
 048            consoleLogger = new ConsoleLogger();
 049        }
 50
 051        protected override void OnBeforeLoadOrReuse() { }
 52
 053        protected override void OnAfterLoadOrReuse() { }
 54
 55        protected override bool AddToLibrary()
 56        {
 057            if (!library.Add(asset))
 58            {
 059                Debug.Log("add to library fail?");
 060                return false;
 61            }
 62
 063            if (asset == null)
 64            {
 065                Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}");
 066                return false;
 67            }
 68
 069            asset = library.Get(asset.id);
 070            return true;
 71        }
 72
 73        protected override void OnCancelLoading()
 74        {
 075            cancellationSource.Cancel();
 076        }
 77
 78        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 79        {
 080            ImportGltfAsync(onSuccess, onFail, cancellationSource.Token);
 081        }
 82
 83        internal override void Unload()
 84        {
 085            base.Unload();
 086            gltFastDownloadProvider.Dispose();
 087        }
 88
 89        private async UniTaskVoid ImportGltfAsync(Action onSuccess, Action<Exception> onFail, CancellationToken cancella
 90        {
 91            try
 92            {
 093                string url = contentProvider.baseUrl + hash;
 94
 095                var gltfImport = new GltfImport(gltFastDownloadProvider, staticDeferAgent, gltFastMaterialGenerator, con
 96
 097                var gltFastSettings = new ImportSettings
 98                {
 99                    GenerateMipMaps = false,
 100                    AnisotropicFilterLevel = 3,
 101                    NodeNameMethod = NameImportMethod.OriginalUnique
 102                };
 103
 0104                bool success = await gltfImport.Load(url, gltFastSettings, cancellationSourceToken).AsUniTask().AttachEx
 105
 0106                if (cancellationSourceToken.IsCancellationRequested)
 107                {
 0108                    gltfImport.Dispose();
 0109                    cancellationSourceToken.ThrowIfCancellationRequested();
 110                }
 111
 0112                if (!success)
 0113                    onFail?.Invoke(new Exception($"[GLTFast] Failed to load asset {url}"));
 114                else
 115                {
 0116                    asset.Setup(gltfImport);
 0117                    onSuccess.Invoke();
 118                }
 0119            }
 0120            catch (Exception e)
 121            {
 0122                if (e is OperationCanceledException)
 0123                    return;
 124
 0125                Debug.LogException(e);
 0126                onFail?.Invoke(e);
 0127            }
 0128        }
 129
 130        private bool FileToUrl(string fileName, out string fileHash) =>
 0131            contentProvider.TryGetContentsUrl(assetDirectoryPath + fileName, out fileHash);
 132    }
 133}