Example #1
0
        public static void Init(TestContext context, BeastSection config)
        {
            TestContextLogger.TestContext = context;

            if (_initialized)
                return;

            _game = Game.Current;
            _game.Start(config);

            Connection = ConnectionManager.Create(new TestContextConnectionFactory(context));

            _initialized = true;
        }
Example #2
0
        public static void InitTest(TestContext context)
        {
            var config = new BeastSection
                         	{
                         		Modules = new ModuleElementCollection()
                         	};
            config.Modules.Add(new ModuleElement{Type = typeof(MongoRepository).AssemblyQualifiedName});
            Init(context, config);

            var repo = new MongoRepository
            {
                ConnectionString = "mongodb://localhost/beast",
                DatabaseName = "beast"
            };
            repo.Initialize();

            Users = repo;
            Templates = repo;
            Places = repo;
            Characters = repo;
        }
Example #3
0
        /// <summary>
        /// Starts the game and begins the main loop.
        /// </summary>
        /// <param name="config">The configuration section used to initialize the game settings.</param>
        public void Start(BeastSection config)
        {
            if (IsRunning)
                return;

            // ====================================================================================
            // LOAD CONFIGURATION
            // ====================================================================================
            if (config == null)
            {
                throw new ConfigurationErrorsException();
            }
            Config = config;

            // ====================================================================================
            // INITIALIZE STARTUP VARIABLES
            // ====================================================================================
            // Signal that the game is running.
            IsRunning = true;
            _clock = new GameClock();
            GameTime = new GameTime();

            try
            {
                // ====================================================================================
                // LOAD MODULES FROM EXTERNAL SOURCES
                // ====================================================================================
                var catalog = new AggregateCatalog();
                catalog.Catalogs.Add(new AssemblyCatalog(typeof(Game).Assembly));

                if (config.ModulesDirectory != null)
                {
                    var modulesDirectory = config.ModulesDirectory.Path;
                    if (config.ModulesDirectory.IsVirtual)
                    {
                        // Using a virtual path
                        modulesDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, config.ModulesDirectory.Path);
                    }

                    if (!string.IsNullOrEmpty(modulesDirectory))
                        catalog.Catalogs.Add(new DirectoryCatalog(modulesDirectory));
                }

                if (config.Modules != null && config.Modules.Count > 0)
                {
                    var moduleTypes = config.Modules.Cast<ModuleElement>()
                        .Select(moduleElement => Type.GetType(moduleElement.Type, false, true))
                        .Where(moduleType => moduleType != null);

                    catalog.Catalogs.Add(new TypeCatalog(moduleTypes));
                }

                var container = new CompositionContainer(catalog, new ConfigurationExportProvider(new FileConfigurationSource()));
                container.ComposeParts(this, Repository, Commands, Users);

                // ====================================================================================
                // INITIALIZE LOGGING
                // ====================================================================================
                Log.Initialize(Loggers);

                // ====================================================================================
                // INITIALIZE BASIC SYSTEMS
                // ====================================================================================
                // Data repository
                Repository.Initialize();
                Log.Info("Initialized repositories.");

                // Crypto services
                ICryptoKeyProvider cryptoKeyProvider = null;
                if (!string.IsNullOrEmpty(config.CryptoKeyProviderType))
                {
                    var cryptoType = FindType(config.CryptoKeyProviderType);
                    if (cryptoType != null)
                        cryptoKeyProvider = Activator.CreateInstance(cryptoType) as ICryptoKeyProvider;
                }
                if (cryptoKeyProvider == null)
                    cryptoKeyProvider = new DefaultCryptKeyProvider();
                Cryptography.Initialize(cryptoKeyProvider);
                Log.Info("Initialized the cryptography service.");

                // Connection manager
                var timeout = DefaultConnectionTimeout;
                if (config.ConnectionTimeout > 0)
                    timeout = TimeSpan.FromMinutes(config.ConnectionTimeout);
                ConnectionManager.Initialize(timeout);
                Log.Info("Initialized the connection manager.");

                // Game World
                World = Repository.GetWorld() ?? new DefaultWorld();
                World.Initialize();
                Log.Info("Initialized the game world.");

                // ====================================================================================
                // INITIALIZE MODULES
                // ====================================================================================
                _modules = LoadedModules.OrderByDescending(m => (int)m.Metadata.Priority).Select(m => m.Value).ToList();
                foreach (var module in _modules)
                {
                    module.Initialize();
                }

                // ====================================================================================
                // START THE MAIN GAME LOOP
                // ====================================================================================
                var interval = DefaultGameStepInterval;
                if (config.GameStepInterval > 0)
                    interval = TimeSpan.FromMilliseconds(config.GameStepInterval);
                _clock.Start(interval.TotalMilliseconds, Update);
            }
            catch (Exception)
            {
                IsRunning = false;
                throw;
            }
        }