< 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:86
Uncovered lines:5
Coverable lines:91
Total lines:206
Line coverage:94.5% (86 of 91)
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%11110100%
InstantiateABGameObjects()0%10.0310093.55%
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 UnityEngine;
 6using System.Collections.Generic;
 7using DCL.Models;
 8using MainScripts.DCL.Analytics.PerformanceAnalytics;
 9
 10namespace DCL
 11{
 12    public class AssetPromise_AB_GameObject : AssetPromise_WithUrl<Asset_AB_GameObject>
 13    {
 4414        public AssetPromiseSettings_Rendering settings = new AssetPromiseSettings_Rendering();
 15        AssetPromise_AB subPromise;
 16        Coroutine loadingCoroutine;
 17
 8818        public AssetPromise_AB_GameObject(string contentUrl, string hash) : base(contentUrl, hash) { }
 19
 3820        protected override void OnLoad(Action OnSuccess, Action<Exception> OnFail) { loadingCoroutine = CoroutineStarter
 21
 22        protected override bool AddToLibrary()
 23        {
 1224            if (!library.Add(asset))
 25            {
 026                return false;
 27            }
 28
 1229            if (settings.forceNewInstance)
 30            {
 231                asset = (library as AssetLibrary_AB_GameObject).GetCopyFromOriginal(asset.id);
 232            }
 33            else
 34            {
 1035                asset = library.Get(asset.id);
 36            }
 37
 38            //NOTE(Brian): Call again this method because we are replacing the asset.
 1239            settings.ApplyBeforeLoad(asset.container.transform);
 40
 1241            return true;
 42        }
 43
 44        protected override void OnReuse(Action OnSuccess)
 45        {
 2546            asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 2547            asset.Show(OnSuccess);
 2548        }
 49
 50        protected override void OnAfterLoadOrReuse()
 51        {
 3752            asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(asset.container);
 3753            settings.ApplyAfterLoad(asset.container.transform);
 3754        }
 55
 56        protected override void OnBeforeLoadOrReuse()
 57        {
 4458            settings.ApplyBeforeLoad(asset.container.transform);
 4459        }
 60
 61        protected override void OnCancelLoading()
 62        {
 763            if (loadingCoroutine != null)
 64            {
 365                PerformanceAnalytics.ABTracker.TrackCancelled();
 366                CoroutineStarter.Stop(loadingCoroutine);
 367                loadingCoroutine = null;
 68            }
 69
 770            if (asset != null)
 771                UnityEngine.Object.Destroy(asset.container);
 72
 773            AssetPromiseKeeper_AB.i.Forget(subPromise);
 774        }
 75
 76        public override string ToString()
 77        {
 078            if (subPromise != null)
 079                return $"{subPromise.ToString()} ... AB_GameObject state = {state}";
 80            else
 081                return $"subPromise == null? state = {state}";
 82        }
 83
 84        public IEnumerator LoadingCoroutine(Action OnSuccess, Action<Exception> OnFail)
 85        {
 1986            PerformanceAnalytics.ABTracker.TrackLoading();
 1987            subPromise = new AssetPromise_AB(contentUrl, hash, asset.container.transform);
 1988            bool success = false;
 1989            Exception loadingException = null;
 3490            subPromise.OnSuccessEvent += (x) => success = true;
 91
 1992            subPromise.OnFailEvent += ( ab,  exception) =>
 93            {
 194                loadingException = exception;
 195                success = false;
 196            };
 97
 1998            asset.ownerPromise = subPromise;
 1999            AssetPromiseKeeper_AB.i.Keep(subPromise);
 100
 19101            yield return subPromise;
 102
 16103            if (success)
 104            {
 15105                yield return InstantiateABGameObjects();
 106
 15107                if (subPromise.asset == null || asset.container == null)
 3108                    success = false;
 109            }
 110
 16111            loadingCoroutine = null;
 112
 16113            if (success)
 114            {
 12115                PerformanceAnalytics.ABTracker.TrackLoaded();
 12116                OnSuccess?.Invoke();
 12117            }
 118            else
 119            {
 4120                PerformanceAnalytics.ABTracker.TrackFailed();
 4121                loadingException ??= new Exception($"AB sub-promise asset or container is null. Asset: {subPromise.asset
 4122                Debug.LogException(loadingException);
 4123                OnFail?.Invoke(loadingException);
 124            }
 16125        }
 126
 127        public IEnumerator InstantiateABGameObjects()
 128        {
 15129            var goList = subPromise.asset.GetAssetsByExtensions<GameObject>("glb", "ltf");
 130
 15131            if (goList.Count == 0)
 132            {
 2133                if (asset.container != null)
 2134                    UnityEngine.Object.Destroy(asset.container);
 135
 2136                asset.container = null;
 137
 2138                AssetPromiseKeeper_AB.i.Forget(subPromise);
 139
 2140                yield break;
 141            }
 142
 50143            for (int i = 0; i < goList.Count; i++)
 144            {
 13145                if (loadingCoroutine == null)
 146                    break;
 147
 13148                if (asset.container == null)
 149                    break;
 150
 12151                GameObject assetBundleModelGO = UnityEngine.Object.Instantiate(goList[i], asset.container.transform);
 152
 12153                asset.renderers = MeshesInfoUtils.ExtractUniqueRenderers(assetBundleModelGO);
 12154                asset.materials = MeshesInfoUtils.ExtractUniqueMaterials(asset.renderers);
 12155                asset.SetTextures(MeshesInfoUtils.ExtractUniqueTextures(asset.materials));
 156
 12157                UploadMeshesToGPU(MeshesInfoUtils.ExtractUniqueMeshes(asset.renderers));
 12158                asset.totalTriangleCount = MeshesInfoUtils.ComputeTotalTriangles(asset.renderers, asset.meshToTriangleCo
 159
 160                //NOTE(Brian): Renderers are enabled in settings.ApplyAfterLoad
 12161                yield return MaterialCachingHelper.Process(asset.renderers.ToList(), enableRenderers: false, settings.ca
 162
 12163                var animators = MeshesInfoUtils.ExtractUniqueAnimations(assetBundleModelGO);
 12164                asset.animationClipSize = 0; // TODO(Brian): Extract animation clip size from metadata
 12165                asset.meshDataSize = 0; // TODO(Brian): Extract mesh clip size from metadata
 166
 24167                foreach (var animator in animators)
 168                {
 0169                    animator.cullingType = AnimationCullingType.AlwaysAnimate;
 170                }
 171
 172#if UNITY_EDITOR
 12173                assetBundleModelGO.name = subPromise.asset.GetName();
 174#endif
 12175                assetBundleModelGO.transform.ResetLocalTRS();
 176
 12177                yield return null;
 12178            }
 13179        }
 180
 181        private void UploadMeshesToGPU(HashSet<Mesh> meshesList)
 182        {
 48183            foreach ( Mesh mesh in meshesList )
 184            {
 12185                if ( !mesh.isReadable )
 186                    continue;
 187
 12188                asset.meshToTriangleCount[mesh] = mesh.triangles.Length;
 12189                asset.meshes.Add(mesh);
 190            }
 12191        }
 192
 193        protected override Asset_AB_GameObject GetAsset(object id)
 194        {
 25195            if (settings.forceNewInstance)
 196            {
 9197                return ((AssetLibrary_AB_GameObject) library).GetCopyFromOriginal(id);
 198            }
 199            else
 200            {
 16201                return base.GetAsset(id);
 202            }
 203        }
 204    }
 205
 206}