< 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:40
Uncovered lines:5
Coverable lines:45
Total lines:156
Line coverage:88.8% (40 of 45)
Covered branches:0
Total branches:0

Metrics

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

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        [System.Serializable]
 22        public class Item
 23        {
 24            public CLASS_ID_COMPONENT classId;
 25            public Component prefab;
 26
 27            [Header("Pool Options")] public bool usePool;
 28
 29            public int prewarmCount;
 30        }
 31
 32        public Item[] factoryList;
 33
 34        Dictionary<CLASS_ID_COMPONENT, Item> factoryDict;
 35
 36        public static PoolableComponentFactory Create()
 37        {
 038            return Resources.Load("RuntimeComponentFactory") as PoolableComponentFactory;
 39        }
 40
 41        public void EnsureFactoryDictionary()
 42        {
 6243            if (factoryDict == null)
 44            {
 345                factoryDict = new Dictionary<CLASS_ID_COMPONENT, Item>();
 46
 7247                for (int i = 0; i < factoryList.Length; i++)
 48                {
 3349                    Item item = factoryList[i];
 50
 3351                    if (!factoryDict.ContainsKey(item.classId))
 52                    {
 3353                        factoryDict.Add(item.classId, item);
 54                    }
 55                }
 56            }
 6257        }
 58
 59        public CLASS_ID_COMPONENT GetIdForType<T>() where T : Component
 60        {
 255261            for (int i = 0; i < factoryList.Length; i++)
 62            {
 120663                Item item = factoryList[i];
 64
 120665                if (item != null && item.prefab != null && item.prefab.GetComponent<T>() != null)
 66                {
 7667                    return item.classId;
 68                }
 69            }
 70
 7071            return CLASS_ID_COMPONENT.NONE;
 72        }
 73
 74        public void PrewarmPools()
 75        {
 768076            for (int i = 0; i < factoryList.Length; i++)
 77            {
 352078                Item item = factoryList[i];
 79
 352080                if (item.usePool)
 81                {
 32082                    EnsurePoolForItem(item);
 32083                    GetPoolForItem(item).ForcePrewarm();
 84                }
 85            }
 32086        }
 87
 88        private Pool GetPoolForItem(Item item)
 89        {
 65090            return PoolManager.i.GetPool(GetIdForPool(item));
 91        }
 92
 93        private object GetIdForPool(Item item)
 94        {
 95#if UNITY_EDITOR
 97096            return item.classId.ToString() + "_POOL";
 97#else
 98            return item.classId;
 99#endif
 100        }
 101
 102        private void EnsurePoolForItem(Item item)
 103        {
 325104            Pool pool = GetPoolForItem(item);
 105
 325106            if (pool != null)
 5107                return;
 108
 320109            GameObject original = Instantiate(item.prefab.gameObject);
 320110            pool = PoolManager.i.AddPool(GetIdForPool(item), original, maxPrewarmCount: item.prewarmCount,
 111                isPersistent: true);
 320112            pool.useLifecycleHandlers = true;
 320113        }
 114
 115        public ItemType CreateItemFromId<ItemType>(CLASS_ID_COMPONENT id)
 116            where ItemType : IPoolableObjectContainer
 117        {
 62118            EnsureFactoryDictionary();
 119
 62120            if (!factoryDict.ContainsKey(id))
 121            {
 122#if UNITY_EDITOR
 0123                Debug.LogError("Class " + id + " can't be instantiated because the field doesn't exist!");
 124#endif
 0125                return default(ItemType);
 126            }
 127
 62128            var factoryItem = factoryDict[id];
 129
 62130            if (factoryItem.prefab == null)
 131            {
 0132                Debug.LogError("Prefab for class " + id + " is null!");
 0133                return default(ItemType);
 134            }
 135
 136            GameObject instancedGo;
 62137            PoolableObject poolableObject = null;
 138
 62139            if (factoryItem.usePool)
 140            {
 5141                EnsurePoolForItem(factoryItem);
 5142                poolableObject = GetPoolForItem(factoryItem).Get();
 5143                instancedGo = poolableObject.gameObject;
 144            }
 145            else
 146            {
 57147                instancedGo = Instantiate(factoryItem.prefab.gameObject);
 148            }
 149
 62150            ItemType item = instancedGo.GetComponent<ItemType>();
 62151            item.poolableObject = poolableObject;
 152
 62153            return item;
 154        }
 155    }
 156}