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