< 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:0
Uncovered lines:72
Coverable lines:72
Total lines:178
Line coverage:0% (0 of 72)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_GLTFast_Instance(...)0%12300%
OnLoad(...)0%2100%
AddToLibrary()0%12300%
OnReuse(...)0%2100%
OnAfterLoadOrReuse()0%2100%
OnBeforeLoadOrReuse()0%2100%
OnCancelLoading()0%12300%
ToString()0%6200%
LoadingCoroutine()0%1321100%
SetupAssetSettings()0%12300%
RemoveCollidersFromRenderers()0%20400%
IsCollider(...)0%2100%
GetAsset(...)0%6200%
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,
 020            : base(contentUrl, hash)
 21        {
 022            this.webRequestController = webRequestController;
 023            this.contentProvider = contentProvider ?? new ContentProvider_Dummy();
 024            this.settings = settings ?? new AssetPromiseSettings_Rendering();
 025        }
 26
 27        protected override void OnLoad(Action onSuccess, Action<Exception> onFail)
 28        {
 029            loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(onSuccess, onFail));
 030        }
 31
 32        protected override bool AddToLibrary()
 33        {
 034            if (!library.Add(asset))
 035                return false;
 36
 037            asset = settings.forceNewInstance ? ((AssetLibrary_GLTFast_Instance)library).GetCopyFromOriginal(asset.id) :
 38
 039            settings.ApplyBeforeLoad(asset.container.transform);
 40
 041            return true;
 42        }
 43
 44        protected override void OnReuse(Action onSuccess)
 45        {
 046            asset.Show(onSuccess);
 047        }
 48
 49        protected override void OnAfterLoadOrReuse()
 50        {
 051            settings.ApplyAfterLoad(asset.container.transform);
 052        }
 53
 54        protected override void OnBeforeLoadOrReuse()
 55        {
 056            settings.ApplyBeforeLoad(asset.container.transform);
 057        }
 58
 59        protected override void OnCancelLoading()
 60        {
 061            if (loadingCoroutine != null)
 62            {
 063                PerformanceAnalytics.GLTFTracker.TrackCancelled();
 064                CoroutineStarter.Stop(loadingCoroutine);
 065                loadingCoroutine = null;
 66            }
 67
 068            if (asset != null)
 069                Object.Destroy(asset.container);
 70
 071            AssetPromiseKeeper_GLTFast.i.Forget(subPromise);
 072        }
 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
 82        private IEnumerator LoadingCoroutine(Action OnSuccess, Action<Exception> OnFail)
 83        {
 084            PerformanceAnalytics.GLTFTracker.TrackLoading();
 85
 86            // Since GLTFast give us an "instantiator" we create another asset promise to create the main and only insta
 087            subPromise = new AssetPromise_GLTFast_Loader(contentUrl, hash, webRequestController, contentProvider);
 88
 089            var success = false;
 090            Exception loadingException = null;
 91
 092            subPromise.OnSuccessEvent += (x) => success = true;
 93
 094            subPromise.OnFailEvent += (ab, exception) =>
 95            {
 096                loadingException = exception;
 097                success = false;
 098            };
 99
 0100            asset.ownerPromise = subPromise;
 0101            AssetPromiseKeeper_GLTFast.i.Keep(subPromise);
 102
 0103            asset.container.SetActive(false);
 104
 0105            yield return subPromise;
 106
 0107            if (success)
 108            {
 0109                if (asset.container == null) { Debug.LogError("this should not happen"); }
 110                else
 111                {
 0112                    yield return subPromise.asset.InstantiateAsync(asset.container.transform).ToCoroutine();
 0113                    yield return RemoveCollidersFromRenderers(asset.container.transform);
 114                }
 115            }
 116
 0117            SetupAssetSettings();
 118
 0119            asset.container.SetActive(true);
 120
 0121            loadingCoroutine = null;
 122
 0123            if (success)
 124            {
 0125                PerformanceAnalytics.GLTFTracker.TrackLoaded();
 0126                OnSuccess?.Invoke();
 127            }
 128            else
 129            {
 0130                PerformanceAnalytics.GLTFTracker.TrackFailed();
 0131                loadingException ??= new Exception($"GLTFast sub-promise asset or container is null. Asset: {subPromise.
 0132                OnFail?.Invoke(loadingException);
 133            }
 0134        }
 135
 136        private void SetupAssetSettings()
 137        {
 0138            if (settings.visibleFlags is AssetPromiseSettings_Rendering.VisibleFlags.INVISIBLE)
 139            {
 0140                var renderers = asset.container.GetComponentsInChildren<Renderer>(true);
 141
 0142                foreach (Renderer renderer in renderers)
 0143                    renderer.enabled = false;
 144            }
 0145        }
 146
 147        /// <summary>
 148        /// Our GLTFs come with visible colliders that we should get rid of, this is a requirement for our systems
 149        /// </summary>
 150        /// <param name="rootGameObject"></param>
 151        /// <returns></returns>
 152        private IEnumerator RemoveCollidersFromRenderers(Transform rootGameObject)
 153        {
 0154            Utils.InverseTransformChildTraversal<Renderer>(renderer =>
 155            {
 0156                if (IsCollider(renderer.transform)) { Object.Destroy(renderer); }
 0157            }, rootGameObject.transform);
 158
 159            // we wait a single frame until the collider renderers are deleted because some systems might be able to get
 0160            yield return new WaitForEndOfFrame();
 0161        }
 162
 163        private static bool IsCollider(Transform transform)
 164        {
 0165            bool transformName = transform.name.ToLower().Contains("_collider");
 0166            bool parentName = transform.parent.name.ToLower().Contains("_collider");
 167
 0168            return parentName || transformName;
 169        }
 170
 0171        protected override Asset_GLTFast_Instance GetAsset(object id) => settings.forceNewInstance ? ((AssetLibrary_GLTF
 172
 173        public void OverrideInitialPosition(Vector3 pos)
 174        {
 0175            settings.initialLocalPosition = pos;
 0176        }
 177    }
 178}