< Summary

Class:DCL.PoolableComponentFactory
Assembly:PoolableComponentFactory
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Factory/PoolableComponentFactory.cs
Covered lines:42
Uncovered lines:4
Coverable lines:46
Total lines:150
Line coverage:91.3% (42 of 46)
Covered branches:0
Total branches:0

Metrics

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

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Factory/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        [System.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
 30            public int prewarmCount;
 31        }
 32
 33        public Item[] factoryList;
 34
 35        Dictionary<CLASS_ID_COMPONENT, Item> factoryDict;
 36
 70837        public static PoolableComponentFactory Create() { return Resources.Load("RuntimeComponentFactory") as PoolableCo
 38
 39        public void EnsureFactoryDictionary()
 40        {
 4741            if (factoryDict == null)
 42            {
 143                factoryDict = new Dictionary<CLASS_ID_COMPONENT, Item>();
 44
 2445                for (int i = 0; i < factoryList.Length; i++)
 46                {
 1147                    Item item = factoryList[i];
 48
 1149                    if (!factoryDict.ContainsKey(item.classId))
 50                    {
 1151                        factoryDict.Add(item.classId, item);
 52                    }
 53                }
 54            }
 4755        }
 56
 57        public CLASS_ID_COMPONENT GetIdForType<T>() where T : Component
 58        {
 239659            for (int i = 0; i < factoryList.Length; i++)
 60            {
 112961                Item item = factoryList[i];
 62
 112963                if (item != null && item.prefab != null && item.prefab.GetComponent<T>() != null)
 64                {
 6665                    return item.classId;
 66                }
 67            }
 68
 6969            return CLASS_ID_COMPONENT.NONE;
 70        }
 71
 72        public void PrewarmPools()
 73        {
 3523274            for (int i = 0; i < factoryList.Length; i++)
 75            {
 1614876                Item item = factoryList[i];
 77
 1614878                if (item.usePool)
 79                {
 146880                    EnsurePoolForItem(item);
 146881                    GetPoolForItem(item).ForcePrewarm();
 82                }
 83            }
 146884        }
 85
 294086        private Pool GetPoolForItem(Item item) { return PoolManager.i.GetPool(GetIdForPool(item)); }
 87
 88        private object GetIdForPool(Item item)
 89        {
 90#if UNITY_EDITOR
 362491            return item.classId.ToString() + "_POOL";
 92#else
 93            return item.classId;
 94#endif
 95        }
 96
 97        private void EnsurePoolForItem(Item item)
 98        {
 147099            Pool pool = GetPoolForItem(item);
 100
 1470101            if (pool != null)
 786102                return;
 103
 684104            GameObject original = Instantiate(item.prefab.gameObject);
 684105            pool = PoolManager.i.AddPool(GetIdForPool(item), original, maxPrewarmCount: item.prewarmCount, isPersistent:
 684106            pool.useLifecycleHandlers = true;
 684107        }
 108
 109        public ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id)
 110            where ItemType : IPoolableObjectContainer
 111        {
 47112            EnsureFactoryDictionary();
 113
 47114            if (!factoryDict.ContainsKey(id))
 115            {
 116#if UNITY_EDITOR
 0117                Debug.LogError("Class " + id + " can't be instantiated because the field doesn't exist!");
 118#endif
 0119                return default(ItemType);
 120            }
 121
 47122            var factoryItem = factoryDict[id];
 123
 47124            if (factoryItem.prefab == null)
 125            {
 0126                Debug.LogError("Prefab for class " + id + " is null!");
 0127                return default(ItemType);
 128            }
 129
 130            GameObject instancedGo;
 47131            PoolableObject poolableObject = null;
 132
 47133            if (factoryItem.usePool)
 134            {
 2135                EnsurePoolForItem(factoryItem);
 2136                poolableObject = GetPoolForItem(factoryItem).Get();
 2137                instancedGo = poolableObject.gameObject;
 2138            }
 139            else
 140            {
 45141                instancedGo = Instantiate(factoryItem.prefab.gameObject);
 142            }
 143
 47144            ItemType item = instancedGo.GetComponent<ItemType>();
 47145            item.poolableObject = poolableObject;
 146
 47147            return item;
 148        }
 149    }
 150}