| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Collections.Generic; |
| | 6 | | using GDX.Collections.Generic; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace GDX.Collections.Pooling |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// A managed pooling system implementation primarily meant for the object oriented patterns, based on the C# ba |
| | 13 | | /// object. |
| | 14 | | /// </summary> |
| | 15 | | [VisualScriptingCompatible(1)] |
| | 16 | | public static class ManagedPools |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// The last issued pool key used by internal dictionary's <see cref="KeyValuePair{TKey,TValue}" /> when ref |
| | 20 | | /// <see cref="IManagedPool" />. |
| | 21 | | /// </summary> |
| | 22 | | /// <remarks> |
| | 23 | | /// This value resets on domain reload, and as such the keys should not be relied on through any sort of |
| | 24 | | /// serialization (including networking) or session based process. |
| | 25 | | /// </remarks> |
| | 26 | | static uint s_LastPoolKey; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// An internal dictionary containing the <see cref="IManagedPool" />s, uniquely indexed by constant ticket- |
| | 30 | | /// system. |
| | 31 | | /// </summary> |
| 1 | 32 | | static readonly Dictionary<uint, IManagedPool> k_Pools = new Dictionary<uint, IManagedPool>(); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Get the next available pool key. |
| | 36 | | /// </summary> |
| | 37 | | /// <remarks>Increments the previously issued stored value, and returns the new value.</remarks> |
| | 38 | | /// <returns>A unique pool identifying key.</returns> |
| | 39 | | public static uint GetNextPoolKey() |
| 14 | 40 | | { |
| 14 | 41 | | s_LastPoolKey++; |
| 14 | 42 | | return s_LastPoolKey; |
| 14 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />. |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param> |
| | 49 | | /// <returns>An <see cref="IManagedPool" /> identified by the provided <paramref name="key" />.</returns> |
| | 50 | | public static IManagedPool GetPool(uint key) |
| 0 | 51 | | { |
| 0 | 52 | | return k_Pools[key]; |
| 0 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param> |
| | 59 | | /// <typeparam name="T">The type of the pool, used to cast the return pool</typeparam> |
| | 60 | | /// <returns>A type casted pool identified by the provided <paramref name="key" />.</returns> |
| | 61 | | public static T GetPool<T>(uint key) |
| 0 | 62 | | { |
| 0 | 63 | | return (T)k_Pools[key]; |
| 0 | 64 | | } |
| | 65 | |
|
| | 66 | | /// <summary> |
| | 67 | | /// Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />, first checking if it |
| | 68 | | /// registered. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param> |
| | 71 | | /// <returns>An <see cref="IManagedPool" /> identified by the provided <paramref name="key" />, null if not foun |
| | 72 | | public static IManagedPool GetPoolWithContainsCheck(uint key) |
| 0 | 73 | | { |
| 0 | 74 | | return k_Pools.ContainsKey(key) ? k_Pools[key] : null; |
| 0 | 75 | | } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />, first checking if it |
| | 79 | | /// registered. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param> |
| | 82 | | /// <typeparam name="T">The type of the pool, used to cast the return pool</typeparam> |
| | 83 | | /// <returns>A type casted pool identified by the provided <paramref name="key" />, null if not found.</returns> |
| | 84 | | public static T GetPoolWithContainsCheck<T>(uint key) where T : class |
| 0 | 85 | | { |
| 0 | 86 | | if (k_Pools.ContainsKey(key)) |
| 0 | 87 | | { |
| 0 | 88 | | return (T)k_Pools[key]; |
| | 89 | | } |
| | 90 | |
|
| 0 | 91 | | return null; |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Is an <see cref="IManagedPool" /> registered with the provided <paramref name="key" />? |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="key">A unique pool key</param> |
| | 98 | | /// <returns>true if a pool is found registered with this system, false otherwise.</returns> |
| | 99 | | public static bool HasPool(uint key) |
| 1 | 100 | | { |
| 1 | 101 | | return k_Pools.ContainsKey(key); |
| 1 | 102 | | } |
| | 103 | |
|
| | 104 | | /// <summary> |
| | 105 | | /// Attempts to return all spawned items to their original <see cref="IManagedPool" />s. |
| | 106 | | /// </summary> |
| | 107 | | /// <param name="shouldShrink">Should the pool be shrunk (destroying created items) to its original set minimum |
| | 108 | | public static void ReturnAll(bool shouldShrink = true) |
| 0 | 109 | | { |
| 0 | 110 | | foreach (KeyValuePair<uint, IManagedPool> p in k_Pools) |
| 0 | 111 | | { |
| 0 | 112 | | p.Value.ReturnAll(shouldShrink); |
| 0 | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// Register a <see cref="IManagedPool" /> with the global management system. |
| | 118 | | /// </summary> |
| | 119 | | /// <param name="managedPool">Target <see cref="IManagedPool" /></param> |
| | 120 | | public static void Register(IManagedPool managedPool) |
| 14 | 121 | | { |
| | 122 | | // Add to numerical index |
| 14 | 123 | | uint key = managedPool.GetKey(); |
| 14 | 124 | | if (!k_Pools.ContainsKey(key)) |
| 14 | 125 | | { |
| 14 | 126 | | k_Pools.Add(key, managedPool); |
| 14 | 127 | | } |
| | 128 | | else |
| 0 | 129 | | { |
| 0 | 130 | | Debug.LogError( |
| | 131 | | "A managed pool attempted to register itself with the ManagedPools, but the provided key is already |
| 0 | 132 | | } |
| 14 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Execute <see cref="IManagedPool.TearDown()" /> (destroying contents) on all registered <see cref="IManag |
| | 137 | | /// which have been flagged to accept it, evaluated by <see cref="IManagedPool.IsAllowedManagedTearDown()" / |
| | 138 | | /// </summary> |
| | 139 | | /// <remarks> |
| | 140 | | /// This will unregister the <see cref="IManagedPool" /> itself as well. |
| | 141 | | /// </remarks> |
| | 142 | | /// <param name="forceAll"> |
| | 143 | | /// Execute <see cref="IManagedPool.TearDown()" /> regardless of the |
| | 144 | | /// <see cref="IManagedPool.IsAllowedManagedTearDown()" /> response. |
| | 145 | | /// </param> |
| | 146 | | public static void TearDown(bool forceAll = false) |
| 0 | 147 | | { |
| | 148 | | // Make removal buffer |
| 0 | 149 | | SimpleList<uint> removeKeyBuffer = new SimpleList<uint>(k_Pools.Count); |
| | 150 | |
|
| | 151 | | // Now we pay the cost, however its during a teardown so, its less bad. |
| 0 | 152 | | int poolCount = k_Pools.Count; |
| | 153 | |
|
| 0 | 154 | | foreach (KeyValuePair<uint, IManagedPool> pool in k_Pools) |
| 0 | 155 | | { |
| 0 | 156 | | if (!pool.Value.IsAllowedManagedTearDown() && !forceAll) |
| 0 | 157 | | { |
| 0 | 158 | | continue; |
| | 159 | | } |
| | 160 | |
|
| 0 | 161 | | pool.Value.TearDown(); |
| 0 | 162 | | removeKeyBuffer.AddUnchecked(pool.Key); |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | // Can't modify the dictionary while iterating over it, so here we are. |
| 0 | 166 | | int removeCount = removeKeyBuffer.Count; |
| 0 | 167 | | for (int r = 0; r < removeCount; r++) |
| 0 | 168 | | { |
| 0 | 169 | | k_Pools.Remove(removeKeyBuffer.Array[r]); |
| 0 | 170 | | } |
| | 171 | |
|
| 0 | 172 | | Debug.Log( |
| | 173 | | $"[PoolSystem::TearDown] Removed {removeCount.ToString()}/{poolCount.ToString()}"); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | /// <summary> |
| | 177 | | /// Attempt to get an <see cref="IManagedPool" /> based on the <paramref name="baseObject" />. |
| | 178 | | /// </summary> |
| | 179 | | /// <param name="baseObject">The <see cref="object" /> which was used to create the pool.</param> |
| | 180 | | /// <param name="pool">The first found <see cref="IManagedPool" /> created with <paramref name="baseObject" />.< |
| | 181 | | /// <returns>true/false if an <see cref="IManagedPool" /> was found.</returns> |
| | 182 | | public static bool TryGetFirstPool(object baseObject, out IManagedPool pool) |
| 14 | 183 | | { |
| 14 | 184 | | pool = null; |
| 42 | 185 | | foreach (KeyValuePair<uint, IManagedPool> kvp in k_Pools) |
| 0 | 186 | | { |
| 0 | 187 | | if (kvp.Value.GetBaseObject() != baseObject) |
| 0 | 188 | | { |
| 0 | 189 | | continue; |
| | 190 | | } |
| | 191 | |
|
| 0 | 192 | | pool = kvp.Value; |
| 0 | 193 | | return true; |
| | 194 | | } |
| | 195 | |
|
| 14 | 196 | | return false; |
| 14 | 197 | | } |
| | 198 | |
|
| | 199 | | /// <summary> |
| | 200 | | /// Unregister a <see cref="IManagedPool" /> from with the management system. |
| | 201 | | /// </summary> |
| | 202 | | /// <param name="managedPool">Target <see cref="IManagedPool" /></param> |
| | 203 | | public static void Unregister(IManagedPool managedPool) |
| 14 | 204 | | { |
| | 205 | | // Well we cant be doing anything with that |
| 14 | 206 | | if (managedPool == null) |
| 0 | 207 | | { |
| 0 | 208 | | return; |
| | 209 | | } |
| | 210 | |
|
| | 211 | | // Get the apparent key |
| 14 | 212 | | uint key = managedPool.GetKey(); |
| | 213 | |
|
| | 214 | | // Checks key and the matching pool |
| 14 | 215 | | if (k_Pools.ContainsKey(key) && k_Pools[key] == managedPool) |
| 14 | 216 | | { |
| | 217 | | // Remove it from the registry |
| 14 | 218 | | k_Pools.Remove(key); |
| 14 | 219 | | } |
| 14 | 220 | | } |
| | 221 | | } |
| | 222 | | } |