public static CustomGameData ShowDialog(string text, string caption, string defaultName = "", string defaultLocation = "") { Form prompt = new Form() { Width = 500, Height = 250, FormBorderStyle = FormBorderStyle.FixedDialog, Text = caption, StartPosition = FormStartPosition.CenterScreen }; Font font = new Font("Microsoft Sans Serif", 12, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); Label selectLabel = new Label() { Left = 30, Top = 20, AutoSize = true, Text = text, Font = font }; Label nameLabel = new Label() { Left = 30, Top = 80, AutoSize = true, Text = "Game Name", Font = font }; TextBox gameLocationTextBox = new TextBox() { Left = 30, Top = 50, Width = 400, Text = defaultLocation }; TextBox gameNameTextBox = new TextBox() { Left = 30, Top = 110, Width = 400, Text = defaultName }; Button getGameLocationButton = new Button() { Left = gameLocationTextBox.Left + gameLocationTextBox.Width, Top = gameLocationTextBox.Top, Width = 30, Height = gameLocationTextBox.Height, Text = "..." }; Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 140, DialogResult = DialogResult.OK }; OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Filter = "Exe files|*.exe|All Files|*.*"; fileDialog.FileOk += (sender, e) => { var execFile = fileDialog.FileName; var list = execFile.Split('\\'); var gameNameExec = list[list.Length - 1]; var gameName = gameNameExec.Substring(0, gameNameExec.Length - 4); gameLocationTextBox.Text = execFile; gameNameTextBox.Text = gameName; }; confirmation.Click += (sender, e) => { prompt.Close(); }; //textBox.Click += (sender, e) => { d.ShowDialog(); }; getGameLocationButton.Click += (sender, e) => { fileDialog.ShowDialog(); }; prompt.Controls.Add(gameLocationTextBox); prompt.Controls.Add(nameLabel); prompt.Controls.Add(confirmation); prompt.Controls.Add(selectLabel); prompt.Controls.Add(getGameLocationButton); prompt.Controls.Add(gameNameTextBox); prompt.AcceptButton = confirmation; CustomGameData customGameData = new CustomGameData(prompt.ShowDialog(), gameNameTextBox.Text, gameLocationTextBox.Text); return(customGameData); }
void EditCustomGame(string defaultName = "", string defaultLocation = "") { CustomGameData customGameData = Prompt.ShowDialog("Select Game Location", "Custom Game", defaultName, defaultLocation); if (customGameData.dialogResult == DialogResult.OK) { if (string.IsNullOrEmpty(customGameData.gameName) || string.IsNullOrEmpty(customGameData.gameLocation)) { MessageBox.Show("Input fields must not be empty!"); EditCustomGame(customGameData.gameName, customGameData.gameLocation); return; } if (!Utils.hasWriteAccessToFolder(Path.GetFullPath(Directory.GetCurrentDirectory()))) { Utils.StartApplicationInAdminMode(); } CustomGame d = new CustomGame(customGameData.gameName, customGameData.gameLocation); List <CustomGame> g = new List <CustomGame>(); if (File.Exists(JeremieLauncher.customGamesFile)) { g = JsonConvert.DeserializeObject <List <CustomGame> >(File.ReadAllText(JeremieLauncher.customGamesFile)); } CustomGame cur = ToCustomGame(); int index = g.FindIndex(delegate(CustomGame custom) { return(custom.Equals(cur)); }); if (index < 0) { MessageBox.Show("Cannot edit game!"); return; } g.RemoveAt(index); g.Insert(index, d); string output = JsonConvert.SerializeObject(g, Formatting.Indented); File.WriteAllText(JeremieLauncher.customGamesFile, output); Game dGame = d.ToGame(); dGame.index = this.index; JeremieLauncher.instance.UpdateCustomGame(dGame); } }
void AddCustomGame(string defaultName = "", string defaultLocation = "") { CustomGameData customGameData = Prompt.ShowDialog("Select Game Location", "Custom Game", defaultName, defaultLocation); if (customGameData.dialogResult == DialogResult.OK) { if (string.IsNullOrEmpty(customGameData.gameName) || string.IsNullOrEmpty(customGameData.gameLocation)) { MessageBox.Show("Input fields must not be empty!"); AddCustomGame(customGameData.gameName, customGameData.gameLocation); return; } if (!Utils.hasWriteAccessToFolder(Path.GetFullPath(Directory.GetCurrentDirectory()))) { Utils.StartApplicationInAdminMode(); } CustomGame d = new CustomGame(customGameData.gameName, customGameData.gameLocation); List <CustomGame> g = new List <CustomGame>(); if (File.Exists(customGamesFile)) { g = JsonConvert.DeserializeObject <List <CustomGame> >(File.ReadAllText(customGamesFile)); } if (g.FindIndex(delegate(CustomGame custom) { return(custom.Equals(d)); }) >= 0) { MessageBox.Show("Custom game with same name already exists"); AddCustomGame(customGameData.gameName, customGameData.gameLocation); return; } g.Add(d); string output = JsonConvert.SerializeObject(g, Formatting.Indented); File.WriteAllText(customGamesFile, output); AddGame(new Game(d.GameName, d.ExecPath, custom: true)); } }