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
This commit is contained in:
Kurt 2017-05-18 18:10:58 -07:00
parent 3d498c6a1e
commit 46c0f4f30c
3 changed files with 87 additions and 53 deletions

View file

@ -62,12 +62,16 @@ namespace PKHeX.WinForms
ContextMenuStrip mnu = new ContextMenuStrip(); ContextMenuStrip mnu = new ContextMenuStrip();
ToolStripMenuItem mnuView = new ToolStripMenuItem("View"); ToolStripMenuItem mnuView = new ToolStripMenuItem("View");
ToolStripMenuItem mnuSaveMG = new ToolStripMenuItem("Save Gift");
ToolStripMenuItem mnuSavePK = new ToolStripMenuItem("Save PKM");
// Assign event handlers // Assign event handlers
mnuView.Click += clickView; mnuView.Click += clickView;
mnuSaveMG.Click += clickSaveMG;
mnuSavePK.Click += clickSavePK;
// Add to main context menu // Add to main context menu
mnu.Items.AddRange(new ToolStripItem[] { mnuView }); mnu.Items.AddRange(new ToolStripItem[] { mnuView, mnuSaveMG, mnuSavePK });
// Assign to datagridview // Assign to datagridview
foreach (PictureBox p in PKXBOXES) foreach (PictureBox p in PKXBOXES)
@ -110,26 +114,44 @@ namespace PKHeX.WinForms
// Important Events // Important Events
private void clickView(object sender, EventArgs e) private void clickView(object sender, EventArgs e)
{ {
sender = ((sender as ToolStripItem)?.Owner as ContextMenuStrip)?.SourceControl ?? sender as PictureBox; int index = getSenderIndex(sender);
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;
}
m_parent.populateFields(Results[index].convertToPKM(Main.SAV), false); m_parent.populateFields(Results[index].convertToPKM(Main.SAV), false);
slotSelected = index; slotSelected = index;
slotColor = Properties.Resources.slotView; slotColor = Properties.Resources.slotView;
FillPKXBoxes(SCR_Box.Value); FillPKXBoxes(SCR_Box.Value);
L_Viewed.Text = string.Format(Viewed, Results[index].FileName); 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() private void populateComboBoxes()
{ {
// Set the Text // Set the Text

View file

@ -115,25 +115,9 @@ namespace PKHeX.WinForms
} }
// Mystery Gift IO (.file<->window) // 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) 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; if (import.ShowDialog() != DialogResult.OK) return;
string path = import.FileName; string path = import.FileName;
@ -147,27 +131,7 @@ namespace PKHeX.WinForms
} }
private void B_Output_Click(object sender, EventArgs e) private void B_Output_Click(object sender, EventArgs e)
{ {
SaveFileDialog outputwc6 = new SaveFileDialog WinFormsUtil.SaveMGDialog(mg);
{
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);
} }
private int getLastUnfilledByType(MysteryGift Gift, MysteryGiftAlbum Album) private int getLastUnfilledByType(MysteryGift Gift, MysteryGiftAlbum Album)

View file

@ -321,5 +321,53 @@ namespace PKHeX.WinForms
} }
return true; return true;
} }
/// <summary>
/// Opens a dialog to save a <see cref="MysteryGift"/> file.
/// </summary>
/// <param name="gift"><see cref="MysteryGift"/> to be saved.</param>
/// <returns>Result of whether or not the file was saved.</returns>
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 "";
}
}
} }
} }