| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using MainScripts.DCL.Controllers.AssetManager; |
| | 3 | | using MainScripts.DCL.Controllers.AssetManager.Texture; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A higher level abstraction to load a texture from either source and apply additional constraints |
| | 13 | | /// </summary> |
| | 14 | | public class TextureAssetResolver : AssetResolverBase, ITextureAssetResolver |
| | 15 | | { |
| | 16 | | private const string PLAIN_BASE64_PROTOCOL = "data:text/plain;base64,"; |
| | 17 | |
|
| | 18 | | private readonly IReadOnlyDictionary<AssetSource, ITextureAssetProvider> providers; |
| | 19 | | private readonly BaseVariable<bool> isDebugMode; |
| | 20 | |
|
| 741 | 21 | | public TextureAssetResolver(IReadOnlyDictionary<AssetSource, ITextureAssetProvider> providers, DataStore_Feature |
| | 22 | | { |
| 741 | 23 | | this.providers = providers; |
| 741 | 24 | | } |
| | 25 | |
|
| | 26 | | public async UniTask<TextureResponse> GetTextureAsync(AssetSource permittedSources, string url, int maxTextureSi |
| | 27 | | CancellationToken cancellationToken = default) |
| | 28 | | { |
| 119 | 29 | | Exception lastException = null; |
| 119 | 30 | | Texture2D texture = null; |
| | 31 | |
|
| 119 | 32 | | if (!url.StartsWith(PLAIN_BASE64_PROTOCOL)) |
| | 33 | | { |
| 118 | 34 | | using var permittedSourcesRent = GetPermittedProviders(this.providers, permittedSources); |
| 118 | 35 | | var permittedProviders = permittedSourcesRent.GetList(); |
| | 36 | |
|
| 358 | 37 | | foreach (var provider in permittedProviders) |
| | 38 | | { |
| | 39 | | try |
| | 40 | | { |
| 354 | 41 | | texture = await provider.GetTextureAsync(url, cancellationToken); |
| | 42 | |
|
| 103 | 43 | | if (texture) |
| | 44 | | { |
| 103 | 45 | | if (TextureUtils.IsQuestionMarkPNG(texture)) |
| | 46 | | { |
| 0 | 47 | | lastException = new Exception("The texture is a question mark"); |
| 0 | 48 | | continue; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | // The valid texture is retrieved |
| 103 | 52 | | AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provi |
| 103 | 53 | | break; |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provider} |
| 0 | 57 | | } |
| 15 | 58 | | catch (Exception e) |
| | 59 | | { |
| 15 | 60 | | lastException = e; |
| | 61 | |
|
| 15 | 62 | | AssetResolverLogger.LogVerbose(featureFlags, e); |
| | 63 | |
|
| | 64 | | // Propagate `OperationCanceledException` further as there is no reason to iterate |
| 15 | 65 | | if (e is OperationCanceledException) |
| 11 | 66 | | break; |
| 4 | 67 | | } |
| 4 | 68 | | } |
| 118 | 69 | | } |
| | 70 | | else |
| | 71 | | { |
| 1 | 72 | | try { texture = LoadFromBase64(url); } |
| 3 | 73 | | catch (Exception e) { lastException = e; } |
| | 74 | | } |
| | 75 | |
|
| 119 | 76 | | if (texture) |
| | 77 | | { |
| 103 | 78 | | Texture2D clampedTexture = TextureHelpers.ClampSize(texture, maxTextureSize, linear: linear, useGPUCopy: |
| 103 | 79 | | float resizingFactor = Mathf.Min(1, TextureHelpers.GetScalingFactor(texture.width, texture.height, maxTe |
| | 80 | |
|
| 103 | 81 | | return new TextureResponse(new TextureSuccessResponse(clampedTexture, resizingFactor)); |
| | 82 | | } |
| | 83 | |
|
| 16 | 84 | | lastException ??= new AssetNotFoundException(permittedSources, url); |
| 16 | 85 | | return new TextureResponse(new TextureFailResponse(lastException)); |
| 119 | 86 | | } |
| | 87 | |
|
| | 88 | | //For Base64 protocols we just take the bytes and create the texture |
| | 89 | | //to avoid Unity's web request issue with large URLs |
| | 90 | | private Texture2D LoadFromBase64(string url) |
| | 91 | | { |
| 1 | 92 | | string substring = url.Substring(PLAIN_BASE64_PROTOCOL.Length); |
| 1 | 93 | | byte[] decodedTexture = Convert.FromBase64String(substring); |
| 0 | 94 | | var texture = new Texture2D(1, 1); |
| 0 | 95 | | texture.LoadImage(decodedTexture); |
| 0 | 96 | | return texture; |
| | 97 | | } |
| | 98 | | } |
| | 99 | | } |