< 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
 68237        public static PoolableComponentFactory Create() { return Resources.Load("RuntimeComponentFactory") as PoolableCo
 38
 39        public void EnsureFactoryDictionary()
 40        {
 4541            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            }
 4555        }
 56
 57        public CLASS_ID_COMPONENT GetIdForType<T>() where T : Component
 58        {
 176659            for (int i = 0; i < factoryList.Length; i++)
 60            {
 83561                Item item = factoryList[i];
 62
 83563                if (item != null && item.prefab != null && item.prefab.GetComponent<T>() != null)
 64                {
 5965                    return item.classId;
 66                }
 67            }
 68
 4869            return CLASS_ID_COMPONENT.NONE;
 70        }
 71
 72        public void PrewarmPools()
 73        {
 1653674            for (int i = 0; i < factoryList.Length; i++)
 75            {
 757976                Item item = factoryList[i];
 77
 757978                if (item.usePool)
 79                {
 68980                    EnsurePoolForItem(item);
 68981                    GetPoolForItem(item).ForcePrewarm();
 82                }
 83            }
 68984        }
 85
 138286        private Pool GetPoolForItem(Item item) { return PoolManager.i.GetPool(GetIdForPool(item)); }
 87
 88        private object GetIdForPool(Item item)
 89        {
 90#if UNITY_EDITOR
 204191            return item.classId.ToString() + "_POOL";
 92#else
 93            return item.classId;
 94#endif
 95        }
 96
 97        private void EnsurePoolForItem(Item item)
 98        {
 69199            Pool pool = GetPoolForItem(item);
 100
 691101            if (pool != null)
 32102                return;
 103
 659104            GameObject original = Instantiate(item.prefab.gameObject);
 659105            pool = PoolManager.i.AddPool(GetIdForPool(item), original, maxPrewarmCount: item.prewarmCount, isPersistent:
 659106            pool.useLifecycleHandlers = true;
 659107        }
 108
 109        public ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id)
 110            where ItemType : IPoolableObjectContainer
 111        {
 45112            EnsureFactoryDictionary();
 113
 45114            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
 45122            var factoryItem = factoryDict[id];
 123
 45124            if (factoryItem.prefab == null)
 125            {
 0126                Debug.LogError("Prefab for class " + id + " is null!");
 0127                return default(ItemType);
 128            }
 129
 130            GameObject instancedGo;
 45131            PoolableObject poolableObject = null;
 132
 45133            if (factoryItem.usePool)
 134            {
 2135                EnsurePoolForItem(factoryItem);
 2136                poolableObject = GetPoolForItem(factoryItem).Get();
 2137                instancedGo = poolableObject.gameObject;
 2138            }
 139            else
 140            {
 43141                instancedGo = Instantiate(factoryItem.prefab.gameObject);
 142            }
 143
 45144            ItemType item = instancedGo.GetComponent<ItemType>();
 45145            item.poolableObject = poolableObject;
 146
 45147            return item;
 148        }
 149    }
 150}