| | 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 | | { |
| 1043 | 12 | | private CancellationTokenSource disposeCts = new CancellationTokenSource(); |
| 1043 | 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 | | { |
| 0 | 19 | | WearableItem[] allItems = await Resolve(wearableIds, ct); |
| | 20 | |
|
| 0 | 21 | | List<WearableItem> wearables = new List<WearableItem>(); |
| 0 | 22 | | List<WearableItem> emotes = new List<WearableItem>(); |
| | 23 | |
|
| 0 | 24 | | for (int i = 0; i < allItems.Length; i++) |
| | 25 | | { |
| 0 | 26 | | if (allItems[i] == null) |
| | 27 | | continue; |
| | 28 | |
|
| 0 | 29 | | if (allItems[i].IsEmote()) |
| 0 | 30 | | emotes.Add(allItems[i]); |
| | 31 | | else |
| 0 | 32 | | wearables.Add(allItems[i]); |
| | 33 | | } |
| | 34 | |
|
| 0 | 35 | | return ( |
| | 36 | | wearables, |
| | 37 | | emotes |
| | 38 | | ); |
| | 39 | | } |
| 0 | 40 | | catch (OperationCanceledException) |
| | 41 | | { |
| | 42 | | //No disposing required |
| 0 | 43 | | throw; |
| | 44 | | } |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public async UniTask<WearableItem[]> Resolve(IEnumerable<string> wearableId, CancellationToken ct = default) |
| | 48 | | { |
| 1 | 49 | | ct.ThrowIfCancellationRequested(); |
| | 50 | |
|
| | 51 | | try |
| | 52 | | { |
| | 53 | |
|
| 17 | 54 | | return await UniTask.WhenAll(wearableId.Select(x => Resolve(x, ct))); |
| | 55 | | } |
| 0 | 56 | | catch (OperationCanceledException) |
| | 57 | | { |
| | 58 | | //No disposing required |
| 0 | 59 | | throw; |
| | 60 | | } |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public async UniTask<WearableItem> Resolve(string wearableId, CancellationToken ct = default) |
| | 64 | | { |
| 15 | 65 | | if (disposeCts == null) |
| 0 | 66 | | disposeCts = new CancellationTokenSource(); |
| 15 | 67 | | using CancellationTokenSource linkedCts = CancellationTokenSource.CreateLinkedTokenSource(ct, disposeCts.Tok |
| | 68 | |
|
| 15 | 69 | | linkedCts.Token.ThrowIfCancellationRequested(); |
| | 70 | |
|
| | 71 | | try |
| | 72 | | { |
| 15 | 73 | | if (wearablesRetrieved.ContainsKey(wearableId)) |
| 0 | 74 | | return wearablesRetrieved[wearableId]; |
| | 75 | |
|
| 15 | 76 | | Promise<WearableItem> promise = CatalogController.RequestWearable(wearableId); |
| | 77 | | // AttachExternalCancellation is needed because a CustomYieldInstruction requires a frame to operate |
| 30 | 78 | | await promise.WithCancellation(linkedCts.Token).AttachExternalCancellation(linkedCts.Token); |
| | 79 | |
|
| | 80 | | // Cancelling is irrelevant at this point, |
| | 81 | | // either we have the wearable and we have to add it to forget it later |
| | 82 | | // or it's null and we just return it |
| 0 | 83 | | if (promise.value != null) |
| 0 | 84 | | wearablesRetrieved.Add(wearableId, promise.value); |
| | 85 | |
|
| 0 | 86 | | return promise.value; |
| | 87 | |
|
| | 88 | | } |
| 0 | 89 | | catch (OperationCanceledException ex) |
| | 90 | | { |
| 0 | 91 | | wearablesRetrieved.Remove(wearableId); |
| | 92 | |
|
| 0 | 93 | | UnityEngine.Debug.LogError($"Resolve wearable OperationCanceledException: {ex.Message} {ex.StackTrace}") |
| 0 | 94 | | return null; |
| | 95 | | } |
| | 96 | | finally |
| | 97 | | { |
| 0 | 98 | | disposeCts?.Dispose(); |
| 0 | 99 | | disposeCts = null; |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | public void Forget(List<string> wearableIds) |
| | 104 | | { |
| 2108 | 105 | | foreach (string wearableId in wearableIds) |
| | 106 | | { |
| 0 | 107 | | wearablesRetrieved.Remove(wearableId); |
| | 108 | | } |
| 1054 | 109 | | CatalogController.RemoveWearablesInUse(wearableIds); |
| 1054 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | public void Forget(string wearableId) { CatalogController.RemoveWearablesInUse(new List<string> { wearableId }); |
| | 113 | |
|
| | 114 | | public void Dispose() |
| | 115 | | { |
| 1054 | 116 | | disposeCts?.Cancel(); |
| 1054 | 117 | | disposeCts?.Dispose(); |
| 1054 | 118 | | disposeCts = null; |
| 1054 | 119 | | Forget(wearablesRetrieved.Keys.ToList()); |
| 1054 | 120 | | wearablesRetrieved.Clear(); |
| 1054 | 121 | | } |
| | 122 | | } |
| | 123 | | } |