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