< Summary

Class:DCL.PoolManager
Assembly:PoolManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PoolManager/PoolManager.cs
Covered lines:85
Uncovered lines:18
Coverable lines:103
Total lines:252
Line coverage:82.5% (85 of 103)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PoolManager()0%110100%
PoolManager()0%110100%
HasPoolable(...)0%110100%
GetPoolable(...)0%330100%
EnsureContainer()0%220100%
OnRenderingStateChanged(...)0%110100%
AddPool(...)0%7.147085.71%
GetPool(...)0%3.333066.67%
RemovePool(...)0%3.213071.43%
ContainsPool(...)0%2.52050%
Release(...)0%3.073080%
Get(...)0%5.024060%
Cleanup()0%4.014092.31%
ReleaseAllFromPool(...)0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PoolManager/PoolManager.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using UnityEngine;
 4using System.Linq;
 5using DCL.Interface;
 6using Object = UnityEngine.Object;
 7
 8namespace DCL
 9{
 10    public class PoolManager : Singleton<PoolManager>
 11    {
 12#if UNITY_EDITOR
 113        public static bool USE_POOL_CONTAINERS = true;
 14#else
 15        public static bool USE_POOL_CONTAINERS = false;
 16#endif
 17
 18        public const int DEFAULT_PREWARM_COUNT = 100;
 119        public static bool enablePrewarm = true;
 120        public bool initializing { get; private set; }
 21
 122        public Dictionary<object, Pool> pools = new Dictionary<object, Pool>();
 123        public Dictionary<GameObject, PoolableObject> poolables = new Dictionary<GameObject, PoolableObject>();
 124        public HashSet<PoolableObject> poolableValues = new HashSet<PoolableObject>();
 25
 26        public event System.Action OnGet;
 27
 28        public bool HasPoolable(PoolableObject poolable)
 29        {
 30            //NOTE(Brian): The only poolableValues use is this. Using ContainsValue in a Dictionary is slow as hell.
 78231            return poolableValues.Contains(poolable);
 32        }
 33
 34        public PoolableObject GetPoolable(GameObject gameObject)
 35        {
 6836            if (gameObject == null)
 137                return null;
 38
 6739            if (poolables.ContainsKey(gameObject))
 40            {
 2441                return poolables[gameObject];
 42            }
 43
 4344            return null;
 45        }
 46
 47        GameObject container
 48        {
 49            get
 50            {
 119551                EnsureContainer();
 119552                return containerValue;
 53            }
 054            set { containerValue = value; }
 55        }
 56
 57        GameObject containerValue = null;
 58
 59        void EnsureContainer()
 60        {
 119661            if (containerValue == null)
 11862                containerValue = new GameObject("_PoolManager");
 119663        }
 64
 65        public System.Action<CrashPayload> CrashPayloadEvent;
 66
 167        public PoolManager()
 68        {
 169            EnsureContainer();
 70
 171            initializing = !CommonScriptableObjects.rendererState.Get();
 172            CommonScriptableObjects.rendererState.OnChange += OnRenderingStateChanged;
 173        }
 74
 275        void OnRenderingStateChanged(bool renderingEnabled, bool prevState) { initializing = !renderingEnabled; }
 76
 77        public Pool AddPool(object id, GameObject original, IPooledObjectInstantiator instantiator = null, int maxPrewar
 78        {
 119779            Pool existingPool = GetPool(id);
 80
 119781            if (existingPool != null && !existingPool.IsValid())
 1082                RemovePool(id);
 83
 119784            if (ContainsPool(id))
 85            {
 286                if (Pool.FindPoolInGameObject(original, out Pool poolAlreadyExists))
 87                {
 088                    Debug.LogWarning("WARNING: Object is already being contained in an existing pool!. Returning it.");
 089                    poolAlreadyExists.persistent = isPersistent;
 090                    return poolAlreadyExists;
 91                }
 92
 293                Pool result = GetPool(id);
 94
 295                result.AddToPool(original);
 296                result.persistent = isPersistent;
 97
 298                return result;
 99            }
 100
 1195101            if (!enablePrewarm)
 1194102                maxPrewarmCount = 0;
 103
 1195104            Pool pool = new Pool(id.ToString(), maxPrewarmCount);
 105
 1195106            pool.id = id;
 1195107            pool.original = original;
 1195108            pool.persistent = isPersistent;
 109
 1195110            if (USE_POOL_CONTAINERS)
 111            {
 1195112                pool.container.transform.parent = container.transform;
 1195113                pool.original.name = "Original";
 1195114                pool.original.transform.SetParent(pool.container.transform, true);
 1195115            }
 116            else
 117            {
 0118                pool.original.transform.SetParent(null, true);
 119            }
 120
 1195121            pool.original.SetActive(false);
 122
 1195123            pool.instantiator = instantiator;
 124
 1195125            pools.Add(id, pool);
 126
 1195127            return pool;
 128        }
 129
 130        public Pool GetPool(object id)
 131        {
 2748132            if (id == null)
 133            {
 0134                Debug.LogError("GetPool >>> id cannot be null!");
 0135                return null;
 136            }
 137
 2748138            if (pools.ContainsKey(id))
 758139                return pools[id];
 140
 1990141            return null;
 142        }
 143
 144        public void RemovePool(object id)
 145        {
 1195146            if (id == null)
 147            {
 0148                Debug.LogError("RemovePool >>> id cannot be null!");
 0149                return;
 150            }
 151
 1195152            if (pools.ContainsKey(id))
 153            {
 1195154                pools[id].Cleanup();
 1195155                pools.Remove(id);
 156            }
 1195157        }
 158
 159        public bool ContainsPool(object id)
 160        {
 1903161            if (id == null)
 162            {
 0163                Debug.LogError("ContainsPool >>> id cannot be null!");
 0164                return false;
 165            }
 166
 1903167            return pools.ContainsKey(id);
 168        }
 169
 170        public bool Release(GameObject gameObject)
 171        {
 123172            if (gameObject == null)
 173            {
 174#if UNITY_EDITOR
 0175                Debug.LogWarning("Release >>> gameObject cannot be null!");
 176#endif
 0177                return false;
 178            }
 179
 180
 123181            if (poolables.TryGetValue(gameObject, out PoolableObject poolableObject))
 182            {
 103183                poolableObject.Release();
 103184            }
 185            else
 186            {
 187#if UNITY_EDITOR
 20188                Debug.LogWarning("Release >>> poolable not found in object, destroying it instead!");
 189#endif
 20190                Object.Destroy(gameObject);
 20191                return false;
 192            }
 193
 103194            return true;
 195        }
 196
 197        public PoolableObject Get(object id)
 198        {
 742199            if (id == null)
 200            {
 0201                Debug.LogError("Get >>> id cannot be null!");
 0202                return null;
 203            }
 204
 205            Pool pool;
 206
 742207            if (pools.ContainsKey(id))
 208            {
 742209                pool = pools[id];
 742210            }
 211            else
 212            {
 0213                Debug.LogError($"Pool doesn't exist for id {id}!");
 0214                return null;
 215            }
 216
 742217            OnGet?.Invoke();
 742218            return pool.Get();
 219        }
 220
 221        public void Cleanup()
 222        {
 756223            if (pools == null)
 0224                return;
 225
 756226            List<object> idsToRemove = new List<object>(10);
 227
 756228            using (var iterator = pools.GetEnumerator())
 229            {
 802230                while (iterator.MoveNext())
 231                {
 46232                    idsToRemove.Add(iterator.Current.Key);
 233                }
 756234            }
 235
 1604236            for (int i = 0; i < idsToRemove.Count; i++)
 237            {
 46238                RemovePool(idsToRemove[i]);
 239            }
 240
 756241            CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged;
 756242        }
 243
 244        public void ReleaseAllFromPool(object id)
 245        {
 1246            if (pools.ContainsKey(id))
 247            {
 1248                pools[id].ReleaseAll();
 249            }
 1250        }
 251    }
 252}