/// <summary>
        /// Event fired when the Change Button is pushed on the App Settings Tab
        /// </summary>
        private void ChangeButton_Click(object sender, EventArgs e)
        {
            using (ModalOverlay overlay = new ModalOverlay(this, 0.2))
            {
                overlay.Show(this);
                string OrigLocation = Program.Config.Bf2InstallDir;
                if (SetupManager.ShowInstallForm(overlay))
                {
                    // Load the BF2 Server
                    try
                    {
                        BF2Client.SetInstallPath(Program.Config.Bf2InstallDir);
                    }
                    catch (Exception E)
                    {
                        MetroMessageBox.Show(this, E.Message, "Battlefield 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Program.Config.Bf2InstallDir = OrigLocation;
                        Program.Config.Save();
                    }
                }
                overlay.Close();
            }

            this.Focus();
        }
        /// <summary>
        /// Event fired when the Launch Battlefield 2 button is pushed on the Launcher Tab
        /// </summary>
        private async void LaunchButton_Click(object sender, EventArgs args)
        {
            // Lock button to prevent spam
            LaunchButton.Enabled = false;

            // Close the app
            if (BF2Client.IsRunning)
            {
                BF2Client.Stop();
                return;
            }

            // Show overlay first, which provides the smokey (Modal) background
            using (ModalOverlay overlay = new ModalOverlay(this, 0.3))
            {
                // Show overlay
                overlay.Show(this);

                // Make sure a mod is selected
                if (ModComboBox.SelectedIndex < 1)
                {
                    MetroMessageBox.Show(overlay,
                                         "Please select a Bf2 Mod before attempting to start the game!",
                                         "No Mod Selected", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, 150
                                         );
                    overlay.Close();

                    // Reset button
                    BF2Client_Exited();

                    // Focus the mod select
                    ModComboBox.Focus();
                    return;
                }

                // Grab our mod and provider
                BF2Mod          Mod      = ModComboBox.SelectedItem as BF2Mod;
                ServiceProvider Provider = ProviderComboBox.SelectedItem as ServiceProvider;
                Server          Server   = ServerComboBox.SelectedItem as Server;

                // Remove old redirects
                Redirector.RemoveRedirects();

                // If we arent using a provider, skip to just launching the game
                if (Provider == null)
                {
                    goto StartClient;
                }

                // Apply redirects in a new thread
                SyncProgress <TaskStep> MyProgress = new SyncProgress <TaskStep>(RedirectStatusUpdate);
                bool Success = await Redirector.ApplyRedirectsAsync(Provider, MyProgress);

                if (!Success)
                {
                    // Show error
                    MetroMessageBox.Show(overlay, ErrorStep.Description, "Redirect Error", MessageBoxButtons.OK, MessageBoxIcon.Error, 180);
                    overlay.Close();

                    // Reset button
                    BF2Client_Exited();
                    return;
                }

                // Show the Task Form
                TaskForm.Show(this, "Launching Battlefield 2", $"Starting Battlefield 2 with mod \"{Mod.Title}\"", false, ProgressBarStyle.Marquee, 0);

                // Our goto to start the game
StartClient:
                {
                    try
                    {
                        // ===
                        // ALWAYS Remove all temporary keys before this next point
                        // ===
                        Params.Reload(LaunchParamsTextBox.Text);
                        Params.ClearTempParams();

                        // If we are auto joining a server, we must login!
                        if (Provider != null && (Server != null || CredentialsCheckBox.Checked))
                        {
                            // Prompt user to login!
                            using (LoginForm f = new LoginForm(Provider))
                            {
                                DialogResult Res = f.ShowDialog(overlay);
                                if (Res == DialogResult.Cancel)
                                {
                                    // Reset button
                                    TaskForm.CloseForm();
                                    BF2Client_Exited();
                                    return;
                                }

                                // Set server params
                                if (Server != null)
                                {
                                    Params.AddOrSet("joinServer", Server.Address);
                                    Params.AddOrSet("port", Server.Port.ToString());
                                }

                                // Set login params
                                Params.AddOrSet("playerName", f.UsernameTextBox.Text);
                                Params.AddOrSet("playerPassword", f.PasswordTextBox.Text);
                            }
                        }

                        // Start the client executable
                        BF2Client.Start(Mod, Params.BuildString(true));
                    }
                    catch (Exception e)
                    {
                        // Show error
                        MetroMessageBox.Show(overlay, e.Message, "Failure to Launch", MessageBoxButtons.OK, MessageBoxIcon.Error, 180);
                        BF2Client_Exited();
                    }
                }

                // Close the task form
                TaskForm.CloseForm();

                // Close Task form and overlay
                using (RunningOverlay = new GameRunningForm(this))
                {
                    RunningOverlay.ShowDialog(overlay);
                }

                // Close Overlay
                overlay.Close();
                LaunchButton.Focus();
            }
        }
示例#3
0
        /// <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;

            // 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.Bf2InstallDir) || !File.Exists(Path.Combine(Config.Bf2InstallDir, "bf2.exe")))
            {
                TraceLog.WriteLine("Empty or Invalid BF2 directory detected, running Install Form.");
                if (!ShowInstallForm())
                {
                    return(false);
                }
            }

            // Create the "My Documents/BF2Statistics" folder
            try
            {
                // Make sure documents folder exists
                if (!Directory.Exists(Program.DocumentsFolder))
                {
                    Directory.CreateDirectory(Program.DocumentsFolder);
                }
            }
            catch (Exception E)
            {
                // Alert the user that there was an error
                MessageBox.Show("We 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
LoadClient:
            {
                // Load the BF2 Server
                try
                {
                    BF2Client.SetInstallPath(Config.Bf2InstallDir);
                }
                catch (Exception E)
                {
                    MetroMessageBox.Show(Form.ActiveForm, E.Message, "Battlefield 2 Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

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

                    goto LoadClient;
                }
            }

            return(true);
        }