RegisterGameMode() public method

Registers the specified game mode.
public RegisterGameMode ( IGameModeFactory p_gmfGameModeFactory ) : void
p_gmfGameModeFactory IGameModeFactory The factory for the game mode to register.
return void
        /// <summary>
        /// Loads the factories for games that have been previously detected as installed.
        /// </summary>
        /// <param name="supportedGameModes">A registry containing the factories for all supported game modes.</param>
        /// <param name="environmentInfo">The application's environment info.</param>
        /// <returns>A registry containing all of the game mode factories for games that were previously detected as installed.</returns>
        public static GameModeRegistry LoadInstalledGameModes(GameModeRegistry supportedGameModes, EnvironmentInfo environmentInfo)
        {
            Trace.TraceInformation("Loading Game Mode Factories for Installed Games...");
            Trace.Indent();

            var installedGameModes = new GameModeRegistry();

            foreach (var gameId in environmentInfo.Settings.InstalledGames)
            {
                Trace.Write($"Loading {gameId}: ");

                if (supportedGameModes.IsRegistered(gameId))
                {
                    Trace.WriteLine("Supported");
                    installedGameModes.RegisterGameMode(supportedGameModes.GetGameMode(gameId));
                }
                else
                {
                    Trace.WriteLine("Not Supported");
                }
            }

            Trace.Unindent();
            return(installedGameModes);
        }
        /// <summary>
        /// Searches for game mode factories in the specified path, and loads
        /// any factories that are found into a registry.
        /// </summary>
        /// <returns>A registry containing all of the discovered game mode factories.</returns>
        public static GameModeRegistry DiscoverSupportedGameModes(EnvironmentInfo p_eifEnvironmentInfo)
        {
            Trace.TraceInformation("Discovering Game Mode Factories...");
            Trace.Indent();

            string strGameModesPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "GameModes");

            Trace.TraceInformation("Looking in: {0}", strGameModesPath);

            GameModeRegistry gmrRegistry = new GameModeRegistry();

            string[] strAssemblies = Directory.GetFiles(strGameModesPath, "*.dll");
            foreach (string strAssembly in strAssemblies)
            {
                Trace.TraceInformation("Checking: {0}", Path.GetFileName(strAssembly));
                Trace.Indent();

                Assembly asmGameMode = Assembly.LoadFrom(strAssembly);
                try
                {
                    Type[] tpeTypes = asmGameMode.GetExportedTypes();
                    foreach (Type tpeType in tpeTypes)
                    {
                        if (typeof(IGameModeFactory).IsAssignableFrom(tpeType) && !tpeType.IsAbstract)
                        {
                            Trace.TraceInformation("Initializing: {0}", tpeType.FullName);
                            Trace.Indent();

                            ConstructorInfo cifConstructor = tpeType.GetConstructor(new Type[] { typeof(IEnvironmentInfo) });
                            if (cifConstructor == null)
                            {
                                Trace.TraceInformation("No constructor accepting one argument of type IEnvironmentInfo found.");
                                Trace.Unindent();
                                continue;
                            }
                            IGameModeFactory gmfGameModeFactory = (IGameModeFactory)cifConstructor.Invoke(new object[] { p_eifEnvironmentInfo });
                            gmrRegistry.RegisterGameMode(gmfGameModeFactory);

                            Trace.Unindent();
                        }
                    }
                }
                catch (FileNotFoundException e)
                {
                    Trace.TraceError(String.Format("Cannot load {0}: cannot find dependency {1}", strAssembly, e.FileName));
                    //some dependencies were missing, so we couldn't load the assembly
                    // given that these are plugins we don't have control over the dependecies:
                    // we may not even know what they (we can get their name, but if it's a custom
                    // dll not part of the client code base, we can't provide it even if we wanted to)
                    // there's nothing we can do, so simply skip the assembly
                }
                Trace.Unindent();
            }
            Trace.Unindent();

            return(gmrRegistry);
        }
		/// <summary>
		/// Searches for game mode factories in the specified path, and loads
		/// any factories that are found into a registry.
		/// </summary>
		/// <returns>A registry containing all of the discovered game mode factories.</returns>
		public static GameModeRegistry DiscoverSupportedGameModes(EnvironmentInfo p_eifEnvironmentInfo)
		{
			Trace.TraceInformation("Discovering Game Mode Factories...");
			Trace.Indent();

			string strGameModesPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "GameModes");

			Trace.TraceInformation("Looking in: {0}", strGameModesPath);

			GameModeRegistry gmrRegistry = new GameModeRegistry();
			string[] strAssemblies = Directory.GetFiles(strGameModesPath, "*.dll");
			foreach (string strAssembly in strAssemblies)
			{
				Trace.TraceInformation("Checking: {0}", Path.GetFileName(strAssembly));
				Trace.Indent();

				Assembly asmGameMode = Assembly.LoadFrom(strAssembly);
				try
				{
					Type[] tpeTypes = asmGameMode.GetExportedTypes();
					foreach (Type tpeType in tpeTypes)
					{
						if (typeof(IGameModeFactory).IsAssignableFrom(tpeType) && !tpeType.IsAbstract)
						{
							Trace.TraceInformation("Initializing: {0}", tpeType.FullName);
							Trace.Indent();

							ConstructorInfo cifConstructor = tpeType.GetConstructor(new Type[] { typeof(IEnvironmentInfo) });
							if (cifConstructor == null)
							{
								Trace.TraceInformation("No constructor accepting one argument of type IEnvironmentInfo found.");
								Trace.Unindent();
								continue;
							}
							IGameModeFactory gmfGameModeFactory = (IGameModeFactory)cifConstructor.Invoke(new object[] { p_eifEnvironmentInfo });
							gmrRegistry.RegisterGameMode(gmfGameModeFactory);

							Trace.Unindent();
						}
					}
				}
				catch (FileNotFoundException e)
				{
					Trace.TraceError(String.Format("Cannot load {0}: cannot find dependency {1}", strAssembly, e.FileName));
					//some dependencies were missing, so we couldn't load the assembly
					// given that these are plugins we don't have control over the dependecies:
					// we may not even know what they (we can get their name, but if it's a custom
					// dll not part of the client code base, we can't provide it even if we wanted to)
					// there's nothing we can do, so simply skip the assembly
				}
				Trace.Unindent();
			}
			Trace.Unindent();

			return gmrRegistry;
		}
        /// <summary>
        /// Loads the factories for games that have been previously detected as installed.
        /// </summary>
        /// <param name="p_gmrSupportedGameModes">A registry containing the factories for all supported game modes.</param>
        /// <param name="p_eifEnvironmentInfo">The application's envrionment info.</param>
        /// <returns>A registry containing all of the game mode factories for games that were previously detected as installed.</returns>
        public static GameModeRegistry LoadInstalledGameModes(GameModeRegistry p_gmrSupportedGameModes, EnvironmentInfo p_eifEnvironmentInfo)
        {
            Trace.TraceInformation("Loading Game Mode Factories for Installed Games...");
            Trace.Indent();

            GameModeRegistry gmrInstalled = new GameModeRegistry();

            foreach (string strGameId in p_eifEnvironmentInfo.Settings.InstalledGames)
            {
                Trace.Write(String.Format("Loading {0}: ", strGameId));
                if (p_gmrSupportedGameModes.IsRegistered(strGameId))
                {
                    Trace.WriteLine("Supported");
                    gmrInstalled.RegisterGameMode(p_gmrSupportedGameModes.GetGameMode(strGameId));
                }
                else
                {
                    Trace.WriteLine("Not Supported");
                }
            }

            Trace.Unindent();
            return(gmrInstalled);
        }
        /// <summary>
        /// Searches for game mode factories in the specified path, and loads
        /// any factories that are found into a registry.
        /// </summary>
        /// <returns>A registry containing all of the discovered game mode factories.</returns>
        public static GameModeRegistry DiscoverSupportedGameModes(EnvironmentInfo environmentInfo)
        {
            Trace.TraceInformation("Discovering Game Mode Factories...");
            Trace.Indent();

            var gameModesPath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "GameModes");

            if (!Directory.Exists(gameModesPath))
            {
                Directory.CreateDirectory(gameModesPath);
            }

            Trace.TraceInformation("Looking in: {0}", gameModesPath);

            var assemblies = Directory.GetFiles(gameModesPath, "*.dll");

            //If there are no assemblies detected then an exception must be thrown
            //to prevent a divide by zero exception further along
            if (!assemblies.Any())
            {
                var debugMessage = string.Empty;
#if DEBUG
                debugMessage = @"Compile the Game Modes directory in the solution.";
#endif

                throw new GameModeRegistryException(gameModesPath, debugMessage);
            }

            var registry = new GameModeRegistry();

            foreach (var assembly in assemblies)
            {
                Trace.TraceInformation("Checking: {0}", Path.GetFileName(assembly));
                Trace.Indent();

                var gameMode = Assembly.LoadFrom(assembly);

                try
                {
                    var types = gameMode.GetExportedTypes();

                    foreach (var type in types)
                    {
                        if (!typeof(IGameModeFactory).IsAssignableFrom(type) || type.IsAbstract)
                        {
                            continue;
                        }

                        Trace.TraceInformation("Initializing: {0}", type.FullName);
                        Trace.Indent();

                        var constructor = type.GetConstructor(new Type[] { typeof(IEnvironmentInfo) });

                        if (constructor == null)
                        {
                            Trace.TraceInformation("No constructor accepting one argument of type IEnvironmentInfo found.");
                            Trace.Unindent();

                            continue;
                        }

                        var gmfGameModeFactory = (IGameModeFactory)constructor.Invoke(new object[] { environmentInfo });
                        registry.RegisterGameMode(gmfGameModeFactory);

                        Trace.Unindent();
                    }
                }
                catch (FileNotFoundException e)
                {
                    Trace.TraceError($"Cannot load {assembly}: cannot find dependency {e.FileName}");
                    // some dependencies were missing, so we couldn't load the assembly
                    // given that these are plugins we don't have control over the dependencies:
                    // we may not even know what they (we can get their name, but if it's a custom
                    // dll not part of the client code base, we can't provide it even if we wanted to)
                    // there's nothing we can do, so simply skip the assembly
                }

                Trace.Unindent();
            }

            Trace.Unindent();

            return(registry);
        }
		/// <summary>
		/// Loads the factories for games that have been previously detected as installed.
		/// </summary>
		/// <param name="p_gmrSupportedGameModes">A registry containing the factories for all supported game modes.</param>
		/// <param name="p_eifEnvironmentInfo">The application's envrionment info.</param>
		/// <returns>A registry containing all of the game mode factories for games that were previously detected as installed.</returns>
		public static GameModeRegistry LoadInstalledGameModes(GameModeRegistry p_gmrSupportedGameModes, EnvironmentInfo p_eifEnvironmentInfo)
		{
			Trace.TraceInformation("Loading Game Mode Factories for Installed Games...");
			Trace.Indent();
			
			GameModeRegistry gmrInstalled = new GameModeRegistry();
			foreach (string strGameId in p_eifEnvironmentInfo.Settings.InstalledGames)
			{
				Trace.Write(String.Format("Loading {0}: ", strGameId));
				if (p_gmrSupportedGameModes.IsRegistered(strGameId))
				{
					Trace.WriteLine("Supported");
					gmrInstalled.RegisterGameMode(p_gmrSupportedGameModes.GetGameMode(strGameId));
				}
				else
					Trace.WriteLine("Not Supported");
			}
			
			Trace.Unindent();
			return gmrInstalled;
		}