< Summary

Class:DCL.PoolManager
Assembly:PoolManager
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/PoolManager/PoolManager.cs
Covered lines:90
Uncovered lines:20
Coverable lines:110
Total lines:276
Line coverage:81.8% (90 of 110)
Covered branches:0
Total branches:0
Covered methods:18
Total methods:20
Method coverage:90% (18 of 20)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
PoolManager()0%110100%
PoolManager()0%110100%
HasPoolable(...)0%110100%
GetPoolable(...)0%3.073080%
EnsureContainer()0%220100%
OnRenderingStateChanged(...)0%110100%
AddPool(...)0%9.389083.33%
GetPool(...)0%3.333066.67%
RemovePool(...)0%3.213071.43%
ContainsPool(...)0%2.52050%
Release(...)0%330100%
Get(...)0%5.44055.56%
CleanupAsync()0%990100%
Cleanup(...)0%2.062075%
Dispose()0%110100%
ReleaseAllFromPool(...)0%6200%

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;
 61721        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.
 78432            return poolableValues.Contains(poolable);
 33        }
 34
 35        public PoolableObject GetPoolable(GameObject gameObject)
 36        {
 6437            if (gameObject == null)
 038                return null;
 39
 6440            if (poolables.ContainsKey(gameObject))
 41            {
 1242                return poolables[gameObject];
 43            }
 44
 5245            return null;
 46        }
 47
 48        GameObject container
 49        {
 50            get
 51            {
 553752                EnsureContainer();
 553753                return containerValue;
 54            }
 055            set { containerValue = value; }
 56        }
 57
 58        GameObject containerValue = null;
 59
 60        void EnsureContainer()
 61        {
 553862            if (containerValue == null)
 36463                containerValue = new GameObject("_PoolManager");
 553864        }
 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        {
 554080            Pool existingPool = GetPool(id);
 81
 554082            if (existingPool != null && !existingPool.IsValid())
 983                RemovePool(id);
 84
 554085            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
 5537102            if (!enablePrewarm)
 5537103                maxPrewarmCount = 0;
 104
 5537105            Pool pool = new Pool(id.ToString(), maxPrewarmCount)
 106                {
 107                    id = id,
 108                    original = original,
 109                    persistent = isPersistent,
 110                };
 111
 5537112            if (USE_POOL_CONTAINERS)
 113            {
 5537114                pool.container.transform.parent = container.transform;
 5537115                pool.original.name = "Original_" + id;
 5537116                pool.original.transform.SetParent(pool.container.transform, true);
 117            }
 118            else
 119            {
 0120                pool.original.transform.SetParent(null, true);
 121            }
 122
 5537123            pool.original.SetActive(false);
 124
 5537125            pool.instantiator = instantiator;
 126
 5537127            pools.Add(id, pool);
 128
 5537129            return pool;
 130        }
 131
 132        public Pool GetPool(object id)
 133        {
 13322134            if (id == null)
 135            {
 0136                Debug.LogError("GetPool >>> id cannot be null!");
 0137                return null;
 138            }
 139
 13322140            if (pools.ContainsKey(id))
 3141141                return pools[id];
 142
 10181143            return null;
 144        }
 145
 146        public void RemovePool(object id)
 147        {
 5518148            if (id == null)
 149            {
 0150                Debug.LogError("RemovePool >>> id cannot be null!");
 0151                return;
 152            }
 153
 5518154            if (pools.ContainsKey(id))
 155            {
 5518156                pools[id].Cleanup();
 5518157                pools.Remove(id);
 158            }
 5518159        }
 160
 161        public bool ContainsPool(object id)
 162        {
 6418163            if (id == null)
 164            {
 0165                Debug.LogError("ContainsPool >>> id cannot be null!");
 0166                return false;
 167            }
 168
 6418169            return pools.ContainsKey(id);
 170        }
 171
 172        public bool Release(GameObject gameObject)
 173        {
 65174            if (gameObject == null)
 175            {
 176#if UNITY_EDITOR
 1177                Debug.LogWarning("Release >>> gameObject cannot be null!");
 178#endif
 1179                return false;
 180            }
 181
 182
 64183            if (poolables.TryGetValue(gameObject, out PoolableObject poolableObject))
 184            {
 54185                poolableObject.Release();
 186            }
 187            else
 188            {
 189#if UNITY_EDITOR
 10190                Debug.LogWarning("Release >>> poolable not found in object, destroying it instead!");
 191#endif
 10192                Object.Destroy(gameObject);
 10193                return false;
 194            }
 195
 54196            return true;
 197        }
 198
 199        public PoolableObject Get(object id)
 200        {
 468201            if (id == null)
 202            {
 0203                Debug.LogError("Get >>> id cannot be null!");
 0204                return null;
 205            }
 206
 207            Pool pool;
 208
 468209            if (pools.ContainsKey(id))
 210            {
 468211                pool = pools[id];
 212            }
 213            else
 214            {
 0215                Debug.LogError($"Pool doesn't exist for id {id}!");
 0216                return null;
 217            }
 218
 468219            OnGet?.Invoke();
 468220            return pool.Get();
 221        }
 222
 223        public IEnumerator CleanupAsync(bool unusedOnly = true, bool nonPersistentOnly = true, bool immediate = false)
 224        {
 773225            List<object> idsToRemove = new List<object>(pools.Count);
 226
 773227            using (var iterator = pools.GetEnumerator())
 228            {
 6512229                while (iterator.MoveNext())
 230                {
 5739231                    Pool pool = iterator.Current.Value;
 5739232                    bool unusedPool = pool.usedObjectsCount == 0; // && pool.unusedObjectsCount > 0;
 5739233                    bool isNonPersistent = !pool.persistent;
 234
 11128235                    if (!unusedOnly) unusedPool = true;
 11128236                    if (!nonPersistentOnly) isNonPersistent = true;
 237
 5739238                    bool shouldRemove = unusedPool && isNonPersistent;
 239
 5739240                    if ( shouldRemove )
 5394241                        idsToRemove.Add(iterator.Current.Key);
 242                }
 773243            }
 244
 12334245            for (int i = 0; i < idsToRemove.Count; i++)
 246            {
 5394247                RemovePool(idsToRemove[i]);
 248
 5394249                if ( !immediate )
 4250                    yield return null;
 251            }
 773252        }
 253
 254        public void Cleanup( bool unusedOnly = false, bool nonPersistentOnly = false )
 255        {
 769256            if (pools == null)
 0257                return;
 258
 769259            CleanupAsync( unusedOnly: unusedOnly, nonPersistentOnly: nonPersistentOnly, immediate: true ).MoveNext();
 769260        }
 261
 262        public void Dispose()
 263        {
 496264            Cleanup();
 496265            CommonScriptableObjects.rendererState.OnChange -= OnRenderingStateChanged;
 496266        }
 267
 268        public void ReleaseAllFromPool(object id)
 269        {
 0270            if (pools.ContainsKey(id))
 271            {
 0272                pools[id].ReleaseAll();
 273            }
 0274        }
 275    }
 276}