| | 1 | | using System.Linq; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCLServices.WearablesCatalogService; |
| | 6 | | using System; |
| | 7 | | using UnityEngine; |
| | 8 | | using Environment = DCL.Environment; |
| | 9 | |
|
| | 10 | | namespace AvatarSystem |
| | 11 | | { |
| | 12 | | public class WearableLoader : IWearableLoader |
| | 13 | | { |
| | 14 | | // TODO: This should be a service |
| 1 | 15 | | internal static IWearableItemResolver defaultWearablesResolver = new WearableItemResolver(Environment.i.serviceL |
| | 16 | | static WearableLoader() |
| | 17 | | { |
| | 18 | | // Prewarm default wearables |
| 1 | 19 | | defaultWearablesResolver.Resolve(WearableLiterals.DefaultWearables.GetDefaultWearables()); |
| 1 | 20 | | } |
| | 21 | |
|
| 2 | 22 | | public WearableItem bodyShape { get; } |
| 6 | 23 | | public Rendereable rendereable => retriever?.rendereable; |
| 64 | 24 | | public IWearableLoader.Status status { get; private set; } |
| | 25 | |
|
| | 26 | | private readonly IWearableRetriever retriever; |
| | 27 | | private AvatarSettings currentSettings; |
| | 28 | |
|
| 2 | 29 | | public WearableLoader(IWearableRetriever retriever, WearableItem wearable) |
| | 30 | | { |
| 2 | 31 | | this.bodyShape = wearable; |
| 2 | 32 | | this.retriever = retriever; |
| 2 | 33 | | } |
| | 34 | |
|
| | 35 | | public async UniTask Load(GameObject container, AvatarSettings settings, CancellationToken ct = default) |
| | 36 | | { |
| 2 | 37 | | ct.ThrowIfCancellationRequested(); |
| | 38 | |
|
| | 39 | | try |
| | 40 | | { |
| 2 | 41 | | bool bodyshapeDirty = currentSettings.bodyshapeId != settings.bodyshapeId; |
| 2 | 42 | | currentSettings = settings; |
| 2 | 43 | | if (status == IWearableLoader.Status.Succeeded && !bodyshapeDirty) |
| | 44 | | { |
| 0 | 45 | | AvatarSystemUtils.PrepareMaterialColors(rendereable, currentSettings.skinColor, currentSettings.hair |
| 0 | 46 | | return; |
| | 47 | | } |
| | 48 | |
|
| 2 | 49 | | retriever.Dispose(); |
| | 50 | |
|
| | 51 | | try |
| | 52 | | { |
| 6 | 53 | | await LoadWearable(container, bodyShape, settings.bodyshapeId, ct); |
| 2 | 54 | | } |
| 0 | 55 | | catch (OperationCanceledException) |
| | 56 | | { |
| 0 | 57 | | throw; |
| | 58 | | } |
| 0 | 59 | | catch (Exception e) |
| | 60 | | { |
| | 61 | | // Ignored so we try to fallback (if needed) |
| 0 | 62 | | } |
| | 63 | |
|
| | 64 | | // Succeeded |
| 2 | 65 | | if (rendereable != null) |
| | 66 | | { |
| 2 | 67 | | AvatarSystemUtils.PrepareMaterialColors(rendereable, currentSettings.skinColor, currentSettings.hair |
| 2 | 68 | | status = IWearableLoader.Status.Succeeded; |
| 2 | 69 | | return; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | //Try getting a default if category is needed |
| 0 | 73 | | if (AvatarSystemUtils.IsCategoryRequired(bodyShape.data.category)) |
| 0 | 74 | | await FallbackToDefault(container, ct); |
| | 75 | |
|
| 0 | 76 | | if (rendereable != null) |
| | 77 | | { |
| 0 | 78 | | AvatarSystemUtils.PrepareMaterialColors(rendereable, currentSettings.skinColor, currentSettings.hair |
| 0 | 79 | | status = IWearableLoader.Status.Defaulted; |
| | 80 | | } |
| | 81 | | else |
| 0 | 82 | | status = IWearableLoader.Status.Failed; |
| 0 | 83 | | } |
| 0 | 84 | | catch (OperationCanceledException) |
| | 85 | | { |
| 0 | 86 | | Dispose(); |
| 0 | 87 | | throw; |
| | 88 | | } |
| 2 | 89 | | } |
| | 90 | |
|
| 0 | 91 | | public void SetBones(Transform rootBone, Transform[] bones) { AvatarSystemUtils.CopyBones(rootBone, bones, rende |
| | 92 | |
|
| | 93 | | private async UniTask FallbackToDefault(GameObject container, CancellationToken ct) |
| | 94 | | { |
| 0 | 95 | | ct.ThrowIfCancellationRequested(); |
| | 96 | |
|
| | 97 | | try |
| | 98 | | { |
| 0 | 99 | | string wearableId = WearableLiterals.DefaultWearables.GetDefaultWearable(currentSettings.bodyshapeId, bo |
| 0 | 100 | | Debug.Log($"Falling back {bodyShape.id} to wearable {wearableId}"); |
| | 101 | |
|
| 0 | 102 | | WearableItem defaultWearable = await defaultWearablesResolver.Resolve(wearableId, ct); |
| | 103 | |
|
| 0 | 104 | | await LoadWearable(container, defaultWearable, currentSettings.bodyshapeId, ct); |
| 0 | 105 | | } |
| 0 | 106 | | catch (OperationCanceledException) |
| | 107 | | { |
| | 108 | | //No disposing required |
| 0 | 109 | | throw; |
| | 110 | | } |
| | 111 | |
|
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | private async UniTask LoadWearable(GameObject container, WearableItem wearableToLoad, string bodyshapeId, Cancel |
| | 115 | | { |
| 2 | 116 | | ct.ThrowIfCancellationRequested(); |
| 6 | 117 | | await retriever.Retrieve(container, wearableToLoad, bodyshapeId, ct); |
| 2 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | public void Dispose() { retriever?.Dispose(); } |
| | 121 | | } |
| | 122 | | } |