< 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:17
Coverable lines:59
Total lines:174
Line coverage:71.1% (42 of 59)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AvatarCurator(...)0%110100%
Curate()0%112.8631056%
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
 103918        public AvatarCurator(IWearableItemResolver wearableItemResolver, IEmotesCatalogService emotesCatalog)
 19        {
 103920            Assert.IsNotNull(wearableItemResolver);
 103921            this.wearableItemResolver = wearableItemResolver;
 103922            this.emotesCatalog = emotesCatalog;
 103923        }
 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 && loadedEmotesFilter.Contains(otherEmote.id))
 64                            {
 065                                emotes.Add(otherEmote);
 66                            }
 67                    }
 68                }
 69
 270                Dictionary<string, WearableItem> wearablesByCategory = new Dictionary<string, WearableItem>();
 2271                for (int i = 0; i < wearableItems.Count; i++)
 72                {
 973                    WearableItem wearableItem = wearableItems[i];
 74
 75                    // Ignore hidden categories
 976                    if (hiddenCategories.Contains(wearableItem.data.category))
 77                        continue;
 78
 79                    // Avoid having two items with the same category.
 980                    if (wearableItem == null || wearablesByCategory.ContainsKey(wearableItem.data.category) )
 81                        continue;
 82
 83                    // Filter wearables without representation for the bodyshape
 984                    if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation))
 85                        continue;
 86
 987                    wearablesByCategory.Add(wearableItem.data.category, wearableItem);
 88                }
 89
 290                if (!wearablesByCategory.ContainsKey(WearableLiterals.Categories.BODY_SHAPE))
 091                    throw new Exception("Set of wearables doesn't contain a bodyshape (or couldn't be resolved)");
 92
 293                WearableItem[] fallbackWearables = await GetFallbackForMissingNeededCategories(settings.bodyshapeId, wea
 94
 1495                for (int i = 0; i < fallbackWearables.Length; i++)
 96                {
 597                    WearableItem wearableItem = fallbackWearables[i];
 598                    if (wearableItem == null)
 099                        throw new Exception($"Fallback wearable is null");
 5100                    if (!wearableItem.TryGetRepresentation(settings.bodyshapeId, out var representation))
 0101                        throw new Exception($"Fallback wearable {wearableItem} doesn't contain a representation for {set
 5102                    if (wearablesByCategory.ContainsKey(wearableItem.data.category))
 0103                        throw new Exception($"A wearable in category {wearableItem.data.category} already exists trying 
 5104                    wearablesByCategory.Add(wearableItem.data.category, wearableItem);
 105                }
 106
 107                // Wearables that are not bodyshape or facialFeatures
 2108                List<WearableItem> wearables = wearablesByCategory.Where(
 109                                                                      x =>
 14110                                                                          x.Key != WearableLiterals.Categories.BODY_SHAP
 111                                                                          x.Key != WearableLiterals.Categories.EYES &&
 112                                                                          x.Key != WearableLiterals.Categories.EYEBROWS 
 113                                                                          x.Key != WearableLiterals.Categories.MOUTH)
 6114                                                                  .Select(x => x.Value)
 115                                                                  .ToList();
 116
 2117                return (
 118                    wearablesByCategory[WearableLiterals.Categories.BODY_SHAPE],
 119                    wearablesByCategory.ContainsKey(WearableLiterals.Categories.EYES) ? wearablesByCategory[WearableLite
 120                    wearablesByCategory.ContainsKey(WearableLiterals.Categories.EYEBROWS) ? wearablesByCategory[Wearable
 121                    wearablesByCategory.ContainsKey(WearableLiterals.Categories.MOUTH) ? wearablesByCategory[WearableLit
 122                    wearables,
 123                    emotes.ToList()
 124                );
 125            }
 0126            catch (OperationCanceledException)
 127            {
 128                //No Disposing required
 0129                throw;
 130            }
 131            catch (Exception e)
 132            {
 0133                Debug.Log("Failed curating avatar wearables");
 0134                ExceptionDispatchInfo.Capture(e).Throw();
 0135                throw;
 136            }
 2137        }
 138
 139        private async UniTask<WearableItem[]> GetFallbackForMissingNeededCategories(string bodyshapeId, Dictionary<strin
 140        {
 2141            ct.ThrowIfCancellationRequested();
 142
 143            try
 144            {
 2145                List<UniTask<WearableItem>> neededWearablesTasks = new List<UniTask<WearableItem>>();
 24146                foreach (string neededCategory in WearableLiterals.Categories.REQUIRED_CATEGORIES)
 147                {
 148                    // If a needed category is hidden we dont need to fallback, we skipped it on purpose
 10149                    if (hiddenCategories.Contains(neededCategory))
 150                        continue;
 151
 152                    // The needed category is present
 10153                    if (wearablesByCategory.ContainsKey(neededCategory))
 154                        continue;
 155
 5156                    string fallbackWearableId = WearableLiterals.DefaultWearables.GetDefaultWearable(bodyshapeId, needed
 157
 5158                    if (fallbackWearableId == null)
 0159                        throw new Exception($"Couldn't find a fallback wearable for bodyshape: {bodyshapeId} and categor
 160
 5161                    neededWearablesTasks.Add(wearableItemResolver.Resolve(fallbackWearableId, ct));
 162                }
 2163                return await UniTask.WhenAll(neededWearablesTasks).AttachExternalCancellation(ct);
 164            }
 0165            catch (OperationCanceledException)
 166            {
 167                //No disposing required
 0168                throw;
 169            }
 2170        }
 171
 2104172        public void Dispose() { wearableItemResolver.Dispose(); }
 173    }
 174}