Пример #1
0
        private static IEnumerable <EngineModule> LoadModules(GameLoop ctx, IEnumerable <Type> types)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException(nameof(ctx));
            }

            if (types == null)
            {
                throw new ArgumentNullException(nameof(types));
            }

            if (types.Any(x => x.BaseType != typeof(EngineModule)))
            {
                throw new InvalidOperationException("Attempted to load types that are not engine modules.");
            }

            var loadQueue = new List <Type>();

            foreach (var type in types)
            {
                GameUtils.Log($" -> Checking for circular dependencies...");
                DetectCircularDependencies(type, GetRequirements(type));

                foreach (var req in CollapseRequirements(type))
                {
                    if (!loadQueue.Contains(req))
                    {
                        loadQueue.Add(req);
                    }
                }

                if (!loadQueue.Contains(type))
                {
                    loadQueue.Add(type);
                }
            }

            GameUtils.Log("Loading modules now...");

            foreach (var item in loadQueue)
            {
                // No need to load mods that are already loaded
                if (ctx.IsModuleActive(item))
                {
                    continue;
                }

                var instance = (EngineModule)Activator.CreateInstance(item, null);

                instance.Register(ctx);

                GameUtils.Log($" -> Loaded: {item.Name}");

                yield return(instance);
            }
        }
        /// <summary>
        /// Registers this module with the specified <paramref name="gameLoop" />.
        /// </summary>
        /// <param name="gameLoop">The <see cref="GameLoop" /> to attach this module to.</param>
        /// <exception cref="ModuleException">This module is already attached to a game loop, or another instance of this module type is attached to it.</exception>
        public void Register(GameLoop gameLoop)
        {
            if (this.GameLoop != null)
            {
                throw new ModuleException("Module has already been registered with the game.");
            }

            this.GameLoop = gameLoop ?? throw new ArgumentNullException(nameof(gameLoop));
        }
Пример #3
0
 internal static IEnumerable <EngineModule> LoadThirdPartyModules(GameLoop ctx)
 {
     foreach (var ass in AppDomain.CurrentDomain.GetAssemblies())
     {
         if (ass != ctx.GetType().Assembly)
         {
             foreach (var mod in LoadModules(ctx, ass))
             {
                 yield return(mod);
             }
         }
     }
 }
Пример #4
0
        public static IEnumerable <EngineModule> LoadModules(GameLoop ctx, Assembly ass)
        {
            var types = FindModulesInAssembly(ass);

            foreach (var module in LoadModules(ctx, types))
            {
                if (!_loadedAssemblies.Contains(ass))
                {
                    _loadedAssemblies.Add(ass);
                }

                yield return(module);
            }

            GameUtils.Log("Successfully loaded modules in assembly.");
        }
Пример #5
0
        public static void Run(Assembly gameAssembly)
        {
            _gameAssembly = gameAssembly ?? throw new ArgumentNullException(nameof(gameAssembly));

            Log($"Starting {GameTitle}...");
            Log($"A game developed by {AuthorName}.");
            Log("Powered by the Pandemic Framework.");
            Log("");
            Log("============================================");
            Log("");

            using var game = new GameLoop();

            game.Run();

            Log("The pandemic is now over.");
        }