| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using System; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using MainScripts.DCL.Analytics.PerformanceAnalytics; |
| | 5 | | using MainScripts.DCL.Controllers.AssetManager; |
| | 6 | | using System.Threading; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace DCL |
| | 10 | | { |
| | 11 | | public class AssetPromise_Texture : AssetPromise<Asset_Texture> |
| | 12 | | { |
| | 13 | | private const TextureWrapMode DEFAULT_WRAP_MODE = TextureWrapMode.Clamp; |
| | 14 | | private const FilterMode DEFAULT_FILTER_MODE = FilterMode.Bilinear; |
| | 15 | |
|
| | 16 | | private readonly string idWithTexSettings; |
| | 17 | | private readonly string idWithDefaultTexSettings; |
| | 18 | | private readonly TextureWrapMode wrapMode; |
| | 19 | | private readonly FilterMode filterMode; |
| | 20 | | private readonly bool storeDefaultTextureInAdvance = false; |
| | 21 | | private readonly bool storeTexAsNonReadable = false; |
| | 22 | | private readonly int maxTextureSize; |
| | 23 | | private readonly AssetSource permittedSources; |
| | 24 | |
|
| | 25 | | private CancellationTokenSource cancellationTokenSource; |
| | 26 | |
|
| | 27 | | private Service<ITextureAssetResolver> textureResolver; |
| | 28 | |
|
| 342 | 29 | | public string url { get; } |
| | 30 | |
|
| 152 | 31 | | public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t |
| | 32 | | int? overrideMaxTextureSize = null, AssetSource permittedSources = AssetSource.WEB) |
| | 33 | | { |
| 152 | 34 | | url = textureUrl; |
| 152 | 35 | | wrapMode = textureWrapMode; |
| 152 | 36 | | filterMode = textureFilterMode; |
| 152 | 37 | | this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance; |
| 152 | 38 | | this.storeTexAsNonReadable = storeTexAsNonReadable; |
| 152 | 39 | | maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get(); |
| 152 | 40 | | idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize); |
| 152 | 41 | | idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f |
| 152 | 42 | | this.permittedSources = permittedSources; |
| 152 | 43 | | } |
| | 44 | |
|
| 87 | 45 | | protected override void OnAfterLoadOrReuse() { } |
| | 46 | |
|
| 150 | 47 | | protected override void OnBeforeLoadOrReuse() { } |
| | 48 | |
|
| | 49 | | protected override object GetLibraryAssetCheckId() |
| | 50 | | { |
| 150 | 51 | | return idWithTexSettings; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | protected override void OnCancelLoading() |
| | 55 | | { |
| 63 | 56 | | cancellationTokenSource?.Cancel(); |
| 63 | 57 | | } |
| | 58 | |
|
| | 59 | | protected override void OnLoad(Action onSuccess, Action<Exception> onFail) |
| | 60 | | { |
| | 61 | | // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi |
| 133 | 62 | | if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode()) |
| | 63 | | { |
| 1 | 64 | | onSuccess?.Invoke(); |
| 1 | 65 | | return; |
| | 66 | | } |
| | 67 | |
|
| 132 | 68 | | cancellationTokenSource = new CancellationTokenSource(); |
| | 69 | |
|
| | 70 | | async UniTaskVoid LaunchRequest() |
| | 71 | | { |
| 394 | 72 | | var result = await textureResolver.Ref.GetTextureAsync(permittedSources, url, |
| | 73 | | maxTextureSize, cancellationTokenSource.Token); |
| | 74 | |
|
| 132 | 75 | | if (result.IsSuccess) |
| | 76 | | { |
| 69 | 77 | | var successResponse = result.GetSuccessResponse(); |
| 69 | 78 | | asset.texture = successResponse.Texture; |
| 69 | 79 | | asset.resizingFactor = successResponse.ResizingFactor; |
| 69 | 80 | | onSuccess?.Invoke(); |
| | 81 | | } |
| | 82 | | else |
| | 83 | | { |
| 63 | 84 | | onFail?.Invoke(result.GetFailResponse().Exception); |
| | 85 | | } |
| 132 | 86 | | } |
| | 87 | |
|
| 132 | 88 | | LaunchRequest().Forget(); |
| 132 | 89 | | } |
| | 90 | |
|
| | 91 | | protected override bool AddToLibrary() |
| | 92 | | { |
| 70 | 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 | |
|
| 70 | 116 | | asset.id = idWithTexSettings; |
| 70 | 117 | | asset.ConfigureTexture(wrapMode, filterMode, storeTexAsNonReadable); |
| | 118 | |
|
| 70 | 119 | | if (!library.Add(asset)) |
| | 120 | | { |
| 0 | 121 | | Debug.Log("add to library fail?"); |
| 0 | 122 | | return false; |
| | 123 | | } |
| | 124 | |
|
| 70 | 125 | | PerformanceAnalytics.PromiseTextureTracker.Track(); |
| | 126 | |
|
| 70 | 127 | | asset = library.Get(asset.id); |
| 70 | 128 | | return true; |
| | 129 | | } |
| | 130 | |
|
| | 131 | | string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode, int maxSize |
| | 132 | | { |
| 163 | 133 | | return $"{((int)textureWrapMode)}{((int)textureFilterMode)}{textureUrl}{maxSize}"; |
| | 134 | | } |
| | 135 | |
|
| | 136 | | public override object GetId() |
| | 137 | | { |
| | 138 | | // We only use the id-with-settings when storing/reading from the library |
| 638 | 139 | | return idWithDefaultTexSettings; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public bool UsesDefaultWrapAndFilterMode() |
| | 143 | | { |
| 197 | 144 | | return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTER_MODE; |
| | 145 | | } |
| | 146 | | } |
| | 147 | | } |