diff --git a/ConfigGenerator/ASFConfig.cs b/ConfigGenerator/ASFConfig.cs index 295957957..3f51144a6 100644 --- a/ConfigGenerator/ASFConfig.cs +++ b/ConfigGenerator/ASFConfig.cs @@ -13,8 +13,9 @@ namespace ConfigGenerator { ASFConfigs.Add(this); } - protected ASFConfig(string filePath) : base() { + protected ASFConfig(string filePath) { FilePath = filePath; + ASFConfigs.Add(this); } internal virtual void Save() { @@ -26,5 +27,37 @@ namespace ConfigGenerator { } } } + + internal virtual void Remove() { + string queryPath = Path.GetFileNameWithoutExtension(FilePath); + lock (FilePath) { + foreach (var configFile in Directory.EnumerateFiles(Program.ConfigDirectory, queryPath + ".*")) { + try { + File.Delete(configFile); + } catch (Exception e) { + Logging.LogGenericException(e); + } + } + } + ASFConfigs.Remove(this); + } + + internal virtual void Rename(string botName) { + if (string.IsNullOrEmpty(botName)) { + return; + } + + string queryPath = Path.GetFileNameWithoutExtension(FilePath); + lock (FilePath) { + foreach (var file in Directory.EnumerateFiles(Program.ConfigDirectory, queryPath + ".*")) { + try { + File.Move(file, Path.Combine(Program.ConfigDirectory, botName + Path.GetExtension(file))); + } catch (Exception e) { + Logging.LogGenericException(e); + } + } + FilePath = Path.Combine(Program.ConfigDirectory, botName + ".json"); + } + } } } diff --git a/ConfigGenerator/BotConfig.cs b/ConfigGenerator/BotConfig.cs index eaf664c9e..d104cab3c 100644 --- a/ConfigGenerator/BotConfig.cs +++ b/ConfigGenerator/BotConfig.cs @@ -124,6 +124,7 @@ namespace ConfigGenerator { private BotConfig(string filePath) : base(filePath) { FilePath = filePath; GamesPlayedWhileIdle.Add(0); + Save(); } } } diff --git a/ConfigGenerator/ConfigGenerator.csproj b/ConfigGenerator/ConfigGenerator.csproj index 057ac5e3a..ae180c55d 100644 --- a/ConfigGenerator/ConfigGenerator.csproj +++ b/ConfigGenerator/ConfigGenerator.csproj @@ -54,6 +54,7 @@ + Component diff --git a/ConfigGenerator/ConfigPage.cs b/ConfigGenerator/ConfigPage.cs index 2b8d28edc..26ee52f76 100644 --- a/ConfigGenerator/ConfigPage.cs +++ b/ConfigGenerator/ConfigPage.cs @@ -7,58 +7,25 @@ using System.Windows.Forms; namespace ConfigGenerator { internal class ConfigPage : TabPage { + internal readonly ASFConfig ASFConfig; + private EnhancedPropertyGrid EnhancedPropertyGrid; - private Button LoadButton, SaveButton; internal ConfigPage(ASFConfig config) : base() { if (config == null) { return; } - Text = Path.GetFileNameWithoutExtension(config.FilePath); + ASFConfig = config; + + RefreshText(); EnhancedPropertyGrid = new EnhancedPropertyGrid(config); Controls.Add(EnhancedPropertyGrid); - - Panel panel = new Panel() { - Height = 20, - Dock = DockStyle.Bottom, - }; - - LoadButton = new Button() { - Dock = DockStyle.Left, - Text = "Load" - }; - - panel.Controls.Add(LoadButton); - - SaveButton = new Button() { - Dock = DockStyle.Right, - Text = "Save" - }; - - SaveButton.Click += SaveButton_Click; - - panel.Controls.Add(SaveButton); - - Controls.Add(panel); } - private async void SaveButton_Click(object sender, EventArgs e) { - if (sender == null || e == null) { - return; - } - - SaveButton.Enabled = false; - - List tasks = new List(ASFConfig.ASFConfigs.Count); - foreach (ASFConfig config in ASFConfig.ASFConfigs) { - tasks.Add(Task.Run(() => config.Save())); - config.Save(); - } - await Task.WhenAll(tasks); - - SaveButton.Enabled = true; + internal void RefreshText() { + Text = Path.GetFileNameWithoutExtension(ASFConfig.FilePath); } private void InitializeComponent() { diff --git a/ConfigGenerator/DialogBox.cs b/ConfigGenerator/DialogBox.cs new file mode 100644 index 000000000..b9e03ed9e --- /dev/null +++ b/ConfigGenerator/DialogBox.cs @@ -0,0 +1,50 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace ConfigGenerator { + class DialogBox { + public static DialogResult InputBox(string title, string promptText, ref string value) { + Form form = new Form(); + Label label = new Label(); + TextBox textBox = new TextBox(); + + textBox.Width = 1000; + Button buttonOk = new Button(); + Button buttonCancel = new Button(); + + form.Text = title; + label.Text = promptText; + textBox.Text = value; + + buttonOk.Text = "OK"; + buttonCancel.Text = "Cancel"; + buttonOk.DialogResult = DialogResult.OK; + buttonCancel.DialogResult = DialogResult.Cancel; + + label.SetBounds(9, 20, 372, 13); + textBox.SetBounds(12, 36, 372, 20); + buttonOk.SetBounds(228, 72, 75, 23); + buttonCancel.SetBounds(309, 72, 75, 23); + + label.AutoSize = true; + textBox.Anchor = textBox.Anchor | AnchorStyles.Right; + buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + + form.ClientSize = new Size(396, 107); + form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel }); + form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height); + form.FormBorderStyle = FormBorderStyle.FixedDialog; + form.StartPosition = FormStartPosition.CenterScreen; + form.MinimizeBox = false; + form.MaximizeBox = false; + form.AcceptButton = buttonOk; + form.CancelButton = buttonCancel; + + DialogResult dialogResult = form.ShowDialog(); + value = textBox.Text; + return dialogResult; + } + } +} diff --git a/ConfigGenerator/EnhancedPropertyGrid.cs b/ConfigGenerator/EnhancedPropertyGrid.cs index de5c276a0..0f2a2476e 100644 --- a/ConfigGenerator/EnhancedPropertyGrid.cs +++ b/ConfigGenerator/EnhancedPropertyGrid.cs @@ -2,16 +2,24 @@ namespace ConfigGenerator { internal sealed class EnhancedPropertyGrid : PropertyGrid { + private ASFConfig ASFConfig; internal EnhancedPropertyGrid(ASFConfig config) : base() { if (config == null) { return; } + ASFConfig = config; + SelectedObject = config; Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top; Dock = DockStyle.Fill; HelpVisible = false; ToolbarVisible = false; } + + protected override void OnPropertyValueChanged(PropertyValueChangedEventArgs e) { + base.OnPropertyValueChanged(e); + ASFConfig.Save(); + } } } diff --git a/ConfigGenerator/GlobalConfig.cs b/ConfigGenerator/GlobalConfig.cs index 65c1870bb..64b8fec37 100644 --- a/ConfigGenerator/GlobalConfig.cs +++ b/ConfigGenerator/GlobalConfig.cs @@ -137,6 +137,7 @@ namespace ConfigGenerator { private GlobalConfig(string filePath) : base(filePath) { FilePath = filePath; Blacklist.AddRange(GlobalBlacklist); + Save(); } } } diff --git a/ConfigGenerator/MainForm.Designer.cs b/ConfigGenerator/MainForm.Designer.cs index da4099542..78e64a259 100644 --- a/ConfigGenerator/MainForm.Designer.cs +++ b/ConfigGenerator/MainForm.Designer.cs @@ -28,9 +28,6 @@ this.FileMenu = new System.Windows.Forms.ToolStripMenuItem(); this.FileMenuHelp = new System.Windows.Forms.ToolStripMenuItem(); this.FileMenuExit = new System.Windows.Forms.ToolStripMenuItem(); - this.BotMenu = new System.Windows.Forms.ToolStripMenuItem(); - this.BotMenuNew = new System.Windows.Forms.ToolStripMenuItem(); - this.BotMenuDelete = new System.Windows.Forms.ToolStripMenuItem(); this.MainTab = new System.Windows.Forms.TabControl(); this.MenuPanel.SuspendLayout(); this.SuspendLayout(); @@ -38,11 +35,11 @@ // MenuPanel // this.MenuPanel.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.FileMenu, - this.BotMenu}); + this.FileMenu}); this.MenuPanel.Location = new System.Drawing.Point(0, 0); this.MenuPanel.Name = "MenuPanel"; - this.MenuPanel.Size = new System.Drawing.Size(784, 24); + this.MenuPanel.Padding = new System.Windows.Forms.Padding(8, 3, 0, 3); + this.MenuPanel.Size = new System.Drawing.Size(780, 25); this.MenuPanel.TabIndex = 0; this.MenuPanel.Text = "menuStrip1"; // @@ -52,67 +49,56 @@ this.FileMenuHelp, this.FileMenuExit}); this.FileMenu.Name = "FileMenu"; - this.FileMenu.Size = new System.Drawing.Size(37, 20); + this.FileMenu.Size = new System.Drawing.Size(37, 19); this.FileMenu.Text = "File"; // // FileMenuHelp // this.FileMenuHelp.Name = "FileMenuHelp"; - this.FileMenuHelp.Size = new System.Drawing.Size(99, 22); + this.FileMenuHelp.Size = new System.Drawing.Size(152, 22); this.FileMenuHelp.Text = "Help"; this.FileMenuHelp.Click += new System.EventHandler(this.FileMenuHelp_Click); // // FileMenuExit // this.FileMenuExit.Name = "FileMenuExit"; - this.FileMenuExit.Size = new System.Drawing.Size(99, 22); + this.FileMenuExit.Size = new System.Drawing.Size(152, 22); this.FileMenuExit.Text = "Exit"; this.FileMenuExit.Click += new System.EventHandler(this.FileMenuExit_Click); // - // BotMenu - // - this.BotMenu.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.BotMenuNew, - this.BotMenuDelete}); - this.BotMenu.Name = "BotMenu"; - this.BotMenu.Size = new System.Drawing.Size(37, 20); - this.BotMenu.Text = "Bot"; - // - // BotMenuNew - // - this.BotMenuNew.Name = "BotMenuNew"; - this.BotMenuNew.Size = new System.Drawing.Size(107, 22); - this.BotMenuNew.Text = "New"; - this.BotMenuNew.Click += new System.EventHandler(this.BotMenuNew_Click); - // - // BotMenuDelete - // - this.BotMenuDelete.Name = "BotMenuDelete"; - this.BotMenuDelete.Size = new System.Drawing.Size(107, 22); - this.BotMenuDelete.Text = "Delete"; - this.BotMenuDelete.Click += new System.EventHandler(this.BotMenuDelete_Click); - // // MainTab // this.MainTab.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.MainTab.Location = new System.Drawing.Point(12, 27); + this.MainTab.HotTrack = true; + this.MainTab.Location = new System.Drawing.Point(16, 33); + this.MainTab.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); + this.MainTab.Multiline = true; this.MainTab.Name = "MainTab"; this.MainTab.SelectedIndex = 0; - this.MainTab.Size = new System.Drawing.Size(760, 522); + this.MainTab.Size = new System.Drawing.Size(748, 509); this.MainTab.TabIndex = 1; + this.MainTab.Selected += new System.Windows.Forms.TabControlEventHandler(this.MainTab_Selected); + this.MainTab.Deselecting += new System.Windows.Forms.TabControlCancelEventHandler(this.MainTab_Deselecting); // // MainForm // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(784, 561); + this.AutoScroll = true; + this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; + this.ClientSize = new System.Drawing.Size(780, 557); this.Controls.Add(this.MainTab); this.Controls.Add(this.MenuPanel); + this.DoubleBuffered = true; + this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238))); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MainMenuStrip = this.MenuPanel; + this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4); this.Name = "MainForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "ASF Config Generator"; this.Load += new System.EventHandler(this.MainForm_Load); this.MenuPanel.ResumeLayout(false); @@ -125,9 +111,6 @@ #endregion private System.Windows.Forms.MenuStrip MenuPanel; - private System.Windows.Forms.ToolStripMenuItem BotMenu; - private System.Windows.Forms.ToolStripMenuItem BotMenuNew; - private System.Windows.Forms.ToolStripMenuItem BotMenuDelete; private System.Windows.Forms.TabControl MainTab; private System.Windows.Forms.ToolStripMenuItem FileMenu; private System.Windows.Forms.ToolStripMenuItem FileMenuHelp; diff --git a/ConfigGenerator/MainForm.cs b/ConfigGenerator/MainForm.cs index e227ab754..19a8a7b1f 100644 --- a/ConfigGenerator/MainForm.cs +++ b/ConfigGenerator/MainForm.cs @@ -5,6 +5,24 @@ using System.Windows.Forms; namespace ConfigGenerator { public partial class MainForm : Form { + private const byte ReservedTabs = 3; + + private ConfigPage ASFTab; + + private TabPage RemoveTab = new TabPage() { + Text = "-", + }; + + private TabPage RenameTab = new TabPage() { + Text = "~", + }; + + private TabPage NewTab = new TabPage() { + Text = "+", + }; + + private TabPage OldTab; + public MainForm() { InitializeComponent(); } @@ -46,16 +64,113 @@ namespace ConfigGenerator { return; } - MainTab.TabPages.Add(new ConfigPage(GlobalConfig.Load(Path.Combine(Program.ConfigDirectory, Program.GlobalConfigFile)))); + ASFTab = new ConfigPage(GlobalConfig.Load(Path.Combine(Program.ConfigDirectory, Program.GlobalConfigFile))); + + MainTab.TabPages.Add(ASFTab); foreach (var configFile in Directory.EnumerateFiles(Program.ConfigDirectory, "*.json")) { string botName = Path.GetFileNameWithoutExtension(configFile); - if (botName.Equals(Program.ASF)) { - continue; + switch (botName) { + case Program.ASF: + case "example": + case "minimal": + continue; } MainTab.TabPages.Add(new ConfigPage(BotConfig.Load(configFile))); } + + MainTab.TabPages.AddRange(new TabPage[] { RemoveTab, RenameTab, NewTab }); + } + + private void MainTab_Selected(object sender, TabControlEventArgs e) { + if (sender == null || e == null) { + return; + } + + if (e.TabPage == RemoveTab) { + ConfigPage configPage = OldTab as ConfigPage; + if (configPage == null) { + MainTab.SelectedIndex = -1; + return; + } + + if (configPage == ASFTab) { + MainTab.SelectedTab = ASFTab; + Logging.LogGenericError("You can't remove global config!"); + return; + } + + MainTab.SelectedIndex = 0; + configPage.ASFConfig.Remove(); + MainTab.TabPages.Remove(configPage); + } else if (e.TabPage == RenameTab) { + ConfigPage configPage = OldTab as ConfigPage; + if (configPage == null) { + MainTab.SelectedIndex = -1; + return; + } + + if (configPage == ASFTab) { + MainTab.SelectedTab = ASFTab; + Logging.LogGenericError("You can't rename global config!"); + return; + } + + MainTab.SelectedTab = configPage; + + string input = null; + if (DialogBox.InputBox("Rename", "Your new bot name:", ref input) != DialogResult.OK) { + return; + } + + if (string.IsNullOrEmpty(input)) { + Logging.LogGenericError("Your bot name is empty!"); + return; + } + + configPage.ASFConfig.Rename(input); + configPage.RefreshText(); + } else if (e.TabPage == NewTab) { + ConfigPage configPage = OldTab as ConfigPage; + if (configPage == null) { + MainTab.SelectedIndex = -1; + return; + } + + MainTab.SelectedTab = configPage; + + string input = null; + if (DialogBox.InputBox("Rename", "Your new bot name:", ref input) != DialogResult.OK) { + return; + } + + if (string.IsNullOrEmpty(input)) { + Logging.LogGenericError("Your bot name is empty!"); + return; + } + + foreach (ASFConfig config in ASFConfig.ASFConfigs) { + if (Path.GetFileNameWithoutExtension(config.FilePath).Equals(input)) { + Logging.LogGenericError("Bot with such name exists already!"); + return; + } + } + + input = Path.Combine(Program.ConfigDirectory, input + ".json"); + + ConfigPage newConfigPage = new ConfigPage(BotConfig.Load(input)); + MainTab.TabPages.Insert(MainTab.TabPages.Count - ReservedTabs, newConfigPage); + MainTab.SelectedTab = newConfigPage; + } + } + + private void MainTab_Deselecting(object sender, TabControlCancelEventArgs e) { + if (sender == null || e == null) { + return; + } + + OldTab = e.TabPage; } } }