< Summary

Class:AvatarSystem.AvatarCurator
Assembly:AvatarSystem
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/AvatarCurator.cs
Covered lines:40
Uncovered lines:15
Coverable lines:55
Total lines:163
Line coverage:72.7% (40 of 55)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarCurator(...)0%110100%
Curate()0%91.6230059.09%
GetFallbackForMissingNeededCategories()0%12.5410070.59%
Dispose()0%110100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/AvatarCurator.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Runtime.ExceptionServices;
 5using System.Threading;
 6using Cysharp.Threading.Tasks;
 7using DCL;
 8using UnityEngine;
 9using UnityEngine.Assertions;
 10
 11namespace AvatarSystem
 12{
 13    public class AvatarCurator : IAvatarCurator
 14    {
 15        private readonly IWearableItemResolver wearableItemResolver;
 16        private readonly IEmotesCatalogService emotesCatalog;
 17
 101718        public AvatarCurator(IWearableItemResolver wearableItemResolver, IEmotesCatalogService emotesCatalog)
 19        {
 101720            Assert.IsNotNull(wearableItemResolver);
 101721            this.wearableItemResolver = wearableItemResolver;
 101722            this.emotesCatalog = emotesCatalog;
 101723        }
 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        {
 343            ct.ThrowIfCancellationRequested();
 44
 45            try
 46            {
 47                //Old flow contains emotes among the wearablesIds
 248                (List<WearableItem> wearableItems, List<WearableItem> emotes) =  await wearableItemResolver.ResolveAndSp
 249                HashSet<string> hiddenCategories = WearableItem.ComposeHiddenCategories(settings.bodyshapeId, wearableIt
 50
 51                //New emotes flow use the emotes catalog
 252                if (emoteIds != null && DataStore.i.emotes.newFlowEnabled.Get())
 53                {
 054                    var moreEmotes = await emotesCatalog.RequestEmotesAsync(emoteIds.ToList(), ct);
 055                    if (moreEmotes != null)
 056                        emotes.AddRange(moreEmotes.Where(x => x != null));
 57                }
 58
 259                Dictionary<string, WearableItem> wearablesByCategory = new Dictionary<string, WearableItem>();
 2260                for (int i = 0; i < wearableItems.Count; i++)
 61                {
 962                    WearableItem wearableItem = wearableItems[i];
 63
 64                    // Ignore hidden categories
 965                    if (hiddenCategories.Contains(wearableItem.data.category))
 66                        continue;
 67
 68                    // Avoid having two items with the same category.
 969                    if (wearableItem == null || wearablesByCategory.ContainsKey(wearableItem.data.category) )
 70                        continue;
 71
 72                    // Filter wearables without representation for the bodyshape
 973                    if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation))
 74                        continue;
 75
 976                    wearablesByCategory.Add(wearableItem.data.category, wearableItem);
 77                }
 78
 279                if (!wearablesByCategory.ContainsKey(WearableLiterals.Categories.BODY_SHAPE))
 080                    throw new Exception("Set of wearables doesn't contain a bodyshape (or couldn't be resolved)");
 81
 282                WearableItem[] fallbackWearables = await GetFallbackForMissingNeededCategories(settings.bodyshapeId, wea
 83
 1484                for (int i = 0; i < fallbackWearables.Length; i++)
 85                {
 586                    WearableItem wearableItem = fallbackWearables[i];
 587                    if (wearableItem == null)
 088                        throw new Exception($"Fallback wearable is null");
 589                    if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation))
 090                        throw new Exception($"Fallback wearable {wearableItem} doesn't contain a representation for {set
 591                    if (wearablesByCategory.ContainsKey(wearableItem.data.category))
 092                        throw new Exception($"A wearable in category {wearableItem.data.category} already exists trying 
 593                    wearablesByCategory.Add(wearableItem.data.category, wearableItem);
 94                }
 95
 96                // Wearables that are not bodyshape or facialFeatures
 297                List<WearableItem> wearables = wearablesByCategory.Where(
 98                                                                      x =>
 1499                                                                          x.Key != WearableLiterals.Categories.BODY_SHAP
 100                                                                          x.Key != WearableLiterals.Categories.EYES &&
 101                                                                          x.Key != WearableLiterals.Categories.EYEBROWS 
 102                                                                          x.Key != WearableLiterals.Categories.MOUTH)
 6103                                                                  .Select(x => x.Value)
 104                                                                  .ToList();
 105
 2106                return (
 107                    wearablesByCategory[WearableLiterals.Categories.BODY_SHAPE],
 108                    wearablesByCategory.ContainsKey(WearableLiterals.Categories.EYES) ? wearablesByCategory[WearableLite
 109                    wearablesByCategory.ContainsKey(WearableLiterals.Categories.EYEBROWS) ? wearablesByCategory[Wearable
 110                    wearablesByCategory.ContainsKey(WearableLiterals.Categories.MOUTH) ? wearablesByCategory[WearableLit
 111                    wearables,
 112                    emotes.ToList()
 113                );
 114            }
 0115            catch (OperationCanceledException)
 116            {
 117                //No Disposing required
 0118                throw;
 119            }
 120            catch (Exception e)
 121            {
 0122                Debug.Log("Failed curating avatar wearables");
 0123                ExceptionDispatchInfo.Capture(e).Throw();
 0124                throw;
 125            }
 2126        }
 127
 128        private async UniTask<WearableItem[]> GetFallbackForMissingNeededCategories(string bodyshapeId, Dictionary<strin
 129        {
 2130            ct.ThrowIfCancellationRequested();
 131
 132            try
 133            {
 2134                List<UniTask<WearableItem>> neededWearablesTasks = new List<UniTask<WearableItem>>();
 24135                foreach (string neededCategory in WearableLiterals.Categories.REQUIRED_CATEGORIES)
 136                {
 137                    // If a needed category is hidden we dont need to fallback, we skipped it on purpose
 10138                    if (hiddenCategories.Contains(neededCategory))
 139                        continue;
 140
 141                    // The needed category is present
 10142                    if (wearablesByCategory.ContainsKey(neededCategory))
 143                        continue;
 144
 5145                    string fallbackWearableId = WearableLiterals.DefaultWearables.GetDefaultWearable(bodyshapeId, needed
 146
 5147                    if (fallbackWearableId == null)
 0148                        throw new Exception($"Couldn't find a fallback wearable for bodyshape: {bodyshapeId} and categor
 149
 5150                    neededWearablesTasks.Add(wearableItemResolver.Resolve(fallbackWearableId, ct));
 151                }
 2152                return await UniTask.WhenAll(neededWearablesTasks).AttachExternalCancellation(ct);
 153            }
 0154            catch (OperationCanceledException)
 155            {
 156                //No disposing required
 0157                throw;
 158            }
 2159        }
 160
 2070161        public void Dispose() { wearableItemResolver.Dispose(); }
 162    }
 163}