コード例 #1
0
ファイル: CsScriptRepository.cs プロジェクト: Babelz/Fracture
        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);
            }
        }
コード例 #2
0
ファイル: CsScriptRepository.cs プロジェクト: Babelz/Fracture
        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);
            }
        }
コード例 #3
0
ファイル: CsScriptRepository.cs プロジェクト: Babelz/Fracture
        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));
            }
        }