From 46c0f4f30cc18a4b7195e3f168577ea2e1dcc36e Mon Sep 17 00:00:00 2001 From: Kurt Date: Thu, 18 May 2017 18:10:58 -0700 Subject: [PATCH] Add exporting pkm/mg from mgdb Closes #1151 by extending functionality instead of limiting it :) Users can now export Mystery Gifts from the database in either MysteryGift files or converted PKM form regardless of current generation --- PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs | 52 +++++++++++++------ .../Subforms/Save Editors/SAV_Wondercard.cs | 40 +------------- PKHeX.WinForms/Util/WinFormsUtil.cs | 48 +++++++++++++++++ 3 files changed, 87 insertions(+), 53 deletions(-) diff --git a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs index 6eb771e15..342a4261f 100644 --- a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs +++ b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs @@ -62,12 +62,16 @@ namespace PKHeX.WinForms ContextMenuStrip mnu = new ContextMenuStrip(); ToolStripMenuItem mnuView = new ToolStripMenuItem("View"); + ToolStripMenuItem mnuSaveMG = new ToolStripMenuItem("Save Gift"); + ToolStripMenuItem mnuSavePK = new ToolStripMenuItem("Save PKM"); // Assign event handlers mnuView.Click += clickView; + mnuSaveMG.Click += clickSaveMG; + mnuSavePK.Click += clickSavePK; // Add to main context menu - mnu.Items.AddRange(new ToolStripItem[] { mnuView }); + mnu.Items.AddRange(new ToolStripItem[] { mnuView, mnuSaveMG, mnuSavePK }); // Assign to datagridview foreach (PictureBox p in PKXBOXES) @@ -110,26 +114,44 @@ namespace PKHeX.WinForms // Important Events private void clickView(object sender, EventArgs e) { - sender = ((sender as ToolStripItem)?.Owner as ContextMenuStrip)?.SourceControl ?? sender as PictureBox; - int index = Array.IndexOf(PKXBOXES, sender); - if (index >= RES_MAX) - { - System.Media.SystemSounds.Exclamation.Play(); - return; - } - index += SCR_Box.Value*RES_MIN; - if (index >= Results.Count) - { - System.Media.SystemSounds.Exclamation.Play(); - return; - } - + int index = getSenderIndex(sender); m_parent.populateFields(Results[index].convertToPKM(Main.SAV), false); slotSelected = index; slotColor = Properties.Resources.slotView; FillPKXBoxes(SCR_Box.Value); L_Viewed.Text = string.Format(Viewed, Results[index].FileName); } + private void clickSavePK(object sender, EventArgs e) + { + int index = getSenderIndex(sender); + var gift = Results[index]; + var pk = gift.convertToPKM(Main.SAV); + WinFormsUtil.SavePKMDialog(pk); + } + private void clickSaveMG(object sender, EventArgs e) + { + int index = getSenderIndex(sender); + var gift = Results[index]; + WinFormsUtil.SaveMGDialog(gift); + } + + private int getSenderIndex(object sender) + { + sender = ((sender as ToolStripItem)?.Owner as ContextMenuStrip)?.SourceControl ?? sender as PictureBox; + int index = Array.IndexOf(PKXBOXES, sender); + if (index >= RES_MAX) + { + System.Media.SystemSounds.Exclamation.Play(); + return -1; + } + index += SCR_Box.Value*RES_MIN; + if (index >= Results.Count) + { + System.Media.SystemSounds.Exclamation.Play(); + return -1; + } + return index; + } private void populateComboBoxes() { // Set the Text diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs index 900585d0b..69ef83790 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_Wondercard.cs @@ -115,25 +115,9 @@ namespace PKHeX.WinForms } // Mystery Gift IO (.file<->window) - private string getFilter() - { - switch (SAV.Generation) - { - case 4: - return "Gen4 Mystery Gift|*.pgt;*.pcd|All Files|*.*"; - case 5: - return "Gen5 Mystery Gift|*.pgf|All Files|*.*"; - case 6: - return "Gen6 Mystery Gift|*.wc6;*.wc6full|All Files|*.*"; - case 7: - return "Gen7 Mystery Gift|*.wc7;*.wc7full|All Files|*.*"; - default: - return ""; - } - } private void B_Import_Click(object sender, EventArgs e) { - OpenFileDialog import = new OpenFileDialog {Filter = getFilter()}; + OpenFileDialog import = new OpenFileDialog {Filter = WinFormsUtil.getMysterGiftFilter(SAV.Generation)}; if (import.ShowDialog() != DialogResult.OK) return; string path = import.FileName; @@ -147,27 +131,7 @@ namespace PKHeX.WinForms } private void B_Output_Click(object sender, EventArgs e) { - SaveFileDialog outputwc6 = new SaveFileDialog - { - Filter = getFilter(), - FileName = Util.CleanFileName(mg.FileName) - }; - if (outputwc6.ShowDialog() != DialogResult.OK) return; - - string path = outputwc6.FileName; - - if (File.Exists(path)) - { - // File already exists, save a .bak - string bakpath = path + ".bak"; - if (!File.Exists(bakpath)) - { - byte[] backupfile = File.ReadAllBytes(path); - File.WriteAllBytes(bakpath, backupfile); - } - } - - File.WriteAllBytes(path, mg.Data); + WinFormsUtil.SaveMGDialog(mg); } private int getLastUnfilledByType(MysteryGift Gift, MysteryGiftAlbum Album) diff --git a/PKHeX.WinForms/Util/WinFormsUtil.cs b/PKHeX.WinForms/Util/WinFormsUtil.cs index b36ddf487..457440e40 100644 --- a/PKHeX.WinForms/Util/WinFormsUtil.cs +++ b/PKHeX.WinForms/Util/WinFormsUtil.cs @@ -321,5 +321,53 @@ namespace PKHeX.WinForms } return true; } + /// + /// Opens a dialog to save a file. + /// + /// to be saved. + /// Result of whether or not the file was saved. + public static bool SaveMGDialog(MysteryGift gift) + { + SaveFileDialog output = new SaveFileDialog + { + Filter = getMysterGiftFilter(gift.Format), + FileName = Util.CleanFileName(gift.FileName) + }; + if (output.ShowDialog() != DialogResult.OK) + return false; + + string path = output.FileName; + + if (File.Exists(path)) + { + // File already exists, save a .bak + string bakpath = path + ".bak"; + if (!File.Exists(bakpath)) + { + byte[] backupfile = File.ReadAllBytes(path); + File.WriteAllBytes(bakpath, backupfile); + } + } + + File.WriteAllBytes(path, gift.Data); + return true; + } + + public static string getMysterGiftFilter(int Format) + { + switch (Format) + { + case 4: + return "Gen4 Mystery Gift|*.pgt;*.pcd|All Files|*.*"; + case 5: + return "Gen5 Mystery Gift|*.pgf|All Files|*.*"; + case 6: + return "Gen6 Mystery Gift|*.wc6;*.wc6full|All Files|*.*"; + case 7: + return "Gen7 Mystery Gift|*.wc7;*.wc7full|All Files|*.*"; + default: + return ""; + } + } } }