| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Emotes; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.Providers; |
| | 5 | | using System; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using PromiseAsyncExtensions = DCL.Helpers.PromiseAsyncExtensions; |
| | 10 | |
|
| | 11 | | namespace DCLServices.EmotesCatalog.EmotesCatalogService |
| | 12 | | { |
| | 13 | | [Obsolete("This service will be deprecated by LambdasEmotesCatalogService in the future.")] |
| | 14 | | public class WebInterfaceEmotesCatalogService : IEmotesCatalogService |
| | 15 | | { |
| | 16 | | private EmotesCatalogBridge bridge; |
| | 17 | |
|
| 40 | 18 | | private readonly Dictionary<string, WearableItem> emotes = new (); |
| 40 | 19 | | private readonly Dictionary<string, HashSet<Promise<WearableItem>>> promises = new (); |
| 40 | 20 | | private readonly Dictionary<string, int> emotesOnUse = new (); |
| 40 | 21 | | private readonly Dictionary<string, HashSet<Promise<IReadOnlyList<WearableItem>>>> ownedEmotesPromisesByUser = n |
| | 22 | | private readonly IAddressableResourceProvider addressableResourceProvider; |
| | 23 | |
|
| | 24 | | private EmbeddedEmotesSO embeddedEmotesSO; |
| | 25 | | private CancellationTokenSource addressableCTS; |
| 40 | 26 | | private int retryCount = 3; |
| | 27 | |
|
| 40 | 28 | | public WebInterfaceEmotesCatalogService(EmotesCatalogBridge bridge, IAddressableResourceProvider addressableReso |
| | 29 | | { |
| 40 | 30 | | this.bridge = bridge; |
| 40 | 31 | | this.addressableResourceProvider = addressableResourceProvider; |
| 40 | 32 | | } |
| | 33 | |
|
| | 34 | | private async UniTaskVoid InitializeAsyncEmbeddedEmotes() |
| | 35 | | { |
| | 36 | | try |
| | 37 | | { |
| 0 | 38 | | addressableCTS = new CancellationTokenSource(); |
| 0 | 39 | | embeddedEmotesSO = await addressableResourceProvider.GetAddressable<EmbeddedEmotesSO>("EmbeddedEmotes.as |
| 0 | 40 | | } |
| 0 | 41 | | catch (OperationCanceledException) { return; } |
| 0 | 42 | | catch (Exception e) |
| | 43 | | { |
| 0 | 44 | | retryCount--; |
| 0 | 45 | | if (retryCount < 0) |
| | 46 | | { |
| 0 | 47 | | embeddedEmotesSO = ScriptableObject.CreateInstance<EmbeddedEmotesSO>(); |
| 0 | 48 | | embeddedEmotesSO.Clear(); |
| 0 | 49 | | throw new Exception("Embedded Emotes retry limit reached, they wont work correctly. Please check the |
| | 50 | | } |
| | 51 | |
|
| 0 | 52 | | Debug.LogWarning("Retrying embedded emotes addressables async request..."); |
| 0 | 53 | | DisposeCT(); |
| 0 | 54 | | InitializeAsyncEmbeddedEmotes().Forget(); |
| 0 | 55 | | } |
| | 56 | |
|
| 0 | 57 | | EmbedEmotes(); |
| 0 | 58 | | } |
| | 59 | |
|
| | 60 | | public void Initialize() |
| | 61 | | { |
| 0 | 62 | | InitializeAsyncEmbeddedEmotes().Forget(); |
| 0 | 63 | | bridge.OnEmotesReceived += OnEmotesReceived; |
| 0 | 64 | | bridge.OnEmoteRejected += OnEmoteRejected; |
| 0 | 65 | | bridge.OnOwnedEmotesReceived += OnOwnedEmotesReceived; |
| 0 | 66 | | } |
| | 67 | |
|
| | 68 | | private void OnEmotesReceived(WearableItem[] receivedEmotes) |
| | 69 | | { |
| 0 | 70 | | for (var i = 0; i < receivedEmotes.Length; i++) |
| | 71 | | { |
| 0 | 72 | | WearableItem emote = receivedEmotes[i]; |
| | 73 | |
|
| 0 | 74 | | if (!emotesOnUse.ContainsKey(emote.id) || emotesOnUse[emote.id] <= 0) |
| | 75 | | continue; |
| | 76 | |
|
| 0 | 77 | | emotes[emote.id] = emote; |
| | 78 | |
|
| 0 | 79 | | if (promises.TryGetValue(emote.id, out var emotePromises)) |
| | 80 | | { |
| 0 | 81 | | foreach (Promise<WearableItem> promise in emotePromises) |
| | 82 | | { |
| 0 | 83 | | promise.Resolve(emote); |
| | 84 | | } |
| | 85 | |
|
| 0 | 86 | | promises.Remove(emote.id); |
| | 87 | | } |
| | 88 | | } |
| 0 | 89 | | } |
| | 90 | |
|
| | 91 | | private void OnEmoteRejected(string emoteId, string errorMessage) |
| | 92 | | { |
| 0 | 93 | | if (promises.TryGetValue(emoteId, out var setOfPromises)) |
| | 94 | | { |
| 0 | 95 | | foreach(var promise in setOfPromises) |
| 0 | 96 | | promise.Reject(errorMessage); |
| | 97 | |
|
| 0 | 98 | | promises.Remove(emoteId); |
| 0 | 99 | | emotesOnUse.Remove(emoteId); |
| 0 | 100 | | emotes.Remove(emoteId); |
| | 101 | | } |
| 0 | 102 | | } |
| | 103 | |
|
| | 104 | | private void OnOwnedEmotesReceived(WearableItem[] receivedEmotes, string userId) |
| | 105 | | { |
| 0 | 106 | | if (!ownedEmotesPromisesByUser.TryGetValue(userId, out HashSet<Promise<IReadOnlyList<WearableItem>>> ownedEm |
| 0 | 107 | | ownedEmotesPromises = new HashSet<Promise<IReadOnlyList<WearableItem>>>(); |
| | 108 | |
|
| | 109 | | //Update emotes on use |
| 0 | 110 | | for (var i = 0; i < receivedEmotes.Length; i++) |
| | 111 | | { |
| 0 | 112 | | var emote = receivedEmotes[i]; |
| 0 | 113 | | if (!emotesOnUse.ContainsKey(emote.id)) |
| 0 | 114 | | emotesOnUse[emote.id] = 0; |
| 0 | 115 | | emotesOnUse[emote.id] += ownedEmotesPromises.Count; |
| | 116 | | } |
| | 117 | |
|
| 0 | 118 | | OnEmotesReceived(receivedEmotes); |
| | 119 | |
|
| | 120 | | //Resolve ownedEmotesPromise |
| 0 | 121 | | ownedEmotesPromisesByUser.Remove(userId); |
| 0 | 122 | | foreach (Promise<IReadOnlyList<WearableItem>> promise in ownedEmotesPromises) |
| | 123 | | { |
| 0 | 124 | | promise.Resolve(receivedEmotes); |
| | 125 | | } |
| 0 | 126 | | } |
| | 127 | |
|
| | 128 | | private void EmbedEmotes() |
| | 129 | | { |
| 0 | 130 | | foreach (EmbeddedEmote embeddedEmote in embeddedEmotesSO.GetAllEmotes()) |
| | 131 | | { |
| 0 | 132 | | emotes[embeddedEmote.id] = embeddedEmote; |
| 0 | 133 | | emotesOnUse[embeddedEmote.id] = 5000; |
| | 134 | | } |
| 0 | 135 | | } |
| | 136 | |
|
| 0 | 137 | | public bool TryGetLoadedEmote(string id, out WearableItem emote) { return emotes.TryGetValue(id, out emote); } |
| | 138 | |
|
| | 139 | | public UniTask<WearableItem> RequestEmoteFromBuilderAsync(string emoteId, CancellationToken cancellationToken) = |
| 0 | 140 | | throw new NotImplementedException("Implemented in LambdasEmotesCatalogService"); |
| | 141 | |
|
| | 142 | | public Promise<IReadOnlyList<WearableItem>> RequestOwnedEmotes(string userId) |
| | 143 | | { |
| 0 | 144 | | var promise = new Promise<IReadOnlyList<WearableItem>>(); |
| 0 | 145 | | if (!ownedEmotesPromisesByUser.ContainsKey(userId) || ownedEmotesPromisesByUser[userId] == null) |
| 0 | 146 | | ownedEmotesPromisesByUser[userId] = new HashSet<Promise<IReadOnlyList<WearableItem>>>(); |
| 0 | 147 | | ownedEmotesPromisesByUser[userId].Add(promise); |
| 0 | 148 | | bridge.RequestOwnedEmotes(userId); |
| | 149 | |
|
| 0 | 150 | | return promise; |
| | 151 | | } |
| | 152 | |
|
| | 153 | | public async UniTask<IReadOnlyList<WearableItem>> RequestOwnedEmotesAsync(string userId, CancellationToken ct = |
| | 154 | | { |
| | 155 | | const int TIMEOUT = 60; |
| 0 | 156 | | CancellationTokenSource timeoutCTS = new CancellationTokenSource(); |
| 0 | 157 | | var timeout = timeoutCTS.CancelAfterSlim(TimeSpan.FromSeconds(TIMEOUT)); |
| 0 | 158 | | var promise = RequestOwnedEmotes(userId); |
| | 159 | | try |
| | 160 | | { |
| 0 | 161 | | ct.ThrowIfCancellationRequested(); |
| 0 | 162 | | var linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCTS.Token); |
| 0 | 163 | | await PromiseAsyncExtensions.WithCancellation(promise, linkedCt.Token); |
| 0 | 164 | | } |
| 0 | 165 | | catch (OperationCanceledException e) |
| | 166 | | { |
| 0 | 167 | | return null; |
| | 168 | | } |
| | 169 | | finally |
| | 170 | | { |
| 0 | 171 | | timeout?.Dispose(); |
| 0 | 172 | | timeoutCTS?.Dispose(); |
| | 173 | | } |
| | 174 | |
|
| 0 | 175 | | return promise.value; |
| 0 | 176 | | } |
| | 177 | |
|
| | 178 | | public UniTask<IReadOnlyList<WearableItem>> RequestEmoteCollectionAsync(IEnumerable<string> collectionIds, |
| | 179 | | CancellationToken cancellationToken, List<WearableItem> emoteBuffer = null) => |
| 0 | 180 | | throw new NotImplementedException("Implemented in LambdasEmotesCatalogService"); |
| | 181 | |
|
| | 182 | | public Promise<WearableItem> RequestEmote(string id) |
| | 183 | | { |
| 0 | 184 | | var promise = new Promise<WearableItem>(); |
| 0 | 185 | | if (!emotesOnUse.ContainsKey(id)) |
| 0 | 186 | | emotesOnUse[id] = 0; |
| 0 | 187 | | emotesOnUse[id]++; |
| 0 | 188 | | if (emotes.TryGetValue(id, out var emote)) |
| | 189 | | { |
| 0 | 190 | | promise.Resolve(emote); |
| 0 | 191 | | return promise; |
| | 192 | | } |
| | 193 | |
|
| 0 | 194 | | if (!promises.ContainsKey(id) || promises[id] == null) |
| 0 | 195 | | promises[id] = new HashSet<Promise<WearableItem>>(); |
| 0 | 196 | | promises[id].Add(promise); |
| 0 | 197 | | bridge.RequestEmote(id); |
| 0 | 198 | | return promise; |
| | 199 | | } |
| | 200 | |
|
| | 201 | | public List<Promise<WearableItem>> RequestEmotes(IList<string> ids) |
| | 202 | | { |
| 0 | 203 | | List<Promise<WearableItem>> requestedPromises = new List<Promise<WearableItem>>(ids.Count); |
| | 204 | |
|
| 0 | 205 | | for (int i = 0; i < ids.Count; i++) |
| | 206 | | { |
| 0 | 207 | | string id = ids[i]; |
| 0 | 208 | | requestedPromises.Add(RequestEmote(id)); |
| | 209 | | } |
| | 210 | |
|
| 0 | 211 | | return requestedPromises; |
| | 212 | | } |
| | 213 | |
|
| | 214 | | public async UniTask<WearableItem> RequestEmoteAsync(string id, CancellationToken ct = default) |
| | 215 | | { |
| | 216 | | const int TIMEOUT = 45; |
| 0 | 217 | | CancellationTokenSource timeoutCTS = new CancellationTokenSource(); |
| 0 | 218 | | var timeout = timeoutCTS.CancelAfterSlim(TimeSpan.FromSeconds(TIMEOUT)); |
| 0 | 219 | | ct.ThrowIfCancellationRequested(); |
| 0 | 220 | | Promise<WearableItem> promise = RequestEmote(id); |
| | 221 | | try |
| | 222 | | { |
| 0 | 223 | | var linkedCt = CancellationTokenSource.CreateLinkedTokenSource(ct, timeoutCTS.Token); |
| 0 | 224 | | await PromiseAsyncExtensions.WithCancellation(promise, linkedCt.Token); |
| 0 | 225 | | } |
| 0 | 226 | | catch (PromiseException ex) |
| | 227 | | { |
| 0 | 228 | | Debug.LogWarning($"Emote with id:{id} was rejected"); |
| 0 | 229 | | return null; |
| | 230 | | } |
| 0 | 231 | | catch (OperationCanceledException ex) |
| | 232 | | { |
| 0 | 233 | | if (promises.ContainsKey(id)) |
| | 234 | | { |
| 0 | 235 | | promises[id].Remove(promise); |
| 0 | 236 | | if (promises[id].Count == 0) |
| 0 | 237 | | promises.Remove(id); |
| | 238 | | } |
| | 239 | |
|
| 0 | 240 | | return null; |
| | 241 | | } |
| | 242 | | finally |
| | 243 | | { |
| 0 | 244 | | timeout?.Dispose(); |
| 0 | 245 | | timeoutCTS?.Dispose(); |
| | 246 | | } |
| | 247 | |
|
| 0 | 248 | | return promise.value; |
| 0 | 249 | | } |
| | 250 | |
|
| | 251 | | public async UniTask<IReadOnlyList<WearableItem>> RequestEmotesAsync(IList<string> ids, CancellationToken ct = d |
| | 252 | | { |
| 0 | 253 | | ct.ThrowIfCancellationRequested(); |
| | 254 | | try |
| | 255 | | { |
| 0 | 256 | | var tasks = ids.Select(x => RequestEmoteAsync(x, ct)); |
| 0 | 257 | | return await UniTask.WhenAll(tasks).AttachExternalCancellation(ct); |
| | 258 | | } |
| 0 | 259 | | catch (OperationCanceledException e) |
| | 260 | | { |
| 0 | 261 | | return null; |
| | 262 | | } |
| 0 | 263 | | } |
| | 264 | |
|
| | 265 | | public void ForgetEmote(string id) |
| | 266 | | { |
| 0 | 267 | | if (emotesOnUse.ContainsKey(id)) |
| | 268 | | { |
| 0 | 269 | | emotesOnUse[id]--; |
| 0 | 270 | | if (emotesOnUse[id] > 0) //We are still using this emote |
| 0 | 271 | | return; |
| 0 | 272 | | emotesOnUse.Remove(id); |
| | 273 | | } |
| | 274 | |
|
| 0 | 275 | | if (!emotes.TryGetValue(id, out WearableItem emote)) |
| 0 | 276 | | return; |
| | 277 | |
|
| 0 | 278 | | emotes.Remove(id); |
| 0 | 279 | | } |
| | 280 | |
|
| | 281 | | public void ForgetEmotes(IList<string> ids) |
| | 282 | | { |
| 0 | 283 | | for (int i = 0; i < ids.Count; i++) |
| | 284 | | { |
| 0 | 285 | | string id = ids[i]; |
| 0 | 286 | | ForgetEmote(id); |
| | 287 | | } |
| 0 | 288 | | } |
| | 289 | |
|
| | 290 | | public bool TryGetOwnedUrn(string shortenedUrn, out string extendedUrn) |
| | 291 | | { |
| 0 | 292 | | extendedUrn = ""; |
| 0 | 293 | | return false; |
| | 294 | | } |
| | 295 | |
|
| | 296 | | public async UniTask<EmbeddedEmotesSO> GetEmbeddedEmotes() |
| | 297 | | { |
| 0 | 298 | | if(embeddedEmotesSO == null) |
| 0 | 299 | | await UniTask.WaitUntil(() => embeddedEmotesSO != null); |
| 0 | 300 | | return embeddedEmotesSO; |
| 0 | 301 | | } |
| | 302 | |
|
| | 303 | | public UniTask<IReadOnlyList<WearableItem>> RequestEmoteCollectionInBuilderAsync(IEnumerable<string> collectionI |
| | 304 | | CancellationToken cancellationToken, List<WearableItem> emoteBuffer = null) => |
| 0 | 305 | | throw new NotImplementedException("Implemented in LambdasEmotesCatalogService"); |
| | 306 | |
|
| | 307 | | public void Dispose() |
| | 308 | | { |
| 0 | 309 | | bridge.OnEmotesReceived -= OnEmotesReceived; |
| 0 | 310 | | bridge.OnEmoteRejected -= OnEmoteRejected; |
| 0 | 311 | | bridge.OnOwnedEmotesReceived -= OnOwnedEmotesReceived; |
| 0 | 312 | | DisposeCT(); |
| 0 | 313 | | } |
| | 314 | |
|
| | 315 | | private void DisposeCT() |
| | 316 | | { |
| 0 | 317 | | if (addressableCTS != null) |
| | 318 | | { |
| 0 | 319 | | addressableCTS.Cancel(); |
| 0 | 320 | | addressableCTS.Dispose(); |
| 0 | 321 | | addressableCTS = null; |
| | 322 | | } |
| 0 | 323 | | } |
| | 324 | |
|
| | 325 | | } |
| | 326 | | } |