< 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:72
Uncovered lines:7
Coverable lines:79
Total lines:191
Line coverage:91.1% (72 of 79)
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 DCL.Configuration;
 6using UnityEngine;
 7using System.Collections.Generic;
 8using DCL.Models;
 9
 10namespace DCL
 11{
 12    public class AssetPromise_AB_GameObject : AssetPromise_WithUrl<Asset_AB_GameObject>
 13    {
 4114        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 15        AssetPromise_AB subPromise;
 16        Coroutine loadingCoroutine;
 17
 8218        public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash) { }
 19
 3220        protected override void OnLoad(Action OnSuccess, Action OnFail) { loadingCoroutine = CoroutineStarter.Start(Load
 21
 22        protected override bool AddToLibrary()
 23        {
 1124            if (!library.Add(asset))
 25            {
 026                return false;
 27            }
 28
 1129            if (settings.forceNewInstance)
 30            {
 231                asset = (library as AssetLibrary_AB_GameObject).GetCopyFromOriginal(asset.id);
 232            }
 33            else
 34            {
 935                asset = library.Get(asset.id);
 36            }
 37
 38            //NOTE(Brian): Call again this method because we are replacing the asset.
 1139            settings.ApplyBeforeLoad(asset.container.transform);
 40
 1141            return true;
 42        }
 43
 44        protected override void OnReuse(Action OnSuccess)
 45        {
 2546            asset.renderers = asset.container.GetComponentsInChildren<Renderer>(true).ToList();
 2547            asset.Show(OnSuccess);
 2548        }
 49
 7250        protected override void OnAfterLoadOrReuse() { settings.ApplyAfterLoad(asset.container.transform); }
 51
 52        protected override void OnBeforeLoadOrReuse()
 53        {
 54#if UNITY_EDITOR
 4155            asset.container.name = "AB: " + hash;
 56#endif
 4157            settings.ApplyBeforeLoad(asset.container.transform);
 4158        }
 59
 60        protected override void OnCancelLoading()
 61        {
 562            if (loadingCoroutine != null)
 63            {
 564                CoroutineStarter.Stop(loadingCoroutine);
 565                loadingCoroutine = null;
 66            }
 67
 568            if (asset != null)
 569                UnityEngine.Object.Destroy(asset.container);
 70
 571            AssetPromiseKeeper_AB.i.Forget(subPromise);
 572        }
 73
 74        public override string ToString()
 75        {
 076            if (subPromise != null)
 077                return $"{subPromise.ToString()} ... AB_GameObject state = {state}";
 78            else
 079                return $"subPromise == null? state = {state}";
 80        }
 81
 82        public IEnumerator LoadingCoroutine(Action OnSuccess, Action OnFail)
 83        {
 1684            subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform);
 1685            bool success = false;
 2986            subPromise.OnSuccessEvent += (x) => success = true;
 1687            asset.ownerPromise = subPromise;
 1688            AssetPromiseKeeper_AB.i.Keep(subPromise);
 89
 1690            yield return subPromise;
 91
 1492            if (success)
 93            {
 1394                yield return InstantiateABGameObjects();
 95
 1396                if (subPromise.asset == null || asset.container == null)
 297                    success = false;
 98            }
 99
 14100            if (success)
 101            {
 11102                OnSuccess?.Invoke();
 11103            }
 104            else
 105            {
 3106                OnFail?.Invoke();
 107            }
 14108        }
 109
 110        public IEnumerator InstantiateABGameObjects()
 111        {
 13112            var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf");
 113
 13114            if (goList.Count == 0)
 115            {
 1116                if (asset.container != null)
 1117                    UnityEngine.Object.Destroy(asset.container);
 118
 1119                asset.container = null;
 120
 1121                AssetPromiseKeeper_AB.i.Forget(subPromise);
 122
 1123                yield break;
 124            }
 125
 46126            for (int i = 0; i < goList.Count; i++)
 127            {
 12128                if (loadingCoroutine == null)
 129                    break;
 130
 12131                if (asset.container == null)
 132                    break;
 133
 11134                GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform);
 135
 11136                List<Renderer> rendererList = assetBundleModelGO.GetComponentsInChildren<Renderer>(true).ToList();
 137
 11138                asset.renderers.AddRange(rendererList);
 11139                UploadMeshesToGPU(MeshesInfoUtils.ExtractMeshes(assetBundleModelGO));
 11140                asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo
 141
 142                //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad
 11143                yield return MaterialCachingHelper.Process(rendererList, enableRenderers: false, settings.cachingFlags);
 144
 11145                var animators = assetBundleModelGO.GetComponentsInChildren<Animation>(true);
 146
 22147                for (int animIndex = 0; animIndex < animators.Length; animIndex++)
 148                {
 0149                    animators[animIndex].cullingType = AnimationCullingType.AlwaysAnimate;
 150                }
 151
 152#if UNITY_EDITOR
 11153                assetBundleModelGO.name = subPromise.asset.assetBundleAssetName;
 154#endif
 155                //assetBundleModelGO.transform.SetParent(asset.container.transform, false);
 11156                assetBundleModelGO.transform.ResetLocalTRS();
 11157                yield return null;
 11158            }
 12159        }
 160
 161        private void UploadMeshesToGPU(List<Mesh> meshesList)
 162        {
 11163            var uploadToGPU = DataStore.i.featureFlags.flags.Get().IsFeatureEnabled(FeatureFlag.GPU_ONLY_MESHES);
 66164            foreach ( Mesh mesh in meshesList )
 165            {
 22166                if ( !mesh.isReadable )
 167                    continue;
 168
 22169                asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 22170                asset.meshes.Add(mesh);
 22171                if (uploadToGPU)
 172                {
 0173                    Physics.BakeMesh(mesh.GetInstanceID(), false);
 0174                    mesh.UploadMeshData(true);
 175                }
 176            }
 11177        }
 178
 179        protected override Asset_AB_GameObject GetAsset(object id)
 180        {
 25181            if (settings.forceNewInstance)
 182            {
 9183                return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id);
 184            }
 185            else
 186            {
 16187                return base.GetAsset(id);
 188            }
 189        }
 190    }
 191}