< Summary

Class:DCL.GLTFast.Wrappers.GltFastDownloadProvider
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTFast/Wrappers/GLTFastDownloadProvider.cs
Covered lines:41
Uncovered lines:5
Coverable lines:46
Total lines:123
Line coverage:89.1% (41 of 46)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:5
Method coverage:100% (5 of 5)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GltFastDownloadProvider(...)0%110100%
RequestAsync()0%5.125083.33%
GetFinalUrl(...)0%5.025090.91%
RequestTextureAsync()0%6.26082.35%
Dispose()0%220100%

File(s)

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

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Collections.Generic;
 4using System.Threading.Tasks;
 5using GLTFast.Loading;
 6using MainScripts.DCL.Controllers.AssetManager;
 7using UnityEngine;
 8using UnityEngine.Networking;
 9
 10namespace DCL.GLTFast.Wrappers
 11{
 12    /// <summary>
 13    /// With this class we replace all of GLTFast web requests with our own
 14    /// </summary>
 15    internal class GltFastDownloadProvider : IDownloadProvider, IDisposable
 16    {
 17        public delegate bool AssetIdConverter(string uri, out string id);
 18
 19        private readonly IWebRequestController webRequestController;
 20        private readonly AssetIdConverter fileToUrl;
 21        private readonly AssetPromiseKeeper_Texture texturePromiseKeeper;
 22
 10323        private List<IDisposable> disposables = new ();
 24        private string baseUrl;
 25        private bool isDisposed;
 26
 10327        public GltFastDownloadProvider(string baseUrl, IWebRequestController webRequestController, AssetIdConverter file
 28        {
 10329            this.baseUrl = baseUrl;
 10330            this.webRequestController = webRequestController;
 10331            this.fileToUrl = fileToUrl;
 10332            this.texturePromiseKeeper = texturePromiseKeeper;
 10333        }
 34
 35        public async Task<IDownload> RequestAsync(Uri uri)
 36        {
 10037            if (isDisposed)
 038                return null;
 39
 10040            string finalUrl = GetFinalUrl(uri, false);
 41
 29842            var asyncOp = await webRequestController.GetAsync(
 43                url: finalUrl,
 44                downloadHandler: new DownloadHandlerBuffer(),
 45                timeout: 30,
 46                requestAttemps: 3);
 47
 10048            GltfDownloaderWrapper wrapper = new GltfDownloaderWrapper(asyncOp);
 10049            disposables.Add(wrapper);
 50
 10051            if (!wrapper.Success) { Debug.LogError($"<color=Red>[GLTFast WebRequest Failed]</color> {asyncOp.url} {async
 52
 10053            return wrapper;
 10054        }
 55
 56        private string GetFinalUrl(Uri uri, bool isTexture)
 57        {
 13458            string fileName = uri.OriginalString;
 59
 13460            if (string.IsNullOrEmpty(baseUrl))
 9461                return fileName;
 62
 4063            fileName = fileName.Replace(baseUrl, "");
 64
 4065            if (fileName.StartsWith("file://"))
 166                return fileName;
 67
 68            // this can return false and the url is valid, only happens with asset with hash as a name ( mostly gltf )
 3969            if (fileToUrl(fileName, out string finalUrl))
 370                return finalUrl;
 71
 3672            if (isTexture)
 073                throw new Exception($"File not found in scene {finalUrl}");
 74
 3675            return uri.OriginalString;
 76        }
 77
 78        public async Task<ITextureDownload> RequestTextureAsync(Uri uri, bool nonReadable, bool forceLinear)
 79        {
 3480            if (isDisposed)
 081                return null;
 82
 3483            string finalUrl = GetFinalUrl(uri, true);
 84
 3485            var promise = new AssetPromise_Texture(
 86                finalUrl,
 87                storeTexAsNonReadable: nonReadable,
 88                overrideMaxTextureSize: DataStore.i.textureConfig.gltfMaxSize.Get(),
 89                overrideCompression:
 90#if UNITY_WEBGL
 91                true
 92#else
 93                false
 94#endif
 95              , linear: forceLinear
 96            );
 97
 3498            var wrapper = new GLTFastTexturePromiseWrapper(texturePromiseKeeper, promise);
 3499            disposables.Add(wrapper);
 100
 34101            Exception promiseException = null;
 34102            promise.OnFailEvent += (_,e) => promiseException = e;
 103
 34104            texturePromiseKeeper.Keep(promise);
 102105            await promise;
 106
 34107            if (!wrapper.Success)
 108            {
 0109                string errorMessage = promiseException != null ? promiseException.Message : wrapper.Error;
 0110                Debug.LogError($"[GLTFast Texture WebRequest Failed] Error: {errorMessage} | url: " + finalUrl);
 111            }
 112
 34113            return wrapper;
 34114        }
 115
 116        public void Dispose()
 117        {
 599118            foreach (IDisposable disposable in disposables) { disposable.Dispose(); }
 103119            disposables.Clear();
 103120            isDisposed = true;
 103121        }
 122    }
 123}