< Summary

Class:DCL.PoolManager
Assembly:PoolManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PoolManager/PoolManager.cs
Covered lines:97
Uncovered lines:19
Coverable lines:116
Total lines:275
Line coverage:83.6% (97 of 116)
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%
CleanupAsync()0%990100%
Cleanup(...)0%2.062075%
Dispose()0%110100%
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;
 3using System.Collections.Generic;
 4using UnityEngine;
 5using System.Linq;
 6using DCL.Interface;
 7using Object = UnityEngine.Object;
 8
 9namespace DCL
 10{
 11    public class PoolManager : Singleton<PoolManager>
 12    {
 13#if UNITY_EDITOR
 114        public static bool USE_POOL_CONTAINERS = true;
 15#else
 16        public static bool USE_POOL_CONTAINERS = false;
 17#endif
 18
 19        public const int DEFAULT_PREWARM_COUNT = 100;
 120        public static bool enablePrewarm = true;
 021        public bool initializing { get; private set; }
 22
 123        public Dictionary<object, Pool> pools = new Dictionary<object, Pool>();
 124        public Dictionary<GameObject, PoolableObject> poolables = new Dictionary<GameObject, PoolableObject>();
 125        public HashSet<PoolableObject> poolableValues = new HashSet<PoolableObject>();
 26
 27        public event System.Action OnGet;
 28
 29        public bool HasPoolable(PoolableObject poolable)
 30        {
 31            //NOTE(Brian): The only poolableValues use is this. Using ContainsValue in a Dictionary is slow as hell.
 241332            return poolableValues.Contains(poolable);
 33        }
 34
 35        public PoolableObject GetPoolable(GameObject gameObject)
 36        {
 10037            if (gameObject == null)
 138                return null;
 39
 9940            if (poolables.ContainsKey(gameObject))
 41            {
 2442                return poolables[gameObject];
 43            }
 44
 7545            return null;
 46        }
 47
 48        GameObject container
 49        {
 50            get
 51            {
 163952                EnsureContainer();
 163953                return containerValue;
 54            }
 055            set { containerValue = value; }
 56        }
 57
 58        GameObject containerValue = null;
 59
 60        void EnsureContainer()
 61        {
 164062            if (containerValue == null)
 63663                containerValue = new GameObject("_PoolManager");
 164064        }
 65
 66        public System.Action<CrashPayload> CrashPayloadEvent;
 67
 168        public PoolManager()
 69        {
 170            EnsureContainer();
 71
 172            initializing = !CommonScriptableObjects.rendererState.Get();
 173            CommonScriptableObjects.rendererState.OnChange += OnRenderingStateChanged;
 174        }
 75
 276        void OnRenderingStateChanged(bool renderingEnabled, bool prevState) { initializing = !renderingEnabled; }
 77
 78        public Pool AddPool(object id, GameObject original, IPooledObjectInstantiator instantiator = null, int maxPrewar
 79        {
 164280            Pool existingPool = GetPool(id);
 81
 164282            if (existingPool != null && !existingPool.IsValid())
 1283                RemovePool(id);
 84
 164285            if (ContainsPool(id))
 86            {
 387                if (Pool.FindPoolInGameObject(original, out Pool poolAlreadyExists))
 88                {
 089                    Debug.LogWarning("WARNING: Object is already being contained in an existing pool!. Returning it.");
 090                    poolAlreadyExists.persistent = isPersistent;
 091                    return poolAlreadyExists;
 92                }
 93
 394                Pool result = GetPool(id);
 95
 396                result.AddToPool(original);
 397                result.persistent = isPersistent;
 98
 399                return result;
 100            }
 101
 1639102            if (!enablePrewarm)
 1639103                maxPrewarmCount = 0;
 104
 1639105            Pool pool = new Pool(id.ToString(), maxPrewarmCount);
 106
 1639107            pool.id = id;
 1639108            pool.original = original;
 1639109            pool.persistent = isPersistent;
 110
 1639111            if (USE_POOL_CONTAINERS)
 112            {
 1639113                pool.container.transform.parent = container.transform;
 1639114                pool.original.name = "Original";
 1639115                pool.original.transform.SetParent(pool.container.transform, true);
 1639116            }
 117            else
 118            {
 0119                pool.original.transform.SetParent(null, true);
 120            }
 121
 1639122            pool.original.SetActive(false);
 123
 1639124            pool.instantiator = instantiator;
 125
 1639126            pools.Add(id, pool);
 127
 1639128            return pool;
 129        }
 130
 131        public Pool GetPool(object id)
 132        {
 2832133            if (id == null)
 134            {
 0135                Debug.LogError("GetPool >>> id cannot be null!");
 0136                return null;
 137            }
 138
 2832139            if (pools.ContainsKey(id))
 696140                return pools[id];
 141
 2136142            return null;
 143        }
 144
 145        public void RemovePool(object id)
 146        {
 1621147            if (id == null)
 148            {
 0149                Debug.LogError("RemovePool >>> id cannot be null!");
 0150                return;
 151            }
 152
 1621153            if (pools.ContainsKey(id))
 154            {
 1621155                pools[id].Cleanup();
 1621156                pools.Remove(id);
 157            }
 1621158        }
 159
 160        public bool ContainsPool(object id)
 161        {
 3119162            if (id == null)
 163            {
 0164                Debug.LogError("ContainsPool >>> id cannot be null!");
 0165                return false;
 166            }
 167
 3119168            return pools.ContainsKey(id);
 169        }
 170
 171        public bool Release(GameObject gameObject)
 172        {
 95173            if (gameObject == null)
 174            {
 175#if UNITY_EDITOR
 0176                Debug.LogWarning("Release >>> gameObject cannot be null!");
 177#endif
 0178                return false;
 179            }
 180
 181
 95182            if (poolables.TryGetValue(gameObject, out PoolableObject poolableObject))
 183            {
 75184                poolableObject.Release();
 75185            }
 186            else
 187            {
 188#if UNITY_EDITOR
 20189                Debug.LogWarning("Release >>> poolable not found in object, destroying it instead!");
 190#endif
 20191                Object.Destroy(gameObject);
 20192                return false;
 193            }
 194
 75195            return true;
 196        }
 197
 198        public PoolableObject Get(object id)
 199        {
 857200            if (id == null)
 201            {
 0202                Debug.LogError("Get >>> id cannot be null!");
 0203                return null;
 204            }
 205
 206            Pool pool;
 207
 857208            if (pools.ContainsKey(id))
 209            {
 857210                pool = pools[id];
 857211            }
 212            else
 213            {
 0214                Debug.LogError($"Pool doesn't exist for id {id}!");
 0215                return null;
 216            }
 217
 857218            OnGet?.Invoke();
 857219            return pool.Get();
 220        }
 221
 222        public IEnumerator CleanupAsync(bool unusedOnly = true, bool nonPersistentOnly = true, bool immediate = false)
 223        {
 1198224            List<object> idsToRemove = new List<object>(pools.Count);
 225
 1198226            using (var iterator = pools.GetEnumerator())
 227            {
 2758228                while (iterator.MoveNext())
 229                {
 1560230                    Pool pool = iterator.Current.Value;
 1560231                    bool unusedPool = pool.usedObjectsCount == 0; // && pool.unusedObjectsCount > 0;
 1560232                    bool isNonPersistent = !pool.persistent;
 233
 3065234                    if (!unusedOnly) unusedPool = true;
 3065235                    if (!nonPersistentOnly) isNonPersistent = true;
 236
 1560237                    bool shouldRemove = unusedPool && isNonPersistent;
 238
 1560239                    if ( shouldRemove )
 1510240                        idsToRemove.Add(iterator.Current.Key);
 241                }
 1198242            }
 243
 5416244            for (int i = 0; i < idsToRemove.Count; i++)
 245            {
 1510246                RemovePool(idsToRemove[i]);
 247
 1510248                if ( !immediate )
 8249                    yield return null;
 250            }
 1198251        }
 252
 253        public void Cleanup( bool unusedOnly = false, bool nonPersistentOnly = false )
 254        {
 1193255            if (pools == null)
 0256                return;
 257
 1193258            CleanupAsync( unusedOnly: unusedOnly, nonPersistentOnly: nonPersistentOnly, immediate: true ).MoveNext();
 1193259        }
 260
 261        public void Dispose()
 262        {
 761263            Cleanup();
 761264            CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged;
 761265        }
 266
 267        public void ReleaseAllFromPool(object id)
 268        {
 1269            if (pools.ContainsKey(id))
 270            {
 1271                pools[id].ReleaseAll();
 272            }
 1273        }
 274    }
 275}