| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using System.Runtime.ExceptionServices; |
| | 5 | | using System.Threading; |
| | 6 | | using Cysharp.Threading.Tasks; |
| | 7 | | using DCL; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Assertions; |
| | 10 | |
|
| | 11 | | namespace AvatarSystem |
| | 12 | | { |
| | 13 | | public class AvatarCurator : IAvatarCurator |
| | 14 | | { |
| | 15 | | private readonly IWearableItemResolver wearableItemResolver; |
| | 16 | | private readonly IEmotesCatalogService emotesCatalog; |
| | 17 | |
|
| 1039 | 18 | | public AvatarCurator(IWearableItemResolver wearableItemResolver, IEmotesCatalogService emotesCatalog) |
| | 19 | | { |
| 1039 | 20 | | Assert.IsNotNull(wearableItemResolver); |
| 1039 | 21 | | this.wearableItemResolver = wearableItemResolver; |
| 1039 | 22 | | this.emotesCatalog = emotesCatalog; |
| 1039 | 23 | | } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Curate a flattened into IDs set of wearables. |
| | 27 | | /// Bear in mind that the bodyshape must be part of the list of wearables |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="settings"></param> |
| | 30 | | /// <param name="wearablesId"></param> |
| | 31 | | /// <param name="ct"></param> |
| | 32 | | /// <returns></returns> |
| | 33 | | /// <exception cref="Exception"></exception> |
| | 34 | | public async UniTask<( |
| | 35 | | WearableItem bodyshape, |
| | 36 | | WearableItem eyes, |
| | 37 | | WearableItem eyebrows, |
| | 38 | | WearableItem mouth, |
| | 39 | | List<WearableItem> wearables, |
| | 40 | | List<WearableItem> emotes |
| | 41 | | )> Curate(AvatarSettings settings, IEnumerable<string> wearablesId, IEnumerable<string> emoteIds, Cancellati |
| | 42 | | { |
| 3 | 43 | | ct.ThrowIfCancellationRequested(); |
| | 44 | |
|
| | 45 | | try |
| | 46 | | { |
| | 47 | | //Old flow contains emotes among the wearablesIds |
| 2 | 48 | | (List<WearableItem> wearableItems, List<WearableItem> emotes) = await wearableItemResolver.ResolveAndSp |
| | 49 | |
|
| 2 | 50 | | HashSet<string> hiddenCategories = WearableItem.ComposeHiddenCategories(settings.bodyshapeId, wearableIt |
| | 51 | |
|
| | 52 | | //New emotes flow use the emotes catalog |
| 2 | 53 | | if (emoteIds != null) |
| | 54 | | { |
| 2 | 55 | | var moreEmotes = await emotesCatalog.RequestEmotesAsync(emoteIds.ToList(), ct); |
| 2 | 56 | | if (moreEmotes != null) |
| | 57 | | { |
| | 58 | | //this filter is needed to make sure there will be no duplicates coming from two sources of emot |
| 0 | 59 | | var loadedEmotesFilter = new HashSet<string>(); |
| 0 | 60 | | emotes.ForEach(e => loadedEmotesFilter.Add(e.id)); |
| | 61 | |
|
| 0 | 62 | | foreach(var otherEmote in moreEmotes) |
| 0 | 63 | | if (otherEmote != null) |
| | 64 | | { |
| 0 | 65 | | if (loadedEmotesFilter.Contains(otherEmote.id)) |
| | 66 | | continue; |
| | 67 | |
|
| 0 | 68 | | emotes.Add(otherEmote); |
| | 69 | | } |
| | 70 | | } |
| | 71 | | } |
| | 72 | |
|
| 2 | 73 | | Dictionary<string, WearableItem> wearablesByCategory = new Dictionary<string, WearableItem>(); |
| 22 | 74 | | for (int i = 0; i < wearableItems.Count; i++) |
| | 75 | | { |
| 9 | 76 | | WearableItem wearableItem = wearableItems[i]; |
| | 77 | |
|
| | 78 | | // Ignore hidden categories |
| 9 | 79 | | if (hiddenCategories.Contains(wearableItem.data.category)) |
| | 80 | | continue; |
| | 81 | |
|
| | 82 | | // Avoid having two items with the same category. |
| 9 | 83 | | if (wearableItem == null || wearablesByCategory.ContainsKey(wearableItem.data.category) ) |
| | 84 | | continue; |
| | 85 | |
|
| | 86 | | // Filter wearables without representation for the bodyshape |
| 9 | 87 | | if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation)) |
| | 88 | | continue; |
| | 89 | |
|
| 9 | 90 | | wearablesByCategory.Add(wearableItem.data.category, wearableItem); |
| | 91 | | } |
| | 92 | |
|
| 2 | 93 | | if (!wearablesByCategory.ContainsKey(WearableLiterals.Categories.BODY_SHAPE)) |
| 0 | 94 | | throw new Exception("Set of wearables doesn't contain a bodyshape (or couldn't be resolved)"); |
| | 95 | |
|
| 2 | 96 | | WearableItem[] fallbackWearables = await GetFallbackForMissingNeededCategories(settings.bodyshapeId, wea |
| | 97 | |
|
| 14 | 98 | | for (int i = 0; i < fallbackWearables.Length; i++) |
| | 99 | | { |
| 5 | 100 | | WearableItem wearableItem = fallbackWearables[i]; |
| 5 | 101 | | if (wearableItem == null) |
| 0 | 102 | | throw new Exception($"Fallback wearable is null"); |
| 5 | 103 | | if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation)) |
| 0 | 104 | | throw new Exception($"Fallback wearable {wearableItem} doesn't contain a representation for {set |
| 5 | 105 | | if (wearablesByCategory.ContainsKey(wearableItem.data.category)) |
| 0 | 106 | | throw new Exception($"A wearable in category {wearableItem.data.category} already exists trying |
| 5 | 107 | | wearablesByCategory.Add(wearableItem.data.category, wearableItem); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | // Wearables that are not bodyshape or facialFeatures |
| 2 | 111 | | List<WearableItem> wearables = wearablesByCategory.Where( |
| | 112 | | x => |
| 14 | 113 | | x.Key != WearableLiterals.Categories.BODY_SHAP |
| | 114 | | x.Key != WearableLiterals.Categories.EYES && |
| | 115 | | x.Key != WearableLiterals.Categories.EYEBROWS |
| | 116 | | x.Key != WearableLiterals.Categories.MOUTH) |
| 6 | 117 | | .Select(x => x.Value) |
| | 118 | | .ToList(); |
| | 119 | |
|
| 2 | 120 | | return ( |
| | 121 | | wearablesByCategory[WearableLiterals.Categories.BODY_SHAPE], |
| | 122 | | wearablesByCategory.ContainsKey(WearableLiterals.Categories.EYES) ? wearablesByCategory[WearableLite |
| | 123 | | wearablesByCategory.ContainsKey(WearableLiterals.Categories.EYEBROWS) ? wearablesByCategory[Wearable |
| | 124 | | wearablesByCategory.ContainsKey(WearableLiterals.Categories.MOUTH) ? wearablesByCategory[WearableLit |
| | 125 | | wearables, |
| | 126 | | emotes.ToList() |
| | 127 | | ); |
| | 128 | | } |
| 0 | 129 | | catch (OperationCanceledException) |
| | 130 | | { |
| | 131 | | //No Disposing required |
| 0 | 132 | | throw; |
| | 133 | | } |
| | 134 | | catch (Exception e) |
| | 135 | | { |
| 0 | 136 | | Debug.Log("Failed curating avatar wearables"); |
| 0 | 137 | | ExceptionDispatchInfo.Capture(e).Throw(); |
| 0 | 138 | | throw; |
| | 139 | | } |
| 2 | 140 | | } |
| | 141 | |
|
| | 142 | | private async UniTask<WearableItem[]> GetFallbackForMissingNeededCategories(string bodyshapeId, Dictionary<strin |
| | 143 | | { |
| 2 | 144 | | ct.ThrowIfCancellationRequested(); |
| | 145 | |
|
| | 146 | | try |
| | 147 | | { |
| 2 | 148 | | List<UniTask<WearableItem>> neededWearablesTasks = new List<UniTask<WearableItem>>(); |
| 24 | 149 | | foreach (string neededCategory in WearableLiterals.Categories.REQUIRED_CATEGORIES) |
| | 150 | | { |
| | 151 | | // If a needed category is hidden we dont need to fallback, we skipped it on purpose |
| 10 | 152 | | if (hiddenCategories.Contains(neededCategory)) |
| | 153 | | continue; |
| | 154 | |
|
| | 155 | | // The needed category is present |
| 10 | 156 | | if (wearablesByCategory.ContainsKey(neededCategory)) |
| | 157 | | continue; |
| | 158 | |
|
| 5 | 159 | | string fallbackWearableId = WearableLiterals.DefaultWearables.GetDefaultWearable(bodyshapeId, needed |
| | 160 | |
|
| 5 | 161 | | if (fallbackWearableId == null) |
| 0 | 162 | | throw new Exception($"Couldn't find a fallback wearable for bodyshape: {bodyshapeId} and categor |
| | 163 | |
|
| 5 | 164 | | neededWearablesTasks.Add(wearableItemResolver.Resolve(fallbackWearableId, ct)); |
| | 165 | | } |
| 2 | 166 | | return await UniTask.WhenAll(neededWearablesTasks).AttachExternalCancellation(ct); |
| | 167 | | } |
| 0 | 168 | | catch (OperationCanceledException) |
| | 169 | | { |
| | 170 | | //No disposing required |
| 0 | 171 | | throw; |
| | 172 | | } |
| 2 | 173 | | } |
| | 174 | |
|
| 2104 | 175 | | public void Dispose() { wearableItemResolver.Dispose(); } |
| | 176 | | } |
| | 177 | | } |