Misc tweaks

no functional change, just rewrite some logic for better flow
fix subform popup count restriction (OwnedForms requires forms to be
added/removed manually; it doesn't automatically update)

sanitize box names when dumping boxes with separate folders (and bad box
names)
This commit is contained in:
Kurt
2019-02-23 14:58:48 -08:00
parent 17f954b4e8
commit 5b29e71954
4 changed files with 76 additions and 71 deletions

View File

@@ -24,22 +24,27 @@ public static int DumpBoxes(this SaveFile SAV, string path, bool boxFolders = fa
return -1;
var boxdata = SAV.BoxData;
int ctr = 0;
foreach (PKM pk in boxdata)
var ctr = 0;
foreach (var pk in boxdata)
{
if (pk.Species == 0 || !pk.Valid)
continue;
ctr++;
string fileName = Util.CleanFileName(pk.FileName);
string boxfolder = string.Empty;
var boxfolder = path;
if (boxFolders)
{
boxfolder = SAV.GetBoxName(pk.Box - 1);
Directory.CreateDirectory(Path.Combine(path, boxfolder));
var boxName = Util.CleanFileName(SAV.GetBoxName(pk.Box - 1));
boxfolder = Path.Combine(path, boxName);
Directory.CreateDirectory(boxfolder);
}
if (!File.Exists(Path.Combine(Path.Combine(path, boxfolder), fileName)))
File.WriteAllBytes(Path.Combine(Path.Combine(path, boxfolder), fileName), pk.DecryptedBoxData);
var fileName = Util.CleanFileName(pk.FileName);
var fn = Path.Combine(boxfolder, fileName);
if (File.Exists(fn))
continue;
File.WriteAllBytes(fn, pk.DecryptedBoxData);
ctr++;
}
return ctr;
}
@@ -57,16 +62,18 @@ public static int DumpBox(this SaveFile SAV, string path, int currentBox)
return -1;
var boxdata = SAV.BoxData;
int ctr = 0;
foreach (PKM pk in boxdata)
var ctr = 0;
foreach (var pk in boxdata)
{
if (pk.Species == 0 || !pk.Valid || pk.Box - 1 != currentBox)
continue;
var fileName = Path.Combine(path, Util.CleanFileName(pk.FileName));
if (File.Exists(fileName))
continue;
File.WriteAllBytes(fileName, pk.DecryptedBoxData);
ctr++;
string fileName = Util.CleanFileName(pk.FileName);
if (!File.Exists(Path.Combine(path, fileName)))
File.WriteAllBytes(Path.Combine(path, fileName), pk.DecryptedBoxData);
}
return ctr;
}

View File

@@ -178,7 +178,7 @@ private void FormLoadInitialFiles(string[] args)
if (PKX.IsPKM(fi.Length))
pkmArg = arg;
else
OpenQuick(arg, force: true);
OpenFromPath(arg);
}
if (C_SAV.SAV == null) // No SAV loaded from exe args
{
@@ -233,16 +233,8 @@ private void FormLoadCheckForUpdates()
try
{
var latestVersion = NetUtil.GetLatestPKHeXVersion();
if (latestVersion == null || latestVersion <= CurrentProgramVersion)
return;
Invoke((MethodInvoker)(() =>
{
L_UpdateAvailable.Visible = true;
var ver = latestVersion;
var date = $"{2000 + ver.Major:00}{ver.Minor:00}{ver.Build:00}";
L_UpdateAvailable.Text = $"{MsgProgramUpdateAvailable} {date}";
}));
if (latestVersion > CurrentProgramVersion)
Invoke((MethodInvoker)(() => NotifyNewVersionAvailable(latestVersion)));
}
catch (Exception ex)
{
@@ -251,7 +243,14 @@ private void FormLoadCheckForUpdates()
});
}
private void FormLoadConfig(out bool BAKprompt, out bool showChangelog)
private void NotifyNewVersionAvailable(Version ver)
{
L_UpdateAvailable.Visible = true;
var date = $"{2000 + ver.Major:00}{ver.Minor:00}{ver.Build:00}";
L_UpdateAvailable.Text = $"{MsgProgramUpdateAvailable} {date}";
}
private static void FormLoadConfig(out bool BAKprompt, out bool showChangelog)
{
BAKprompt = false;
showChangelog = false;
@@ -319,6 +318,7 @@ private static void DeleteConfig(string settingsFilename)
}
Process.GetCurrentProcess().Kill();
}
// Main Menu Strip UI Functions
private void MainMenuOpen(object sender, EventArgs e)
{
@@ -347,13 +347,24 @@ private void MainMenuExit(object sender, EventArgs e)
private void MainMenuAbout(object sender, EventArgs e) => new About().ShowDialog();
private bool OpenWindowExists<T>() where T : Form
{
var form = WinFormsUtil.FirstFormOfType<T>();
if (form == null)
return false;
form.CenterToForm(this);
form.BringToFront();
return true;
}
// Sub Menu Options
private void MainMenuBoxReport(object sender, EventArgs e)
{
if (this.FirstFormOfType<ReportGrid>() is ReportGrid z)
{ z.CenterToForm(this); z.BringToFront(); return; }
if (OpenWindowExists<ReportGrid>())
return;
ReportGrid report = new ReportGrid();
var report = new ReportGrid();
report.Show();
report.PopulateData(C_SAV.SAV.BoxData);
}
@@ -362,39 +373,31 @@ private void MainMenuDatabase(object sender, EventArgs e)
{
if (ModifierKeys == Keys.Shift)
{
if (this.FirstFormOfType<KChart>() is KChart c)
{ c.CenterToForm(this); c.BringToFront(); }
else
{
if (!OpenWindowExists<KChart>())
new KChart(C_SAV.SAV).Show();
}
return;
}
if (this.FirstFormOfType<SAV_Database>() is SAV_Database z)
{ z.CenterToForm(this); z.BringToFront(); return; }
if (Directory.Exists(DatabasePath))
new SAV_Database(PKME_Tabs, C_SAV).Show();
else
if (!Directory.Exists(DatabasePath))
{
WinFormsUtil.Alert(MsgDatabase, string.Format(MsgDatabaseAdvice, DatabasePath));
return;
}
if (!OpenWindowExists<SAV_Database>())
new SAV_Database(PKME_Tabs, C_SAV).Show();
}
private void Menu_EncDatabase_Click(object sender, EventArgs e)
{
if (this.FirstFormOfType<SAV_Encounters>() is SAV_Encounters z)
{ z.CenterToForm(this); z.BringToFront(); return; }
new SAV_Encounters(PKME_Tabs).Show();
if (!OpenWindowExists<SAV_Encounters>())
new SAV_Encounters(PKME_Tabs).Show();
}
private void MainMenuMysteryDB(object sender, EventArgs e)
{
if (this.FirstFormOfType<SAV_MysteryGiftDB>() is SAV_MysteryGiftDB z)
{ z.CenterToForm(this); z.BringToFront(); return; }
new SAV_MysteryGiftDB(PKME_Tabs, C_SAV).Show();
if (!OpenWindowExists<SAV_MysteryGiftDB>())
new SAV_MysteryGiftDB(PKME_Tabs, C_SAV).Show();
}
private void MainMenuSettings(object sender, EventArgs e)
@@ -477,16 +480,8 @@ private void MainMenuBatchEditor(object sender, EventArgs e)
private void MainMenuFolder(object sender, EventArgs e)
{
var ofType = Application.OpenForms.OfType<SAV_FolderList>().FirstOrDefault();
if (ofType != null)
{
ofType.CenterToForm(this);
ofType.BringToFront();
}
else
{
if (!OpenWindowExists<SAV_FolderList>())
new SAV_FolderList(s => OpenSAV(SaveUtil.GetVariantSAV(s.FilePath), s.FilePath)).Show();
}
}
// Misc Options
@@ -537,13 +532,18 @@ private void ClickShowdownExportPKM(object sender, EventArgs e)
private void ClickShowdownExportCurrentBox(object sender, EventArgs e) => C_SAV.ClickShowdownExportCurrentBox(sender, e);
// Main Menu Subfunctions
private void OpenQuick(string path, bool force = false)
private void OpenQuick(string path)
{
if (!(CanFocus || force))
if (!CanFocus)
{
SystemSounds.Asterisk.Play();
return;
}
OpenFromPath(path);
}
private void OpenFromPath(string path)
{
if (Plugins.Any(p => p.TryLoadFile(path)))
return; // handled by plugin
@@ -555,7 +555,6 @@ private void OpenQuick(string path, bool force = false)
if (!fi.Exists)
return;
string ext = Path.GetExtension(path);
if (FileUtil.IsFileTooBig(fi.Length))
{
WinFormsUtil.Error(MsgFileSizeLarge + Environment.NewLine + string.Format(MsgFileSize, fi.Length), path);
@@ -569,6 +568,7 @@ private void OpenQuick(string path, bool force = false)
byte[] input; try { input = File.ReadAllBytes(path); }
catch (Exception e) { WinFormsUtil.Error(MsgFileInUse + path, e); return; }
string ext = fi.Extension;
#if DEBUG
OpenFile(input, path, ext);
#else
@@ -1113,16 +1113,14 @@ private void Dragout_MouseDown(object sender, MouseEventArgs e)
return;
// Create Temp File to Drag
PKM pk = PreparePKM();
bool encrypt = ModifierKeys == Keys.Control;
string fn = pk.FileNameWithoutExtension;
string filename = fn + (encrypt ? $".ek{pk.Format}" : $".{pk.Extension}");
byte[] dragdata = encrypt ? pk.EncryptedBoxData : pk.DecryptedBoxData;
var pk = PreparePKM();
var encrypt = ModifierKeys == Keys.Control;
var newfile = FileUtil.GetPKMTempFileName(pk, encrypt);
var data = encrypt ? pk.EncryptedBoxData : pk.DecryptedBoxData;
// Make file
string newfile = Path.Combine(Path.GetTempPath(), Util.CleanFileName(filename));
try
{
File.WriteAllBytes(newfile, dragdata);
File.WriteAllBytes(newfile, data);
C_SAV.M.DragInfo.Source.PKM = pk;
var pb = (PictureBox)sender;
@@ -1181,7 +1179,7 @@ private static void SaveSettings()
{
var settings = Settings.Default;
settings.Draw = Draw.ToString();
Settings.Default.Save();
settings.Save();
}
catch (Exception x)
{

View File

@@ -356,7 +356,7 @@ private static void Refresh(DataGridView dgData)
dgData.Invalidate();
}
private static void LoadEntryInitial(DataGridView dgData, SaveList<SavePreview> list, SavePreview sav)
private static void LoadEntryInitial(DataGridView dgData, ICollection<SavePreview> list, SavePreview sav)
{
list.Add(sav);
dgData.DataSource = list;

View File

@@ -22,7 +22,7 @@ internal static void CenterToForm(this Control child, Control parent)
child.Location = new Point(Math.Max(x, 0), Math.Max(y, 0));
}
public static Form FirstFormOfType<T>(this Form f) => Array.Find(f.OwnedForms, form => form is T);
public static T FirstFormOfType<T>() where T : Form => (T)Application.OpenForms.Cast<Form>().FirstOrDefault(form => form is T);
public static T FindFirstControlOfType<T>(Control aParent) where T : class
{