From d604c89de58e9c402e733094339678611b575e61 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 23 Apr 2015 01:41:56 +0000 Subject: [PATCH] MultiDiskBundler - progress --- .../MultiDiskBundler.Designer.cs | 55 +++++--- .../MultiDiskBundler/MultiDiskBundler.cs | 121 ++++++++++++++++-- .../MultiDiskFileSelector.Designer.cs | 25 ++-- .../MultiDiskBundler/MultiDiskFileSelector.cs | 22 ++-- 4 files changed, 175 insertions(+), 48 deletions(-) diff --git a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.Designer.cs b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.Designer.cs index c4fc90009c..b11809f3f9 100644 --- a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.Designer.cs +++ b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.Designer.cs @@ -31,8 +31,9 @@ System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MultiDiskBundler)); this.MultiDiskMenuStrip = new System.Windows.Forms.MenuStrip(); this.SaveRunButton = new System.Windows.Forms.Button(); - this.buttonCancel = new System.Windows.Forms.Button(); + this.CancelBtn = new System.Windows.Forms.Button(); this.groupBox3 = new System.Windows.Forms.GroupBox(); + this.BrowseBtn = new System.Windows.Forms.Button(); this.NameBox = new System.Windows.Forms.TextBox(); this.FileSelectorPanel = new System.Windows.Forms.Panel(); this.AddButton = new System.Windows.Forms.Button(); @@ -43,7 +44,7 @@ // this.MultiDiskMenuStrip.Location = new System.Drawing.Point(0, 0); this.MultiDiskMenuStrip.Name = "MultiDiskMenuStrip"; - this.MultiDiskMenuStrip.Size = new System.Drawing.Size(476, 24); + this.MultiDiskMenuStrip.Size = new System.Drawing.Size(506, 24); this.MultiDiskMenuStrip.TabIndex = 0; this.MultiDiskMenuStrip.Text = "menuStrip1"; // @@ -51,7 +52,7 @@ // this.SaveRunButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.SaveRunButton.Enabled = false; - this.SaveRunButton.Location = new System.Drawing.Point(313, 329); + this.SaveRunButton.Location = new System.Drawing.Point(343, 329); this.SaveRunButton.Name = "SaveRunButton"; this.SaveRunButton.Size = new System.Drawing.Size(85, 23); this.SaveRunButton.TabIndex = 9; @@ -59,38 +60,51 @@ this.SaveRunButton.UseVisualStyleBackColor = true; this.SaveRunButton.Click += new System.EventHandler(this.SaveRunButton_Click); // - // buttonCancel + // CancelBtn // - this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.buttonCancel.Location = new System.Drawing.Point(404, 329); - this.buttonCancel.Name = "buttonCancel"; - this.buttonCancel.Size = new System.Drawing.Size(60, 23); - this.buttonCancel.TabIndex = 10; - this.buttonCancel.Text = "&Cancel"; - this.buttonCancel.UseVisualStyleBackColor = true; - this.buttonCancel.Click += new System.EventHandler(this.buttonCancel_Click); + this.CancelBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.CancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel; + this.CancelBtn.Location = new System.Drawing.Point(434, 329); + this.CancelBtn.Name = "CancelBtn"; + this.CancelBtn.Size = new System.Drawing.Size(60, 23); + this.CancelBtn.TabIndex = 10; + this.CancelBtn.Text = "&Cancel"; + this.CancelBtn.UseVisualStyleBackColor = true; + this.CancelBtn.Click += new System.EventHandler(this.CancelBtn_Click); // // groupBox3 // this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); + this.groupBox3.Controls.Add(this.BrowseBtn); this.groupBox3.Controls.Add(this.NameBox); this.groupBox3.Location = new System.Drawing.Point(8, 28); this.groupBox3.Name = "groupBox3"; - this.groupBox3.Size = new System.Drawing.Size(456, 45); + this.groupBox3.Size = new System.Drawing.Size(486, 45); this.groupBox3.TabIndex = 11; this.groupBox3.TabStop = false; this.groupBox3.Text = "Name"; // + // BrowseBtn + // + this.BrowseBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.BrowseBtn.Location = new System.Drawing.Point(417, 18); + this.BrowseBtn.Name = "BrowseBtn"; + this.BrowseBtn.Size = new System.Drawing.Size(63, 23); + this.BrowseBtn.TabIndex = 14; + this.BrowseBtn.Text = "Browse..."; + this.BrowseBtn.UseVisualStyleBackColor = true; + this.BrowseBtn.Click += new System.EventHandler(this.BrowseBtn_Click); + // // NameBox // this.NameBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.NameBox.Location = new System.Drawing.Point(6, 19); this.NameBox.Name = "NameBox"; - this.NameBox.Size = new System.Drawing.Size(444, 20); + this.NameBox.Size = new System.Drawing.Size(405, 20); this.NameBox.TabIndex = 0; + this.NameBox.TextChanged += new System.EventHandler(this.NameBox_TextChanged); // // FileSelectorPanel // @@ -101,7 +115,7 @@ this.FileSelectorPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.FileSelectorPanel.Location = new System.Drawing.Point(8, 79); this.FileSelectorPanel.Name = "FileSelectorPanel"; - this.FileSelectorPanel.Size = new System.Drawing.Size(456, 244); + this.FileSelectorPanel.Size = new System.Drawing.Size(486, 244); this.FileSelectorPanel.TabIndex = 12; // // AddButton @@ -109,7 +123,7 @@ this.AddButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.AddButton.Location = new System.Drawing.Point(8, 329); this.AddButton.Name = "AddButton"; - this.AddButton.Size = new System.Drawing.Size(75, 23); + this.AddButton.Size = new System.Drawing.Size(60, 23); this.AddButton.TabIndex = 13; this.AddButton.Text = "Add"; this.AddButton.UseVisualStyleBackColor = true; @@ -119,11 +133,11 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(476, 364); + this.ClientSize = new System.Drawing.Size(506, 364); this.Controls.Add(this.AddButton); this.Controls.Add(this.FileSelectorPanel); this.Controls.Add(this.groupBox3); - this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.CancelBtn); this.Controls.Add(this.SaveRunButton); this.Controls.Add(this.MultiDiskMenuStrip); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); @@ -143,10 +157,11 @@ private System.Windows.Forms.MenuStrip MultiDiskMenuStrip; private System.Windows.Forms.Button SaveRunButton; - private System.Windows.Forms.Button buttonCancel; + private System.Windows.Forms.Button CancelBtn; private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.TextBox NameBox; private System.Windows.Forms.Panel FileSelectorPanel; private System.Windows.Forms.Button AddButton; + private System.Windows.Forms.Button BrowseBtn; } } \ No newline at end of file diff --git a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs index c25a9830ba..90eabe64ad 100644 --- a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs +++ b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskBundler.cs @@ -1,16 +1,23 @@ -using System; +using BizHawk.Client.Common; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; +using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Windows.Forms; +using System.Xml.Linq; +using BizHawk.Client.EmuHawk.WinFormExtensions; namespace BizHawk.Client.EmuHawk { public partial class MultiDiskBundler : Form, IToolFormAutoConfig { + private XElement _currentXml = null; + public MultiDiskBundler() { InitializeComponent(); @@ -51,7 +58,7 @@ namespace BizHawk.Client.EmuHawk #endregion - private void buttonCancel_Click(object sender, EventArgs e) + private void CancelBtn_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel; Close(); @@ -59,8 +66,23 @@ namespace BizHawk.Client.EmuHawk private void SaveRunButton_Click(object sender, EventArgs e) { - DialogResult = DialogResult.OK; - Close(); + if (Recalculate()) + { + var fileInfo = new FileInfo(NameBox.Text); + if (fileInfo.Exists) + { + var result = MessageBox.Show(this, "File already exists, overwrite?", "File exists", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); + if (result != DialogResult.OK) + { + return; + } + } + + File.WriteAllText(fileInfo.FullName, _currentXml.ToString()); + + DialogResult = DialogResult.OK; + Close(); + } } private void AddButton_Click(object sender, EventArgs e) @@ -75,14 +97,97 @@ namespace BizHawk.Client.EmuHawk Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top }; - groupBox.Controls.Add(new MultiDiskFileSelector + var mdf = new MultiDiskFileSelector { - Location = new Point(5, 8) - }); + Location = new Point(5, 8), + Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top + }; + + mdf.NameChanged += FileSelector_NameChanged; + + groupBox.Controls.Add(mdf); FileSelectorPanel.Controls.Add(groupBox); } - + private void FileSelector_NameChanged(object sender, EventArgs e) + { + Recalculate(); + } + + private bool Recalculate() + { + try + { + var fileSelectors = FileSelectorPanel.Controls + .OfType() + .SelectMany(g => g.Controls.OfType()) + .ToList(); + + var names = fileSelectors.Select(f => f.GetName()); + + var name = NameBox.Text; + + if (string.IsNullOrWhiteSpace(name)) + { + throw new Exception("Blank Names"); + } + + if (names.Any(n => string.IsNullOrWhiteSpace(n))) + { + throw new Exception("Blank Names"); + } + + string system = Global.Emulator.SystemId; // TODO: have the user pick this? + + var tagNames = names.Select(n => Path.GetFileNameWithoutExtension(n)); + + _currentXml = new XElement("BizHawk-XMLGame", + new XAttribute("System", system), + new XAttribute("Name", Path.GetFileNameWithoutExtension(name)), + new XElement("LoadAssets", + names.Select(n => new XElement( + ConvertToTag(Path.GetFileNameWithoutExtension(n)), + new XAttribute("FileName", n) + )) + ) + ); + + SaveRunButton.Enabled = true; + return true; + } + catch (Exception) + { + _currentXml = null; + SaveRunButton.Enabled = false; + return false; + } + } + + private static string ConvertToTag(string name) + { + return new Regex("[^A-Za-z0-9]").Replace(name, string.Empty); + } + + private void NameBox_TextChanged(object sender, EventArgs e) + { + Recalculate(); + } + + private void BrowseBtn_Click(object sender, EventArgs e) + { + var sfd = new SaveFileDialog + { + FileName = Path.ChangeExtension(GlobalWin.MainForm.CurrentlyOpenRom, ".xml"), + InitialDirectory = PathManager.MakeAbsolutePath(Global.Config.PathEntries["Global_NULL", "ROM"].Path, "Global_NULL"), + Filter = "xml (*.xml)|*.xml|All Files|*.*" + }; + + var result = sfd.ShowHawkDialog(); + if (result != DialogResult.Cancel) + { + NameBox.Text = sfd.FileName; + } + } } } diff --git a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.Designer.cs b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.Designer.cs index 61b79393b0..007a9a7478 100644 --- a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.Designer.cs +++ b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.Designer.cs @@ -29,7 +29,7 @@ private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); - this.textBox1 = new System.Windows.Forms.TextBox(); + this.PathBox = new System.Windows.Forms.TextBox(); this.UseCurrentRomButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // @@ -44,17 +44,18 @@ this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // - // textBox1 + // PathBox // - this.textBox1.AllowDrop = true; - this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + this.PathBox.AllowDrop = true; + this.PathBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.textBox1.Location = new System.Drawing.Point(3, 5); - this.textBox1.Name = "textBox1"; - this.textBox1.Size = new System.Drawing.Size(285, 20); - this.textBox1.TabIndex = 1; - this.textBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop); - this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter); + this.PathBox.Location = new System.Drawing.Point(3, 5); + this.PathBox.Name = "PathBox"; + this.PathBox.Size = new System.Drawing.Size(285, 20); + this.PathBox.TabIndex = 1; + this.PathBox.TextChanged += new System.EventHandler(this.PathBox_TextChanged); + this.PathBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.textBox1_DragDrop); + this.PathBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter); // // UseCurrentRomButton // @@ -72,7 +73,7 @@ this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit; this.Controls.Add(this.UseCurrentRomButton); this.Controls.Add(this.button1); - this.Controls.Add(this.textBox1); + this.Controls.Add(this.PathBox); this.Name = "MultiDiskFileSelector"; this.Size = new System.Drawing.Size(425, 29); this.Load += new System.EventHandler(this.DualGBFileSelector_Load); @@ -84,7 +85,7 @@ #endregion private System.Windows.Forms.Button button1; - private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.TextBox PathBox; private System.Windows.Forms.Button UseCurrentRomButton; } diff --git a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs index acb758a88f..92aa9bc601 100644 --- a/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs +++ b/BizHawk.Client.EmuHawk/tools/MultiDiskBundler/MultiDiskFileSelector.cs @@ -10,6 +10,7 @@ using System.Windows.Forms; using BizHawk.Client.Common; using BizHawk.Emulation.Common; using BizHawk.Emulation.Cores.Nintendo.Gameboy; +using BizHawk.Client.EmuHawk.WinFormExtensions; namespace BizHawk.Client.EmuHawk { @@ -17,12 +18,12 @@ namespace BizHawk.Client.EmuHawk { public string GetName() { - return textBox1.Text; + return PathBox.Text; } public void SetName(string val) { - textBox1.Text = val; + PathBox.Text = val; } public event EventHandler NameChanged; @@ -35,7 +36,7 @@ namespace BizHawk.Client.EmuHawk public MultiDiskFileSelector() { InitializeComponent(); - textBox1.TextChanged += this.HandleLabelTextChanged; + PathBox.TextChanged += this.HandleLabelTextChanged; } protected virtual void OnNameChanged(EventArgs e) @@ -67,9 +68,9 @@ namespace BizHawk.Client.EmuHawk var ff = (string[])e.Data.GetData(DataFormats.FileDrop); if (ff.Length == 1) { - textBox1.Text = ff[0]; + PathBox.Text = ff[0]; } - } + } } private void button1_Click(object sender, EventArgs e) @@ -81,17 +82,17 @@ namespace BizHawk.Client.EmuHawk RestoreDirectory = true }) { - var result = ofd.ShowDialog(this); + var result = ofd.ShowHawkDialog(); if (result == DialogResult.OK) { - textBox1.Text = ofd.FileName; + PathBox.Text = ofd.FileName; } } } private void UseCurrentRomButton_Click(object sender, EventArgs e) { - textBox1.Text = GlobalWin.MainForm.CurrentlyOpenRom; + PathBox.Text = GlobalWin.MainForm.CurrentlyOpenRom; } private void DualGBFileSelector_Load(object sender, EventArgs e) @@ -106,5 +107,10 @@ namespace BizHawk.Client.EmuHawk && !GlobalWin.MainForm.CurrentlyOpenRom.Contains('|') && // Can't be archive !GlobalWin.MainForm.CurrentlyOpenRom.Contains(".xml"); // Can't already be an xml } + + private void PathBox_TextChanged(object sender, EventArgs e) + { + OnNameChanged(e); + } } }