< Summary

Class:AvatarSystem.WearableRetriever
Assembly:AvatarSystemLoaders
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/Loader/WearableRetriever.cs
Covered lines:31
Uncovered lines:26
Coverable lines:57
Total lines:131
Line coverage:54.3% (31 of 57)
Covered branches:0
Total branches:0
Covered methods:5
Total methods:6
Method coverage:83.3% (5 of 6)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Retrieve()0%33.5316059.09%
IsNewAssetBundleFlagEnabled()0%110100%
FetchSceneAssetBundles()0%12300%
Dispose()0%2.52050%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/AvatarSystem/Loader/WearableRetriever.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using DCL;
 3using DCL.Components;
 4using DCL.Helpers;
 5using MainScripts.DCL.Controllers.AssetManager.AssetBundles.SceneAB;
 6using System;
 7using System.Threading;
 8using UnityEngine;
 9
 10namespace AvatarSystem
 11{
 12    public class WearableRetriever : IWearableRetriever
 13    {
 14        private const string FEATURE_NEW_ASSET_BUNDLES = "ab-new-cdn";
 1215        public Rendereable rendereable { get; private set; }
 16
 17        private RendereableAssetLoadHelper loaderAssetHelper;
 18
 19        public async UniTask<Rendereable> Retrieve(GameObject container, WearableItem wearable, string bodyShapeId, Canc
 20        {
 221            ct.ThrowIfCancellationRequested();
 22
 223            WearableItem.Representation representation = wearable.GetRepresentation(bodyShapeId);
 224            if (representation == null)
 25            {
 026                Debug.Log($"No representation for body shape {bodyShapeId} of wearable {wearable.id}");
 027                return null;
 28            }
 29
 230            var contentProvider = wearable.GetContentProvider(bodyShapeId);
 231            string baseUrl = wearable.baseUrlBundles;
 232            string mainFile = representation.mainFile;
 33
 34            try
 35            {
 236                loaderAssetHelper?.Unload();
 37
 38                // Before loading the asset, we check if asset bundles exist, then we fill the content provider with it
 239                if (IsNewAssetBundleFlagEnabled())
 40                {
 041                    if (string.IsNullOrEmpty(wearable.entityId))
 42                    {
 43                        //Debug.LogWarning(mainFile + " has no entity ID, ignore this message if you are on a testnet");
 44                    }
 45                    else
 46                    {
 047                        var sceneAb = await FetchSceneAssetBundles(wearable.entityId, contentProvider.assetBundlesBaseUr
 48
 049                        if (sceneAb != null && sceneAb.IsSceneConverted())
 50                        {
 051                            contentProvider.assetBundles = sceneAb.GetConvertedFiles();
 052                            contentProvider.assetBundlesBaseUrl = sceneAb.GetBaseUrl();
 053                            contentProvider.assetBundlesVersion = sceneAb.GetVersion();
 54                        }
 55#if UNITY_EDITOR
 56                        else
 57                        {
 058                            Debug.Log($"<color=red>Wearable AB FAILED -> {mainFile} {wearable.entityId} use this ID to r
 59                        }
 60#endif
 61
 062                        contentProvider.assetBundlesFetched = true;
 63                    }
 64
 65                    // even if it we not fetch the asset bundle because the wearable has no ID, we set this to true to a
 066                    contentProvider.assetBundlesFetched = true;
 67                }
 68
 269                loaderAssetHelper = new RendereableAssetLoadHelper(contentProvider, baseUrl);
 70
 271                loaderAssetHelper.settings.forceNewInstance = false;
 72                // TODO Review this hardcoded offset and try to solve it by offseting the Avatar container
 273                loaderAssetHelper.settings.initialLocalPosition = Vector3.up * AvatarSystemUtils.AVATAR_Y_OFFSET;
 274                loaderAssetHelper.settings.visibleFlags = AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE;
 275                loaderAssetHelper.settings.parent = container.transform;
 276                loaderAssetHelper.settings.layer = container.layer;
 77
 278                bool done = false;
 279                Exception exception = null;
 80
 81                void OnSuccessWrapper(Rendereable rendereable)
 82                {
 283                    loaderAssetHelper?.ClearEvents();
 284                    done = true;
 285                    this.rendereable = rendereable;
 286                }
 87
 88                void OnFailEventWrapper(Exception e)
 89                {
 090                    exception = e;
 091                    loaderAssetHelper?.ClearEvents();
 092                    done = true;
 093                    rendereable = null;
 094                }
 95
 296                loaderAssetHelper.OnSuccessEvent += OnSuccessWrapper;
 297                loaderAssetHelper.OnFailEvent += OnFailEventWrapper;
 298                loaderAssetHelper.Load(mainFile, AvatarSystemUtils.UseAssetBundles() ? RendereableAssetLoadHelper.Loadin
 99
 100                // AttachExternalCancellation is needed because a cancelled WaitUntil UniTask requires a frame
 6101                await UniTaskUtils.WaitForBoolean(ref done, cancellationToken: ct).AttachExternalCancellation(ct);
 102
 2103                if (exception != null)
 0104                    throw exception;
 105
 2106                if (rendereable == null)
 0107                    throw new Exception($"Couldnt retrieve Wearable assets at: {mainFile}");
 108
 2109                return rendereable;
 110            }
 0111            catch (OperationCanceledException e)
 112            {
 0113                Dispose();
 0114                throw;
 115            }
 2116        }
 117
 118        private bool IsNewAssetBundleFlagEnabled() =>
 2119            DataStore.i.featureFlags.flags.Get().IsFeatureEnabled(FEATURE_NEW_ASSET_BUNDLES);
 120
 121        private async UniTask<Asset_SceneAB> FetchSceneAssetBundles(string sceneId, string dataBaseUrlBundles)
 122        {
 0123            AssetPromise_SceneAB promiseSceneAb = new AssetPromise_SceneAB(dataBaseUrlBundles, sceneId);
 0124            AssetPromiseKeeper_SceneAB.i.Keep(promiseSceneAb);
 0125            await promiseSceneAb.ToUniTask();
 0126            return promiseSceneAb.asset;
 0127        }
 128
 2129        public void Dispose() { loaderAssetHelper?.Unload(); }
 130    }
 131}