< Summary

Class:AvatarSystem.AvatarCurator
Assembly:AvatarSystem
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/AvatarCurator.cs
Covered lines:42
Uncovered lines:18
Coverable lines:60
Total lines:177
Line coverage:70% (42 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarCurator(...)0%110100%
Curate()0%119.1631054.9%
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
 104618        public AvatarCurator(IWearableItemResolver wearableItemResolver, IEmotesCatalogService emotesCatalog)
 19        {
 104620            Assert.IsNotNull(wearableItemResolver);
 104621            this.wearableItemResolver = wearableItemResolver;
 104622            this.emotesCatalog = emotesCatalog;
 104623        }
 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
 49
 250                HashSet<string> hiddenCategories = WearableItem.ComposeHiddenCategories(settings.bodyshapeId, wearableIt
 51
 52                //New emotes flow use the emotes catalog
 253                if (emoteIds != null)
 54                {
 255                    var moreEmotes = await emotesCatalog.RequestEmotesAsync(emoteIds.ToList(), ct);
 256                    if (moreEmotes != null)
 57                    {
 58                        //this filter is needed to make sure there will be no duplicates coming from two sources of emot
 059                        var loadedEmotesFilter = new HashSet<string>();
 060                        emotes.ForEach(e => loadedEmotesFilter.Add(e.id));
 61
 062                        foreach(var otherEmote in moreEmotes)
 063                            if (otherEmote != null)
 64                            {
 065                                if (loadedEmotesFilter.Contains(otherEmote.id))
 66                                    continue;
 67
 068                                emotes.Add(otherEmote);
 69                            }
 70                    }
 71                }
 72
 273                Dictionary<string, WearableItem> wearablesByCategory = new Dictionary<string, WearableItem>();
 2274                for (int i = 0; i < wearableItems.Count; i++)
 75                {
 976                    WearableItem wearableItem = wearableItems[i];
 77
 78                    // Ignore hidden categories
 979                    if (hiddenCategories.Contains(wearableItem.data.category))
 80                        continue;
 81
 82                    // Avoid having two items with the same category.
 983                    if (wearableItem == null || wearablesByCategory.ContainsKey(wearableItem.data.category) )
 84                        continue;
 85
 86                    // Filter wearables without representation for the bodyshape
 987                    if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation))
 88                        continue;
 89
 990                    wearablesByCategory.Add(wearableItem.data.category, wearableItem);
 91                }
 92
 293                if (!wearablesByCategory.ContainsKey(WearableLiterals.Categories.BODY_SHAPE))
 094                    throw new Exception("Set of wearables doesn't contain a bodyshape (or couldn't be resolved)");
 95
 296                WearableItem[] fallbackWearables = await GetFallbackForMissingNeededCategories(settings.bodyshapeId, wea
 97
 1498                for (int i = 0; i < fallbackWearables.Length; i++)
 99                {
 5100                    WearableItem wearableItem = fallbackWearables[i];
 5101                    if (wearableItem == null)
 0102                        throw new Exception($"Fallback wearable is null");
 5103                    if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation))
 0104                        throw new Exception($"Fallback wearable {wearableItem} doesn't contain a representation for {set
 5105                    if (wearablesByCategory.ContainsKey(wearableItem.data.category))
 0106                        throw new Exception($"A wearable in category {wearableItem.data.category} already exists trying 
 5107                    wearablesByCategory.Add(wearableItem.data.category, wearableItem);
 108                }
 109
 110                // Wearables that are not bodyshape or facialFeatures
 2111                List<WearableItem> wearables = wearablesByCategory.Where(
 112                                                                      x =>
 14113                                                                          x.Key != WearableLiterals.Categories.BODY_SHAP
 114                                                                          x.Key != WearableLiterals.Categories.EYES &&
 115                                                                          x.Key != WearableLiterals.Categories.EYEBROWS 
 116                                                                          x.Key != WearableLiterals.Categories.MOUTH)
 6117                                                                  .Select(x => x.Value)
 118                                                                  .ToList();
 119
 2120                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            }
 0129            catch (OperationCanceledException)
 130            {
 131                //No Disposing required
 0132                throw;
 133            }
 134            catch (Exception e)
 135            {
 0136                Debug.Log("Failed curating avatar wearables");
 0137                ExceptionDispatchInfo.Capture(e).Throw();
 0138                throw;
 139            }
 2140        }
 141
 142        private async UniTask<WearableItem[]> GetFallbackForMissingNeededCategories(string bodyshapeId, Dictionary<strin
 143        {
 2144            ct.ThrowIfCancellationRequested();
 145
 146            try
 147            {
 2148                List<UniTask<WearableItem>> neededWearablesTasks = new List<UniTask<WearableItem>>();
 24149                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
 10152                    if (hiddenCategories.Contains(neededCategory))
 153                        continue;
 154
 155                    // The needed category is present
 10156                    if (wearablesByCategory.ContainsKey(neededCategory))
 157                        continue;
 158
 5159                    string fallbackWearableId = WearableLiterals.DefaultWearables.GetDefaultWearable(bodyshapeId, needed
 160
 5161                    if (fallbackWearableId == null)
 0162                        throw new Exception($"Couldn't find a fallback wearable for bodyshape: {bodyshapeId} and categor
 163
 5164                    neededWearablesTasks.Add(wearableItemResolver.Resolve(fallbackWearableId, ct));
 165                }
 2166                return await UniTask.WhenAll(neededWearablesTasks).AttachExternalCancellation(ct);
 167            }
 0168            catch (OperationCanceledException)
 169            {
 170                //No disposing required
 0171                throw;
 172            }
 2173        }
 174
 2118175        public void Dispose() { wearableItemResolver.Dispose(); }
 176    }
 177}