| | 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 | |
|
| 697 | 21 | | public TextureAssetResolver(IReadOnlyDictionary<AssetSource, ITextureAssetProvider> providers, DataStore_Feature |
| | 22 | | { |
| 697 | 23 | | this.providers = providers; |
| 697 | 24 | | } |
| | 25 | |
|
| | 26 | | public async UniTask<TextureResponse> GetTextureAsync(AssetSource permittedSources, string url, int maxTextureSi |
| | 27 | | { |
| 132 | 28 | | Exception lastException = null; |
| 132 | 29 | | Texture2D texture = null; |
| | 30 | |
|
| 132 | 31 | | if (!url.StartsWith(PLAIN_BASE64_PROTOCOL)) |
| | 32 | | { |
| 131 | 33 | | using var permittedSourcesRent = GetPermittedProviders(this.providers, permittedSources); |
| 131 | 34 | | var permittedProviders = permittedSourcesRent.GetList(); |
| | 35 | |
|
| 538 | 36 | | foreach (var provider in permittedProviders) |
| | 37 | | { |
| | 38 | | try |
| | 39 | | { |
| 534 | 40 | | texture = await provider.GetTextureAsync(url, cancellationToken); |
| | 41 | |
|
| 116 | 42 | | if (texture) |
| | 43 | | { |
| 69 | 44 | | if (TextureUtils.IsQuestionMarkPNG(texture)) |
| | 45 | | { |
| 0 | 46 | | lastException = new Exception("The texture is a question mark"); |
| 0 | 47 | | continue; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | // The valid texture is retrieved |
| 69 | 51 | | AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provi |
| 69 | 52 | | break; |
| | 53 | | } |
| | 54 | |
|
| 47 | 55 | | AssetResolverLogger.LogVerbose(featureFlags, LogType.Log, $"Texture {url} loaded from {provider} |
| 47 | 56 | | } |
| 62 | 57 | | catch (Exception e) |
| | 58 | | { |
| 62 | 59 | | lastException = e; |
| | 60 | |
|
| 62 | 61 | | AssetResolverLogger.LogVerbose(featureFlags, e); |
| | 62 | |
|
| | 63 | | // Propagate `OperationCanceledException` further as there is no reason to iterate |
| 62 | 64 | | if (e is OperationCanceledException) |
| 11 | 65 | | break; |
| 51 | 66 | | } |
| 98 | 67 | | } |
| 131 | 68 | | } |
| | 69 | | else |
| | 70 | | { |
| 1 | 71 | | try { texture = LoadFromBase64(url); } |
| 3 | 72 | | catch (Exception e) { lastException = e; } |
| | 73 | | } |
| | 74 | |
|
| 132 | 75 | | if (texture) |
| | 76 | | { |
| 69 | 77 | | var clampedTexture = TextureHelpers.ClampSize(texture, maxTextureSize, useGPUCopy: false); |
| | 78 | |
|
| 69 | 79 | | var resizingFactor = Mathf.Min(1, |
| | 80 | | TextureHelpers.GetScalingFactor(texture.width, texture.height, maxTextureSize)); |
| | 81 | |
|
| 69 | 82 | | return new TextureResponse(new TextureSuccessResponse(clampedTexture, resizingFactor)); |
| | 83 | | } |
| | 84 | |
|
| 63 | 85 | | lastException ??= new AssetNotFoundException(permittedSources, url); |
| 63 | 86 | | return new TextureResponse(new TextureFailResponse(lastException)); |
| 132 | 87 | | } |
| | 88 | |
|
| | 89 | | //For Base64 protocols we just take the bytes and create the texture |
| | 90 | | //to avoid Unity's web request issue with large URLs |
| | 91 | | private Texture2D LoadFromBase64(string url) |
| | 92 | | { |
| 1 | 93 | | string substring = url.Substring(PLAIN_BASE64_PROTOCOL.Length); |
| 1 | 94 | | byte[] decodedTexture = Convert.FromBase64String(substring); |
| 0 | 95 | | var texture = new Texture2D(1, 1); |
| 0 | 96 | | texture.LoadImage(decodedTexture); |
| 0 | 97 | | return texture; |
| | 98 | | } |
| | 99 | | } |
| | 100 | | } |