< 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:75
Uncovered lines:5
Coverable lines:80
Total lines:197
Line coverage:93.7% (75 of 80)
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%330100%
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    {
 3914        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 15        AssetPromise_AB subPromise;
 16        Coroutine loadingCoroutine;
 17
 3918        public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash)
 19        {
 3920        }
 21
 22        protected override void OnLoad(Action OnSuccess, Action OnFail)
 23        {
 1524            loadingCoroutine = CoroutineStarter.Start(LoadingCoroutine(OnSuccess, OnFail));
 1525        }
 26
 27        protected override bool AddToLibrary()
 28        {
 1029            if (!library.Add(asset))
 30            {
 031                return false;
 32            }
 33
 1034            if (settings.forceNewInstance)
 35            {
 236                asset = (library as AssetLibrary_AB_GameObject).GetCopyFromOriginal(asset.id);
 237            }
 38            else
 39            {
 840                asset = library.Get(asset.id);
 41            }
 42
 43            //NOTE(Brian): Call again this method because we are replacing the asset.
 1044            settings.ApplyBeforeLoad(asset.container.transform);
 45
 1046            return true;
 47        }
 48
 49        protected override void OnReuse(Action OnSuccess)
 50        {
 2451            asset.renderers = asset.container.GetComponentsInChildren<Renderer>(true).ToList();
 2452            asset.Show(OnSuccess);
 2453        }
 54
 55        protected override void OnAfterLoadOrReuse()
 56        {
 3457            settings.ApplyAfterLoad(asset.container.transform);
 3458        }
 59
 60        protected override void OnBeforeLoadOrReuse()
 61        {
 62#if UNITY_EDITOR
 3963            asset.container.name = "AB: " + hash;
 64#endif
 3965            settings.ApplyBeforeLoad(asset.container.transform);
 3966        }
 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
 83        public override string ToString()
 84        {
 085            if (subPromise != null)
 086                return $"{subPromise.ToString()} ... AB_GameObject state = {state}";
 87            else
 088                return $"subPromise == null? state = {state}";
 89        }
 90
 91        public IEnumerator LoadingCoroutine(Action OnSuccess, Action OnFail)
 92        {
 1593            subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform);
 1594            bool success = false;
 2795            subPromise.OnSuccessEvent += (x) => success = true;
 1596            asset.ownerPromise = subPromise;
 1597            AssetPromiseKeeper_AB.i.Keep(subPromise);
 98
 1599            yield return subPromise;
 100
 13101            if (success)
 102            {
 12103                yield return InstantiateABGameObjects();
 104
 12105                if (subPromise.asset == null || asset.container == null)
 2106                    success = false;
 107            }
 108
 13109            if (success)
 110            {
 10111                OnSuccess?.Invoke();
 10112            }
 113            else
 114            {
 3115                OnFail?.Invoke();
 116            }
 13117        }
 118
 119
 120        public IEnumerator InstantiateABGameObjects()
 121        {
 12122            var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf");
 123
 12124            if (goList.Count == 0)
 125            {
 1126                if (asset.container != null)
 1127                    UnityEngine.Object.Destroy(asset.container);
 128
 1129                asset.container = null;
 130
 1131                AssetPromiseKeeper_AB.i.Forget(subPromise);
 132
 1133                yield break;
 134            }
 135
 42136            for (int i = 0; i < goList.Count; i++)
 137            {
 11138                if (loadingCoroutine == null)
 139                    break;
 140
 11141                if (asset.container == null)
 142                    break;
 143
 10144                GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform);
 145
 10146                List<Renderer> rendererList = assetBundleModelGO.GetComponentsInChildren<Renderer>(true).ToList();
 147
 10148                asset.renderers.AddRange(rendererList);
 10149                UploadMeshesToGPU(MeshesInfoUtils.ExtractMeshes(assetBundleModelGO));
 10150                asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo
 151
 152                //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad
 10153                yield return MaterialCachingHelper.Process(rendererList, enableRenderers: false, settings.cachingFlags);
 154
 10155                var animators = assetBundleModelGO.GetComponentsInChildren<Animation>(true);
 156
 20157                for (int animIndex = 0; animIndex < animators.Length; animIndex++)
 158                {
 0159                    animators[animIndex].cullingType = AnimationCullingType.AlwaysAnimate;
 160                }
 161
 162#if UNITY_EDITOR
 10163                assetBundleModelGO.name = subPromise.asset.assetBundleAssetName;
 164#endif
 165                //assetBundleModelGO.transform.SetParent(asset.container.transform, false);
 10166                assetBundleModelGO.transform.ResetLocalTRS();
 10167                yield return null;
 10168            }
 11169        }
 170
 171        private void UploadMeshesToGPU(List<Mesh> meshesList)
 172        {
 60173            foreach ( Mesh mesh in meshesList )
 174            {
 20175                if ( !mesh.isReadable )
 176                    continue;
 177
 20178                Physics.BakeMesh(mesh.GetInstanceID(), false);
 20179                asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 20180                asset.meshes.Add(mesh);
 20181                mesh.UploadMeshData(true);
 182            }
 10183        }
 184
 185        protected override Asset_AB_GameObject GetAsset(object id)
 186        {
 24187            if (settings.forceNewInstance)
 188            {
 9189                return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id);
 190            }
 191            else
 192            {
 15193                return base.GetAsset(id);
 194            }
 195        }
 196    }
 197}