Example #1
0
        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);
            }
        }
Example #2
0
        public UniqueCsScriptRepository(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));
            }

            loaded  = new List <T>();
            scripts = new Dictionary <string, T>();
            loads   = new Dictionary <string, int>();

            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 instance = (T)Activator.CreateInstance(type, engine, script.Name);

                scripts.Add(script.Name, instance);

                loads.Add(script.Name, 0);
            }
        }
Example #3
0
        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));
            }
        }
Example #4
0
        public ICsScriptRepository <T> Create <T>(IGameEngine engine) where T : CsScript
        {
            if (!CsScriptTypeLoader.TryGetScriptDefinitionAttribute(typeof(T), out var definition))
            {
                throw new MissingAttributeException(typeof(T), typeof(CsScriptDefinitionAttribute));
            }

            if (definition.Unique)
            {
                return(new UniqueCsScriptRepository <T>(engine));
            }

            if (definition.Reusable)
            {
                return(new ReusableCsScriptRepository <T>(engine));
            }

            return(new DisposableCsScriptRepository <T>(engine));
        }