| | 1 | | using System; |
| | 2 | | using System.Threading; |
| | 3 | | using Cysharp.Threading.Tasks; |
| | 4 | | using DCL; |
| | 5 | | using DCL.Components; |
| | 6 | | using DCL.Helpers; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace AvatarSystem |
| | 10 | | { |
| | 11 | | public class WearableRetriever : IWearableRetriever |
| | 12 | | { |
| 24 | 13 | | public Rendereable rendereable { get; private set; } |
| | 14 | |
|
| | 15 | | private RendereableAssetLoadHelper loaderAssetHelper; |
| | 16 | |
|
| | 17 | | public async UniTask<Rendereable> Retrieve(GameObject container, ContentProvider contentProvider, string baseUrl |
| | 18 | | { |
| 84 | 19 | | ct.ThrowIfCancellationRequested(); |
| | 20 | |
|
| | 21 | | try |
| | 22 | | { |
| 84 | 23 | | loaderAssetHelper?.Unload(); |
| | 24 | |
|
| 84 | 25 | | loaderAssetHelper = new RendereableAssetLoadHelper(contentProvider, baseUrl); |
| | 26 | |
|
| 84 | 27 | | loaderAssetHelper.settings.forceNewInstance = false; |
| | 28 | | // TODO Review this hardcoded offset and try to solve it by offseting the Avatar container |
| 84 | 29 | | loaderAssetHelper.settings.initialLocalPosition = Vector3.up * AvatarSystemUtils.AVATAR_Y_OFFSET; |
| 84 | 30 | | loaderAssetHelper.settings.cachingFlags = MaterialCachingHelper.Mode.CACHE_SHADERS; |
| 84 | 31 | | loaderAssetHelper.settings.visibleFlags = AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE; |
| 84 | 32 | | loaderAssetHelper.settings.parent = container.transform; |
| 84 | 33 | | loaderAssetHelper.settings.layer = container.layer; |
| | 34 | |
|
| 84 | 35 | | bool done = false; |
| 84 | 36 | | Exception exception = null; |
| | 37 | |
|
| | 38 | | void OnSuccessWrapper(Rendereable rendereable) |
| | 39 | | { |
| 8 | 40 | | loaderAssetHelper?.ClearEvents(); |
| 8 | 41 | | done = true; |
| 8 | 42 | | this.rendereable = rendereable; |
| 8 | 43 | | } |
| | 44 | |
|
| | 45 | | void OnFailEventWrapper(Exception e) |
| | 46 | | { |
| 0 | 47 | | exception = e; |
| 0 | 48 | | loaderAssetHelper?.ClearEvents(); |
| 0 | 49 | | done = true; |
| 0 | 50 | | rendereable = null; |
| 0 | 51 | | } |
| | 52 | |
|
| 84 | 53 | | loaderAssetHelper.OnSuccessEvent += OnSuccessWrapper; |
| 84 | 54 | | loaderAssetHelper.OnFailEvent += OnFailEventWrapper; |
| 84 | 55 | | loaderAssetHelper.Load(mainFile, AvatarSystemUtils.UseAssetBundles() ? RendereableAssetLoadHelper.Loadin |
| | 56 | |
|
| | 57 | | // AttachExternalCancellation is needed because a cancelled WaitUntil UniTask requires a frame |
| 337 | 58 | | await UniTask.WaitUntil(() => done, cancellationToken: ct).AttachExternalCancellation(ct); |
| | 59 | |
|
| 8 | 60 | | if (exception != null) |
| 0 | 61 | | throw exception; |
| | 62 | |
|
| 8 | 63 | | if (rendereable == null) |
| 0 | 64 | | throw new Exception($"Couldnt retrieve Wearable assets at: {mainFile}"); |
| | 65 | |
|
| 8 | 66 | | return rendereable; |
| | 67 | | } |
| 76 | 68 | | catch (OperationCanceledException e) |
| | 69 | | { |
| 76 | 70 | | Dispose(); |
| 76 | 71 | | throw; |
| | 72 | | } |
| 8 | 73 | | } |
| | 74 | |
|
| 827 | 75 | | public void Dispose() { loaderAssetHelper?.Unload(); } |
| | 76 | | } |
| | 77 | | } |