< Summary

Class:DCL.AssetPromise_AB_GameObject
Assembly:AssetPromiseKeeper
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/AB_GameObject/AssetPromise_AB_GameObject.cs
Covered lines:64
Uncovered lines:5
Coverable lines:69
Total lines:176
Line coverage:92.7% (64 of 69)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
AssetPromise_AB_GameObject(...)0%110100%
OnLoad(...)0%110100%
AddToLibrary()0%3.023087.5%
OnReuse(...)0%110100%
OnAfterLoadOrReuse()0%110100%
OnBeforeLoadOrReuse()0%110100%
OnCancelLoading()0%330100%
ToString()0%6200%
LoadingCoroutine()0%10100100%
InstantiateABGameObjects()0%10.0510092%
GetAsset(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/AssetManager/AssetBundles/AB_GameObject/AssetPromise_AB_GameObject.cs

#LineLine coverage
 1using System;
 2using System.Collections;
 3using System.Linq;
 4using DCL.Helpers;
 5using DCL.Configuration;
 6using UnityEngine;
 7using System.Collections.Generic;
 8
 9namespace DCL
 10{
 11    public class AssetPromise_AB_GameObject : AssetPromise_WithUrl<Asset_AB_GameObject>
 12    {
 3913        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 14        AssetPromise_AB subPromise;
 15        Coroutine loadingCoroutine;
 16
 3917        public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash)
 18        {
 3919        }
 20
 21        protected override void OnLoad(Action OnSuccess, Action OnFail)
 22        {
 1523            loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(OnSuccess, OnFail));
 1524        }
 25
 26        protected override bool AddToLibrary()
 27        {
 1028            if (!library.Add(asset))
 29            {
 030                return false;
 31            }
 32
 1033            if (settings.forceNewInstance)
 34            {
 235                asset = (library as AssetLibrary_AB_GameObject).GetCopyFromOriginal(asset.id);
 236            }
 37            else
 38            {
 839                asset = library.Get(asset.id);
 40            }
 41
 42            //NOTE(Brian): Call again this method because we are replacing the asset.
 1043            settings.ApplyBeforeLoad(asset.container.transform);
 44
 1045            return true;
 46        }
 47
 48        protected override void OnReuse(Action OnSuccess)
 49        {
 2450            asset.Show(OnSuccess);
 2451        }
 52
 53        protected override void OnAfterLoadOrReuse()
 54        {
 3455            settings.ApplyAfterLoad(asset.container.transform);
 3456        }
 57
 58        protected override void OnBeforeLoadOrReuse()
 59        {
 60#if UNITY_EDITOR
 3961            asset.container.name = "AB: " + hash;
 62#endif
 3963            settings.ApplyBeforeLoad(asset.container.transform);
 3964        }
 65
 66        protected override void OnCancelLoading()
 67        {
 568            if (loadingCoroutine != null)
 69            {
 570                CoroutineStarter.Stop(loadingCoroutine);
 571                loadingCoroutine = null;
 72            }
 73
 574            if (asset != null)
 575                UnityEngine.Object.Destroy(asset.container);
 76
 577            AssetPromiseKeeper_AB.i.Forget(subPromise);
 578        }
 79
 80
 81        public override string ToString()
 82        {
 083            if (subPromise != null)
 084                return $"{subPromise.ToString()} ... AB_GameObject state = {state}";
 85            else
 086                return $"subPromise == null? state = {state}";
 87        }
 88
 89        public IEnumerator LoadingCoroutine(Action OnSuccess, Action OnFail)
 90        {
 1591            subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform);
 1592            bool success = false;
 2793            subPromise.OnSuccessEvent += (x) => success = true;
 1594            asset.ownerPromise = subPromise;
 1595            AssetPromiseKeeper_AB.i.Keep(subPromise);
 96
 1597            yield return subPromise;
 98
 1399            if (success)
 100            {
 12101                yield return InstantiateABGameObjects();
 102
 12103                if (subPromise.asset == null || asset.container == null)
 2104                    success = false;
 105            }
 106
 13107            if (success)
 108            {
 10109                OnSuccess?.Invoke();
 10110            }
 111            else
 112            {
 3113                OnFail?.Invoke();
 114            }
 13115        }
 116
 117
 118        public IEnumerator InstantiateABGameObjects()
 119        {
 12120            var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf");
 121
 12122            if (goList.Count == 0)
 123            {
 1124                if (asset.container != null)
 1125                    UnityEngine.Object.Destroy(asset.container);
 126
 1127                asset.container = null;
 128
 1129                AssetPromiseKeeper_AB.i.Forget(subPromise);
 130
 1131                yield break;
 132            }
 133
 42134            for (int i = 0; i < goList.Count; i++)
 135            {
 11136                if (loadingCoroutine == null)
 137                    break;
 138
 11139                if (asset.container == null)
 140                    break;
 141
 10142                GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform);
 10143                var list = new List<Renderer>(assetBundleModelGO.GetComponentsInChildren<Renderer>(true));
 144
 145                //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad
 10146                yield return MaterialCachingHelper.Process(list, enableRenderers: false, settings.cachingFlags);
 147
 10148                var animators = assetBundleModelGO.GetComponentsInChildren<Animation>(true);
 149
 20150                for (int animIndex = 0; animIndex < animators.Length; animIndex++)
 151                {
 0152                    animators[animIndex].cullingType = AnimationCullingType.AlwaysAnimate;
 153                }
 154
 155#if UNITY_EDITOR
 10156                assetBundleModelGO.name = subPromise.asset.assetBundleAssetName;
 157#endif
 158                //assetBundleModelGO.transform.SetParent(asset.container.transform, false);
 10159                assetBundleModelGO.transform.ResetLocalTRS();
 10160                yield return null;
 10161            }
 11162        }
 163
 164        protected override Asset_AB_GameObject GetAsset(object id)
 165        {
 24166            if (settings.forceNewInstance)
 167            {
 9168                return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id);
 169            }
 170            else
 171            {
 15172                return base.GetAsset(id);
 173            }
 174        }
 175    }
 176}