< 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:77
Uncovered lines:7
Coverable lines:84
Total lines:198
Line coverage:91.6% (77 of 84)
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.0310093.1%
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 = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 2551            asset.Show(OnSuccess);
 2552        }
 53
 54        protected override void OnAfterLoadOrReuse()
 55        {
 3656            asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 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                asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(assetBundleModelGO);
 11145                asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers);
 11146                asset.textures = MeshesInfoUtils.ExtractUniqueTextures(asset.materials);
 11147                UploadMeshesToGPU(MeshesInfoUtils.ExtractUniqueMeshes(asset.renderers));
 11148                asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo
 149
 150                //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad
 11151                yield return MaterialCachingHelper.Process(asset.renderers.ToList(), enableRenderers: false, settings.ca
 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
 11163                assetBundleModelGO.transform.ResetLocalTRS();
 11164                yield return null;
 11165            }
 12166        }
 167
 168        private void UploadMeshesToGPU(HashSet<Mesh> meshesList)
 169        {
 11170            var uploadToGPU = DataStore.i.featureFlags.flags.Get().IsFeatureEnabled(FeatureFlag.GPU_ONLY_MESHES);
 44171            foreach ( Mesh mesh in meshesList )
 172            {
 11173                if ( !mesh.isReadable )
 174                    continue;
 175
 11176                asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 11177                asset.meshes.Add(mesh);
 11178                if (uploadToGPU)
 179                {
 0180                    Physics.BakeMesh(mesh.GetInstanceID(), false);
 0181                    mesh.UploadMeshData(true);
 182                }
 183            }
 11184        }
 185
 186        protected override Asset_AB_GameObject GetAsset(object id)
 187        {
 25188            if (settings.forceNewInstance)
 189            {
 9190                return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id);
 191            }
 192            else
 193            {
 16194                return base.GetAsset(id);
 195            }
 196        }
 197    }
 198}