BF2Statistics.BF2Server.SetServerPath C# (CSharp) Method

SetServerPath() public static method

Loads a battlefield 2 server into this object for use.
public static SetServerPath ( string ServerPath ) : void
ServerPath string The full root path to the server's executable file
return void
        public static void SetServerPath(string ServerPath)
        {
            // Make sure we have a valid server path
            if (!File.Exists(Path.Combine(ServerPath, "bf2_w32ded.exe")))
                throw new ArgumentException("Invalid server path");

            // Make sure we actually changed server paths before processing
            if (!String.IsNullOrEmpty(RootPath) && (new Uri(ServerPath)) == (new Uri(RootPath)))
            {
                // Same path is selected, just return
                return;
            }

            // Temporary variables
            string Modpath = Path.Combine(ServerPath, "mods");
            string PyPath = Path.Combine(ServerPath, "python", "bf2");
            List<BF2Mod> TempMods = new List<BF2Mod>();

            // Make sure the server has the required folders
            if (!Directory.Exists(Modpath))
            {
                throw new Exception("Unable to locate the 'mods' folder. Please make sure you have selected a valid "
                    + "battlefield 2 installation path before proceeding.");

            }
            else if (!Directory.Exists(PyPath))
            {
                throw new Exception("Unable to locate the 'python/bf2' folder. Please make sure you have selected a valid "
                    + "battlefield 2 installation path before proceeding.");
            }

            // Load all found mods, discarding invalid mods
            ModLoadErrors = new List<string>();
            IEnumerable<string> ModList = from dir in Directory.GetDirectories(Modpath) select dir.Substring(Modpath.Length + 1);
            foreach (string Name in ModList)
            {
                try
                {
                    // Create a new instance of the mod, and store it for later
                    BF2Mod Mod = new BF2Mod(Modpath, Name);
                    TempMods.Add(Mod);
                }
                catch (InvalidModException E)
                {
                    ModLoadErrors.Add(E.Message);
                    continue;
                }
                catch (Exception E)
                {
                    ModLoadErrors.Add(E.Message);
                    Program.ErrorLog.Write(E.Message);
                }
            }

            // We need mods bro...
            if (TempMods.Count == 0)
                throw new Exception("No valid battlefield 2 mods could be found in the Bf2 Server mods folder!");

            // Define var values after we now know this server apears valid
            RootPath = ServerPath;
            PythonPath = PyPath;
            Mods = TempMods;

            // Fire change event
            if (ServerPathChanged != null)
                ServerPathChanged();

            // Recheck server process
            CheckServerProcess();
        }

Usage Example

        /// <summary>
        /// Entry point... this will check if we are at the initial setup
        /// phase, and show the installation forms
        /// </summary>
        /// <returns>Returns false if the user cancels the setup before the basic settings are setup, true otherwise</returns>
        public static bool Run()
        {
            // Load the program config
            Settings Config        = Settings.Default;
            bool     PromptDbSetup = false;

            // If this is the first time running a new update, we need to update the config file
            if (!Config.SettingsUpdated)
            {
                Config.Upgrade();
                Config.SettingsUpdated = true;
                Config.Save();
            }

            // If this is the first run, Get client and server install paths
            if (String.IsNullOrWhiteSpace(Config.ServerPath) || !File.Exists(Path.Combine(Config.ServerPath, "bf2_w32ded.exe")))
            {
                PromptDbSetup = true;
                if (!ShowInstallForm())
                {
                    return(false);
                }
            }

            // Create the "My Documents/BF2Statistics" folder
            try
            {
                // Make sure documents folder exists
                if (!Directory.Exists(Paths.DocumentsFolder))
                {
                    Directory.CreateDirectory(Paths.DocumentsFolder);
                }

                // Create the database backups folder
                string bFolder = Path.Combine(Paths.DocumentsFolder, "Database Backups");
                if (!Directory.Exists(bFolder))
                {
                    // In 1.x.x versions, this folder was called Backups rather then Database Backups
                    string OldB = Path.Combine(Paths.DocumentsFolder, "Backups");
                    if (Directory.Exists(OldB))
                    {
                        Directory.Move(OldB, bFolder);
                    }
                    else
                    {
                        Directory.CreateDirectory(bFolder);
                    }
                }
            }
            catch (Exception E)
            {
                // Alert the user that there was an error
                MessageBox.Show("Bf2Statistics encountered an error trying to create the required \"My Documents/BF2Statistics\" folder!"
                                + Environment.NewLine.Repeat(1) + E.Message,
                                "Setup Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error
                                );
                return(false);
            }

            // Load server go.. If we fail to load a valid server, we will come back to here
LoadServer:
            {
                // Load the BF2 Server
                try
                {
                    BF2Server.SetServerPath(Config.ServerPath);
                }
                catch (Exception E)
                {
                    MessageBox.Show(E.Message, "Battlefield 2 Server Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // Re-prompt
                    if (!ShowInstallForm())
                    {
                        return(false);
                    }

                    goto LoadServer;
                }
            }

            // Fresh install? Show database config prompt
            if (PromptDbSetup)
            {
                string message = "In order to use the Private Stats feature of this program, we need to setup a database. "
                                 + "You may choose to do this later by clicking \"Cancel\". Would you like to setup the database now?";
                DialogResult R = MessageBox.Show(message, "Stats Database Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                // Just return if the user doesnt want to set up the databases
                if (R == DialogResult.No)
                {
                    return(true);
                }

                // Show Stats DB
                ShowDatabaseSetupForm(DatabaseMode.Stats, null);

                message = "In order to use the Gamespy Login Emulation feature of this program, we need to setup a database. "
                          + "You may choose to do this later by clicking \"Cancel\". Would you like to setup the database now?";
                R = MessageBox.Show(message, "Gamespy Database Setup", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                // Just return if the user doesnt want to set up the databases
                if (R == DialogResult.No)
                {
                    return(true);
                }

                ShowDatabaseSetupForm(DatabaseMode.Gamespy, null);
            }

            return(true);
        }