| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | | using DCL; |
| | 5 | | using Object = UnityEngine.Object; |
| | 6 | |
|
| | 7 | | //In the future the AssetManager will do this |
| | 8 | | public static class ThumbnailsManager |
| | 9 | | { |
| | 10 | | #if UNITY_EDITOR |
| 1 | 11 | | public static bool bypassRequests = false; |
| | 12 | | #endif |
| 1 | 13 | | private static readonly Queue<EnqueuedThumbnail> promiseQueue = new Queue<EnqueuedThumbnail>(); |
| 1 | 14 | | private static readonly List<AssetPromise_Texture> progressList = new List<AssetPromise_Texture>(); |
| 1 | 15 | | private static readonly Dictionary<Texture2D, Sprite> spriteCache = new Dictionary<Texture2D, Sprite>(); |
| 1 | 16 | | private static readonly Dictionary<string, AssetPromise_Texture> promiseCache = new Dictionary<string, AssetPromise_ |
| | 17 | | private const int CONCURRENT_LIMIT = 10; |
| | 18 | |
|
| | 19 | | public static void Clear() |
| | 20 | | { |
| 1280 | 21 | | foreach (EnqueuedThumbnail thumbnail in promiseQueue) |
| | 22 | | { |
| 0 | 23 | | AssetPromiseKeeper_Texture.i.Forget(thumbnail.Promise); |
| | 24 | | } |
| 640 | 25 | | promiseQueue.Clear(); |
| | 26 | |
|
| 1280 | 27 | | foreach (AssetPromise_Texture promiseTexture in progressList) |
| | 28 | | { |
| 0 | 29 | | AssetPromiseKeeper_Texture.i.Forget(promiseTexture); |
| | 30 | | } |
| 640 | 31 | | progressList.Clear(); |
| | 32 | |
|
| 1374 | 33 | | foreach (KeyValuePair<string,AssetPromise_Texture> assetPromiseTexture in promiseCache) |
| | 34 | | { |
| 47 | 35 | | AssetPromiseKeeper_Texture.i.Forget(assetPromiseTexture.Value); |
| | 36 | | } |
| 640 | 37 | | promiseCache.Clear(); |
| | 38 | |
|
| 1280 | 39 | | foreach (KeyValuePair<Texture2D,Sprite> sprite in spriteCache) |
| | 40 | | { |
| 0 | 41 | | Object.DestroyImmediate(sprite.Value); |
| | 42 | | } |
| 640 | 43 | | spriteCache.Clear(); |
| 640 | 44 | | } |
| | 45 | |
|
| | 46 | | public static AssetPromise_Texture PreloadThumbnail(string url) |
| | 47 | | { |
| | 48 | | #if UNITY_EDITOR |
| 4 | 49 | | if (bypassRequests) |
| 4 | 50 | | return null; |
| | 51 | | #endif |
| 0 | 52 | | if (string.IsNullOrEmpty(url)) |
| 0 | 53 | | return null; |
| | 54 | |
|
| 0 | 55 | | var promise = new AssetPromise_Texture(url); |
| 0 | 56 | | AssetPromiseKeeper_Texture.i.Keep(promise); |
| | 57 | |
|
| 0 | 58 | | return promise; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public static void ForgetThumbnail(AssetPromise_Texture promise) |
| | 62 | | { |
| 0 | 63 | | if (promise == null) |
| 0 | 64 | | return; |
| | 65 | |
|
| | 66 | | //Debug.Log("Forget thumbnail " + promise.asset.id); |
| 0 | 67 | | AssetPromiseKeeper_Texture.i.Forget(promise); |
| 0 | 68 | | } |
| | 69 | |
|
| | 70 | | public static void GetThumbnail(string url, Action<Asset_Texture> OnComplete) |
| | 71 | | { |
| | 72 | | #if UNITY_EDITOR |
| 140 | 73 | | if (bypassRequests) |
| 8 | 74 | | return; |
| | 75 | | #endif |
| 132 | 76 | | if (string.IsNullOrEmpty(url)) |
| 0 | 77 | | return; |
| | 78 | |
|
| 132 | 79 | | if (promiseCache.ContainsKey(url)) |
| | 80 | | { |
| 85 | 81 | | var promise = promiseCache[url]; |
| | 82 | |
|
| 85 | 83 | | if (promise.state == AssetPromiseState.FINISHED) |
| | 84 | | { |
| 0 | 85 | | OnComplete(promise.asset); |
| 0 | 86 | | } |
| | 87 | | else |
| | 88 | | { |
| 85 | 89 | | promise.OnSuccessEvent += OnComplete; |
| | 90 | | } |
| | 91 | |
|
| 85 | 92 | | return; |
| | 93 | | } |
| | 94 | |
|
| 47 | 95 | | var newPromise = new AssetPromise_Texture(url); |
| 47 | 96 | | promiseCache.Add(url, newPromise); |
| | 97 | |
|
| 47 | 98 | | AddToQueue(new EnqueuedThumbnail(newPromise, OnComplete)); |
| 47 | 99 | | CheckQueue(); |
| 47 | 100 | | } |
| | 101 | | private static void CheckQueue() |
| | 102 | | { |
| 94 | 103 | | var availableSlots = Mathf.Max(CONCURRENT_LIMIT - progressList.Count, 0); |
| | 104 | |
|
| 188 | 105 | | for (int i = progressList.Count - 1; i >= 0 ; i--) |
| | 106 | | { |
| 0 | 107 | | if (progressList[i].state == AssetPromiseState.IDLE_AND_EMPTY) |
| | 108 | | { |
| 0 | 109 | | progressList.RemoveAt(i); |
| | 110 | | } |
| | 111 | | } |
| | 112 | |
|
| 94 | 113 | | if (availableSlots > 0) |
| | 114 | | { |
| 94 | 115 | | var availableDownloads = Mathf.Min(availableSlots, promiseQueue.Count); |
| | 116 | |
|
| 94 | 117 | | if (availableDownloads > 0) |
| | 118 | | { |
| 188 | 119 | | for (int i = 0; i < availableDownloads; i++) |
| | 120 | | { |
| 47 | 121 | | if (promiseQueue.Count == 0) |
| | 122 | | break; |
| | 123 | |
|
| 47 | 124 | | var promise = promiseQueue.Dequeue(); |
| 47 | 125 | | AssetPromise_Texture assetPromiseTexture = promise.Promise; |
| 47 | 126 | | BeginDownload(assetPromiseTexture, promise.OnComplete); |
| | 127 | | } |
| | 128 | | } |
| | 129 | | } |
| 94 | 130 | | } |
| | 131 | | private static void AddToQueue(EnqueuedThumbnail enqueuedThumbnail) |
| | 132 | | { |
| 47 | 133 | | promiseQueue.Enqueue(enqueuedThumbnail); |
| 47 | 134 | | } |
| | 135 | | private static void BeginDownload(AssetPromise_Texture promise, Action<Asset_Texture> OnComplete) |
| | 136 | | { |
| 47 | 137 | | progressList.Add(promise); |
| | 138 | |
|
| 47 | 139 | | promise.OnSuccessEvent += t => |
| | 140 | | { |
| 0 | 141 | | progressList.Remove(promise); |
| 0 | 142 | | OnComplete(t); |
| 0 | 143 | | CheckQueue(); |
| 0 | 144 | | }; |
| | 145 | |
|
| 47 | 146 | | promise.OnFailEvent += (x, error) => |
| | 147 | | { |
| 47 | 148 | | progressList.Remove(promise); |
| 47 | 149 | | Debug.Log($"Error downloading: {promise.url}, Exception: {error}"); |
| 47 | 150 | | CheckQueue(); |
| 47 | 151 | | }; |
| | 152 | |
|
| 47 | 153 | | AssetPromiseKeeper_Texture.i.Keep(promise); |
| 47 | 154 | | } |
| | 155 | |
|
| | 156 | | public static Sprite GetOrCreateSpriteFromTexture(Texture2D texture, out bool wasCreated) |
| | 157 | | { |
| 0 | 158 | | wasCreated = false; |
| 0 | 159 | | if (!spriteCache.ContainsKey(texture)) |
| | 160 | | { |
| 0 | 161 | | spriteCache[texture] = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); |
| 0 | 162 | | wasCreated = true; |
| | 163 | | } |
| 0 | 164 | | return spriteCache[texture]; |
| | 165 | | } |
| | 166 | | } |
| | 167 | |
|
| | 168 | | public struct EnqueuedThumbnail |
| | 169 | | { |
| | 170 | | public readonly AssetPromise_Texture Promise; |
| | 171 | | public readonly Action<Asset_Texture> OnComplete; |
| | 172 | | public EnqueuedThumbnail(AssetPromise_Texture promise, Action<Asset_Texture> complete) |
| | 173 | | { |
| | 174 | | this.Promise = promise; |
| | 175 | | OnComplete = complete; |
| | 176 | | } |
| | 177 | | } |