| | 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 FacialFeatureRetriever : IFacialFeatureRetriever |
| | 10 | | { |
| | 11 | | AssetPromise_Texture mainTexturePromise = null; |
| | 12 | | AssetPromise_Texture maskTexturePromise = null; |
| | 13 | |
|
| | 14 | | public async UniTask<(Texture main, Texture mask)> Retrieve(WearableItem facialFeature, string bodyshapeId, Canc |
| | 15 | | { |
| 0 | 16 | | ct.ThrowIfCancellationRequested(); |
| | 17 | |
|
| | 18 | | try |
| | 19 | | { |
| 0 | 20 | | (string mainTextureUrl, string maskTextureUrl) = AvatarSystemUtils.GetFacialFeatureTexturesUrls(bodyshap |
| | 21 | |
|
| 0 | 22 | | AssetPromiseKeeper_Texture.i.Forget(mainTexturePromise); |
| 0 | 23 | | AssetPromiseKeeper_Texture.i.Forget(maskTexturePromise); |
| | 24 | |
|
| 0 | 25 | | Texture2D mainTexture = null; |
| 0 | 26 | | Texture2D maskTexture = null; |
| 0 | 27 | | Exception mainTextureException = null; |
| 0 | 28 | | mainTexturePromise = new AssetPromise_Texture(mainTextureUrl); |
| 0 | 29 | | mainTexturePromise.OnSuccessEvent += (x) => mainTexture = x.texture; |
| 0 | 30 | | mainTexturePromise.OnFailEvent += (x, e) => |
| | 31 | | { |
| 0 | 32 | | mainTextureException = e; |
| 0 | 33 | | mainTexture = null; |
| 0 | 34 | | }; |
| | 35 | |
|
| 0 | 36 | | AssetPromiseKeeper_Texture.i.Keep(mainTexturePromise); |
| | 37 | |
|
| 0 | 38 | | Exception maskTextureException = null; |
| 0 | 39 | | if (!string.IsNullOrEmpty(maskTextureUrl)) |
| | 40 | | { |
| 0 | 41 | | maskTexturePromise = new AssetPromise_Texture(maskTextureUrl); |
| 0 | 42 | | maskTexturePromise.OnSuccessEvent += (x) => maskTexture = x.texture; |
| 0 | 43 | | maskTexturePromise.OnFailEvent += (x, e) => |
| | 44 | | { |
| 0 | 45 | | maskTextureException = e; |
| 0 | 46 | | maskTexture = null; |
| 0 | 47 | | }; |
| | 48 | |
|
| 0 | 49 | | AssetPromiseKeeper_Texture.i.Keep(maskTexturePromise); |
| | 50 | | } |
| | 51 | |
|
| | 52 | | // AttachExternalCancellation is needed, otherwise the cancellation takes a frame to effect |
| 0 | 53 | | await mainTexturePromise.ToUniTask(cancellationToken: ct).AttachExternalCancellation(ct); |
| 0 | 54 | | if (mainTextureException != null) |
| 0 | 55 | | throw mainTextureException; |
| | 56 | |
|
| 0 | 57 | | if (mainTexture == null) |
| 0 | 58 | | throw new Exception($"Couldn't fetch main texture for {facialFeature.id} at {mainTextureUrl}"); |
| | 59 | |
|
| 0 | 60 | | if (maskTexturePromise != null) |
| | 61 | | { |
| 0 | 62 | | await maskTexturePromise.ToUniTask(cancellationToken: ct).AttachExternalCancellation(ct); |
| | 63 | |
|
| 0 | 64 | | if (maskTextureException != null) |
| 0 | 65 | | throw maskTextureException; |
| | 66 | |
|
| 0 | 67 | | if (maskTexture == null) |
| 0 | 68 | | throw new Exception($"Couldn't fetch mask texture for {facialFeature.id} at {maskTextureUrl}"); |
| | 69 | | } |
| | 70 | |
|
| 0 | 71 | | return (mainTexture, maskTexture); |
| | 72 | | } |
| 0 | 73 | | catch (OperationCanceledException) |
| | 74 | | { |
| 0 | 75 | | Dispose(); |
| 0 | 76 | | throw; |
| | 77 | | } |
| 0 | 78 | | } |
| | 79 | |
|
| | 80 | | public void Dispose() |
| | 81 | | { |
| 861 | 82 | | AssetPromiseKeeper_Texture.i.Forget(mainTexturePromise); |
| 861 | 83 | | AssetPromiseKeeper_Texture.i.Forget(maskTexturePromise); |
| 861 | 84 | | } |
| | 85 | | } |
| | 86 | | } |