| | 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 bool? overrideCompression; |
| | 23 | | private readonly bool linear; |
| | 24 | | private readonly bool useGPUCopy; |
| | 25 | | private readonly int maxTextureSize; |
| | 26 | | private readonly AssetSource permittedSources; |
| | 27 | |
|
| | 28 | | private CancellationTokenSource cancellationTokenSource; |
| | 29 | |
|
| | 30 | | private Service<ITextureAssetResolver> textureResolver; |
| | 31 | |
|
| 301 | 32 | | public string url { get; } |
| | 33 | |
|
| 167 | 34 | | public AssetPromise_Texture(string textureUrl, TextureWrapMode textureWrapMode = DEFAULT_WRAP_MODE, FilterMode t |
| | 35 | | bool storeDefaultTextureInAdvance = false, bool storeTexAsNonReadable = true, |
| | 36 | | int? overrideMaxTextureSize = null, AssetSource permittedSources = AssetSource.WEB, |
| | 37 | | bool? overrideCompression = null, bool linear = false, bool useGPUCopy = false) |
| | 38 | | { |
| 167 | 39 | | url = textureUrl; |
| 167 | 40 | | wrapMode = textureWrapMode; |
| 167 | 41 | | filterMode = textureFilterMode; |
| 167 | 42 | | this.storeDefaultTextureInAdvance = storeDefaultTextureInAdvance; |
| 167 | 43 | | this.storeTexAsNonReadable = storeTexAsNonReadable; |
| 167 | 44 | | this.overrideCompression = overrideCompression; |
| 167 | 45 | | this.linear = linear; |
| 167 | 46 | | this.useGPUCopy = useGPUCopy; |
| 167 | 47 | | maxTextureSize = overrideMaxTextureSize ?? DataStore.i.textureConfig.generalMaxSize.Get(); |
| 167 | 48 | | idWithDefaultTexSettings = ConstructId(url, DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, maxTextureSize); |
| 167 | 49 | | idWithTexSettings = UsesDefaultWrapAndFilterMode() ? idWithDefaultTexSettings : ConstructId(url, wrapMode, f |
| 167 | 50 | | this.permittedSources = permittedSources; |
| 167 | 51 | | } |
| | 52 | |
|
| 121 | 53 | | protected override void OnAfterLoadOrReuse() { } |
| | 54 | |
|
| 141 | 55 | | protected override void OnBeforeLoadOrReuse() { } |
| | 56 | |
|
| | 57 | | protected override object GetLibraryAssetCheckId() |
| | 58 | | { |
| 141 | 59 | | return idWithTexSettings; |
| | 60 | | } |
| | 61 | |
|
| | 62 | | protected override void OnCancelLoading() |
| | 63 | | { |
| 20 | 64 | | cancellationTokenSource?.Cancel(); |
| 20 | 65 | | } |
| | 66 | |
|
| | 67 | | protected override void OnLoad(Action onSuccess, Action<Exception> onFail) |
| | 68 | | { |
| | 69 | | // Reuse the already-stored default texture, we duplicate it and set the needed config afterwards in AddToLi |
| 124 | 70 | | if (library.Contains(idWithDefaultTexSettings) && !UsesDefaultWrapAndFilterMode()) |
| | 71 | | { |
| 1 | 72 | | onSuccess?.Invoke(); |
| 1 | 73 | | return; |
| | 74 | | } |
| | 75 | |
|
| 123 | 76 | | cancellationTokenSource = new CancellationTokenSource(); |
| | 77 | |
|
| | 78 | | async UniTaskVoid LaunchRequest() |
| | 79 | | { |
| 359 | 80 | | MainScripts.DCL.Controllers.AssetManager.Texture.TextureResponse result = await textureResolver.Ref.GetT |
| | 81 | | maxTextureSize, linear, useGPUCopy, cancellationTokenSource.Token); |
| | 82 | |
|
| 119 | 83 | | if (result.IsSuccess) |
| | 84 | | { |
| 103 | 85 | | var successResponse = result.GetSuccessResponse(); |
| 103 | 86 | | asset.texture = successResponse.Texture; |
| 103 | 87 | | asset.resizingFactor = successResponse.ResizingFactor; |
| 103 | 88 | | onSuccess?.Invoke(); |
| | 89 | | } |
| | 90 | | else |
| | 91 | | { |
| 16 | 92 | | onFail?.Invoke(result.GetFailResponse().Exception); |
| | 93 | | } |
| 119 | 94 | | } |
| | 95 | |
|
| 123 | 96 | | LaunchRequest().Forget(); |
| 123 | 97 | | } |
| | 98 | |
|
| | 99 | | protected override bool AddToLibrary() |
| | 100 | | { |
| 104 | 101 | | if (storeDefaultTextureInAdvance && !UsesDefaultWrapAndFilterMode()) |
| | 102 | | { |
| 1 | 103 | | if (!library.Contains(idWithDefaultTexSettings)) |
| | 104 | | { |
| | 105 | | // Save default texture asset |
| 1 | 106 | | asset.id = idWithDefaultTexSettings; |
| 1 | 107 | | asset.ConfigureTexture(DEFAULT_WRAP_MODE, DEFAULT_FILTER_MODE, overrideCompression, false); |
| | 108 | |
|
| 1 | 109 | | if (!library.Add(asset)) |
| | 110 | | { |
| 0 | 111 | | Debug.Log("add to library fail?"); |
| 0 | 112 | | return false; |
| | 113 | | } |
| | 114 | | } |
| | 115 | |
|
| | 116 | | // By always using library.Get() for the default tex we have stored, we increase its references counter, |
| | 117 | | // that will come in handy for removing that default tex when there is no one using it |
| 1 | 118 | | var defaultTexAsset = library.Get(idWithDefaultTexSettings); |
| 1 | 119 | | asset = defaultTexAsset.Clone() as Asset_Texture; |
| 1 | 120 | | asset.dependencyAsset = defaultTexAsset; |
| 1 | 121 | | asset.texture = TextureHelpers.CopyTexture(defaultTexAsset.texture); |
| | 122 | | } |
| | 123 | |
|
| 104 | 124 | | asset.id = idWithTexSettings; |
| 104 | 125 | | asset.ConfigureTexture(wrapMode, filterMode, overrideCompression, storeTexAsNonReadable); |
| | 126 | |
|
| 104 | 127 | | if (!library.Add(asset)) |
| | 128 | | { |
| 0 | 129 | | Debug.Log("add to library fail?"); |
| 0 | 130 | | return false; |
| | 131 | | } |
| | 132 | |
|
| 104 | 133 | | PerformanceAnalytics.PromiseTextureTracker.Track(); |
| | 134 | |
|
| 104 | 135 | | asset = library.Get(asset.id); |
| 104 | 136 | | return true; |
| | 137 | | } |
| | 138 | |
|
| | 139 | | string ConstructId(string textureUrl, TextureWrapMode textureWrapMode, FilterMode textureFilterMode, int maxSize |
| | 140 | | { |
| 178 | 141 | | return $"{((int)textureWrapMode)}{((int)textureFilterMode)}{textureUrl}{maxSize}"; |
| | 142 | | } |
| | 143 | |
|
| | 144 | | public override object GetId() |
| | 145 | | { |
| | 146 | | // We only use the id-with-settings when storing/reading from the library |
| 683 | 147 | | return idWithDefaultTexSettings; |
| | 148 | | } |
| | 149 | |
|
| | 150 | | public bool UsesDefaultWrapAndFilterMode() |
| | 151 | | { |
| 212 | 152 | | return wrapMode == DEFAULT_WRAP_MODE && filterMode == DEFAULT_FILTER_MODE; |
| | 153 | | } |
| | 154 | | } |
| | 155 | | } |