public ReusableCsScriptRepository(IGameEngine engine) { if (engine == null) { throw new ArgumentNullException(nameof(engine)); } if (!CsScriptTypeLoader.TryGetScriptDefinitionAttribute(typeof(T), out var definition)) { throw new MissingAttributeException(typeof(T), typeof(CsScriptDefinitionAttribute)); } pools = new Dictionary <string, IPool <T> >(); scripts = new List <T>(); var types = CsScriptTypeLoader.GetScriptTypes(definition); foreach (var type in types) { if (!CsScriptTypeLoader.TryGetScriptAttribute(type, out var script)) { throw new MissingAttributeException(typeof(T), typeof(CsScriptAttribute)); } var ctor = type.GetConstructor(new[] { typeof(IGameEngine), typeof(string) }); var activator = (ScriptActivator <T>)DynamicConstructorBinder.Bind(ctor, typeof(ScriptActivator <>).MakeGenericType(type)); pools.Add(script.Name, new LinearPool <T>( new LinearStorageObject <T>( new LinearGrowthArray <T>( definition.Allocations)), () => activator(engine, script.Name), definition.Allocations)); } }
public DisposableCsScriptRepository(IGameEngine engine) { if (!CsScriptTypeLoader.TryGetScriptDefinitionAttribute(typeof(T), out var definition)) { throw new MissingAttributeException(typeof(T), typeof(CsScriptDefinitionAttribute)); } this.engine = engine ?? throw new ArgumentNullException(nameof(engine)); activators = new Dictionary <string, ScriptActivator <T> >(); scripts = new List <T>(); var types = CsScriptTypeLoader.GetScriptTypes(definition); foreach (var type in types) { if (!CsScriptTypeLoader.TryGetScriptAttribute(type, out var script)) { throw new MissingAttributeException(typeof(T), typeof(CsScriptAttribute)); } var ctor = type.GetConstructor(new[] { typeof(IGameEngine), typeof(string) }); var activator = (ScriptActivator <T>)DynamicConstructorBinder.Bind(ctor, typeof(ScriptActivator <>).MakeGenericType(type)); activators.Add(script.Name, activator); } }
/// <summary> /// Creates new instance of <see cref="Pool{T}"/> with given initial objects count and /// optional capacity. /// </summary> public Pool(IStorageObject <T> storage, int initialStoredObjectsCount, int capacity) : base(storage, initialStoredObjectsCount, capacity) { // Wrap constructor call to delegate, this is a much faster than calling new on the generic T. instantiate = (Func <T>)DynamicConstructorBinder.Bind(typeof(T).GetConstructor(Type.EmptyTypes), typeof(Func <T>)); CreateObjects(initialStoredObjectsCount); }
/// <summary> /// Creates new linear pool allocating new objects with public parameterless constructor. In case /// no constructor exists, an exception will be thrown. /// </summary> public LinearPool(IStorageObject <T> storage, int allocations) : this(storage, (Func <T>)DynamicConstructorBinder.Bind(typeof(T).GetConstructor(Type.EmptyTypes), typeof(Func <T>)), allocations) { }