< Summary

Class:DCL.AssetPromise_GLTFast_Instance
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTFast/AssetPromise_GLTFast_Instance.cs
Covered lines:62
Uncovered lines:17
Coverable lines:79
Total lines:188
Line coverage:78.4% (62 of 79)
Covered branches:0
Total branches:0
Covered methods:13
Total methods:15
Method coverage:86.6% (13 of 15)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_GLTFast_Instance(...)0%330100%
OnLoad(...)0%110100%
AddToLibrary()0%3.073080%
OnReuse(...)0%110100%
OnAfterLoadOrReuse()0%110100%
OnBeforeLoadOrReuse()0%110100%
OnCancelLoading()0%330100%
ToString()0%6200%
LoadingCoroutine()0%14.213080.77%
SetupAssetSettings()0%330100%
RemoveCollidersFromRenderers()0%440100%
IsCollider(...)0%110100%
GetAsset(...)0%220100%
OverrideInitialPosition(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/GLTFast/AssetPromise_GLTFast_Instance.cs

#LineLine coverage
 1using Cysharp.Threading.Tasks;
 2using System;
 3using System.Collections;
 4using DCL.Helpers;
 5using UnityEngine;
 6using MainScripts.DCL.Analytics.PerformanceAnalytics;
 7using Object = UnityEngine.Object;
 8
 9namespace DCL
 10{
 11    public class AssetPromise_GLTFast_Instance : AssetPromise_WithUrl<Asset_GLTFast_Instance>
 12    {
 13        private readonly IWebRequestController webRequestController;
 14        private readonly ContentProvider contentProvider;
 15        private readonly AssetPromiseSettings_Rendering settings;
 16        private AssetPromise_GLTFast_Loader subPromise;
 17        private Coroutine loadingCoroutine;
 18
 19        public AssetPromise_GLTFast_Instance(string contentUrl, string hash, IWebRequestController webRequestController,
 11020            : base(contentUrl, hash)
 21        {
 11022            this.webRequestController = webRequestController;
 11023            this.contentProvider = contentProvider ?? new ContentProvider_Dummy();
 11024            this.settings = settings ?? new AssetPromiseSettings_Rendering();
 11025        }
 26
 27        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 28        {
 10329            loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(onSuccess, onFail));
 10330        }
 31
 32        protected override bool AddToLibrary()
 33        {
 9434            if (!library.Add(asset))
 035                return false;
 36
 9437            asset = settings.forceNewInstance ? ((AssetLibrary_GLTFast_Instance)library).GetCopyFromOriginal(asset.id) :
 38
 9439            settings.ApplyBeforeLoad(asset.container.transform);
 40
 9441            return true;
 42        }
 43
 44        protected override void OnReuse(Action onSuccess)
 45        {
 746            asset.Show(onSuccess);
 747        }
 48
 49        protected override void OnAfterLoadOrReuse()
 50        {
 10151            settings.ApplyAfterLoad(asset.container.transform);
 10152        }
 53
 54        protected override void OnBeforeLoadOrReuse()
 55        {
 11056            settings.ApplyBeforeLoad(asset.container.transform);
 11057        }
 58
 59        protected override void OnCancelLoading()
 60        {
 961            if (loadingCoroutine != null)
 62            {
 963                PerformanceAnalytics.GLTFTracker.TrackCancelled();
 964                CoroutineStarter.Stop(loadingCoroutine);
 965                loadingCoroutine = null;
 66            }
 67
 968            if (asset != null)
 969                Object.Destroy(asset.container);
 70
 971            AssetPromiseKeeper_GLTFast.i.Forget(subPromise);
 972        }
 73
 74        public override string ToString()
 75        {
 076            if (subPromise != null)
 077                return $"{subPromise.ToString()} ... GLTFast_GameObject state = {state}";
 78            else
 079                return $"subPromise == null? state = {state}";
 80        }
 81
 21982        public override bool keepWaiting => loadingCoroutine != null;
 83
 84        private IEnumerator LoadingCoroutine(Action OnSuccess, Action<Exception> OnFail)
 85        {
 10386            PerformanceAnalytics.GLTFTracker.TrackLoading();
 87
 88            // Since GLTFast give us an "instantiator" we create another asset promise to create the main and only insta
 10389            subPromise = new AssetPromise_GLTFast_Loader(contentUrl, hash, webRequestController, contentProvider);
 90
 10391            var success = false;
 10392            Exception loadingException = null;
 93
 20394            subPromise.OnSuccessEvent += (x) => success = true;
 95
 10396            subPromise.OnFailEvent += (ab, exception) =>
 97            {
 098                loadingException = exception;
 099                success = false;
 0100            };
 101
 103102            asset.ownerPromise = subPromise;
 103103            AssetPromiseKeeper_GLTFast.i.Keep(subPromise);
 104
 103105            asset.container.SetActive(false);
 106
 103107            yield return subPromise;
 108
 100109            if (success)
 110            {
 100111                if (!asset.container)
 112                {
 0113                    OnFail?.Invoke(new Exception("Object was destroyed during loading"));
 0114                    yield break;
 115                }
 116
 100117                yield return subPromise.asset.InstantiateAsync(asset.container.transform).ToCoroutine(e =>
 118                {
 0119                    if (e is not OperationCanceledException)
 0120                        throw e;
 0121                });
 98122                yield return RemoveCollidersFromRenderers(asset.container.transform);
 123            }
 124
 94125            SetupAssetSettings();
 126
 94127            asset.container.SetActive(true);
 128
 94129            loadingCoroutine = null;
 130
 94131            if (success)
 132            {
 94133                PerformanceAnalytics.GLTFTracker.TrackLoaded();
 94134                OnSuccess?.Invoke();
 135            }
 136            else
 137            {
 0138                PerformanceAnalytics.GLTFTracker.TrackFailed();
 0139                loadingException ??= new Exception($"GLTFast sub-promise asset or container is null. Asset: {subPromise.
 0140                OnFail?.Invoke(loadingException);
 141            }
 94142        }
 143
 144        private void SetupAssetSettings()
 145        {
 94146            if (settings.visibleFlags is AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE)
 147            {
 1148                var renderers = asset.container.GetComponentsInChildren<Renderer>(true);
 149
 6150                foreach (Renderer renderer in renderers)
 2151                    renderer.enabled = false;
 152            }
 94153        }
 154
 155        /// <summary>
 156        /// Our GLTFs come with visible colliders that we should get rid of, this is a requirement for our systems
 157        /// </summary>
 158        /// <param name="rootGameObject"></param>
 159        /// <returns></returns>
 160        private IEnumerator RemoveCollidersFromRenderers(Transform rootGameObject)
 161        {
 98162            Utils.InverseTransformChildTraversal<Renderer>(renderer =>
 163            {
 269164                if (IsCollider(renderer.transform))
 40165                    Utils.SafeDestroy(renderer);
 269166            }, rootGameObject.transform);
 167
 168            // we wait a single frame until the collider renderers are deleted because some systems might be able to get
 98169            yield return null;
 94170        }
 171
 172        private static bool IsCollider(Transform transform)
 173        {
 269174            bool transformName = transform.name.ToLower().Contains("_collider");
 269175            bool parentName = transform.parent.name.ToLower().Contains("_collider");
 176
 269177            return parentName || transformName;
 178        }
 179
 180        protected override Asset_GLTFast_Instance GetAsset(object id) =>
 7181            settings.forceNewInstance ? ((AssetLibrary_GLTFast_Instance)library).GetCopyFromOriginal(id) : base.GetAsset
 182
 183        public void OverrideInitialPosition(Vector3 pos)
 184        {
 0185            settings.initialLocalPosition = pos;
 0186        }
 187    }
 188}