Update Project

This commit is contained in:
2026-03-20 13:04:43 +02:00
parent 9b587e6cba
commit beae2dea89
2295 changed files with 251259 additions and 33 deletions
@@ -0,0 +1,36 @@
using ModestTree;
namespace Zenject
{
public class FactoryProviderWrapper<TContract> : IFactory<TContract>
{
readonly IProvider _provider;
readonly InjectContext _injectContext;
public FactoryProviderWrapper(
IProvider provider, InjectContext injectContext)
{
Assert.That(injectContext.MemberType.DerivesFromOrEqual<TContract>());
_provider = provider;
_injectContext = injectContext;
}
public TContract Create()
{
var instance = _provider.GetInstance(_injectContext);
if (_injectContext.Container.IsValidating)
{
// During validation it is sufficient to just call the _provider.GetInstance
return default(TContract);
}
Assert.That(instance == null
|| instance.GetType().DerivesFromOrEqual(_injectContext.MemberType));
return (TContract)instance;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f1157303ef95f2448a355067327cb83a
timeCreated: 1488133028
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,62 @@
namespace Zenject
{
public interface IFactory
{
}
public interface IFactory<out TValue> : IFactory
{
TValue Create();
}
public interface IFactory<in TParam1, out TValue> : IFactory
{
TValue Create(TParam1 param);
}
public interface IFactory<in TParam1, in TParam2, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, in TParam4, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, in TParam7, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, in TParam7, in TParam8, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7, TParam8 param8);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, in TParam7, in TParam8, in TParam9, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7, TParam8 param8, TParam9 param9);
}
public interface IFactory<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, in TParam7, in TParam8, in TParam9, in TParam10, out TValue> : IFactory
{
TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7, TParam8 param8, TParam9 param9, TParam10 param10);
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 62c651ad6c58c1140ac224dc6284fa5e
timeCreated: 1461708050
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,193 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ModestTree;
using ModestTree.Util;
namespace Zenject
{
public abstract class KeyedFactoryBase<TBase, TKey> : IValidatable
{
[Inject]
readonly DiContainer _container = null;
[InjectOptional]
readonly List<ValuePair<TKey, Type>> _typePairs = null;
Dictionary<TKey, Type> _typeMap = null;
[InjectOptional]
readonly Type _fallbackType = null;
protected DiContainer Container
{
get { return _container; }
}
protected abstract IEnumerable<Type> ProvidedTypes
{
get;
}
public ICollection<TKey> Keys
{
get { return _typeMap.Keys; }
}
protected Dictionary<TKey, Type> TypeMap
{
get { return _typeMap; }
}
[Inject]
public void Initialize()
{
Assert.That(_fallbackType == null || _fallbackType.DerivesFromOrEqual<TBase>(),
"Expected fallback type '{0}' to derive from '{1}'", _fallbackType, typeof(TBase));
#if UNITY_EDITOR
var duplicates = _typePairs.Select(x => x.First).GetDuplicates();
if (!duplicates.IsEmpty())
{
throw Assert.CreateException(
"Found duplicate values in KeyedFactory: {0}", duplicates.Select(x => x.ToString()).Join(", "));
}
#endif
_typeMap = _typePairs.ToDictionary(x => x.First, x => x.Second);
_typePairs.Clear();
}
public bool HasKey(TKey key)
{
return _typeMap.ContainsKey(key);
}
protected Type GetTypeForKey(TKey key)
{
Type keyedType;
if (!_typeMap.TryGetValue(key, out keyedType))
{
Assert.IsNotNull(_fallbackType, "Could not find instance for key '{0}'", key);
return _fallbackType;
}
return keyedType;
}
public virtual void Validate()
{
foreach (var constructType in _typeMap.Values)
{
Container.InstantiateExplicit(
constructType, ValidationUtil.CreateDefaultArgs(ProvidedTypes.ToArray()));
}
}
protected static ConditionCopyNonLazyBinder AddBindingInternal<TDerived>(DiContainer container, TKey key)
where TDerived : TBase
{
return container.Bind<ValuePair<TKey, Type>>()
.FromInstance(ValuePair.New(key, typeof(TDerived)));
}
}
// Zero parameters
public class KeyedFactory<TBase, TKey> : KeyedFactoryBase<TBase, TKey>
{
protected override IEnumerable<Type> ProvidedTypes
{
get { return new Type[0]; }
}
public virtual TBase Create(TKey key)
{
var type = GetTypeForKey(key);
return (TBase)Container.Instantiate(type);
}
}
// One parameter
public class KeyedFactory<TBase, TKey, TParam1> : KeyedFactoryBase<TBase, TKey>
{
protected override IEnumerable<Type> ProvidedTypes
{
get { return new[] { typeof(TParam1) }; }
}
public virtual TBase Create(TKey key, TParam1 param1)
{
return (TBase)Container.InstantiateExplicit(
GetTypeForKey(key),
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1)
});
}
}
// Two parameters
public class KeyedFactory<TBase, TKey, TParam1, TParam2> : KeyedFactoryBase<TBase, TKey>
{
protected override IEnumerable<Type> ProvidedTypes
{
get { return new[] { typeof(TParam1), typeof(TParam2) }; }
}
public virtual TBase Create(TKey key, TParam1 param1, TParam2 param2)
{
return (TBase)Container.InstantiateExplicit(
GetTypeForKey(key),
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2)
});
}
}
// Three parameters
public class KeyedFactory<TBase, TKey, TParam1, TParam2, TParam3> : KeyedFactoryBase<TBase, TKey>
{
protected override IEnumerable<Type> ProvidedTypes
{
get { return new[] { typeof(TParam1), typeof(TParam2), typeof(TParam3) }; }
}
public virtual TBase Create(TKey key, TParam1 param1, TParam2 param2, TParam3 param3)
{
return (TBase)Container.InstantiateExplicit(
GetTypeForKey(key),
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2),
InjectUtil.CreateTypePair(param3)
});
}
}
// Four parameters
public class KeyedFactory<TBase, TKey, TParam1, TParam2, TParam3, TParam4> : KeyedFactoryBase<TBase, TKey>
{
protected override IEnumerable<Type> ProvidedTypes
{
get { return new[] { typeof(TParam1), typeof(TParam2), typeof(TParam3), typeof(TParam4) }; }
}
public virtual TBase Create(TKey key, TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4)
{
return (TBase)Container.InstantiateExplicit(
GetTypeForKey(key),
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2),
InjectUtil.CreateTypePair(param3),
InjectUtil.CreateTypePair(param4)
});
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 73ed4b67b639aaf4c94f61339a6ff413
timeCreated: 1461708051
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,302 @@
using System;
using System.Collections.Generic;
#if !NOT_UNITY3D
using JetBrains.Annotations;
#endif
namespace Zenject
{
// Zero parameters
public class PlaceholderFactory<TValue> : PlaceholderFactoryBase<TValue>, IFactory<TValue>
{
// Note: Most of the time you should not override this method and should instead
// use BindFactory<>.FromIFactory if you want to do some custom logic
#if !NOT_UNITY3D
[NotNull]
#endif
public virtual TValue Create()
{
return CreateInternal(new List<TypeValuePair>());
}
protected sealed override IEnumerable<Type> ParamTypes
{
get { yield break; }
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TValue> : PlaceholderFactory<TValue>
{
}
// One parameter
public class PlaceholderFactory<TParam1, TValue>
: PlaceholderFactoryBase<TValue>, IFactory<TParam1, TValue>
{
// Note: Most of the time you should not override this method and should instead
// use BindFactory<>.FromIFactory if you want to do some custom logic
#if !NOT_UNITY3D
[NotNull]
#endif
public virtual TValue Create(TParam1 param)
{
return CreateInternal(
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param)
});
}
protected sealed override IEnumerable<Type> ParamTypes
{
get { yield return typeof(TParam1); }
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TParam1, TValue> : PlaceholderFactory<TParam1, TValue>
{
}
// Two parameters
public class PlaceholderFactory<TParam1, TParam2, TValue>
: PlaceholderFactoryBase<TValue>, IFactory<TParam1, TParam2, TValue>
{
// Note: Most of the time you should not override this method and should instead
// use BindFactory<>.FromIFactory if you want to do some custom logic
#if !NOT_UNITY3D
[NotNull]
#endif
public virtual TValue Create(TParam1 param1, TParam2 param2)
{
return CreateInternal(
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2)
});
}
protected sealed override IEnumerable<Type> ParamTypes
{
get
{
yield return typeof(TParam1);
yield return typeof(TParam2);
}
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TParam1, TParam2, TValue> : PlaceholderFactory<TParam1, TParam2, TValue>
{
}
// Three parameters
public class PlaceholderFactory<TParam1, TParam2, TParam3, TValue>
: PlaceholderFactoryBase<TValue>, IFactory<TParam1, TParam2, TParam3, TValue>
{
// Note: Most of the time you should not override this method and should instead
// use BindFactory<>.FromIFactory if you want to do some custom logic
#if !NOT_UNITY3D
[NotNull]
#endif
public virtual TValue Create(TParam1 param1, TParam2 param2, TParam3 param3)
{
return CreateInternal(
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2),
InjectUtil.CreateTypePair(param3)
});
}
protected sealed override IEnumerable<Type> ParamTypes
{
get
{
yield return typeof(TParam1);
yield return typeof(TParam2);
yield return typeof(TParam3);
}
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TParam1, TParam2, TParam3, TValue> : PlaceholderFactory<TParam1, TParam2, TParam3, TValue>
{
}
// Four parameters
public class PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TValue>
: PlaceholderFactoryBase<TValue>, IFactory<TParam1, TParam2, TParam3, TParam4, TValue>
{
// Note: Most of the time you should not override this method and should instead
// use BindFactory<>.FromIFactory if you want to do some custom logic
#if !NOT_UNITY3D
[NotNull]
#endif
public virtual TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4)
{
return CreateInternal(
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2),
InjectUtil.CreateTypePair(param3),
InjectUtil.CreateTypePair(param4)
});
}
protected sealed override IEnumerable<Type> ParamTypes
{
get
{
yield return typeof(TParam1);
yield return typeof(TParam2);
yield return typeof(TParam3);
yield return typeof(TParam4);
}
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TParam1, TParam2, TParam3, TParam4, TValue>
: PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TValue>
{
}
// Five parameters
public class PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
: PlaceholderFactoryBase<TValue>, IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
{
// Note: Most of the time you should not override this method and should instead
// use BindFactory<>.FromIFactory if you want to do some custom logic
#if !NOT_UNITY3D
[NotNull]
#endif
public virtual TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5)
{
return CreateInternal(
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2),
InjectUtil.CreateTypePair(param3),
InjectUtil.CreateTypePair(param4),
InjectUtil.CreateTypePair(param5)
});
}
protected sealed override IEnumerable<Type> ParamTypes
{
get
{
yield return typeof(TParam1);
yield return typeof(TParam2);
yield return typeof(TParam3);
yield return typeof(TParam4);
yield return typeof(TParam5);
}
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
: PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
{
}
// Six parameters
public class PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
: PlaceholderFactoryBase<TValue>, IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
{
// Note: Most of the time you should not override this method and should instead
// use BindFactory<>.FromIFactory if you want to do some custom logic
#if !NOT_UNITY3D
[NotNull]
#endif
public virtual TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6)
{
return CreateInternal(
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2),
InjectUtil.CreateTypePair(param3),
InjectUtil.CreateTypePair(param4),
InjectUtil.CreateTypePair(param5),
InjectUtil.CreateTypePair(param6)
});
}
protected sealed override IEnumerable<Type> ParamTypes
{
get
{
yield return typeof(TParam1);
yield return typeof(TParam2);
yield return typeof(TParam3);
yield return typeof(TParam4);
yield return typeof(TParam5);
yield return typeof(TParam6);
}
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
: PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
{
}
// Ten parameters
public class PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10, TValue>
: PlaceholderFactoryBase<TValue>, IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10, TValue>
{
// If you were hoping to override this method, use BindFactory<>.ToFactory instead
public virtual TValue Create(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7, TParam8 param8, TParam9 param9, TParam10 param10)
{
return CreateInternal(
new List<TypeValuePair>
{
InjectUtil.CreateTypePair(param1),
InjectUtil.CreateTypePair(param2),
InjectUtil.CreateTypePair(param3),
InjectUtil.CreateTypePair(param4),
InjectUtil.CreateTypePair(param5),
InjectUtil.CreateTypePair(param6),
InjectUtil.CreateTypePair(param7),
InjectUtil.CreateTypePair(param8),
InjectUtil.CreateTypePair(param9),
InjectUtil.CreateTypePair(param10)
});
}
protected sealed override IEnumerable<Type> ParamTypes
{
get
{
yield return typeof(TParam1);
yield return typeof(TParam2);
yield return typeof(TParam3);
yield return typeof(TParam4);
yield return typeof(TParam5);
yield return typeof(TParam6);
yield return typeof(TParam7);
yield return typeof(TParam8);
yield return typeof(TParam9);
yield return typeof(TParam10);
}
}
}
[Obsolete("Zenject.Factory has been renamed to PlaceholderFactory. Zenject.Factory will be removed in future versions")]
public class Factory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10, TValue>
: PlaceholderFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TParam9, TParam10, TValue>
{
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 665a56f53857092408155478e576cfd0
timeCreated: 1486691381
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ModestTree;
namespace Zenject
{
public interface IPlaceholderFactory : IValidatable
{
}
// Placeholder factories can be used to choose a creation method in an installer, using FactoryBinder
public abstract class PlaceholderFactoryBase<TValue> : IPlaceholderFactory
{
IProvider _provider;
InjectContext _injectContext;
[Inject]
void Construct(IProvider provider, InjectContext injectContext)
{
Assert.IsNotNull(provider);
Assert.IsNotNull(injectContext);
_provider = provider;
_injectContext = injectContext;
}
protected TValue CreateInternal(List<TypeValuePair> extraArgs)
{
try
{
var result = _provider.GetInstance(_injectContext, extraArgs);
if (_injectContext.Container.IsValidating && result is ValidationMarker)
{
return default(TValue);
}
Assert.That(result == null || result.GetType().DerivesFromOrEqual<TValue>());
return (TValue) result;
}
catch (Exception e)
{
throw new ZenjectException(
"Error during construction of type '{0}' via {1}.Create method!".Fmt(typeof(TValue), GetType()), e);
}
}
public virtual void Validate()
{
_provider.GetInstance(
_injectContext, ValidationUtil.CreateDefaultArgs(ParamTypes.ToArray()));
}
protected abstract IEnumerable<Type> ParamTypes
{
get;
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 2a15cf923ae4d72418e9951480e9f178
timeCreated: 1527952932
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 0f3fb191ad2b6c74c97f488543a89c82
folderAsset: yes
timeCreated: 1484511595
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,90 @@
using System;
namespace Zenject
{
public interface IMemoryPool
{
int NumTotal { get; }
int NumActive { get; }
int NumInactive { get; }
Type ItemType
{
get;
}
/// <summary>
/// Changes pool size by creating new elements or destroying existing elements
/// This bypasses the configured expansion method (OneAtATime or Doubling)
/// </summary>
void Resize(int desiredPoolSize);
void Clear();
/// <summary>
/// Expands the pool by the additional size.
/// This bypasses the configured expansion method (OneAtATime or Doubling)
/// </summary>
/// <param name="numToAdd">The additional number of items to allocate in the pool</param>
void ExpandBy(int numToAdd);
/// <summary>
/// Shrinks the MemoryPool by removing a given number of elements
/// This bypasses the configured expansion method (OneAtATime or Doubling)
/// </summary>
/// <param name="numToRemove">The amount of items to remove from the pool</param>
void ShrinkBy(int numToRemove);
void Despawn(object obj);
}
public interface IDespawnableMemoryPool<TValue> : IMemoryPool
{
void Despawn(TValue item);
}
public interface IMemoryPool<TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn();
}
public interface IMemoryPool<in TParam1, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param);
}
public interface IMemoryPool<in TParam1, in TParam2, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param1, TParam2 param2);
}
public interface IMemoryPool<in TParam1, in TParam2, in TParam3, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3);
}
public interface IMemoryPool<in TParam1, in TParam2, in TParam3, in TParam4, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4);
}
public interface IMemoryPool<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5);
}
public interface IMemoryPool<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6);
}
public interface IMemoryPool<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, in TParam7, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7);
}
public interface IMemoryPool<in TParam1, in TParam2, in TParam3, in TParam4, in TParam5, in TParam6, in TParam7, in TParam8, TValue> : IDespawnableMemoryPool<TValue>
{
TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7, TParam8 param8);
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 13b8377ec575a1a44a1028ee324ba17a
timeCreated: 1485699960
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,316 @@
namespace Zenject
{
// Zero parameters
public class MemoryPool<TValue> : MemoryPoolBase<TValue>, IMemoryPool<TValue>, IFactory<TValue>
{
public TValue Spawn()
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(item);
}
}
return item;
}
protected virtual void Reinitialize(TValue item)
{
// Optional
}
TValue IFactory<TValue>.Create()
{
return Spawn();
}
}
// One parameter
public class MemoryPool<TParam1, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TValue>, IFactory<TParam1, TValue>
{
public TValue Spawn(TParam1 param)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TValue>.Create(TParam1 p1)
{
return Spawn(p1);
}
}
// Two parameters
public class MemoryPool<TParam1, TParam2, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TValue>, IFactory<TParam1, TParam2, TValue>
{
public TValue Spawn(TParam1 param1, TParam2 param2)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param1, param2, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TParam2 p2, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TParam2, TValue>.Create(TParam1 p1, TParam2 p2)
{
return Spawn(p1, p2);
}
}
// Three parameters
public class MemoryPool<TParam1, TParam2, TParam3, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TValue>, IFactory<TParam1, TParam2, TParam3, TValue>
{
public TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param1, param2, param3, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TParam2, TParam3, TValue>.Create(TParam1 p1, TParam2 p2, TParam3 p3)
{
return Spawn(p1, p2, p3);
}
}
// Four parameters
public class MemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>, IFactory<TParam1, TParam2, TParam3, TParam4, TValue>
{
public TValue Spawn(TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param1, param2, param3, param4, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TParam2, TParam3, TParam4, TValue>.Create(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4)
{
return Spawn(p1, p2, p3, p4);
}
}
// Five parameters
public class MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>, IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
{
public TValue Spawn(
TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param1, param2, param3, param4, param5, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>.Create(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5)
{
return Spawn(p1, p2, p3, p4, p5);
}
}
// Six parameters
public class MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>,
IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
{
public TValue Spawn(
TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param1, param2, param3, param4, param5, param6, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>.Create(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6)
{
return Spawn(p1, p2, p3, p4, p5, p6);
}
}
// Seven parameters
public class MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>,
IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
{
public TValue Spawn(
TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param1, param2, param3, param4, param5, param6, param7, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>.Create(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7)
{
return Spawn(p1, p2, p3, p4, p5, p6, p7);
}
}
// Eight parameters
public class MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>
: MemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>,
IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>
{
public TValue Spawn(
TParam1 param1, TParam2 param2, TParam3 param3, TParam4 param4, TParam5 param5, TParam6 param6, TParam7 param7, TParam8 param8)
{
var item = GetInternal();
if (!Container.IsValidating)
{
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.Reinitialize", GetType()))
#endif
{
Reinitialize(param1, param2, param3, param4, param5, param6, param7, param8, item);
}
}
return item;
}
protected virtual void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TParam8 p8, TValue item)
{
// Optional
}
TValue IFactory<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>.Create(
TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TParam8 p8)
{
return Spawn(p1, p2, p3, p4, p5, p6, p7, p8);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 305d88c8b2af3f2479cb45ffb6f2923c
timeCreated: 1485699960
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,290 @@
using System;
using System.Collections.Generic;
using ModestTree;
namespace Zenject
{
[NoReflectionBaking]
public class PoolExceededFixedSizeException : Exception
{
public PoolExceededFixedSizeException(string errorMessage)
: base(errorMessage)
{
}
}
[Serializable]
public class MemoryPoolSettings
{
public int InitialSize;
public int MaxSize;
public PoolExpandMethods ExpandMethod;
public MemoryPoolSettings()
{
InitialSize = 0;
MaxSize = int.MaxValue;
ExpandMethod = PoolExpandMethods.OneAtATime;
}
public MemoryPoolSettings(int initialSize, int maxSize, PoolExpandMethods expandMethod)
{
InitialSize = initialSize;
MaxSize = maxSize;
ExpandMethod = expandMethod;
}
public static readonly MemoryPoolSettings Default = new MemoryPoolSettings();
}
[ZenjectAllowDuringValidation]
public class MemoryPoolBase<TContract> : IValidatable, IMemoryPool, IDisposable
{
Stack<TContract> _inactiveItems;
IFactory<TContract> _factory;
MemoryPoolSettings _settings;
DiContainer _container;
int _activeCount;
[Inject]
void Construct(
IFactory<TContract> factory,
DiContainer container,
[InjectOptional]
MemoryPoolSettings settings)
{
_settings = settings ?? MemoryPoolSettings.Default;
_factory = factory;
_container = container;
_inactiveItems = new Stack<TContract>(_settings.InitialSize);
if (!container.IsValidating)
{
for (int i = 0; i < _settings.InitialSize; i++)
{
_inactiveItems.Push(AllocNew());
}
}
#if UNITY_EDITOR
StaticMemoryPoolRegistry.Add(this);
#endif
}
protected DiContainer Container
{
get { return _container; }
}
public IEnumerable<TContract> InactiveItems
{
get { return _inactiveItems; }
}
public int NumTotal
{
get { return NumInactive + NumActive; }
}
public int NumInactive
{
get { return _inactiveItems.Count; }
}
public int NumActive
{
get { return _activeCount; }
}
public Type ItemType
{
get { return typeof(TContract); }
}
public void Dispose()
{
#if UNITY_EDITOR
StaticMemoryPoolRegistry.Remove(this);
#endif
}
void IMemoryPool.Despawn(object item)
{
Despawn((TContract)item);
}
public void Despawn(TContract item)
{
Assert.That(!_inactiveItems.Contains(item),
"Tried to return an item to pool {0} twice", GetType());
_activeCount--;
_inactiveItems.Push(item);
#if ZEN_INTERNAL_PROFILING
using (ProfileTimers.CreateTimedBlock("User Code"))
#endif
#if UNITY_EDITOR
using (ProfileBlock.Start("{0}.OnDespawned", GetType()))
#endif
{
OnDespawned(item);
}
if (_inactiveItems.Count > _settings.MaxSize)
{
Resize(_settings.MaxSize);
}
}
TContract AllocNew()
{
try
{
var item = _factory.Create();
if (!_container.IsValidating)
{
Assert.IsNotNull(item, "Factory '{0}' returned null value when creating via {1}!", _factory.GetType(), GetType());
OnCreated(item);
}
return item;
}
catch (Exception e)
{
throw new ZenjectException(
"Error during construction of type '{0}' via {1}.Create method!".Fmt(
typeof(TContract), GetType()), e);
}
}
void IValidatable.Validate()
{
try
{
_factory.Create();
}
catch (Exception e)
{
throw new ZenjectException(
"Validation for factory '{0}' failed".Fmt(GetType()), e);
}
}
public void Clear()
{
Resize(0);
}
public void ShrinkBy(int numToRemove)
{
Resize(_inactiveItems.Count - numToRemove);
}
public void ExpandBy(int numToAdd)
{
Resize(_inactiveItems.Count + numToAdd);
}
protected TContract GetInternal()
{
if (_inactiveItems.Count == 0)
{
ExpandPool();
Assert.That(!_inactiveItems.IsEmpty());
}
var item = _inactiveItems.Pop();
_activeCount++;
OnSpawned(item);
return item;
}
public void Resize(int desiredPoolSize)
{
if (_inactiveItems.Count == desiredPoolSize)
{
return;
}
if (_settings.ExpandMethod == PoolExpandMethods.Disabled)
{
throw new PoolExceededFixedSizeException(
"Pool factory '{0}' attempted resize but pool set to fixed size of '{1}'!"
.Fmt(GetType(), _inactiveItems.Count));
}
Assert.That(desiredPoolSize >= 0, "Attempted to resize the pool to a negative amount");
while (_inactiveItems.Count > desiredPoolSize)
{
OnDestroyed(_inactiveItems.Pop());
}
while (desiredPoolSize > _inactiveItems.Count)
{
_inactiveItems.Push(AllocNew());
}
Assert.IsEqual(_inactiveItems.Count, desiredPoolSize);
}
void ExpandPool()
{
switch (_settings.ExpandMethod)
{
case PoolExpandMethods.Disabled:
{
throw new PoolExceededFixedSizeException(
"Pool factory '{0}' exceeded its fixed size of '{1}'!"
.Fmt(GetType(), _inactiveItems.Count));
}
case PoolExpandMethods.OneAtATime:
{
ExpandBy(1);
break;
}
case PoolExpandMethods.Double:
{
if (NumTotal == 0)
{
ExpandBy(1);
}
else
{
ExpandBy(NumTotal);
}
break;
}
default:
{
throw Assert.CreateException();
}
}
}
protected virtual void OnDespawned(TContract item)
{
// Optional
}
protected virtual void OnSpawned(TContract item)
{
// Optional
}
protected virtual void OnCreated(TContract item)
{
// Optional
}
protected virtual void OnDestroyed(TContract item)
{
// Optional
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: ba9d727a5d866fb46a576ab76012b727
timeCreated: 1485703991
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,254 @@
using UnityEngine;
namespace Zenject
{
// Zero parameters
// NOTE: For this to work, the given component must be at the root game object of the thing
// you want to use in a pool
public class MonoMemoryPool<TValue> : MemoryPool<TValue>
where TValue : Component
{
Transform _originalParent;
[Inject]
public MonoMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
// Record the original parent which will be set to whatever is used in the UnderTransform method
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnSpawned(TValue item)
{
item.gameObject.SetActive(true);
}
protected override void OnDespawned(TValue item)
{
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
}
// One parameter
// NOTE: For this to work, the given component must be at the root game object of the thing
// you want to use in a pool
public class MonoMemoryPool<TParam1, TValue> : MemoryPool<TParam1, TValue>
where TValue : Component
{
Transform _originalParent;
[Inject]
public MonoMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
// Record the original parent which will be set to whatever is used in the UnderTransform method
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnSpawned(TValue item)
{
item.gameObject.SetActive(true);
}
protected override void OnDespawned(TValue item)
{
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
}
// Two parameters
// NOTE: For this to work, the given component must be at the root game object of the thing
// you want to use in a pool
public class MonoMemoryPool<TParam1, TParam2, TValue>
: MemoryPool<TParam1, TParam2, TValue>
where TValue : Component
{
Transform _originalParent;
[Inject]
public MonoMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
// Record the original parent which will be set to whatever is used in the UnderTransform method
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnSpawned(TValue item)
{
item.gameObject.SetActive(true);
}
protected override void OnDespawned(TValue item)
{
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
}
// Three parameters
// NOTE: For this to work, the given component must be at the root game object of the thing
// you want to use in a pool
public class MonoMemoryPool<TParam1, TParam2, TParam3, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TValue>
where TValue : Component
{
Transform _originalParent;
[Inject]
public MonoMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
// Record the original parent which will be set to whatever is used in the UnderTransform method
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnSpawned(TValue item)
{
item.gameObject.SetActive(true);
}
protected override void OnDespawned(TValue item)
{
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
}
// Four parameters
// NOTE: For this to work, the given component must be at the root game object of the thing
// you want to use in a pool
public class MonoMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
where TValue : Component
{
Transform _originalParent;
[Inject]
public MonoMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
// Record the original parent which will be set to whatever is used in the UnderTransform method
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnSpawned(TValue item)
{
item.gameObject.SetActive(true);
}
protected override void OnDespawned(TValue item)
{
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
}
// Five parameters
// NOTE: For this to work, the given component must be at the root game object of the thing
// you want to use in a pool
public class MonoMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
where TValue : Component
{
Transform _originalParent;
[Inject]
public MonoMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
// Record the original parent which will be set to whatever is used in the UnderTransform method
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnSpawned(TValue item)
{
item.gameObject.SetActive(true);
}
protected override void OnDespawned(TValue item)
{
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1334aecb808e6c145a993cb441f43bdb
timeCreated: 1485703991
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,372 @@
using UnityEngine;
namespace Zenject
{
// Zero parameters
public class MonoPoolableMemoryPool<TValue> : MemoryPool<TValue>
where TValue : Component, IPoolable
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned();
}
}
// One parameters
public class MonoPoolableMemoryPool<TParam1, TValue>
: MemoryPool<TParam1, TValue>
where TValue : Component, IPoolable<TParam1>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1);
}
}
// Two parameters
public class MonoPoolableMemoryPool<TParam1, TParam2, TValue>
: MemoryPool<TParam1, TParam2, TValue>
where TValue : Component, IPoolable<TParam1, TParam2>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1, p2);
}
}
// Three parameters
public class MonoPoolableMemoryPool<TParam1, TParam2, TParam3, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TValue>
where TValue : Component, IPoolable<TParam1, TParam2, TParam3>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1, p2, p3);
}
}
// Four parameters
public class MonoPoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
where TValue : Component, IPoolable<TParam1, TParam2, TParam3, TParam4>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1, p2, p3, p4);
}
}
// Five parameters
public class MonoPoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
where TValue : Component, IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1, p2, p3, p4, p5);
}
}
// Six parameters
public class MonoPoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
where TValue : Component, IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1, p2, p3, p4, p5, p6);
}
}
// Seven parameters
public class MonoPoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
where TValue : Component, IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1, p2, p3, p4, p5, p6, p7);
}
}
// Eight parameters
public class MonoPoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>
where TValue : Component, IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8>
{
Transform _originalParent;
[Inject]
public MonoPoolableMemoryPool()
{
}
protected override void OnCreated(TValue item)
{
item.gameObject.SetActive(false);
_originalParent = item.transform.parent;
}
protected override void OnDestroyed(TValue item)
{
GameObject.Destroy(item.gameObject);
}
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
item.gameObject.SetActive(false);
if (item.transform.parent != _originalParent)
{
item.transform.SetParent(_originalParent, false);
}
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TParam8 p8, TValue item)
{
item.gameObject.SetActive(true);
item.OnSpawned(p1, p2, p3, p4, p5, p6, p7, p8);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 5475433f41ca61c4a9095affbf60b848
timeCreated: 1528535898
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,38 @@
using System;
namespace Zenject
{
// Here we assume that each spawned object does the work of returning itself to the pool
// in its own Dispose method
public class PoolWrapperFactory<T> : IFactory<T>
where T : IDisposable
{
readonly IMemoryPool<T> _pool;
public PoolWrapperFactory(IMemoryPool<T> pool)
{
_pool = pool;
}
public T Create()
{
return _pool.Spawn();
}
}
public class PoolWrapperFactory<TParam1, TValue> : IFactory<TParam1, TValue>
where TValue : IDisposable
{
readonly IMemoryPool<TParam1, TValue> _pool;
public PoolWrapperFactory(IMemoryPool<TParam1, TValue> pool)
{
_pool = pool;
}
public TValue Create(TParam1 arg)
{
return _pool.Spawn(arg);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 125bde4ab63a4984484c450640ee1702
timeCreated: 1518946218
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,147 @@
namespace Zenject
{
// Zero parameters
public class PoolableMemoryPool<TValue>
: MemoryPool<TValue>
where TValue : IPoolable
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TValue item)
{
item.OnSpawned();
}
}
// One parameters
public class PoolableMemoryPool<TParam1, TValue>
: MemoryPool<TParam1, TValue>
where TValue : IPoolable<TParam1>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TValue item)
{
item.OnSpawned(p1);
}
}
// Two parameters
public class PoolableMemoryPool<TParam1, TParam2, TValue>
: MemoryPool<TParam1, TParam2, TValue>
where TValue : IPoolable<TParam1, TParam2>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TValue item)
{
item.OnSpawned(p1, p2);
}
}
// Three parameters
public class PoolableMemoryPool<TParam1, TParam2, TParam3, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TValue>
where TValue : IPoolable<TParam1, TParam2, TParam3>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TValue item)
{
item.OnSpawned(p1, p2, p3);
}
}
// Four parameters
public class PoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
where TValue : IPoolable<TParam1, TParam2, TParam3, TParam4>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TValue item)
{
item.OnSpawned(p1, p2, p3, p4);
}
}
// Five parameters
public class PoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
where TValue : IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TValue item)
{
item.OnSpawned(p1, p2, p3, p4, p5);
}
}
// Six parameters
public class PoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
where TValue : IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TValue item)
{
item.OnSpawned(p1, p2, p3, p4, p5, p6);
}
}
// Seven parameters
public class PoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
where TValue : IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TValue item)
{
item.OnSpawned(p1, p2, p3, p4, p5, p6, p7);
}
}
// Eight parameters
public class PoolableMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>
: MemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8, TValue>
where TValue : IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TParam8>
{
protected override void OnDespawned(TValue item)
{
item.OnDespawned();
}
protected override void Reinitialize(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TParam8 p8, TValue item)
{
item.OnSpawned(p1, p2, p3, p4, p5, p6, p7, p8);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 7fa0c435e1800a048ac5f7c53e03472d
timeCreated: 1517205083
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 28d06bc0b4cdf374ca41d78b0ec5dc0a
folderAsset: yes
timeCreated: 1515014902
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,154 @@
namespace Zenject
{
public class PoolableStaticMemoryPool<TValue> : StaticMemoryPool<TValue>
where TValue : class, IPoolable, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TValue value)
{
value.OnSpawned();
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
public class PoolableStaticMemoryPool<TParam1, TValue> : StaticMemoryPool<TParam1, TValue>
where TValue : class, IPoolable<TParam1>, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TParam1 p1, TValue value)
{
value.OnSpawned(p1);
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
public class PoolableStaticMemoryPool<TParam1, TParam2, TValue> : StaticMemoryPool<TParam1, TParam2, TValue>
where TValue : class, IPoolable<TParam1, TParam2>, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TParam1 p1, TParam2 p2, TValue value)
{
value.OnSpawned(p1, p2);
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
public class PoolableStaticMemoryPool<TParam1, TParam2, TParam3, TValue> : StaticMemoryPool<TParam1, TParam2, TParam3, TValue>
where TValue : class, IPoolable<TParam1, TParam2, TParam3>, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TParam1 p1, TParam2 p2, TParam3 p3, TValue value)
{
value.OnSpawned(p1, p2, p3);
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
public class PoolableStaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue> : StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
where TValue : class, IPoolable<TParam1, TParam2, TParam3, TParam4>, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TValue value)
{
value.OnSpawned(p1, p2, p3, p4);
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
public class PoolableStaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue> : StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
where TValue : class, IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5>, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TValue value)
{
value.OnSpawned(p1, p2, p3, p4, p5);
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
public class PoolableStaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue> : StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
where TValue : class, IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6>, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TValue value)
{
value.OnSpawned(p1, p2, p3, p4, p5, p6);
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
public class PoolableStaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue> : StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
where TValue : class, IPoolable<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7>, new()
{
public PoolableStaticMemoryPool()
: base(OnSpawned, OnDespawned)
{
}
static void OnSpawned(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7, TValue value)
{
value.OnSpawned(p1, p2, p3, p4, p5, p6, p7);
}
static void OnDespawned(TValue value)
{
value.OnDespawned();
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 3bf78918a4d09d44695230ef305b6e1a
timeCreated: 1528618262
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,557 @@
using System;
using System.Collections.Generic;
using ModestTree;
namespace Zenject
{
[NoReflectionBaking]
public abstract class StaticMemoryPoolBaseBase<TValue> : IDespawnableMemoryPool<TValue>, IDisposable
where TValue : class
{
// I also tried using ConcurrentBag instead of Stack + lock here but that performed much much worse
readonly Stack<TValue> _stack = new Stack<TValue>();
Action<TValue> _onDespawnedMethod;
int _activeCount;
#if ZEN_MULTITHREADING
protected readonly object _locker = new object();
#endif
public StaticMemoryPoolBaseBase(Action<TValue> onDespawnedMethod)
{
_onDespawnedMethod = onDespawnedMethod;
#if UNITY_EDITOR
StaticMemoryPoolRegistry.Add(this);
#endif
}
public Action<TValue> OnDespawnedMethod
{
set { _onDespawnedMethod = value; }
}
public int NumTotal
{
get { return NumInactive + NumActive; }
}
public int NumActive
{
get
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
return _activeCount;
}
}
}
public int NumInactive
{
get
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
return _stack.Count;
}
}
}
public Type ItemType
{
get { return typeof(TValue); }
}
public void Resize(int desiredPoolSize)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
ResizeInternal(desiredPoolSize);
}
}
// We assume here that we're in a lock
void ResizeInternal(int desiredPoolSize)
{
Assert.That(desiredPoolSize >= 0, "Attempted to resize the pool to a negative amount");
while (_stack.Count > desiredPoolSize)
{
_stack.Pop();
}
while (desiredPoolSize > _stack.Count)
{
_stack.Push(Alloc());
}
Assert.IsEqual(_stack.Count, desiredPoolSize);
}
public void Dispose()
{
#if UNITY_EDITOR
StaticMemoryPoolRegistry.Remove(this);
#endif
}
public void ClearActiveCount()
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
_activeCount = 0;
}
}
public void Clear()
{
Resize(0);
}
public void ShrinkBy(int numToRemove)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
ResizeInternal(_stack.Count - numToRemove);
}
}
public void ExpandBy(int numToAdd)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
ResizeInternal(_stack.Count + numToAdd);
}
}
// We assume here that we're in a lock
protected TValue SpawnInternal()
{
TValue element;
if (_stack.Count == 0)
{
element = Alloc();
}
else
{
element = _stack.Pop();
}
_activeCount++;
return element;
}
void IMemoryPool.Despawn(object item)
{
Despawn((TValue)item);
}
public void Despawn(TValue element)
{
if (_onDespawnedMethod != null)
{
_onDespawnedMethod(element);
}
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
Assert.That(!_stack.Contains(element), "Attempted to despawn element twice!");
_activeCount--;
_stack.Push(element);
}
}
protected abstract TValue Alloc();
}
[NoReflectionBaking]
public abstract class StaticMemoryPoolBase<TValue> : StaticMemoryPoolBaseBase<TValue>
where TValue : class, new()
{
public StaticMemoryPoolBase(Action<TValue> onDespawnedMethod)
: base(onDespawnedMethod)
{
}
protected override TValue Alloc()
{
return new TValue();
}
}
// Zero parameters
[NoReflectionBaking]
public class StaticMemoryPool<TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TValue>
where TValue : class, new()
{
Action<TValue> _onSpawnMethod;
public StaticMemoryPool(
Action<TValue> onSpawnMethod = null, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
_onSpawnMethod = onSpawnMethod;
}
public Action<TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn()
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(item);
}
return item;
}
}
}
// One parameter
[NoReflectionBaking]
public class StaticMemoryPool<TParam1, TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TParam1, TValue>
where TValue : class, new()
{
Action<TParam1, TValue> _onSpawnMethod;
public StaticMemoryPool(
Action<TParam1, TValue> onSpawnMethod, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
// What's the point of having a param otherwise?
Assert.IsNotNull(onSpawnMethod);
_onSpawnMethod = onSpawnMethod;
}
public Action<TParam1, TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn(TParam1 param)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(param, item);
}
return item;
}
}
}
// Two parameter
[NoReflectionBaking]
public class StaticMemoryPool<TParam1, TParam2, TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TValue>
where TValue : class, new()
{
Action<TParam1, TParam2, TValue> _onSpawnMethod;
public StaticMemoryPool(
Action<TParam1, TParam2, TValue> onSpawnMethod, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
// What's the point of having a param otherwise?
Assert.IsNotNull(onSpawnMethod);
_onSpawnMethod = onSpawnMethod;
}
public Action<TParam1, TParam2, TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn(TParam1 p1, TParam2 p2)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(p1, p2, item);
}
return item;
}
}
}
// Three parameters
[NoReflectionBaking]
public class StaticMemoryPool<TParam1, TParam2, TParam3, TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TValue>
where TValue : class, new()
{
Action<TParam1, TParam2, TParam3, TValue> _onSpawnMethod;
public StaticMemoryPool(
Action<TParam1, TParam2, TParam3, TValue> onSpawnMethod, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
// What's the point of having a param otherwise?
Assert.IsNotNull(onSpawnMethod);
_onSpawnMethod = onSpawnMethod;
}
public Action<TParam1, TParam2, TParam3, TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn(TParam1 p1, TParam2 p2, TParam3 p3)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(p1, p2, p3, item);
}
return item;
}
}
}
// Four parameters
[NoReflectionBaking]
public class StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TValue>
where TValue : class, new()
{
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TValue> _onSpawnMethod;
public StaticMemoryPool(
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TValue> onSpawnMethod, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
// What's the point of having a param otherwise?
Assert.IsNotNull(onSpawnMethod);
_onSpawnMethod = onSpawnMethod;
}
public
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(p1, p2, p3, p4, item);
}
return item;
}
}
}
// Five parameters
[NoReflectionBaking]
public class StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TValue>
where TValue : class, new()
{
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TValue> _onSpawnMethod;
public StaticMemoryPool(
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TValue> onSpawnMethod, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
// What's the point of having a param otherwise?
Assert.IsNotNull(onSpawnMethod);
_onSpawnMethod = onSpawnMethod;
}
public
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(p1, p2, p3, p4, p5, item);
}
return item;
}
}
}
// Six parameters
[NoReflectionBaking]
public class StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue>
where TValue : class, new()
{
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue> _onSpawnMethod;
public StaticMemoryPool(
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue> onSpawnMethod, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
// What's the point of having a param otherwise?
Assert.IsNotNull(onSpawnMethod);
_onSpawnMethod = onSpawnMethod;
}
public
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(p1, p2, p3, p4, p5, p6, item);
}
return item;
}
}
}
// Seven parameters
[NoReflectionBaking]
public class StaticMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue> : StaticMemoryPoolBase<TValue>, IMemoryPool<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue>
where TValue : class, new()
{
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue> _onSpawnMethod;
public StaticMemoryPool(
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue> onSpawnMethod, Action<TValue> onDespawnedMethod = null)
: base(onDespawnedMethod)
{
// What's the point of having a param otherwise?
Assert.IsNotNull(onSpawnMethod);
_onSpawnMethod = onSpawnMethod;
}
public
#if !NET_4_6
ModestTree.Util.
#endif
Action<TParam1, TParam2, TParam3, TParam4, TParam5, TParam6, TParam7, TValue> OnSpawnMethod
{
set { _onSpawnMethod = value; }
}
public TValue Spawn(TParam1 p1, TParam2 p2, TParam3 p3, TParam4 p4, TParam5 p5, TParam6 p6, TParam7 p7)
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
var item = SpawnInternal();
if (_onSpawnMethod != null)
{
_onSpawnMethod(p1, p2, p3, p4, p5, p6, p7, item);
}
return item;
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 56b2eb81b3385fd47a0500abeac7b60e
timeCreated: 1528024394
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: 6214acccd2e0c2044921cc068fb3d0a6
folderAsset: yes
timeCreated: 1520607639
licenseType: Free
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,54 @@
using System.Collections.Generic;
namespace Zenject
{
public class ArrayPool<T> : StaticMemoryPoolBaseBase<T[]>
{
readonly int _length;
public ArrayPool(int length)
: base(OnDespawned)
{
_length = length;
}
static void OnDespawned(T[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i] = default(T);
}
}
public T[] Spawn()
{
#if ZEN_MULTITHREADING
lock (_locker)
#endif
{
return SpawnInternal();
}
}
protected override T[] Alloc()
{
return new T[_length];
}
static readonly Dictionary<int, ArrayPool<T>> _pools =
new Dictionary<int, ArrayPool<T>>();
public static ArrayPool<T> GetPool(int length)
{
ArrayPool<T> pool;
if (!_pools.TryGetValue(length, out pool))
{
pool = new ArrayPool<T>(length);
_pools.Add(length, pool);
}
return pool;
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 891d6e713fcfdfc4a95271fdee018e94
timeCreated: 1537870146
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,32 @@
using System.Collections.Generic;
using ModestTree;
namespace Zenject
{
public class DictionaryPool<TKey, TValue> : StaticMemoryPool<Dictionary<TKey, TValue>>
{
static DictionaryPool<TKey, TValue> _instance = new DictionaryPool<TKey, TValue>();
public DictionaryPool()
{
OnSpawnMethod = OnSpawned;
OnDespawnedMethod = OnDespawned;
}
public static DictionaryPool<TKey, TValue> Instance
{
get { return _instance; }
}
static void OnSpawned(Dictionary<TKey, TValue> items)
{
Assert.That(items.IsEmpty());
}
static void OnDespawned(Dictionary<TKey, TValue> items)
{
items.Clear();
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 031a0490d1684424e9e33b22106cb178
timeCreated: 1520670682
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,31 @@
using System.Collections.Generic;
using ModestTree;
namespace Zenject
{
public class HashSetPool<T> : StaticMemoryPool<HashSet<T>>
{
static HashSetPool<T> _instance = new HashSetPool<T>();
public HashSetPool()
{
OnSpawnMethod = OnSpawned;
OnDespawnedMethod = OnDespawned;
}
public static HashSetPool<T> Instance
{
get { return _instance; }
}
static void OnSpawned(HashSet<T> items)
{
Assert.That(items.IsEmpty());
}
static void OnDespawned(HashSet<T> items)
{
items.Clear();
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 89c66035001289a4199cb04e17d0bc28
timeCreated: 1520670682
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,24 @@
using System.Collections.Generic;
namespace Zenject
{
public class ListPool<T> : StaticMemoryPool<List<T>>
{
static ListPool<T> _instance = new ListPool<T>();
public ListPool()
{
OnDespawnedMethod = OnDespawned;
}
public static ListPool<T> Instance
{
get { return _instance; }
}
void OnDespawned(List<T> list)
{
list.Clear();
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: f30364d1ac067164ea56051cd000b49f
timeCreated: 1520670682
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using ModestTree;
namespace Zenject
{
// If you want to ensure that all items are always returned to the pool, include the following
// in an installer
// Container.BindInterfacesTo<PoolCleanupChecker>().AsSingle()
public class PoolCleanupChecker : ILateDisposable
{
readonly List<IMemoryPool> _poolFactories;
readonly List<Type> _ignoredPools;
public PoolCleanupChecker(
[Inject(Optional = true, Source = InjectSources.Local)]
List<IMemoryPool> poolFactories,
[Inject(Source = InjectSources.Local)]
List<Type> ignoredPools)
{
_poolFactories = poolFactories;
_ignoredPools = ignoredPools;
Assert.That(ignoredPools.All(x => x.DerivesFrom<IMemoryPool>()));
}
public void LateDispose()
{
foreach (var pool in _poolFactories)
{
if (!_ignoredPools.Contains(pool.GetType()))
{
Assert.IsEqual(pool.NumActive, 0,
"Found active objects in pool '{0}' during dispose. Did you forget to despawn an object of type '{1}'?".Fmt(pool.GetType(), pool.ItemType));
}
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 69aee89274cd16b48aa790ddd5a061d2
timeCreated: 1520670682
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using ModestTree;
namespace Zenject
{
#if UNITY_EDITOR
public static class StaticMemoryPoolRegistry
{
public static event Action<IMemoryPool> PoolAdded = delegate {};
public static event Action<IMemoryPool> PoolRemoved = delegate {};
readonly static List<IMemoryPool> _pools = new List<IMemoryPool>();
public static IEnumerable<IMemoryPool> Pools
{
get { return _pools; }
}
public static void Add(IMemoryPool memoryPool)
{
_pools.Add(memoryPool);
PoolAdded(memoryPool);
}
public static void Remove(IMemoryPool memoryPool)
{
_pools.RemoveWithConfirm(memoryPool);
PoolRemoved(memoryPool);
}
}
#endif
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: cccc66ed95242b544bd7f501f0ecb89b
timeCreated: 1521390236
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,127 @@
#if !NOT_UNITY3D
using ModestTree;
using UnityEngine;
namespace Zenject
{
// This factory type can be useful if you want to control where the prefab comes from at runtime
// rather than from within the installers
//No parameters
public class PrefabFactory<T> : IFactory<UnityEngine.Object, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(UnityEngine.Object prefab)
{
Assert.That(prefab != null,
"Null prefab given to factory create method when instantiating object with type '{0}'.", typeof(T));
return _container.InstantiatePrefabForComponent<T>(prefab);
}
// Note: We can't really validate here without access to the prefab
// We could validate the class directly with the current container but that fails when the
// class is inside a GameObjectContext
}
// One parameter
public class PrefabFactory<P1, T> : IFactory<UnityEngine.Object, P1, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(UnityEngine.Object prefab, P1 param)
{
Assert.That(prefab != null,
"Null prefab given to factory create method when instantiating object with type '{0}'.", typeof(T));
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param));
}
}
// Two parameters
public class PrefabFactory<P1, P2, T> : IFactory<UnityEngine.Object, P1, P2, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(UnityEngine.Object prefab, P1 param, P2 param2)
{
Assert.That(prefab != null,
"Null prefab given to factory create method when instantiating object with type '{0}'.", typeof(T));
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param, param2));
}
}
// Three parameters
public class PrefabFactory<P1, P2, P3, T> : IFactory<UnityEngine.Object, P1, P2, P3, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(UnityEngine.Object prefab, P1 param, P2 param2, P3 param3)
{
Assert.That(prefab != null,
"Null prefab given to factory create method when instantiating object with type '{0}'.", typeof(T));
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param, param2, param3));
}
}
// Four parameters
public class PrefabFactory<P1, P2, P3, P4, T> : IFactory<UnityEngine.Object, P1, P2, P3, P4, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(UnityEngine.Object prefab, P1 param, P2 param2, P3 param3, P4 param4)
{
Assert.That(prefab != null,
"Null prefab given to factory create method when instantiating object with type '{0}'.", typeof(T));
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param, param2, param3, param4));
}
}
}
#endif
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 39782cb4430018d459f5707002dff73c
timeCreated: 1461708049
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,136 @@
#if !NOT_UNITY3D
using ModestTree;
using UnityEngine;
namespace Zenject
{
// This factory type can be useful if you want to control where the prefab comes from at runtime
// rather than from within the installers
//No parameters
public class PrefabResourceFactory<T> : IFactory<string, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(string prefabResourceName)
{
Assert.That(!string.IsNullOrEmpty(prefabResourceName),
"Null or empty prefab resource name given to factory create method when instantiating object with type '{0}'.", typeof(T));
var prefab = (GameObject)Resources.Load(prefabResourceName);
return _container.InstantiatePrefabForComponent<T>(prefab);
}
// Note: We can't really validate here without access to the prefab
// We could validate the class directly with the current container but that fails when the
// class is inside a GameObjectContext
}
// One parameter
public class PrefabResourceFactory<P1, T> : IFactory<string, P1, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(string prefabResourceName, P1 param)
{
Assert.That(!string.IsNullOrEmpty(prefabResourceName),
"Null or empty prefab resource name given to factory create method when instantiating object with type '{0}'.", typeof(T));
var prefab = (GameObject)Resources.Load(prefabResourceName);
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param));
}
}
// Two parameters
public class PrefabResourceFactory<P1, P2, T> : IFactory<string, P1, P2, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(string prefabResourceName, P1 param, P2 param2)
{
Assert.That(!string.IsNullOrEmpty(prefabResourceName),
"Null or empty prefab resource name given to factory create method when instantiating object with type '{0}'.", typeof(T));
var prefab = (GameObject)Resources.Load(prefabResourceName);
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param, param2));
}
}
// Three parameters
public class PrefabResourceFactory<P1, P2, P3, T> : IFactory<string, P1, P2, P3, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(string prefabResourceName, P1 param, P2 param2, P3 param3)
{
Assert.That(!string.IsNullOrEmpty(prefabResourceName),
"Null or empty prefab resource name given to factory create method when instantiating object with type '{0}'.", typeof(T));
var prefab = (GameObject)Resources.Load(prefabResourceName);
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param, param2, param3));
}
}
// Four parameters
public class PrefabResourceFactory<P1, P2, P3, P4, T> : IFactory<string, P1, P2, P3, P4, T>
//where T : Component
{
[Inject]
readonly DiContainer _container = null;
public DiContainer Container
{
get { return _container; }
}
public virtual T Create(string prefabResourceName, P1 param, P2 param2, P3 param3, P4 param4)
{
Assert.That(!string.IsNullOrEmpty(prefabResourceName),
"Null or empty prefab resource name given to factory create method when instantiating object with type '{0}'.", typeof(T));
var prefab = (GameObject)Resources.Load(prefabResourceName);
return (T)_container.InstantiatePrefabForComponentExplicit(
typeof(T), prefab, InjectUtil.CreateArgListExplicit(param, param2, param3, param4));
}
}
}
#endif
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 5fd50037209abd647b7ae7391717f015
timeCreated: 1534319565
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: