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