< 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:46
Uncovered lines:8
Coverable lines:54
Total lines:200
Line coverage:85.1% (46 of 54)
Covered branches:0
Total branches:0
Covered methods:11
Total methods:11
Method coverage:100% (11 of 11)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_GLTFast_Loader(...)0%440100%
GetDirectoryName(...)0%110100%
OnBeforeLoadOrReuse()0%110100%
OnAfterLoadOrReuse()0%110100%
AddToLibrary()0%4.123050%
OnCancelLoading()0%110100%
OnLoad(...)0%110100%
Unload()0%110100%
ImportGltfAsync()0%9.499081.82%
FileToUrl(...)0%110100%

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 System.Diagnostics;
 9using System.IO;
 10using UnityEngine;
 11using Debug = UnityEngine.Debug;
 12
 13// Disable async call not being awaited warning
 14#pragma warning disable CS4014
 15
 16namespace DCL
 17{
 18    /// <summary>
 19    /// Do not use this class directly to instantiate a new GLTF, use AssetPromise_GLTFast_Instance instead
 20    /// </summary>
 21    public class AssetPromise_GLTFast_Loader : AssetPromise_WithUrl<Asset_GLTFast_Loader>
 22    {
 23        private const string SHADER_DCL_LIT = "DCL/Universal Render Pipeline/Lit";
 24        private const string GLTFAST_THROTTLER_NAME = "GLTFastThrottler";
 25        private readonly ContentProvider contentProvider;
 26        private readonly string assetDirectoryPath;
 27        private readonly GltFastDownloadProvider gltFastDownloadProvider;
 28        private readonly CancellationTokenSource cancellationSource;
 29
 30        private readonly IMaterialGenerator gltFastMaterialGenerator;
 31        private readonly GltfastEditorLogger consoleLogger;
 32
 33        private static IDeferAgent staticDeferAgent;
 34        private bool isLoading = false;
 35
 36        public AssetPromise_GLTFast_Loader(string contentUrl, string hash, IWebRequestController requestController, Cont
 10337            : base(contentUrl, hash)
 38        {
 10339            this.contentProvider = contentProvider;
 10340            assetDirectoryPath = GetDirectoryName(contentUrl);
 41
 10342            if (staticDeferAgent == null)
 43            {
 144                var agentObject = new GameObject(GLTFAST_THROTTLER_NAME);
 145                staticDeferAgent = agentObject.AddComponent<GltFastDeferAgent>();
 46            }
 47
 10348            string baseUrl = contentProvider is null ? string.Empty : contentProvider.baseUrl;
 10349            gltFastDownloadProvider = new GltFastDownloadProvider(baseUrl, requestController, FileToUrl, AssetPromiseKee
 10350            cancellationSource = new CancellationTokenSource();
 10351            gltFastMaterialGenerator = new DecentralandMaterialGenerator(SHADER_DCL_LIT);
 10352            consoleLogger = new GltfastEditorLogger();
 10353        }
 54
 55        private static string GetDirectoryName(string fullPath)
 56        {
 10357            var fileName = Path.GetFileName(fullPath);
 10358            return fullPath.Substring(0, fullPath.Length - fileName.Length);
 59        }
 60
 10361        protected override void OnBeforeLoadOrReuse() { }
 62
 10063        protected override void OnAfterLoadOrReuse() { }
 64
 65        protected override bool AddToLibrary()
 66        {
 9767            if (!library.Add(asset))
 68            {
 069                Debug.Log("add to library fail?");
 070                return false;
 71            }
 72
 9773            if (asset == null)
 74            {
 075                Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}");
 076                return false;
 77            }
 78
 9779            asset = library.Get(asset.id);
 9780            return true;
 81        }
 82
 83        protected override void OnCancelLoading()
 84        {
 385            cancellationSource.Cancel();
 386        }
 87
 88        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 89        {
 10090            isLoading = true;
 10091            ImportGltfAsync(onSuccess, onFail, cancellationSource.Token);
 10092        }
 93
 94        internal override void Unload()
 95        {
 10396            gltFastDownloadProvider.Dispose();
 10397            base.Unload();
 10398        }
 99
 449100        public override bool keepWaiting => isLoading;
 101
 102        private async UniTaskVoid ImportGltfAsync(Action onSuccess, Action<Exception> onFail, CancellationToken cancella
 103        {
 104            try
 105            {
 100106                string url = contentProvider.baseUrl + hash;
 107
 100108                var gltfImport = new GltfImport(gltFastDownloadProvider, staticDeferAgent, gltFastMaterialGenerator, con
 109
 100110                var gltFastSettings = new ImportSettings
 111                {
 112                    GenerateMipMaps = false,
 113                    AnisotropicFilterLevel = 3,
 114                    NodeNameMethod = NameImportMethod.OriginalUnique
 115                };
 116
 298117                bool success = await gltfImport.Load(url, gltFastSettings, cancellationSourceToken);
 118
 100119                if (cancellationSourceToken.IsCancellationRequested)
 120                {
 3121                    gltfImport.Dispose();
 3122                    cancellationSourceToken.ThrowIfCancellationRequested();
 123                }
 124
 97125                if (!success)
 0126                    onFail?.Invoke(new GltFastLoadException($"[GLTFast] Load failed: {consoleLogger.LastErrorCode}"));
 127                else
 128                {
 97129                    asset.Setup(gltfImport);
 97130                    onSuccess.Invoke();
 131                }
 97132            }
 3133            catch (Exception e)
 134            {
 3135                if (e is OperationCanceledException)
 3136                    return;
 137
 0138                Debug.LogException(e);
 0139                onFail?.Invoke(e);
 0140            }
 100141            finally { isLoading = false; }
 100142        }
 143
 144        private bool FileToUrl(string fileName, out string fileHash) =>
 39145            contentProvider.TryGetContentsUrl(assetDirectoryPath + fileName, out fileHash);
 146    }
 147
 148    public class GltfastEditorLogger : ICodeLogger
 149    {
 150        public LogCode LastErrorCode { get; private set; }
 151
 152        public void Error(LogCode code, params string[] messages)
 153        {
 154            LastErrorCode = code;
 155            Debug.LogError(LogMessages.GetFullMessage(code, messages));
 156        }
 157
 158        public void Warning(LogCode code, params string[] messages)
 159        {
 160            LogWarning(LogMessages.GetFullMessage(code, messages));
 161        }
 162
 163        public void Info(LogCode code, params string[] messages)
 164        {
 165            LogVerbose(LogMessages.GetFullMessage(code, messages));
 166        }
 167
 168        public void Error(string message)
 169        {
 170            Debug.LogError(message);
 171        }
 172
 173        public void Warning(string message)
 174        {
 175            LogWarning(message);
 176        }
 177
 178        public void Info(string message)
 179        {
 180            LogVerbose(message);
 181        }
 182
 183        [Conditional("UNITY_EDITOR")]
 184        private void LogWarning(string message)
 185        {
 186            Debug.LogWarning(message);
 187        }
 188
 189        [Conditional("UNITY_EDITOR")]
 190        private void LogVerbose(string message)
 191        {
 192            Debug.Log(message);
 193        }
 194    }
 195
 196    public class GltFastLoadException : Exception
 197    {
 198        public GltFastLoadException(string message) : base(message) { }
 199    }
 200}