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