/// <summary>
        /// Retrieves the Mod Loader configuration file struct.
        /// </summary>
        /// <param name="gameConfigDirectory">The directory containing the configuration file for the game. e.g. $LOADERPATH\\Reloaded-Mods\\Games\\Sonic-Heroes</param>
        /// <returns></returns>
        public static GameConfig ParseConfig(string gameConfigDirectory)
        {
            // Sets the configuration file location for the game.
            string configFile = gameConfigDirectory + $"\\{Strings.Parsers.ConfigFile}";

            // Try parsing the config file, else backup to default one.
            GameConfig config;

            try
            {
                config = File.Exists(configFile)
                    ? JsonConvert.DeserializeObject <GameConfig>(File.ReadAllText(configFile))
                    : new GameConfig();
            }
            catch { config = new GameConfig(); }


            // Set the directory of this config.
            config.ConfigLocation = configFile;

            // Override names if default config.
            if (gameConfigDirectory == LoaderPaths.GetGlobalGameConfigDirectory()) // Function call gemerates !Global if it does not exist.
            {
                config = GameConfig.SetGlobalConfigProperties(config);
            }

            // Create mod directory if nonexistant.
            if (!Directory.Exists(LoaderPaths.GetModLoaderModDirectory() + $"\\{config.ModDirectory}"))
            {
                Directory.CreateDirectory(LoaderPaths.GetModLoaderModDirectory() + $"\\{config.ModDirectory}");
            }

            // Return the config file.
            return(config);
        }
        /// <summary>
        /// Retrieves the Mod Loader configuration file struct.
        /// </summary>
        /// <param name="gameConfigDirectory">The directory containing the configuration file for the game. e.g. $LOADERPATH\\Reloaded-Mods\\Games\\Sonic-Heroes</param>
        /// <returns></returns>
        public static GameConfig ParseConfig(string gameConfigDirectory)
        {
            // Sets the configuration file location for the game.
            string configFile = gameConfigDirectory + $"\\{Strings.Parsers.ConfigFile}";

            // Try parsing the config file, else backup to default one.
            GameConfig config;

            try
            {
                config = File.Exists(configFile)
                         ? JsonConvert.DeserializeObject <GameConfig>(File.ReadAllText(configFile))
                         : new GameConfig();
            }
            catch { config = new GameConfig(); }


            // Set the directory of this config.
            config.ConfigLocation = configFile;

            // Override names if default config.
            if (gameConfigDirectory == LoaderPaths.GetGlobalGameConfigDirectory())
            {
                config = GameConfig.SetGlobalConfigProperties(config);
            }

            // Return the config file.
            return(config);
        }
        /// <summary>
        /// Applies a set of global configuration properties to the supplied configuration.
        /// all configuration which is used to load mods for all games.
        /// </summary>
        /// <returns>The global configuration properties</returns>
        public static GameConfig SetGlobalConfigProperties(GameConfig gameConfig)
        {
            gameConfig.ExecutableLocation = "All Executables";
            gameConfig.ModDirectory       = Strings.Common.GlobalModFolder;
            gameConfig.ConfigLocation     = LoaderPaths.GetGlobalGameConfigDirectory() + $"\\{Strings.Parsers.ConfigFile}";
            gameConfig.GameName           = Strings.Common.GlobalModName;
            gameConfig.GameVersion        = "Reloaded";
            gameConfig.GameDirectory      = "Between Time and Space";

            return(gameConfig);
        }
        /// <summary>
        /// Retrieves all currently enabled global modifications, executed regardless of
        /// the individual mod configuration.
        /// </summary>
        /// <returns>A list of currently globally enabled mods.</returns>
        public static List <ModConfig> GetEnabledGlobalMods()
        {
            // Get global mod configuration.
            GameConfig globalModConfig = GameConfig.ParseConfig(LoaderPaths.GetGlobalGameConfigDirectory());

            // Get all mods for the global configuration.
            List <ModConfig> modConfigurations = ConfigManager.GetAllModsForGame(globalModConfig);

            // Filter out the enabled mods.
            modConfigurations = modConfigurations.Where(x =>
                                                        // Get folder name containing the mod = Path.GetFileName(Path.GetDirectoryName(x.ModLocation))
                                                        // Check if it's contained in the enabled mods list
                                                        globalModConfig.EnabledMods.Contains(Path.GetFileName(Path.GetDirectoryName(x.ModLocation)))).ToList();

            return(modConfigurations);
        }
            /// <summary>
            /// Retrieves the properties of the special "global" configuration, a master above
            /// all configuration which is used to load mods for all games.
            /// </summary>
            /// <returns>The global configuration properties</returns>
            public static GameConfig GetGlobalConfigProperties()
            {
                // Creates the global mod directory if it does not exist.
                // This is just to ensure safe usage of the global config.
                LoaderPaths.GetGlobalModDirectory();

                return(new GameConfig()
                {
                    ExecutableLocation = "All Executables",
                    ModDirectory = Strings.Common.GlobalModFolder,
                    ConfigLocation = LoaderPaths.GetGlobalGameConfigDirectory() + $"\\{Strings.Parsers.ConfigFile}",
                    GameDirectory = "Between Time and Space",
                    GameName = Strings.Common.GlobalModName,
                    GameVersion = "Reloaded"
                });
            }
        /// <summary>
        /// Deletes a game configuration from the known configurations.
        /// </summary>
        private void DeleteGame(object sender, EventArgs e)
        {
            // Get current selected game.
            GameComboBoxDetails comboBoxDetails = GetSelectedGame();

            // If there is no game (null), return.
            if (comboBoxDetails == null)
            {
                return;
            }

            // Find and remove first by details.
            for (int x = 0; x < Global.GameConfigurations.Count; x++)
            {
                // Find the first match to game name, executable and version.
                if (Global.GameConfigurations[x].GameName == comboBoxDetails.GameName &&
                    Global.GameConfigurations[x].ExecutableLocation == comboBoxDetails.ExecutableRelativeLocation &&
                    Global.GameConfigurations[x].GameVersion == comboBoxDetails.GameVersion)
                {
                    // Check if global config.
                    if (Global.GameConfigurations[x].ConfigLocation == LoaderPaths.GetGlobalGameConfigDirectory())
                    {
                        MessageBox.Show($"It's no use {Environment.UserName}, give up.");
                        return;
                    }

                    // Maintain currently open banners.
                    try { Global.BaseForm.ChildrenForms.MainMenu.item_GameBanner.BackgroundImage.Dispose(); } catch { }
                    try { box_GameBanner.BackgroundImage.Dispose(); } catch { }

                    // Remove game from list & Switch game.
                    borderless_CurrentGame.Items.RemoveAt(x);
                    try { borderless_CurrentGame.SelectedIndex = borderless_CurrentGame.Items.Count - 1; } catch { }

                    // Garbage collect old possible image references.
                    GC.Collect();
                    GC.WaitForPendingFinalizers();

                    // Remove game config & physical location.
                    try { Directory.Delete(Global.GameConfigurations[x].ConfigLocation, true); } catch { }
                    Global.GameConfigurations.RemoveAt(x);

                    break;
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Saves the current game configuration.
        /// </summary>
        private void SaveCurrentGame()
        {
            // Get current selected game.
            GameComboBoxDetails comboBoxDetails = GetSelectedGame();

            // Find and remove first by details.
            foreach (GameConfig gameConfig in Global.GameConfigurations)
            {
                // Find the first match to game name, executable and version.
                if (gameConfig.GameName == comboBoxDetails.GameName &&
                    gameConfig.ExecutableLocation == comboBoxDetails.ExecutableRelativeLocation &&
                    gameConfig.GameVersion == comboBoxDetails.GameVersion)
                {
                    // Check if global config.
                    if (gameConfig.ConfigLocation == LoaderPaths.GetGlobalGameConfigDirectory())
                    {
                        MessageBox.Show($"I'm sorry {Environment.UserName}, I'm afraid I can't let you do that.");
                        return;
                    }

                    // Set the new game details.
                    gameConfig.GameName           = borderless_GameName.Text;
                    gameConfig.GameVersion        = borderless_GameVersion.Text;
                    gameConfig.GameDirectory      = borderless_GameDirectory.Text;
                    gameConfig.ExecutableLocation = borderless_GameExecutableDirectory.Text;
                    gameConfig.ModDirectory       = borderless_GameModDirectory.Text;
                    gameConfig.CommandLineArgs    = borderless_CommandLineArguments.Text;

                    // Change the current item name to reflect new changes.
                    borderless_CurrentGame.Items[borderless_CurrentGame.SelectedIndex] =
                        borderless_GameName.Text + " " + Theme.ThemeProperties.TitleProperties.LoaderTitleDelimiter + " " +
                        borderless_GameExecutableDirectory.Text + " " + Theme.ThemeProperties.TitleProperties.LoaderTitleDelimiter + " " +
                        borderless_GameVersion.Text;

                    break;
                }
            }
        }
 /// <summary>
 /// Retrieves the properties of the special "global" configuration, a master above
 /// all configuration which is used to load mods for all games.
 /// </summary>
 /// <returns>The hardcoded global configuration properties.</returns>
 public static GameConfig GetGlobalConfig()
 {
     return(ParseConfig(LoaderPaths.GetGlobalGameConfigDirectory()));
 }
        /// <summary>
        /// Finds the mods that are currently enabled for the game and injects into the target process.
        /// </summary>
        /// <param name="gameConfiguration">The game configuration which contains the current directory and list of mods to load.</param>
        /// <param name="reloadedProcess">The reloaded process to inject the modifications into.</param>
        public static void LoadMods(GameConfigParser.GameConfig gameConfiguration, ReloadedProcess reloadedProcess)
        {
            // Get directory containing the global mod list.
            GameConfigParser.GameConfig globalModConfig = GameConfigParser.ParseConfig(LoaderPaths.GetGlobalGameConfigDirectory());

            // Get directory containing the game's mod list
            string gameModDirectory   = Path.Combine(LoaderPaths.GetModLoaderModDirectory(), gameConfiguration.ModDirectory);
            string globalModDirectory = LoaderPaths.GetGlobalModDirectory();

            // Get directories containing enabled mods.
            List <string> modLibraries = new List <string>(gameConfiguration.EnabledMods.Count);

            // Get the game mod dll locations.
            foreach (string modDirectory in gameConfiguration.EnabledMods)
            {
                // Add native or not native.
                if (ReloadedArchitecture.IsGame32Bit)
                {
                    modLibraries.Add(Path.Combine(gameModDirectory, modDirectory, Strings.Loader.Mod32BitDllFile));
                }
                else
                {
                    modLibraries.Add(Path.Combine(gameModDirectory, modDirectory, Strings.Loader.Mod64BitDllFile));
                }
            }

            // Get the global mod dll locations.
            foreach (string modDirectory in globalModConfig.EnabledMods)
            {
                // Add native or not native.
                if (ReloadedArchitecture.IsGame32Bit)
                {
                    modLibraries.Add(Path.Combine(globalModDirectory, modDirectory, Strings.Loader.Mod32BitDllFile));
                }
                else
                {
                    modLibraries.Add(Path.Combine(globalModDirectory, modDirectory, Strings.Loader.Mod64BitDllFile));
                }
            }

            // Initialize DLL Injector
            DllInjector reloadedDllInjector = new DllInjector(reloadedProcess);

            // If the main.dll exists, load it.
            foreach (string modLibrary in modLibraries)
            {
                // If the DLL Exists, Try to Load It
                if (File.Exists(modLibrary))
                {
                    // Allocate Memory for Server Port In Game Memory
                    IntPtr parameterAddress = reloadedProcess.AllocateMemory(IntPtr.Size);

                    // Write Server Port to Game Memory
                    reloadedProcess.WriteMemoryExternal(parameterAddress, BitConverter.GetBytes(LoaderServer.ServerPort));

                    // Inject the individual DLL.
                    reloadedDllInjector.InjectDll(modLibrary, parameterAddress);
                }
            }

            // Resume game after injection.
            reloadedProcess.ResumeAllThreads();
        }