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