< Summary

Class:DCL.GltFastLoadException
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:1
Coverable lines:1
Total lines:200
Line coverage:0% (0 of 1)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:1
Method coverage:0% (0 of 1)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GltFastLoadException(...)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 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
 37            : base(contentUrl, hash)
 38        {
 39            this.contentProvider = contentProvider;
 40            assetDirectoryPath = GetDirectoryName(contentUrl);
 41
 42            if (staticDeferAgent == null)
 43            {
 44                var agentObject = new GameObject(GLTFAST_THROTTLER_NAME);
 45                staticDeferAgent = agentObject.AddComponent<GltFastDeferAgent>();
 46            }
 47
 48            string baseUrl = contentProvider is null ? string.Empty : contentProvider.baseUrl;
 49            gltFastDownloadProvider = new GltFastDownloadProvider(baseUrl, requestController, FileToUrl, AssetPromiseKee
 50            cancellationSource = new CancellationTokenSource();
 51            gltFastMaterialGenerator = new DecentralandMaterialGenerator(SHADER_DCL_LIT);
 52            consoleLogger = new GltfastEditorLogger();
 53        }
 54
 55        private static string GetDirectoryName(string fullPath)
 56        {
 57            var fileName = Path.GetFileName(fullPath);
 58            return fullPath.Substring(0, fullPath.Length - fileName.Length);
 59        }
 60
 61        protected override void OnBeforeLoadOrReuse() { }
 62
 63        protected override void OnAfterLoadOrReuse() { }
 64
 65        protected override bool AddToLibrary()
 66        {
 67            if (!library.Add(asset))
 68            {
 69                Debug.Log("add to library fail?");
 70                return false;
 71            }
 72
 73            if (asset == null)
 74            {
 75                Debug.LogWarning($"Asset is null when trying to add it to the library: hash == {this.GetId()}");
 76                return false;
 77            }
 78
 79            asset = library.Get(asset.id);
 80            return true;
 81        }
 82
 83        protected override void OnCancelLoading()
 84        {
 85            cancellationSource.Cancel();
 86        }
 87
 88        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 89        {
 90            isLoading = true;
 91            ImportGltfAsync(onSuccess, onFail, cancellationSource.Token);
 92        }
 93
 94        internal override void Unload()
 95        {
 96            gltFastDownloadProvider.Dispose();
 97            base.Unload();
 98        }
 99
 100        public override bool keepWaiting => isLoading;
 101
 102        private async UniTaskVoid ImportGltfAsync(Action onSuccess, Action<Exception> onFail, CancellationToken cancella
 103        {
 104            try
 105            {
 106                string url = contentProvider.baseUrl + hash;
 107
 108                var gltfImport = new GltfImport(gltFastDownloadProvider, staticDeferAgent, gltFastMaterialGenerator, con
 109
 110                var gltFastSettings = new ImportSettings
 111                {
 112                    GenerateMipMaps = false,
 113                    AnisotropicFilterLevel = 3,
 114                    NodeNameMethod = NameImportMethod.OriginalUnique
 115                };
 116
 117                bool success = await gltfImport.Load(url, gltFastSettings, cancellationSourceToken);
 118
 119                if (cancellationSourceToken.IsCancellationRequested)
 120                {
 121                    gltfImport.Dispose();
 122                    cancellationSourceToken.ThrowIfCancellationRequested();
 123                }
 124
 125                if (!success)
 126                    onFail?.Invoke(new GltFastLoadException($"[GLTFast] Load failed: {consoleLogger.LastErrorCode}"));
 127                else
 128                {
 129                    asset.Setup(gltfImport);
 130                    onSuccess.Invoke();
 131                }
 132            }
 133            catch (Exception e)
 134            {
 135                if (e is OperationCanceledException)
 136                    return;
 137
 138                Debug.LogException(e);
 139                onFail?.Invoke(e);
 140            }
 141            finally { isLoading = false; }
 142        }
 143
 144        private bool FileToUrl(string fileName, out string fileHash) =>
 145            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    {
 0198        public GltFastLoadException(string message) : base(message) { }
 199    }
 200}