/// <summary> /// Saves the maplist to the maplist.com file /// </summary> private void SaveButton_Click(object sender, EventArgs e) { // Make sure we have something in the maplist! if (String.IsNullOrWhiteSpace(MapListBox.Text)) { MessageBox.Show("You must at least have 1 map before saving the maplist!", "User Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } // Append the prefix to each map line BF2Mod Mod = MainForm.SelectedMod; // Clear out old Junk, and add new Mod.MapList.Entries.Clear(); for (int i = 0; i < MapListBox.Lines.Length; i++) { // Validate that the line was not tampered with if (!Mod.MapList.AddFromString(MapListBox.Lines[i])) { MessageBox.Show( "Error parsing map entry on line " + i + ". Operation aborted.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Warning ); } } // Save and close Mod.MapList.SaveToFile(Mod.MaplistFilePath); this.Close(); }
public XpackMedalsConfigForm() { InitializeComponent(); int controlsAdded = 0; // Add each valid mod found in the servers Mods directory for (int i = 0; i < BF2Server.Mods.Count; i++) { // Get our current working mod BF2Mod Mod = BF2Server.Mods[i]; // Bf2 is predefined if (Mod.Name.Equals("bf2", StringComparison.InvariantCultureIgnoreCase)) { checkBox1.Tag = Mod.Name; checkBox1.Checked = StatsPython.Config.XpackMedalMods.Contains(Mod.Name.ToLowerInvariant()); continue; } // Xpack is predefined if (Mod.Name.Equals("xpack", StringComparison.InvariantCultureIgnoreCase)) { checkBox2.Tag = Mod.Name; checkBox2.Checked = StatsPython.Config.XpackMedalMods.Contains(Mod.Name.ToLowerInvariant()); continue; } // Stop if over 10 added mods. I chose to use continue here instead of break // So that Xpack can get ticked if it may be at the bottom of the list if (controlsAdded >= 10) { continue; } try { // Fetch our checkbox int index = controlsAdded + 3; CheckBox ChkBox = panel2.Controls["checkBox" + index] as CheckBox; // Configure the Checkbox string title = (Mod.Title.Length > 32) ? Mod.Title.CutTolength(29) + "..." : Mod.Title; ChkBox.Text = String.Format("{0} [{1}]", title, Mod.Name); ChkBox.Checked = StatsPython.Config.XpackMedalMods.Contains(Mod.Name.ToLowerInvariant()); ChkBox.Tag = Mod.Name; ChkBox.Show(); // Add tooltip to checkbox with the full title Tipsy.SetToolTip(ChkBox, Mod.Title); } catch { continue; } // Increment controlsAdded++; } }
/// <summary> /// Fetches the BF2 Mod into an object. If the Mod has already been loaded /// into an object previously, that object will be returned instead. /// </summary> /// <param name="Name">The mod's FOLDER name</param> /// <returns></returns> public static BF2Mod LoadMod(string Name) { // Check 2 things, 1, does the mod exist, and 2, if we have loaded it already if (!Mods.Contains(Name)) throw new ArgumentException("Bf2 Mod " + Name + " does not exist!"); else if (LoadedMods.ContainsKey(Name)) return LoadedMods[Name]; // Create a new instance of the mod, and store it for later BF2Mod Mod = new BF2Mod(Path.Combine(RootPath, "mods"), Name); LoadedMods.Add(Name, Mod); return Mod; }
/// <summary> /// Starts the Battlefield 2 Server application /// </summary> /// <param name="Mod">The battlefield 2 mod that the server is to use</param> /// <param name="ExtraArgs">Any arguments to be past to the application on startup</param> /// <param name="ShowConsole">If false, a console will not be created</param> /// <param name="MinConsole">If <see cref="ShowConsole"/> is enabled, true will start the console minimized</param> public static void Start(BF2Mod Mod, string ExtraArgs, bool ShowConsole, bool MinConsole) { // Make sure the server isnt running already if (IsRunning) { throw new Exception("The Battlefield 2 server is already running!"); } // Make sure the mod is supported! if (!Mods.Contains(Mod)) { throw new Exception("The battlefield 2 mod cannot be located in the mods folder"); } // Start new BF2 proccess ProcessStartInfo Info = new ProcessStartInfo(); Info.Arguments = String.Format(" +modPath mods/{0}", Mod.Name.ToLower()); if (!String.IsNullOrEmpty(ExtraArgs)) { Info.Arguments += " " + ExtraArgs; } // Hide window if user specifies this... if (!ShowConsole) { Info.WindowStyle = ProcessWindowStyle.Hidden; } else if (MinConsole) { Info.WindowStyle = ProcessWindowStyle.Minimized; } // Start process. Set working directory so we dont get errors! Info.FileName = "bf2_w32ded.exe"; Info.WorkingDirectory = RootPath; ServerProcess = Process.Start(Info); // Hook into the proccess so we know when its running, and register a closing event ServerProcess.EnableRaisingEvents = true; ServerProcess.Exited += ServerProcess_Exited; // Call event if (Started != null) { Started(); } }
/// <summary> /// Starts the Battlefield 2 Server application /// </summary> /// <param name="Mod">The battlefield 2 mod that the server is to use</param> /// <param name="ExtraArgs">Any arguments to be past to the application on startup</param> /// <param name="ShowConsole">If false, a console will not be created</param> /// <param name="MinConsole">If <see cref="ShowConsole"/> is enabled, true will start the console minimized</param> public static void Start(BF2Mod Mod, string ExtraArgs, bool ShowConsole, bool MinConsole) { // Make sure the server isnt running already if (IsRunning) throw new Exception("The Battlefield 2 server is already running!"); // Make sure the mod is supported! if (!Mods.Contains(Mod)) throw new Exception("The battlefield 2 mod cannot be located in the mods folder"); // Start new BF2 proccess ProcessStartInfo Info = new ProcessStartInfo(); Info.Arguments = String.Format(" +modPath mods/{0}", Mod.Name.ToLower()); if (!String.IsNullOrEmpty(ExtraArgs)) Info.Arguments += " " + ExtraArgs; // Hide window if user specifies this... if (!ShowConsole) Info.WindowStyle = ProcessWindowStyle.Hidden; else if (MinConsole) Info.WindowStyle = ProcessWindowStyle.Minimized; // Start process. Set working directory so we dont get errors! Info.FileName = "bf2_w32ded.exe"; Info.WorkingDirectory = RootPath; ServerProcess = Process.Start(Info); // Hook into the proccess so we know when its running, and register a closing event ServerProcess.EnableRaisingEvents = true; ServerProcess.Exited += ServerProcess_Exited; // Call event if (Started != null) Started(); }
/// <summary> /// Loads a battlefield 2 server into this object for use. /// </summary> /// <param name="ServerPath">The full root path to the server's executable file</param> 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(); }
/// <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(); }
/// <summary> /// Loads a battlefield 2 server into this object for use. /// </summary> /// <param name="ServerPath">The full root path to the server's executable file</param> 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(); }