| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using UnityEngine.Assertions; |
| | 5 | |
|
| | 6 | | namespace DCL.Helpers |
| | 7 | | { |
| | 8 | | public interface ILazyTextureObserver |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Adds a listener for this LazyTextureObserver. |
| | 12 | | /// |
| | 13 | | /// The listener will be called when anyone reloads the texture by calling any of the refresh methods. |
| | 14 | | /// Also, calling this method will ensure the texture starts loading lazily if it was not loaded yet. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="listener"></param> |
| | 17 | | public void AddListener(Action<Texture2D> listener); |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Remove the given listener. If no listeners want this texture anymore, it will be unloaded. |
| | 21 | | /// |
| | 22 | | /// Note that as this method uses the AssetPromiseKeeper_Texture service, |
| | 23 | | /// if any other systems use the same texture it will not be unloaded. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="listener"></param> |
| | 26 | | public void RemoveListener(Action<Texture2D> listener); |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Set the texture to be loaded from the given URI when any listener is subscribed. |
| | 30 | | /// If we already have listeners, the loading will start immediately. |
| | 31 | | /// |
| | 32 | | /// The uri method has precedence over the texture method. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="uri"></param> |
| | 35 | | void RefreshWithUri(string uri); |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Sets the listened texture as the passed texture object. |
| | 39 | | /// |
| | 40 | | /// If we already have listeners, they will be called immediately with the new texture. |
| | 41 | | /// New listeners will receive the new texture. |
| | 42 | | /// |
| | 43 | | /// The uri method has precedence over the texture method. |
| | 44 | | /// </summary> |
| | 45 | | /// <param name="texture"></param> |
| | 46 | | void RefreshWithTexture(Texture2D texture); |
| | 47 | | } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// The LazyTextureObserver class is a reactive texture loader wrapper. |
| | 51 | | /// <br/> <br/> |
| | 52 | | /// This class is useful for cases where a texture needs to be used, but it has to be lazy loaded, |
| | 53 | | /// and can be changed by an external party. |
| | 54 | | /// <br/> <br/> |
| | 55 | | /// <ul> |
| | 56 | | /// <li>Calling <b>RefreshWithUri</b> and <b>RefreshWithTexture</b> will only load the texture if someone is registe |
| | 57 | | /// <li>If <b>AddListener</b> is called and the texture is not loaded, it will start being loaded.</li><br/> |
| | 58 | | /// <li>If <b>RemoveListener</b> is called and no more listeners need this image, the image will be unloaded.</li><b |
| | 59 | | /// </ul> |
| | 60 | | /// All existing listeners will be keep informed of <b>RefreshWithX</b> calls to update the texture if it changes. |
| | 61 | | /// </summary> |
| | 62 | | public class LazyTextureObserver : ILazyTextureObserver |
| | 63 | | { |
| | 64 | | enum State |
| | 65 | | { |
| | 66 | | NONE, |
| | 67 | | IN_PROGRESS, |
| | 68 | | LOADED |
| | 69 | | } |
| | 70 | |
|
| | 71 | | private State state = State.NONE; |
| | 72 | | private Action<Texture2D> OnLoaded; |
| | 73 | | private Action<Exception> OnFail; |
| | 74 | |
|
| 1436 | 75 | | private HashSet<Action<Texture2D>> subscriptions = new HashSet<Action<Texture2D>>(); |
| | 76 | |
|
| | 77 | | private Texture2D texture; |
| | 78 | | private string uri; |
| | 79 | | private ITextureLoader textureLoader; |
| | 80 | |
|
| 2852 | 81 | | public LazyTextureObserver () : this(new TextureLoader()) { } |
| | 82 | |
|
| 1436 | 83 | | internal LazyTextureObserver (ITextureLoader textureLoader) |
| | 84 | | { |
| 1436 | 85 | | this.textureLoader = textureLoader; |
| | 86 | |
|
| 1436 | 87 | | textureLoader.OnSuccess += x => |
| | 88 | | { |
| 7 | 89 | | state = State.LOADED; |
| 7 | 90 | | OnLoaded?.Invoke(x); |
| 7 | 91 | | }; |
| | 92 | |
|
| 1436 | 93 | | textureLoader.OnFail += error => |
| | 94 | | { |
| 1 | 95 | | state = State.NONE; |
| 1 | 96 | | uri = null; |
| 1 | 97 | | OnFail?.Invoke(error); |
| 1 | 98 | | }; |
| 1436 | 99 | | } |
| | 100 | |
|
| | 101 | | public void AddListener(Action<Texture2D> listener) |
| | 102 | | { |
| 244 | 103 | | Assert.IsNotNull(listener, "Listener can't be null!"); |
| | 104 | |
|
| 243 | 105 | | if (!subscriptions.Contains(listener)) |
| | 106 | | { |
| 233 | 107 | | subscriptions.Add(listener); |
| 233 | 108 | | this.OnLoaded += listener; |
| 234 | 109 | | this.OnFail += e => listener.Invoke(null); |
| | 110 | | } |
| | 111 | |
|
| | 112 | | // First, check if we did set a texture directly and return it if so. |
| 243 | 113 | | if ( texture != null ) |
| | 114 | | { |
| 4 | 115 | | listener.Invoke(texture); |
| 4 | 116 | | return; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | // If not, we try to load the texture if not already loaded. |
| 239 | 120 | | if ( state == State.NONE ) |
| | 121 | | { |
| 231 | 122 | | TryToLoadAndDispatch(); |
| 231 | 123 | | return; |
| | 124 | | } |
| | 125 | |
|
| | 126 | | // From now on we assume that the promise exists. |
| | 127 | | // -- |
| | 128 | | // It must be in progress or available by now. If its available we just return it for this listener only. |
| | 129 | | // If not, the texture promise flow will call all the listeners when it finishes. |
| 8 | 130 | | if ( state == State.LOADED ) |
| | 131 | | { |
| 4 | 132 | | listener.Invoke(textureLoader.GetTexture()); |
| | 133 | | } |
| 8 | 134 | | } |
| | 135 | |
|
| | 136 | | public void RemoveListener(Action<Texture2D> listener) |
| | 137 | | { |
| 953 | 138 | | Assert.IsNotNull(listener, "Listener can't be null!"); |
| | 139 | |
|
| | 140 | | // Not using assert here because this case is more probable, I want to fail silently in this case. |
| 952 | 141 | | if (!subscriptions.Contains(listener)) |
| 739 | 142 | | return; |
| | 143 | |
|
| 213 | 144 | | this.OnLoaded -= listener; |
| 213 | 145 | | subscriptions.Remove(listener); |
| | 146 | |
|
| 213 | 147 | | if ( subscriptions.Count == 0 ) |
| | 148 | | { |
| 207 | 149 | | textureLoader.Unload(); |
| 207 | 150 | | state = State.NONE; |
| | 151 | | } |
| 213 | 152 | | } |
| | 153 | |
|
| | 154 | | public void RefreshWithUri(string uri) |
| | 155 | | { |
| 17 | 156 | | if (string.IsNullOrEmpty(uri)) |
| 3 | 157 | | return; |
| | 158 | |
|
| 14 | 159 | | this.uri = uri; |
| 14 | 160 | | this.texture = null; |
| | 161 | |
|
| 14 | 162 | | if ( subscriptions.Count > 0 ) |
| 4 | 163 | | TryToLoadAndDispatch(); |
| 14 | 164 | | } |
| | 165 | |
|
| | 166 | | public void RefreshWithTexture(Texture2D texture) |
| | 167 | | { |
| 8 | 168 | | if (texture == null || texture == this.texture) |
| 1 | 169 | | return; |
| | 170 | |
|
| 7 | 171 | | textureLoader.Unload(); |
| 7 | 172 | | state = State.NONE; |
| | 173 | |
|
| 7 | 174 | | this.uri = null; |
| 7 | 175 | | this.texture = texture; |
| | 176 | |
|
| 7 | 177 | | if ( subscriptions.Count > 0 ) |
| 2 | 178 | | TryToLoadAndDispatch(); |
| 7 | 179 | | } |
| | 180 | |
|
| | 181 | | private void TryToLoadAndDispatch() |
| | 182 | | { |
| 237 | 183 | | if (DispatchTextureByInjectedReference()) |
| 2 | 184 | | return; |
| | 185 | |
|
| 235 | 186 | | DispatchLoadedTexture(); |
| 235 | 187 | | } |
| | 188 | |
|
| | 189 | | private bool DispatchTextureByInjectedReference() |
| | 190 | | { |
| 237 | 191 | | if (texture == null) |
| 235 | 192 | | return false; |
| | 193 | |
|
| 2 | 194 | | state = State.LOADED; |
| 2 | 195 | | OnLoaded?.Invoke(texture); |
| 2 | 196 | | return true; |
| | 197 | | } |
| | 198 | |
|
| | 199 | | private bool DispatchLoadedTexture() |
| | 200 | | { |
| 235 | 201 | | if (string.IsNullOrEmpty(uri)) |
| 225 | 202 | | return false; |
| | 203 | |
|
| 10 | 204 | | state = State.IN_PROGRESS; |
| 10 | 205 | | textureLoader.Load(uri); |
| 10 | 206 | | return true; |
| | 207 | | } |
| | 208 | |
|
| | 209 | | public void Dispose() |
| | 210 | | { |
| 12 | 211 | | state = State.NONE; |
| 12 | 212 | | textureLoader.Unload(); |
| 12 | 213 | | subscriptions.Clear(); |
| 12 | 214 | | OnLoaded = null; |
| 12 | 215 | | } |
| | 216 | |
|
| | 217 | | public static LazyTextureObserver CreateWithUri(string uri) |
| | 218 | | { |
| 0 | 219 | | var result = new LazyTextureObserver(); |
| 0 | 220 | | result.RefreshWithUri(uri); |
| 0 | 221 | | return result; |
| | 222 | | } |
| | 223 | |
|
| | 224 | | public static LazyTextureObserver CreateWithTexture(Texture2D texture) |
| | 225 | | { |
| 2 | 226 | | var result = new LazyTextureObserver(); |
| 2 | 227 | | result.RefreshWithTexture(texture); |
| 2 | 228 | | return result; |
| | 229 | | } |
| | 230 | | } |
| | 231 | | } |