| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using MainScripts.DCL.Analytics.PerformanceAnalytics; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Networking; |
| | 6 | |
|
| | 7 | | namespace DCL |
| | 8 | | { |
| | 9 | | public class AssetPromise_Texture : AssetPromise<Asset_Texture> |
| | 10 | | { |
| | 11 | | private const string PLAIN_BASE64_PROTOCOL = "data:text/plain;base64,"; |
| | 12 | | private const TextureWrapMode DEFAULT_WRAP_MODE = TextureWrapMode.Clamp; |
| | 13 | | private const FilterMode DEFAULT_FILTER_MODE = FilterMode.Bilinear; |
| | 14 | |
|
| | 15 | | private readonly string idWithTexSettings; |
| | 16 | | private readonly string idWithDefaultTexSettings; |
| | 17 | | private readonly TextureWrapMode wrapMode; |
| | 18 | | private readonly FilterMode filterMode; |
| | 19 | | private readonly bool storeDefaultTextureInAdvance = false; |
| | 20 | | private readonly bool storeTexAsNonReadable = false; |
| | 21 | | private readonly int maxTextureSize; |
| | 22 | |
|
| 0 | 23 | | public string url { get; } |
| | 24 | |
|
| | 25 | | WebRequestAsyncOperation webRequestOp = null; |
| | 26 | |
|
| 156 | 27 | | public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t |
| | 28 | | { |
| 156 | 29 | | url = textureUrl; |
| 156 | 30 | | wrapMode = textureWrapMode; |
| 156 | 31 | | filterMode = textureFilterMode; |
| 156 | 32 | | this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance; |
| 156 | 33 | | this.storeTexAsNonReadable = storeTexAsNonReadable; |
| 156 | 34 | | maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get(); |
| 156 | 35 | | idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize); |
| 156 | 36 | | idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f |
| 156 | 37 | | } |
| | 38 | |
|
| 83 | 39 | | protected override void OnAfterLoadOrReuse() { } |
| | 40 | |
|
| 149 | 41 | | protected override void OnBeforeLoadOrReuse() { } |
| | 42 | |
|
| 149 | 43 | | protected override object GetLibraryAssetCheckId() { return idWithTexSettings; } |
| | 44 | |
|
| | 45 | | protected override void OnCancelLoading() |
| | 46 | | { |
| 66 | 47 | | if (webRequestOp != null) |
| 63 | 48 | | webRequestOp.Dispose(); |
| 66 | 49 | | } |
| | 50 | |
|
| | 51 | | protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) |
| | 52 | | { |
| | 53 | | // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi |
| 135 | 54 | | if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode()) |
| | 55 | | { |
| 2 | 56 | | OnSuccess?.Invoke(); |
| 2 | 57 | | return; |
| | 58 | | } |
| | 59 | |
|
| 133 | 60 | | if (!url.StartsWith(PLAIN_BASE64_PROTOCOL)) |
| | 61 | | { |
| 132 | 62 | | webRequestOp = DCL.Environment.i.platform.webRequest.GetTexture( |
| | 63 | | url: url, |
| | 64 | | OnSuccess: (webRequestResult) => |
| | 65 | | { |
| 68 | 66 | | if (asset != null) |
| | 67 | | { |
| 68 | 68 | | Texture2D texture = DownloadHandlerTexture.GetContent(webRequestResult.webRequest); |
| 68 | 69 | | asset.texture = TextureHelpers.ClampSize(texture, maxTextureSize, useGPUCopy: false); |
| 68 | 70 | | asset.resizingFactor = Mathf.Min(1, TextureHelpers.GetScalingFactor(texture.width, texture.h |
| 68 | 71 | | if (TextureUtils.IsQuestionMarkPNG(asset.texture)) |
| 1 | 72 | | OnFail?.Invoke(new Exception("The texture is a question mark")); |
| | 73 | | else |
| 67 | 74 | | OnSuccess?.Invoke(); |
| 67 | 75 | | } |
| | 76 | | else |
| | 77 | | { |
| 0 | 78 | | OnFail?.Invoke(new Exception("The texture asset is null")); |
| | 79 | | } |
| 0 | 80 | | }, |
| | 81 | | OnFail: (webRequestResult) => |
| | 82 | | { |
| 51 | 83 | | OnFail?.Invoke(new Exception($"Texture promise failed: {webRequestResult?.webRequest?.error}")); |
| 51 | 84 | | }); |
| 132 | 85 | | } |
| | 86 | | else |
| | 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 | | try { |
| 1 | 91 | | string substring = url.Substring(PLAIN_BASE64_PROTOCOL.Length); |
| 1 | 92 | | byte[] decodedTexture = Convert.FromBase64String(substring); |
| 0 | 93 | | asset.texture = new Texture2D(1, 1); |
| 0 | 94 | | asset.texture.LoadImage(decodedTexture); |
| 0 | 95 | | asset.resizingFactor = TextureHelpers.GetScalingFactor(asset.texture.width, asset.texture.height, ma |
| 0 | 96 | | asset.texture = TextureHelpers.ClampSize(asset.texture, maxTextureSize); |
| 0 | 97 | | OnSuccess?.Invoke(); |
| 0 | 98 | | } |
| 1 | 99 | | catch (Exception e) |
| | 100 | | { |
| 1 | 101 | | OnFail?.Invoke(e); |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | } |
| 1 | 105 | | } |
| | 106 | |
|
| | 107 | | protected override bool AddToLibrary() |
| | 108 | | { |
| 69 | 109 | | if (storeDefaultTextureInAdvance && !UsesDefaultWrapAndFilterMode()) |
| | 110 | | { |
| 1 | 111 | | if (!library.Contains(idWithDefaultTexSettings)) |
| | 112 | | { |
| | 113 | | // Save default texture asset |
| 1 | 114 | | asset.id = idWithDefaultTexSettings; |
| 1 | 115 | | asset.ConfigureTexture(DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, false); |
| | 116 | |
|
| 1 | 117 | | if (!library.Add(asset)) |
| | 118 | | { |
| 0 | 119 | | Debug.Log("add to library fail?"); |
| 0 | 120 | | return false; |
| | 121 | | } |
| | 122 | | } |
| | 123 | |
|
| | 124 | | // By always using library.Get() for the default tex we have stored, we increase its references counter, |
| | 125 | | // that will come in handy for removing that default tex when there is no one using it |
| 1 | 126 | | var defaultTexAsset = library.Get(idWithDefaultTexSettings); |
| 1 | 127 | | asset = defaultTexAsset.Clone() as Asset_Texture; |
| 1 | 128 | | asset.dependencyAsset = defaultTexAsset; |
| 1 | 129 | | asset.texture = TextureHelpers.CopyTexture(defaultTexAsset.texture); |
| | 130 | | } |
| | 131 | |
|
| 69 | 132 | | asset.id = idWithTexSettings; |
| 69 | 133 | | asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable); |
| | 134 | |
|
| 69 | 135 | | if (!library.Add(asset)) |
| | 136 | | { |
| 0 | 137 | | Debug.Log("add to library fail?"); |
| 0 | 138 | | return false; |
| | 139 | | } |
| | 140 | |
|
| 69 | 141 | | PerformanceAnalytics.PromiseTextureTracker.Track(); |
| | 142 | |
|
| 69 | 143 | | asset = library.Get(asset.id); |
| 69 | 144 | | return true; |
| | 145 | | } |
| | 146 | |
|
| 168 | 147 | | string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode, int maxSize |
| | 148 | |
|
| | 149 | | public override object GetId() |
| | 150 | | { |
| | 151 | | // We only use the id-with-settings when storing/reading from the library |
| 630 | 152 | | return idWithDefaultTexSettings; |
| | 153 | | } |
| | 154 | |
|
| 202 | 155 | | public bool UsesDefaultWrapAndFilterMode() { return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTE |
| | 156 | | } |
| | 157 | | } |