< Summary

Class:DCL.PoolManager
Assembly:PoolManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PoolManager/PoolManager.cs
Covered lines:80
Uncovered lines:23
Coverable lines:103
Total lines:252
Line coverage:77.6% (80 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%8.637067.86%
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.
 67231            return poolableValues.Contains(poolable);
 32        }
 33
 34        public PoolableObject GetPoolable(GameObject gameObject)
 35        {
 6236            if (gameObject == null)
 137                return null;
 38
 6139            if (poolables.ContainsKey(gameObject))
 40            {
 2441                return poolables[gameObject];
 42            }
 43
 3744            return null;
 45        }
 46
 47        GameObject container
 48        {
 49            get
 50            {
 97251                EnsureContainer();
 97252                return containerValue;
 53            }
 054            set { containerValue = value; }
 55        }
 56
 57        GameObject containerValue = null;
 58
 59        void EnsureContainer()
 60        {
 97361            if (containerValue == null)
 10062                containerValue = new GameObject("_PoolManager");
 97363        }
 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        {
 97279            Pool existingPool = GetPool(id);
 80
 97281            if (existingPool != null && !existingPool.IsValid())
 1082                RemovePool(id);
 83
 97284            if (ContainsPool(id))
 85            {
 086                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
 093                Pool result = GetPool(id);
 94
 095                result.AddToPool(original);
 096                result.persistent = isPersistent;
 97
 098                return result;
 99            }
 100
 972101            if (!enablePrewarm)
 971102                maxPrewarmCount = 0;
 103
 972104            Pool pool = new Pool(id.ToString(), maxPrewarmCount);
 105
 972106            pool.id = id;
 972107            pool.original = original;
 972108            pool.persistent = isPersistent;
 109
 972110            if (USE_POOL_CONTAINERS)
 111            {
 972112                pool.container.transform.parent = container.transform;
 972113                pool.original.name = "Original";
 972114                pool.original.transform.SetParent(pool.container.transform, true);
 972115            }
 116            else
 117            {
 0118                pool.original.transform.SetParent(null, true);
 119            }
 120
 972121            pool.original.SetActive(false);
 122
 972123            pool.instantiator = instantiator;
 124
 972125            pools.Add(id, pool);
 126
 972127            return pool;
 128        }
 129
 130        public Pool GetPool(object id)
 131        {
 2246132            if (id == null)
 133            {
 0134                Debug.LogError("GetPool >>> id cannot be null!");
 0135                return null;
 136            }
 137
 2246138            if (pools.ContainsKey(id))
 615139                return pools[id];
 140
 1631141            return null;
 142        }
 143
 144        public void RemovePool(object id)
 145        {
 972146            if (id == null)
 147            {
 0148                Debug.LogError("RemovePool >>> id cannot be null!");
 0149                return;
 150            }
 151
 972152            if (pools.ContainsKey(id))
 153            {
 972154                pools[id].Cleanup();
 972155                pools.Remove(id);
 156            }
 972157        }
 158
 159        public bool ContainsPool(object id)
 160        {
 1576161            if (id == null)
 162            {
 0163                Debug.LogError("ContainsPool >>> id cannot be null!");
 0164                return false;
 165            }
 166
 1576167            return pools.ContainsKey(id);
 168        }
 169
 170        public bool Release(GameObject gameObject)
 171        {
 99172            if (gameObject == null)
 173            {
 174#if UNITY_EDITOR
 0175                Debug.LogWarning("Release >>> gameObject cannot be null!");
 176#endif
 0177                return false;
 178            }
 179
 180
 99181            if (poolables.TryGetValue(gameObject, out PoolableObject poolableObject))
 182            {
 78183                poolableObject.Release();
 78184            }
 185            else
 186            {
 187#if UNITY_EDITOR
 21188                Debug.LogWarning("Release >>> poolable not found in object, destroying it instead!");
 189#endif
 21190                Object.Destroy(gameObject);
 21191                return false;
 192            }
 193
 78194            return true;
 195        }
 196
 197        public PoolableObject Get(object id)
 198        {
 635199            if (id == null)
 200            {
 0201                Debug.LogError("Get >>> id cannot be null!");
 0202                return null;
 203            }
 204
 205            Pool pool;
 206
 635207            if (pools.ContainsKey(id))
 208            {
 635209                pool = pools[id];
 635210            }
 211            else
 212            {
 0213                Debug.LogError($"Pool doesn't exist for id {id}!");
 0214                return null;
 215            }
 216
 635217            OnGet?.Invoke();
 635218            return pool.Get();
 219        }
 220
 221        public void Cleanup()
 222        {
 528223            if (pools == null)
 0224                return;
 225
 528226            List<object> idsToRemove = new List<object>(10);
 227
 528228            using (var iterator = pools.GetEnumerator())
 229            {
 565230                while (iterator.MoveNext())
 231                {
 37232                    idsToRemove.Add(iterator.Current.Key);
 233                }
 528234            }
 235
 1130236            for (int i = 0; i < idsToRemove.Count; i++)
 237            {
 37238                RemovePool(idsToRemove[i]);
 239            }
 240
 528241            CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged;
 528242        }
 243
 244        public void ReleaseAllFromPool(object id)
 245        {
 1246            if (pools.ContainsKey(id))
 247            {
 1248                pools[id].ReleaseAll();
 249            }
 1250        }
 251    }
 252}