< 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:76
Uncovered lines:7
Coverable lines:83
Total lines:199
Line coverage:91.5% (76 of 83)
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.0410092.86%
UploadMeshesToGPU(...)0%4.14081.82%
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 UnityEngine;
 6using System.Collections.Generic;
 7using DCL.Models;
 8
 9namespace DCL
 10{
 11    public class AssetPromise_AB_GameObject : AssetPromise_WithUrl<Asset_AB_GameObject>
 12    {
 4113        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 14        AssetPromise_AB subPromise;
 15        Coroutine loadingCoroutine;
 16
 4117        public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash)
 18        {
 4119        }
 20
 21        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail)
 22        {
 1623            loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(OnSuccess, OnFail));
 1624        }
 25
 26        protected override bool AddToLibrary()
 27        {
 1128            if (!library.Add(asset))
 29            {
 030                return false;
 31            }
 32
 1133            if (settings.forceNewInstance)
 34            {
 235                asset = (library as AssetLibrary_AB_GameObject).GetCopyFromOriginal(asset.id);
 236            }
 37            else
 38            {
 939                asset = library.Get(asset.id);
 40            }
 41
 42            //NOTE(Brian): Call again this method because we are replacing the asset.
 1143            settings.ApplyBeforeLoad(asset.container.transform);
 44
 1145            return true;
 46        }
 47
 48        protected override void OnReuse(Action OnSuccess)
 49        {
 2550            asset.renderers = asset.container.GetComponentsInChildren<Renderer>(true).ToList();
 2551            asset.Show(OnSuccess);
 2552        }
 53
 54        protected override void OnAfterLoadOrReuse()
 55        {
 3656            asset.renderers = asset.container.GetComponentsInChildren<Renderer>(true).ToList();
 3657            settings.ApplyAfterLoad(asset.container.transform);
 3658        }
 59
 60        protected override void OnBeforeLoadOrReuse()
 61        {
 62#if UNITY_EDITOR
 4163            asset.container.name = "AB: " + hash;
 64#endif
 4165            settings.ApplyBeforeLoad(asset.container.transform);
 4166        }
 67
 68        protected override void OnCancelLoading()
 69        {
 570            if (loadingCoroutine != null)
 71            {
 572                CoroutineStarter.Stop(loadingCoroutine);
 573                loadingCoroutine = null;
 74            }
 75
 576            if (asset != null)
 577                UnityEngine.Object.Destroy(asset.container);
 78
 579            AssetPromiseKeeper_AB.i.Forget(subPromise);
 580        }
 81
 82        public override string ToString()
 83        {
 084            if (subPromise != null)
 085                return $"{subPromise.ToString()} ... AB_GameObject state = {state}";
 86            else
 087                return $"subPromise == null? state = {state}";
 88        }
 89
 90        public IEnumerator LoadingCoroutine(Action OnSuccess, Action<Exception> OnFail)
 91        {
 1692            subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform);
 1693            bool success = false;
 2994            subPromise.OnSuccessEvent += (x) => success = true;
 1695            asset.ownerPromise = subPromise;
 1696            AssetPromiseKeeper_AB.i.Keep(subPromise);
 97
 1698            yield return subPromise;
 99
 14100            if (success)
 101            {
 13102                yield return InstantiateABGameObjects();
 103
 13104                if (subPromise.asset == null || asset.container == null)
 2105                    success = false;
 106            }
 107
 14108            if (success)
 109            {
 11110                OnSuccess?.Invoke();
 11111            }
 112            else
 113            {
 3114                OnFail?.Invoke(new Exception($"AB sub-promise asset or container is null. Asset: {subPromise.asset}, con
 115            }
 14116        }
 117
 118        public IEnumerator InstantiateABGameObjects()
 119        {
 13120            var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf");
 121
 13122            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
 46134            for (int i = 0; i < goList.Count; i++)
 135            {
 12136                if (loadingCoroutine == null)
 137                    break;
 138
 12139                if (asset.container == null)
 140                    break;
 141
 11142                GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform);
 143
 11144                List<Renderer> rendererList = assetBundleModelGO.GetComponentsInChildren<Renderer>(true).ToList();
 145
 11146                asset.renderers.AddRange(rendererList);
 11147                UploadMeshesToGPU(MeshesInfoUtils.ExtractMeshes(assetBundleModelGO));
 11148                asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo
 149
 150                //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad
 11151                yield return MaterialCachingHelper.Process(rendererList, enableRenderers: false, settings.cachingFlags);
 152
 11153                var animators = assetBundleModelGO.GetComponentsInChildren<Animation>(true);
 154
 22155                for (int animIndex = 0; animIndex < animators.Length; animIndex++)
 156                {
 0157                    animators[animIndex].cullingType = AnimationCullingType.AlwaysAnimate;
 158                }
 159
 160#if UNITY_EDITOR
 11161                assetBundleModelGO.name = subPromise.asset.assetBundleAssetName;
 162#endif
 163                //assetBundleModelGO.transform.SetParent(asset.container.transform, false);
 11164                assetBundleModelGO.transform.ResetLocalTRS();
 11165                yield return null;
 11166            }
 12167        }
 168
 169        private void UploadMeshesToGPU(List<Mesh> meshesList)
 170        {
 11171            var uploadToGPU = DataStore.i.featureFlags.flags.Get().IsFeatureEnabled(FeatureFlag.GPU_ONLY_MESHES);
 66172            foreach ( Mesh mesh in meshesList )
 173            {
 22174                if ( !mesh.isReadable )
 175                    continue;
 176
 22177                asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 22178                asset.meshes.Add(mesh);
 22179                if (uploadToGPU)
 180                {
 0181                    Physics.BakeMesh(mesh.GetInstanceID(), false);
 0182                    mesh.UploadMeshData(true);
 183                }
 184            }
 11185        }
 186
 187        protected override Asset_AB_GameObject GetAsset(object id)
 188        {
 25189            if (settings.forceNewInstance)
 190            {
 9191                return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id);
 192            }
 193            else
 194            {
 16195                return base.GetAsset(id);
 196            }
 197        }
 198    }
 199}