< Summary

Class:DCL.PoolableComponentFactory
Assembly:DCL.Runtime
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/RuntimeComponentFactory/PoolableComponentFactory.cs
Covered lines:43
Uncovered lines:4
Coverable lines:47
Total lines:158
Line coverage:91.4% (43 of 47)
Covered branches:0
Total branches:0
Covered methods:7
Total methods:7
Method coverage:100% (7 of 7)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EnsureFactoryDictionary()0%440100%
GetIdForType[T]()0%550100%
PrewarmPools()0%330100%
GetPoolForItem(...)0%110100%
GetIdForPool(...)0%110100%
EnsurePoolForItem(...)0%220100%
CreateItemFromId[ItemType](...)0%5.235078.95%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/RuntimeComponentFactory/PoolableComponentFactory.cs

#LineLine coverage
 1using System;
 2using DCL.Components;
 3using DCL.Models;
 4using System.Collections.Generic;
 5using UnityEngine;
 6
 7namespace DCL
 8{
 9    public interface IPoolableComponentFactory
 10    {
 11        void EnsureFactoryDictionary();
 12        CLASS_ID_COMPONENT GetIdForType<T>() where T : Component;
 13        void PrewarmPools();
 14
 15        ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id)
 16            where ItemType : IPoolableObjectContainer;
 17    }
 18
 19    public class PoolableComponentFactory : ScriptableObject, IPoolableComponentFactory
 20    {
 21        [Serializable]
 22        public class Item
 23        {
 24            public CLASS_ID_COMPONENT classId;
 25            public Component prefab;
 26
 27            [Header("Pool Options")]
 28            public bool usePool;
 29            public int prewarmCount;
 30        }
 31
 32        public Item[] factoryList;
 33
 34        private Dictionary<CLASS_ID_COMPONENT, Item> factoryDict;
 35
 36        public void EnsureFactoryDictionary()
 37        {
 5338            if (factoryDict == null)
 39            {
 340                factoryDict = new Dictionary<CLASS_ID_COMPONENT, Item>();
 41
 6642                for (int i = 0; i < factoryList.Length; i++)
 43                {
 3044                    Item item = factoryList[i];
 45
 3046                    if (!factoryDict.ContainsKey(item.classId))
 47                    {
 3048                        factoryDict.Add(item.classId, item);
 49                    }
 50                }
 51            }
 5352        }
 53
 54        public CLASS_ID_COMPONENT GetIdForType<T>() where T : Component
 55        {
 228456            for (int i = 0; i < factoryList.Length; i++)
 57            {
 107258                Item item = factoryList[i];
 59
 107260                if (item != null && item.prefab != null && item.prefab.GetComponent<T>() != null)
 61                {
 6862                    return item.classId;
 63                }
 64            }
 65
 7066            return CLASS_ID_COMPONENT.NONE;
 67        }
 68
 69        public void PrewarmPools()
 70        {
 675471            for (int i = 0; i < factoryList.Length; i++)
 72            {
 307073                Item item = factoryList[i];
 74
 307075                if (item.usePool)
 76                {
 61477                    EnsurePoolForItem(item);
 78
 61479                    bool forceActivate = item.classId == CLASS_ID_COMPONENT.TEXT_SHAPE;
 61480                    GetPoolForItem(item).ForcePrewarm(forceActivate);
 81                }
 82            }
 30783        }
 84
 85        private Pool GetPoolForItem(Item item)
 86        {
 125287            return PoolManager.i.GetPool(GetIdForPool(item));
 88        }
 89
 90        private object GetIdForPool(Item item)
 91        {
 92#if UNITY_EDITOR
 186693            return item.classId + "_POOL";
 94#else
 95            return item.classId;
 96#endif
 97        }
 98
 99
 100        private void EnsurePoolForItem(Item item)
 101        {
 626102            Pool pool = GetPoolForItem(item);
 103
 626104            if (pool != null)
 12105                return;
 106
 614107            GameObject original = Instantiate(item.prefab.gameObject);
 614108            pool = PoolManager.i.AddPool(GetIdForPool(item), original, maxPrewarmCount: item.prewarmCount, isPersistent:
 614109            pool.useLifecycleHandlers = true;
 614110        }
 111
 112        public ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id)
 113            where ItemType : IPoolableObjectContainer
 114        {
 53115            EnsureFactoryDictionary();
 116
 53117            if (!factoryDict.ContainsKey(id))
 118            {
 119#if UNITY_EDITOR
 0120                Debug.LogError("Class " + id + " can't be instantiated because the field doesn't exist!");
 121#endif
 0122                return default(ItemType);
 123            }
 124
 53125            var factoryItem = factoryDict[id];
 126
 53127            if (factoryItem.prefab == null)
 128            {
 0129                Debug.LogError("Prefab for class " + id + " is null!");
 0130                return default(ItemType);
 131            }
 132
 133            GameObject instancedGo;
 53134            PoolableObject poolableObject = null;
 135
 53136            if (factoryItem.usePool)
 137            {
 12138                EnsurePoolForItem(factoryItem);
 12139                poolableObject = GetPoolForItem(factoryItem).Get();
 12140                instancedGo = poolableObject.gameObject;
 141            }
 142            else
 143            {
 41144                instancedGo = Instantiate(factoryItem.prefab.gameObject);
 145            }
 146
 53147            ItemType item = default;
 148
 53149            if (instancedGo != null)
 150            {
 53151                item = instancedGo.GetComponent<ItemType>();
 53152                item.poolableObject = poolableObject;
 153            }
 154
 53155            return item;
 156        }
 157    }
 158}