BF2Statistics.MainForm.LaunchClientBtn_Click C# (CSharp) Method

LaunchClientBtn_Click() private method

Client Launcher Button Click
private LaunchClientBtn_Click ( object sender, EventArgs e ) : void
sender object
e EventArgs
return void
        private async void LaunchClientBtn_Click(object sender, EventArgs e)
        {
            // Lock button to prevent spam
            LaunchClientBtn.Enabled = false;

            // Launching
            if (ClientProcess == null)
            {
                // Make sure the Bf2 client supports this mod
                if (!Directory.Exists(Path.Combine(Config.ClientPath, "mods", SelectedMod.Name)))
                {
                    MessageBox.Show("The Battlefield 2 client installation does not have the selected mod installed." +
                        " Please install the mod before launching the BF2 client", "Mod Error", MessageBoxButtons.OK,
                        MessageBoxIcon.Exclamation);
                    return;
                }

                // Test the ASP stats service here if redirects are enabled. 
                if (Redirector.RedirectsEnabled && Redirector.StatsServerAddress != null)
                {
                    DnsCacheResult Result;

                    // First things first, we fecth the IP address from the DNS cache of our stats server
                    try
                    {
                        // Fetch the address
                        Result = await Networking.ValidateAddressAsync("bf2web.gamespy.com", Redirector.StatsServerAddress);
                        if (!Result.GotExpectedResult)
                        {
                            MessageBox.Show(
                                "Redirect IP address does not match the IP address found by Windows DNS."
                                + Environment.NewLine.Repeat(1)
                                + "Expected: " + Result.ExpectedAddress + "; Found: " + Result.ResultAddresses[0]
                                + Environment.NewLine + "This error can be caused if the HOSTS file cannot be read by windows "
                                + "(Ex: permissions too strict for System)",
                                "Stats Redirect Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning
                            );

                            // Unlock Btn
                            LaunchClientBtn.Enabled = true;
                            return;
                        }
                    }
                    catch (Exception Ex)
                    {
                        // ALert user
                        MessageBox.Show(
                            "Failed to obtain an IP address for the ASP stats server from Windows DNS: "
                            + Environment.NewLine.Repeat(1)
                            + Ex.Message 
                            + Environment.NewLine.Repeat(1)
                            + "You may choose to ignore this message and continue, but note that stats may not be working correctly in the BFHQ.",
                            "Stats Redirect Error",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Warning
                        );

                        // Unlock Btn
                        LaunchClientBtn.Enabled = true;
                        return;
                    }

                    // Loopback for the Retry Button
                    CheckAsp:
                    {
                        try
                        {
                            // Check ASP service
                            await Task.Run(() => StatsManager.ValidateASPService("http://" + Result.ResultAddresses[0]));
                        }
                        catch (Exception Ex)
                        {
                            // ALert user
                            DialogResult Res = MessageBox.Show(
                                "There was an error trying to validate The ASP Stats server defined in your HOSTS file."
                                + Environment.NewLine.Repeat(1)
                                + "Error Message: " + Ex.Message + Environment.NewLine
                                + "Server Address: " + Result.ResultAddresses[0]
                                + Environment.NewLine.Repeat(1)
                                + "You may choose to ignore this message and continue, but note that stats will not be working correctly in the BFHQ.",
                                "Stats Server Verification",
                                MessageBoxButtons.AbortRetryIgnore,
                                MessageBoxIcon.Warning
                            );

                            // User Button Selection
                            if (Res == DialogResult.Retry)
                            {
                                goto CheckAsp;
                            }
                            else if (Res == DialogResult.Abort || Res != DialogResult.Ignore)
                            {
                                // Unlock Btn
                                LaunchClientBtn.Enabled = true;
                                return;
                            }
                        }
                    }
                }

                // Start new BF2 proccess
                ProcessStartInfo Info = new ProcessStartInfo();
                Info.Arguments = String.Format(" +modPath mods/{0} {1}", SelectedMod.Name, ParamBox.Text.Trim());
                Info.FileName = "bf2.exe";
                Info.WorkingDirectory = Config.ClientPath;
                ClientProcess = Process.Start(Info);

                // Hook into the proccess so we know when its running, and register a closing event
                ClientProcess.EnableRaisingEvents = true;
                ClientProcess.Exited += BF2Client_Exited;

                // Update button
                LaunchClientBtn.Enabled = true;
                LaunchClientBtn.Text = "Shutdown Battlefield 2";
            }
            else
            {
                try
                {
                    // prevent button spam
                    LoadingForm.ShowScreen(this);
                    SetNativeEnabled(false);
                    ClientProcess.Kill();
                }
                catch (Exception E)
                {
                    MessageBox.Show("Unable to stop Battlefield 2 client process!"
                        + Environment.NewLine.Repeat(1) + E.Message,
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
MainForm