| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Threading; |
| | 5 | | using Cysharp.Threading.Tasks; |
| | 6 | | using DCL.Helpers; |
| | 7 | |
|
| | 8 | | namespace AvatarSystem |
| | 9 | | { |
| | 10 | | public class WearableItemResolver : IWearableItemResolver |
| | 11 | | { |
| 1305 | 12 | | private CancellationTokenSource disposeCts = new CancellationTokenSource(); |
| 1305 | 13 | | private readonly Dictionary<string, WearableItem> wearablesRetrieved = new Dictionary<string, WearableItem>(); |
| | 14 | |
|
| | 15 | | public async UniTask<(List<WearableItem> wearables, List<WearableItem> emotes)> ResolveAndSplit(IEnumerable<stri |
| | 16 | | { |
| | 17 | | try |
| | 18 | | { |
| 79 | 19 | | WearableItem[] allItems = await Resolve(wearableIds, ct); |
| | 20 | |
|
| 76 | 21 | | List<WearableItem> wearables = new List<WearableItem>(); |
| 76 | 22 | | List<WearableItem> emotes = new List<WearableItem>(); |
| | 23 | |
|
| 1276 | 24 | | for (int i = 0; i < allItems.Length; i++) |
| | 25 | | { |
| 562 | 26 | | if (allItems[i].IsEmote()) |
| 0 | 27 | | emotes.Add(allItems[i]); |
| | 28 | | else |
| 562 | 29 | | wearables.Add(allItems[i]); |
| | 30 | | } |
| | 31 | |
|
| 76 | 32 | | return ( |
| | 33 | | wearables, |
| | 34 | | emotes |
| | 35 | | ); |
| | 36 | | } |
| 1 | 37 | | catch (OperationCanceledException) |
| | 38 | | { |
| | 39 | | //No disposing required |
| 1 | 40 | | throw; |
| | 41 | | } |
| 76 | 42 | | } |
| | 43 | |
|
| | 44 | | public async UniTask<WearableItem[]> Resolve(IEnumerable<string> wearableId, CancellationToken ct = default) |
| | 45 | | { |
| 78 | 46 | | ct.ThrowIfCancellationRequested(); |
| | 47 | |
|
| | 48 | | try |
| | 49 | | { |
| | 50 | |
|
| 690 | 51 | | return await UniTask.WhenAll(wearableId.Select(x => Resolve(x, ct))); |
| | 52 | | } |
| 1 | 53 | | catch (OperationCanceledException) |
| | 54 | | { |
| | 55 | | //No disposing required |
| 1 | 56 | | throw; |
| | 57 | | } |
| 76 | 58 | | } |
| | 59 | |
|
| | 60 | | public async UniTask<WearableItem> Resolve(string wearableId, CancellationToken ct = default) |
| | 61 | | { |
| 609 | 62 | | if (disposeCts == null) |
| 526 | 63 | | disposeCts = new CancellationTokenSource(); |
| 609 | 64 | | using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Tok |
| | 65 | |
|
| 609 | 66 | | linkedCts.Token.ThrowIfCancellationRequested(); |
| | 67 | |
|
| | 68 | | try |
| | 69 | | { |
| 609 | 70 | | if (wearablesRetrieved.ContainsKey(wearableId)) |
| 0 | 71 | | return wearablesRetrieved[wearableId]; |
| | 72 | |
|
| 609 | 73 | | Promise<WearableItem> promise = CatalogController.RequestWearable(wearableId); |
| | 74 | | // AttachExternalCancellation is needed because a CustomYieldInstruction requires a frame to operate |
| 670 | 75 | | await promise.WithCancellation(linkedCts.Token).AttachExternalCancellation(linkedCts.Token); |
| | 76 | |
|
| | 77 | | // Cancelling is irrelevant at this point, |
| | 78 | | // either we have the wearable and we have to add it to forget it later |
| | 79 | | // or it's null and we just return it |
| 571 | 80 | | if (promise.value != null) |
| 571 | 81 | | wearablesRetrieved.Add(wearableId, promise.value); |
| | 82 | |
|
| 571 | 83 | | return promise.value; |
| | 84 | |
|
| | 85 | | } |
| 23 | 86 | | catch (OperationCanceledException) |
| | 87 | | { |
| | 88 | | //No disposing required |
| 23 | 89 | | throw; |
| | 90 | | } |
| | 91 | | finally |
| | 92 | | { |
| 594 | 93 | | disposeCts?.Dispose(); |
| 594 | 94 | | disposeCts = null; |
| | 95 | | } |
| 571 | 96 | | } |
| | 97 | |
|
| | 98 | | public void Forget(List<string> wearableIds) |
| | 99 | | { |
| 3952 | 100 | | foreach (string wearableId in wearableIds) |
| | 101 | | { |
| 571 | 102 | | wearablesRetrieved.Remove(wearableId); |
| | 103 | | } |
| 1405 | 104 | | CatalogController.RemoveWearablesInUse(wearableIds); |
| 1405 | 105 | | } |
| | 106 | |
|
| 0 | 107 | | public void Forget(string wearableId) { CatalogController.RemoveWearablesInUse(new List<string> { wearableId }); |
| | 108 | |
|
| | 109 | | public void Dispose() |
| | 110 | | { |
| 1405 | 111 | | disposeCts?.Cancel(); |
| 1405 | 112 | | disposeCts?.Dispose(); |
| 1405 | 113 | | disposeCts = null; |
| 1405 | 114 | | Forget(wearablesRetrieved.Keys.ToList()); |
| 1405 | 115 | | wearablesRetrieved.Clear(); |
| 1405 | 116 | | } |
| | 117 | | } |
| | 118 | | } |