/// <summary> /// Event fired when the Test button is clicked /// </summary> private async void TestBtn_Click(object sender, EventArgs e) { // Build Connection String MySqlConnectionStringBuilder Builder = new MySqlConnectionStringBuilder(); Builder.Server = Hostname.Text; Builder.Port = (uint)Port.Value; Builder.UserID = Username.Text; Builder.Password = Password.Text; Builder.Database = DBName.Text; // Attempt to connect, reporting any and all errors try { // Show loading form LoadingForm.ShowScreen(this, true, "Connecting to MySQL Database..."); SetNativeEnabled(false); // Dont lock up the program await Task.Run(() => { using (MySqlConnection Connection = new MySqlConnection(Builder.ConnectionString)) { Connection.Open(); Connection.Close(); } }); } catch (Exception E) { MessageBox.Show(E.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000 // Force window on top ); return; } finally { LoadingForm.CloseForm(); SetNativeEnabled(true); } // Show success after loading form has been called to close MessageBox.Show("Connection Successful!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0x40000 // Force window on top ); }
private void ExportSettingsMenuItem_Click(object sender, EventArgs e) { // Warn the user about saved changes DialogResult res = MessageBox.Show( "Changes made since this window was opened will not reflect in the ScoringSettings.xml file " + "without reloading the saved changes. Would you like me to reload the last saved values?", "Reload Settings", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question ); // Return if user cancels if (res == DialogResult.Cancel) { return; } // Show loading form LoadingForm.ShowScreen(this); // Reload settings! if (res == DialogResult.Yes && (!LoadConqFile() || !LoadCoopFile() || !LoadScoringCommon())) { LoadingForm.CloseForm(); return; } try { // Define our file path and string file = Path.Combine(Paths.DocumentsFolder, "ScoringSettings.xml"); // Generate our mappings Dictionary <string, string[]>[] types = { Scores, ConqScores, CoopScores }; string[] names = { "general", "conquest", "coop" }; // Create XML Settings XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = "\t"; settings.NewLineChars = Environment.NewLine; settings.NewLineHandling = NewLineHandling.Replace; // Write to file using (FileStream stream = File.Open(file, FileMode.Create)) using (XmlWriter Writer = XmlWriter.Create(stream, settings)) { // Player Element Writer.WriteStartDocument(); // Write editing warning Writer.WriteComment(" Auto Generated :: Please DO NOT Edit Me! "); // Begin Writer.WriteStartElement("settings"); // Itterate through all setting catagories for (int i = 0; i < 3; i++) { // Start general Element Writer.WriteStartElement(names[i]); // Add each scoring item to the XML foreach (KeyValuePair <string, string[]> item in types[i]) { // Open Row tag Writer.WriteStartElement("item"); Writer.WriteAttributeString("name", item.Key); Writer.WriteValue(item.Value[0]); Writer.WriteEndElement(); } // Close settings element Writer.WriteEndElement(); } // Close Tags and File Writer.WriteEndElement(); // Close general Element Writer.WriteEndDocument(); // End and Save file } // Notify user Notify.Show("Settings Exported", "Scoring settings were exported successfully!", AlertType.Success); } finally { LoadingForm.CloseForm(); } }
/// <summary> /// Backs up the asp database /// </summary> private void ExportAsASPBtn_Click(object sender, EventArgs e) { // Define backup folder for this backup, and create it if it doesnt exist string Folder = Path.Combine(Paths.DocumentsFolder, "Database Backups", "bak_" + DateTime.Now.ToString("yyyyMMdd_HHmm")); if (!Directory.Exists(Folder)) { Directory.CreateDirectory(Folder); } // Abortion indicator bool Aborted = false; // Open the database connection StatsDatabase Database; try { Database = new StatsDatabase(); } catch (Exception Ex) { MessageBox.Show( "Unable to connect to database\r\n\r\nMessage: " + Ex.Message, "Database Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error ); // Stop the ASP server, and close this form HttpServer.Stop(); this.Close(); return; } // Show loading screen LoadingForm.ShowScreen(this); // Backup each table into its own bak file foreach (string Table in StatsDatabase.StatsTables) { // Create file path string BakFile = Path.Combine(Folder, Table + ".bak"); // Backup tables try { using (Stream Str = File.Open(BakFile, FileMode.OpenOrCreate)) using (StreamWriter Wtr = new StreamWriter(Str)) { // Use a memory efficient way to export this stuff foreach (Dictionary <string, object> Row in Database.QueryReader("SELECT * FROM " + Table)) { Wtr.WriteLine(String.Join("\t", Row.Values)); } Wtr.Flush(); } } catch (Exception Ex) { // Close loading form LoadingForm.CloseForm(); // Display the Exception Form using (ExceptionForm Form = new ExceptionForm(Ex, false)) { Form.Message = "An error occured while trying to backup the \"" + Table + "\" table. " + "The backup operation will now be cancelled."; DialogResult Result = Form.ShowDialog(); } Aborted = true; // Try and remove backup folder try { DirectoryInfo Dir = new DirectoryInfo(Folder); Dir.Delete(true); } catch { } } if (Aborted) { break; } } // Only display success message if we didnt abort if (!Aborted) { // Close loading form LoadingForm.CloseForm(); string NL = Environment.NewLine; MessageBox.Show( String.Concat("Backup has been completed successfully!", NL, NL, "Backup files have been saved to:", NL, Folder), "Backup Success", MessageBoxButtons.OK, MessageBoxIcon.Information ); } // Close the connection Database.Dispose(); }
/// <summary> /// Event fired when the Generate Button is clicked /// Does the random Generating /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void GenerateBtn_Click(object sender, EventArgs e) { // Initialize lists List <string> Modes = new List <string>(); List <string> Sizes = new List <string>(); // Get list of supported Game Modes the user wants if (ConquestBox.Checked) { Modes.Add("gpm_cq"); } if (CoopBox.Checked) { Modes.Add("gpm_coop"); } // Get list of sizes the user wants if (s16Box.Checked) { Sizes.Add("16"); } if (s32Box.Checked) { Sizes.Add("32"); } if (s64Box.Checked) { Sizes.Add("64"); } // Make sure we have at least 1 mode and size if (Modes.Count == 0 || Sizes.Count == 0) { // Handle Message MessageBox.Show("You must select at least 1 GameMode and Map Size!", "User Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Initialize internal variables BF2Mod Mod = MainForm.SelectedMod; Random Rnd = new Random(); int NumOfMapsToAdd = (int)NumMaps.Value; int MapCount = Mod.Levels.Count; string[] gModes = Modes.ToArray(); StringBuilder Sb = new StringBuilder(); // Shuffle the maplist Mod.Levels.Shuffle(); // Show loading form LoadingForm.ShowScreen(this); SetNativeEnabled(false); // Don't lockup GUI, run in a new task await Task.Run(() => { // Loop through, the number of times the user specified, adding a map for (int i = 0; i < NumOfMapsToAdd; i++) { // Prevent infinite looping and/or quit if we have reached the map count if (i > 255 || (noDupesBox.Checked && i == MapCount)) { break; } // Grab a random map from the levels array try { // Try and load the map... if an exception is thrown, this loop doesnt count BF2Map Map = Mod.LoadMap((noDupesBox.Checked) ? Mod.Levels[i] : Mod.Levels[Rnd.Next(0, MapCount)]); // Get the common intersected gamemodes that the map has in common with what the user wants string[] Common = Map.GameModes.Keys.ToArray(); Common = gModes.Intersect(Common).ToArray(); // No common game modes if (Common.Length == 0) { NumOfMapsToAdd++; continue; } // Get a random gamemode key string Mode = Common[Rnd.Next(0, Common.Length)]; // Get the common map sizes between what the user wants, and what the map supports Common = Map.GameModes[Mode].Intersect(Sizes).ToArray(); if (Common.Length == 0) { // No common sizes, try another map NumOfMapsToAdd++; continue; } // Get a random size, and add the map string Size = Common[Rnd.Next(0, Common.Length)]; Sb.AppendLine(Map.Name + " " + Mode + " " + Size); } catch (InvalidMapException) { NumOfMapsToAdd++; } } }); // Add new maplist to the maplist box MapListBox.Text = Sb.ToString(); SetNativeEnabled(true); LoadingForm.CloseForm(); }