| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Emotes; |
| | 3 | | using DCLServices.WearablesCatalogService; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Linq; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | | using UnityEngine.Assertions; |
| | 10 | | using UnityEngine.Pool; |
| | 11 | |
|
| | 12 | | namespace AvatarSystem |
| | 13 | | { |
| | 14 | | public class AvatarCurator : IAvatarCurator |
| | 15 | | { |
| | 16 | | private readonly IWearableItemResolver wearableItemResolver; |
| | 17 | | private readonly IEmotesCatalogService emotesCatalog; |
| | 18 | | private EmbeddedEmotesSO embedEmotes; |
| | 19 | | private string[] embedEmotesId; |
| | 20 | |
|
| 518 | 21 | | public AvatarCurator(IWearableItemResolver wearableItemResolver, IEmotesCatalogService emotesCatalog) |
| | 22 | | { |
| 518 | 23 | | Assert.IsNotNull(wearableItemResolver); |
| 518 | 24 | | this.wearableItemResolver = wearableItemResolver; |
| 518 | 25 | | this.emotesCatalog = emotesCatalog; |
| | 26 | |
|
| 518 | 27 | | } |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Curate a flattened into IDs set of wearables. |
| | 31 | | /// Bear in mind that the bodyshape must be part of the list of wearables |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="settings"></param> |
| | 34 | | /// <param name="wearablesId"></param> |
| | 35 | | /// <param name="emoteIds"></param> |
| | 36 | | /// <param name="ct"></param> |
| | 37 | | /// <returns></returns> |
| | 38 | | /// <exception cref="Exception"></exception> |
| | 39 | | public async UniTask<( |
| | 40 | | BodyWearables bodyWearables, |
| | 41 | | List<WearableItem> wearables, |
| | 42 | | List<WearableItem> emotes |
| | 43 | | )> Curate(AvatarSettings settings, IEnumerable<string> wearablesId, IEnumerable<string> emoteIds, Cancellati |
| | 44 | | { |
| 0 | 45 | | ct.ThrowIfCancellationRequested(); |
| | 46 | |
|
| | 47 | | try |
| | 48 | | { |
| | 49 | | //Old flow contains emotes among the wearablesIds |
| 0 | 50 | | (List<WearableItem> wearableItems, List<WearableItem> emotes) = await wearableItemResolver.ResolveAndSpl |
| | 51 | |
|
| 0 | 52 | | HashSet<string> hiddenCategories = WearableItem.ComposeHiddenCategoriesOrdered(settings.bodyshapeId, set |
| | 53 | |
|
| | 54 | | //New emotes flow use the emotes catalog |
| 0 | 55 | | if (emoteIds != null) |
| | 56 | | { |
| 0 | 57 | | embedEmotes ??= await emotesCatalog.GetEmbeddedEmotes(); |
| 0 | 58 | | embedEmotesId ??= embedEmotes.GetAllIds(); |
| | 59 | |
|
| 0 | 60 | | DateTime startLoadTime = DateTime.Now; |
| | 61 | |
|
| 0 | 62 | | var emoteIdsList = emoteIds.Select(ExtendedUrnParser.GetShortenedUrn).ToList(); |
| 0 | 63 | | IReadOnlyList<WearableItem> resolvedEmotes = await emotesCatalog.RequestEmotesAsync(emoteIdsList, ct |
| 0 | 64 | | List<WearableItem> nonPublishedEmotes = ListPool<WearableItem>.Get(); |
| | 65 | |
|
| 0 | 66 | | foreach (string nonPublishedEmoteId in emoteIdsList) |
| | 67 | | { |
| 0 | 68 | | if (nonPublishedEmoteId.StartsWith("urn")) continue; |
| 0 | 69 | | bool wasResolved = resolvedEmotes?.Any(item => item?.id == nonPublishedEmoteId) ?? false; |
| 0 | 70 | | if (wasResolved) continue; |
| 0 | 71 | | bool isEmbedded = embedEmotesId.Contains(nonPublishedEmoteId); |
| 0 | 72 | | if (isEmbedded) continue; |
| 0 | 73 | | WearableItem nonPublishedEmote = await emotesCatalog.RequestEmoteFromBuilderAsync(nonPublishedEm |
| 0 | 74 | | if (nonPublishedEmote != null) |
| 0 | 75 | | nonPublishedEmotes.Add(nonPublishedEmote); |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | var loadTimeDelta = DateTime.Now - startLoadTime; |
| | 79 | |
|
| 0 | 80 | | if (loadTimeDelta.TotalSeconds > 5) |
| | 81 | | { |
| | 82 | | //This error is good to have to detect too long load times early |
| 0 | 83 | | Debug.LogError("Curate: emotes load time is too high: " + (DateTime.Now - startLoadTime)); |
| | 84 | | } |
| | 85 | |
|
| | 86 | | //this filter is needed to make sure there will be no duplicates coming from two sources of emotes |
| 0 | 87 | | var loadedEmotesFilter = new HashSet<string>(); |
| 0 | 88 | | emotes.ForEach(e => loadedEmotesFilter.Add(e.id)); |
| | 89 | |
|
| 0 | 90 | | if (resolvedEmotes != null) |
| | 91 | | { |
| 0 | 92 | | foreach (var otherEmote in resolvedEmotes) |
| 0 | 93 | | if (otherEmote != null) |
| | 94 | | { |
| 0 | 95 | | if (loadedEmotesFilter.Contains(otherEmote.id)) |
| | 96 | | continue; |
| | 97 | |
|
| 0 | 98 | | emotes.Add(otherEmote); |
| | 99 | | } |
| | 100 | | } |
| | 101 | |
|
| 0 | 102 | | foreach (WearableItem emote in nonPublishedEmotes) |
| | 103 | | { |
| 0 | 104 | | if (emote == null) continue; |
| 0 | 105 | | if (loadedEmotesFilter.Contains(emote.id)) continue; |
| 0 | 106 | | emotes.Add(emote); |
| | 107 | | } |
| | 108 | |
|
| 0 | 109 | | ListPool<WearableItem>.Release(nonPublishedEmotes); |
| 0 | 110 | | } |
| | 111 | |
|
| 0 | 112 | | Dictionary<string, WearableItem> wearablesByCategory = new Dictionary<string, WearableItem>(); |
| | 113 | |
|
| 0 | 114 | | for (int i = 0; i < wearableItems.Count; i++) |
| | 115 | | { |
| 0 | 116 | | WearableItem wearableItem = wearableItems[i]; |
| | 117 | |
|
| | 118 | | // Ignore hidden categories |
| 0 | 119 | | if (hiddenCategories.Contains(wearableItem.data.category)) |
| | 120 | | continue; |
| | 121 | |
|
| | 122 | | // Avoid having two items with the same category. |
| 0 | 123 | | if (wearableItem == null || wearablesByCategory.ContainsKey(wearableItem.data.category)) |
| | 124 | | continue; |
| | 125 | |
|
| | 126 | | // Filter wearables without representation for the bodyshape |
| 0 | 127 | | if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation)) |
| | 128 | | continue; |
| | 129 | |
|
| 0 | 130 | | wearablesByCategory.Add(wearableItem.data.category, wearableItem); |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | if (!wearablesByCategory.ContainsKey(WearableLiterals.Categories.BODY_SHAPE)) |
| 0 | 134 | | throw new Exception("Set of wearables doesn't contain a bodyshape (or couldn't be resolved)"); |
| | 135 | |
|
| 0 | 136 | | WearableItem[] fallbackWearables = await GetFallbackForMissingNeededCategories(settings.bodyshapeId, wea |
| | 137 | |
|
| 0 | 138 | | for (int i = 0; i < fallbackWearables.Length; i++) |
| | 139 | | { |
| 0 | 140 | | WearableItem wearableItem = fallbackWearables[i]; |
| | 141 | |
|
| 0 | 142 | | if (wearableItem == null) |
| 0 | 143 | | throw new Exception($"Fallback wearable is null"); |
| | 144 | |
|
| 0 | 145 | | if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation)) |
| 0 | 146 | | throw new Exception($"Fallback wearable {wearableItem} doesn't contain a representation for {set |
| | 147 | |
|
| 0 | 148 | | if (wearablesByCategory.ContainsKey(wearableItem.data.category)) |
| 0 | 149 | | throw new Exception($"A wearable in category {wearableItem.data.category} already exists trying |
| | 150 | |
|
| 0 | 151 | | wearablesByCategory.Add(wearableItem.data.category, wearableItem); |
| | 152 | | } |
| | 153 | |
|
| | 154 | | // Wearables that are not bodyshape or facialFeatures |
| 0 | 155 | | List<WearableItem> wearables = wearablesByCategory.Where( |
| | 156 | | x => |
| 0 | 157 | | x.Key != WearableLiterals.Categories.BODY_SHA |
| | 158 | | x.Key != WearableLiterals.Categories.EYES && |
| | 159 | | x.Key != WearableLiterals.Categories.EYEBROWS |
| | 160 | | x.Key != WearableLiterals.Categories.MOUTH) |
| 0 | 161 | | .Select(x => x.Value) |
| | 162 | | .ToList(); |
| | 163 | |
|
| 0 | 164 | | var bodyWearables = new BodyWearables |
| | 165 | | ( |
| | 166 | | wearablesByCategory[WearableLiterals.Categories.BODY_SHAPE], |
| | 167 | | wearablesByCategory.TryGetValue(WearableLiterals.Categories.EYES, out WearableItem eyes) ? eyes : nu |
| | 168 | | wearablesByCategory.TryGetValue(WearableLiterals.Categories.EYEBROWS, out WearableItem eyebrows) ? e |
| | 169 | | wearablesByCategory.TryGetValue(WearableLiterals.Categories.MOUTH, out WearableItem mouth) ? mouth : |
| | 170 | | ); |
| | 171 | |
|
| 0 | 172 | | return (bodyWearables, wearables, emotes.ToList()); |
| | 173 | | } |
| 0 | 174 | | catch (OperationCanceledException) |
| | 175 | | { |
| | 176 | | //No Disposing required |
| 0 | 177 | | throw; |
| | 178 | | } |
| 0 | 179 | | catch (Exception e) |
| | 180 | | { |
| 0 | 181 | | Debug.Log("Failed curating avatar wearables"); |
| 0 | 182 | | throw; |
| | 183 | | } |
| 0 | 184 | | } |
| | 185 | |
|
| | 186 | | private async UniTask<WearableItem[]> GetFallbackForMissingNeededCategories(string bodyshapeId, Dictionary<strin |
| | 187 | | { |
| 0 | 188 | | ct.ThrowIfCancellationRequested(); |
| | 189 | |
|
| | 190 | | try |
| | 191 | | { |
| 0 | 192 | | List<UniTask<WearableItem>> neededWearablesTasks = new List<UniTask<WearableItem>>(); |
| | 193 | |
|
| 0 | 194 | | foreach (string neededCategory in WearableLiterals.Categories.REQUIRED_CATEGORIES) |
| | 195 | | { |
| | 196 | | // If a needed category is hidden we dont need to fallback, we skipped it on purpose |
| 0 | 197 | | if (hiddenCategories.Contains(neededCategory)) |
| | 198 | | continue; |
| | 199 | |
|
| | 200 | | // The needed category is present |
| 0 | 201 | | if (wearablesByCategory.ContainsKey(neededCategory)) |
| | 202 | | continue; |
| | 203 | |
|
| 0 | 204 | | string fallbackWearableId = WearableLiterals.DefaultWearables.GetDefaultWearable(bodyshapeId, needed |
| | 205 | |
|
| 0 | 206 | | if (fallbackWearableId == null) |
| 0 | 207 | | throw new Exception($"Couldn't find a fallback wearable for bodyshape: {bodyshapeId} and categor |
| | 208 | |
|
| 0 | 209 | | neededWearablesTasks.Add(wearableItemResolver.Resolve(fallbackWearableId, ct)); |
| | 210 | | } |
| | 211 | |
|
| 0 | 212 | | return await UniTask.WhenAll(neededWearablesTasks).AttachExternalCancellation(ct); |
| | 213 | | } |
| 0 | 214 | | catch (OperationCanceledException) |
| | 215 | | { |
| | 216 | | //No disposing required |
| 0 | 217 | | throw; |
| | 218 | | } |
| 0 | 219 | | } |
| | 220 | |
|
| | 221 | | public void Dispose() |
| | 222 | | { |
| 521 | 223 | | wearableItemResolver.Dispose(); |
| 521 | 224 | | } |
| | 225 | | } |
| | 226 | | } |