| | 1 | | using System.Collections.Generic; |
| | 2 | | using UnityEngine; |
| | 3 | |
|
| | 4 | | namespace DCL |
| | 5 | | { |
| | 6 | | public class AssetLibrary_Poolable<AssetType> : AssetLibrary<AssetType> |
| | 7 | | where AssetType : Asset_WithPoolableContainer |
| | 8 | | { |
| | 9 | | private readonly string salt = null; |
| | 10 | | IPooledObjectInstantiator instantiator; |
| 43 | 11 | | public Dictionary<object, AssetType> masterAssets = new Dictionary<object, AssetType>(); |
| | 12 | |
|
| 0 | 13 | | public AssetLibrary_Poolable(IPooledObjectInstantiator instantiator) : this(instantiator, null) { } |
| 43 | 14 | | public AssetLibrary_Poolable(IPooledObjectInstantiator instantiator, string salt) |
| | 15 | | { |
| 43 | 16 | | this.instantiator = instantiator; |
| 43 | 17 | | this.salt = salt; |
| 43 | 18 | | } |
| | 19 | |
|
| | 20 | | private void OnPoolRemoved(Pool pool) |
| | 21 | | { |
| 112 | 22 | | pool.OnCleanup -= OnPoolRemoved; |
| 112 | 23 | | object assetId = PoolIdToAssetId(pool.id); |
| | 24 | |
|
| 112 | 25 | | if (masterAssets.ContainsKey(assetId)) |
| | 26 | | { |
| 22 | 27 | | masterAssets[assetId].Cleanup(); |
| 22 | 28 | | masterAssets.Remove(assetId); |
| | 29 | | } |
| 112 | 30 | | } |
| | 31 | |
|
| | 32 | | public override bool Add(AssetType asset) |
| | 33 | | { |
| 112 | 34 | | if (asset == null || asset.container == null) |
| | 35 | | { |
| 0 | 36 | | Debug.LogWarning("asset or asset.container == null? This shouldn't happen"); |
| 0 | 37 | | return false; |
| | 38 | | } |
| | 39 | |
|
| 112 | 40 | | if (!masterAssets.ContainsKey(asset.id)) |
| 112 | 41 | | masterAssets.Add(asset.id, asset); |
| | 42 | |
|
| 112 | 43 | | object poolId = AssetIdToPoolId(asset.id); |
| 112 | 44 | | Pool pool = PoolManager.i.AddPool(poolId, asset.container, instantiator); |
| | 45 | |
|
| 112 | 46 | | pool.OnCleanup -= OnPoolRemoved; |
| 112 | 47 | | pool.OnCleanup += OnPoolRemoved; |
| | 48 | |
|
| 112 | 49 | | if (asset.container == null) |
| 0 | 50 | | return false; |
| | 51 | |
|
| 112 | 52 | | return true; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public override AssetType Get(object id) |
| | 56 | | { |
| 141 | 57 | | if (!Contains(id)) |
| 0 | 58 | | return null; |
| | 59 | |
|
| 141 | 60 | | AssetType clone = masterAssets[id].Clone() as AssetType; |
| 141 | 61 | | object poolId = AssetIdToPoolId(id); |
| | 62 | |
|
| 141 | 63 | | if (PoolManager.i.ContainsPool(poolId)) |
| | 64 | | { |
| 141 | 65 | | clone.container = PoolManager.i.Get(poolId).gameObject; |
| 141 | 66 | | } |
| | 67 | | else |
| | 68 | | { |
| 0 | 69 | | Debug.LogError("Pool was removed and AssetLibrary didn't notice?!"); |
| 0 | 70 | | return null; |
| | 71 | | } |
| | 72 | |
|
| 141 | 73 | | return clone; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | public AssetType GetCopyFromOriginal(object id) |
| | 77 | | { |
| 22 | 78 | | if (!Contains(id)) |
| 0 | 79 | | return null; |
| | 80 | |
|
| 22 | 81 | | AssetType clone = masterAssets[id].Clone() as AssetType; |
| 22 | 82 | | object poolId = AssetIdToPoolId(id); |
| | 83 | |
|
| 22 | 84 | | if (PoolManager.i.ContainsPool(poolId)) |
| | 85 | | { |
| 22 | 86 | | clone.container = PoolManager.i.GetPool(poolId).InstantiateAsOriginal(); |
| 22 | 87 | | } |
| | 88 | | else |
| | 89 | | { |
| 0 | 90 | | Debug.LogError("Pool was removed and AssetLibrary didn't notice?!"); |
| 0 | 91 | | return null; |
| | 92 | | } |
| | 93 | |
|
| 22 | 94 | | return clone; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | public override void Release(AssetType asset) |
| | 98 | | { |
| 139 | 99 | | if (!Contains(asset)) |
| | 100 | | { |
| 0 | 101 | | Debug.LogError("ERROR: Trying to release an asset not added to this library!"); |
| 0 | 102 | | return; |
| | 103 | | } |
| | 104 | |
|
| 139 | 105 | | if (asset == null) |
| | 106 | | { |
| 0 | 107 | | Debug.LogError("ERROR: Trying to release null asset"); |
| 0 | 108 | | return; |
| | 109 | | } |
| | 110 | |
|
| 139 | 111 | | PoolManager.i.Release(asset.container); |
| 139 | 112 | | } |
| | 113 | |
|
| | 114 | | public override bool Contains(object id) |
| | 115 | | { |
| 831 | 116 | | if (masterAssets == null) |
| 0 | 117 | | return false; |
| | 118 | |
|
| 831 | 119 | | return masterAssets.ContainsKey(id); |
| | 120 | | } |
| | 121 | |
|
| | 122 | | public override bool Contains(AssetType asset) |
| | 123 | | { |
| 308 | 124 | | if (asset == null) |
| 2 | 125 | | return false; |
| | 126 | |
|
| 306 | 127 | | return Contains(asset.id); |
| | 128 | | } |
| | 129 | |
|
| | 130 | | public override void Cleanup() |
| | 131 | | { |
| 2864 | 132 | | foreach (var kvp in masterAssets) |
| | 133 | | { |
| 90 | 134 | | kvp.Value.Cleanup(); |
| | 135 | | } |
| | 136 | |
|
| 1342 | 137 | | masterAssets.Clear(); |
| 1342 | 138 | | } |
| | 139 | |
|
| 280 | 140 | | public object AssetIdToPoolId(object assetId) { return salt == null ? assetId : $"{assetId}{GetSaltSuffix()}"; } |
| | 141 | |
|
| | 142 | | public object PoolIdToAssetId(object poolId) |
| | 143 | | { |
| 112 | 144 | | if (salt == null) |
| 0 | 145 | | return poolId; |
| | 146 | |
|
| 112 | 147 | | string id = poolId.ToString(); |
| 112 | 148 | | string saltSuffix = GetSaltSuffix(); |
| 112 | 149 | | if (id.EndsWith(saltSuffix)) |
| | 150 | | { |
| 112 | 151 | | return id.Remove(id.Length - saltSuffix.Length); |
| | 152 | | } |
| 0 | 153 | | return poolId; |
| | 154 | | } |
| | 155 | |
|
| 392 | 156 | | string GetSaltSuffix() { return $"_{salt}"; } |
| | 157 | | } |
| | 158 | | } |