From dcf2b5fd9cfd27fe6399d14742890d8d98d130bc Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 4 Sep 2017 22:22:02 -0700 Subject: [PATCH] Improve database loading speed do db population in another thread, invoke an update when complete --- PKHeX.WinForms/Subforms/SAV_Database.cs | 93 ++++++++++---------- PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs | 45 +++++----- 2 files changed, 68 insertions(+), 70 deletions(-) diff --git a/PKHeX.WinForms/Subforms/SAV_Database.cs b/PKHeX.WinForms/Subforms/SAV_Database.cs index 821db51fc..9c67d321e 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.cs +++ b/PKHeX.WinForms/Subforms/SAV_Database.cs @@ -44,18 +44,8 @@ public SAV_Database(PKMEditor f1, SAVEditor saveditor) }; // Enable Scrolling when hovered over - PAN_Box.MouseWheel += (sender, e) => - { - if (ActiveForm == this) - SCR_Box.Focus(); - }; foreach (var slot in PKXBOXES) { - slot.MouseWheel += (sender, e) => - { - if (ActiveForm == this) - SCR_Box.Focus(); - }; // Enable Click slot.MouseClick += (sender, e) => { @@ -91,43 +81,9 @@ public SAV_Database(PKMEditor f1, SAVEditor saveditor) p.ContextMenuStrip = mnu; // Load Data - var dbTemp = new ConcurrentBag(); - var files = Directory.GetFiles(DatabasePath, "*", SearchOption.AllDirectories); - Parallel.ForEach(files, file => - { - FileInfo fi = new FileInfo(file); - if (!fi.Extension.Contains(".pk") || !PKX.IsPKM(fi.Length)) return; - var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(file), file, prefer: (fi.Extension.Last() - 0x30)&7); - if (pk != null) - dbTemp.Add(pk); - }); - -#if DEBUG - if (SaveUtil.GetSavesFromFolder(Main.BackupPath, false, out IEnumerable result)) - { - Parallel.ForEach(result, file => - { - var sav = SaveUtil.GetVariantSAV(File.ReadAllBytes(file)); - var path = EXTERNAL_SAV + new FileInfo(file).Name; - if (sav.HasBox) - foreach (var pk in sav.BoxData) - addPKM(pk); - - void addPKM(PKM pk) - { - pk.Identifier = Path.Combine(path, pk.Identifier); - dbTemp.Add(pk); - } - }); - } -#endif - - // Prepare Database - RawDB = new List(dbTemp.OrderBy(pk => pk.Identifier) - .Concat(SAV.BoxData.Where(pk => pk.Species != 0)) // Fetch from save file - .Where(pk => pk.ChecksumValid && pk.Species != 0 && pk.Sanity == 0) - .Distinct()); - SetResults(RawDB); + B_Search.Enabled = false; + L_Count.Text = "Loading..."; + new Task(LoadDatabase).Start(); Menu_SearchSettings.DropDown.Closing += (sender, e) => { @@ -369,6 +325,48 @@ private void GenerateDBReport(object sender, EventArgs e) reportGrid.PopulateData(Results.ToArray()); } + private void LoadDatabase() + { + var dbTemp = new ConcurrentBag(); + var files = Directory.GetFiles(DatabasePath, "*", SearchOption.AllDirectories); + Parallel.ForEach(files, file => + { + FileInfo fi = new FileInfo(file); + if (!fi.Extension.Contains(".pk") || !PKX.IsPKM(fi.Length)) return; + var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(file), file, prefer: (fi.Extension.Last() - 0x30) & 7); + if (pk != null) + dbTemp.Add(pk); + }); + +#if DEBUG + if (SaveUtil.GetSavesFromFolder(Main.BackupPath, false, out IEnumerable result)) + { + Parallel.ForEach(result, file => + { + var sav = SaveUtil.GetVariantSAV(File.ReadAllBytes(file)); + var path = EXTERNAL_SAV + new FileInfo(file).Name; + if (sav.HasBox) + foreach (var pk in sav.BoxData) + addPKM(pk); + + void addPKM(PKM pk) + { + pk.Identifier = Path.Combine(path, pk.Identifier); + dbTemp.Add(pk); + } + }); + } +#endif + + // Prepare Database + RawDB = new List(dbTemp.OrderBy(pk => pk.Identifier) + .Concat(SAV.BoxData.Where(pk => pk.Species != 0)) // Fetch from save file + .Where(pk => pk.ChecksumValid && pk.Species != 0 && pk.Sanity == 0) + .Distinct()); + + BeginInvoke(new MethodInvoker(() => SetResults(RawDB))); + } + // IO Usage private void OpenDB(object sender, EventArgs e) { @@ -597,6 +595,7 @@ private void SetResults(List res) FillPKXBoxes(0); L_Count.Text = string.Format(Counter, Results.Count); + B_Search.Enabled = true; } private void FillPKXBoxes(int start) { diff --git a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs index 6ee0cc3fa..b6c03bcdd 100644 --- a/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs +++ b/PKHeX.WinForms/Subforms/SAV_MysteryGiftDB.cs @@ -4,6 +4,7 @@ using System.Drawing; using System.IO; using System.Linq; +using System.Threading.Tasks; using System.Windows.Forms; using PKHeX.Core; using PKHeX.WinForms.Controls; @@ -40,18 +41,8 @@ public SAV_MysteryGiftDB(PKMEditor tabs, SAVEditor sav) }; // Enable Scrolling when hovered over - PAN_Box.MouseWheel += (sender, e) => - { - if (ActiveForm == this) - SCR_Box.Focus(); - }; foreach (var slot in PKXBOXES) { - slot.MouseWheel += (sender, e) => - { - if (ActiveForm == this) - SCR_Box.Focus(); - }; // Enable Click slot.MouseClick += (sender, e) => { @@ -84,30 +75,21 @@ public SAV_MysteryGiftDB(PKMEditor tabs, SAVEditor sav) p.ContextMenuStrip = mnu; // Load Data - RawDB = new List(); - RawDB.AddRange(Legal.MGDB_G4); - RawDB.AddRange(Legal.MGDB_G5); - RawDB.AddRange(Legal.MGDB_G6); - RawDB.AddRange(Legal.MGDB_G7); - - RawDB = new List(RawDB.Where(mg => !mg.IsItem && mg.IsPokémon && mg.Species > 0).Distinct().Concat(Legal.MGDB_G3).OrderBy(mg => mg.Species)); - foreach (var mg in RawDB) - mg.GiftUsed = false; - SetResults(RawDB); + B_Search.Enabled = false; + L_Count.Text = "Loading..."; + new Task(LoadDatabase).Start(); Menu_SearchSettings.DropDown.Closing += (sender, e) => { if (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked) e.Cancel = true; }; - - PopulateComboBoxes(); CenterToParent(); } private readonly PictureBox[] PKXBOXES; private readonly string DatabasePath = Main.MGDatabasePath; private List Results; - private readonly List RawDB; + private List RawDB; private int slotSelected = -1; // = null; private Image slotColor; private const int RES_MAX = 66; @@ -216,6 +198,23 @@ private void ResetFilters(object sender, EventArgs e) if (sender != null) System.Media.SystemSounds.Asterisk.Play(); } + private void LoadDatabase() + { + RawDB = new List(); + RawDB.AddRange(Legal.MGDB_G4); + RawDB.AddRange(Legal.MGDB_G5); + RawDB.AddRange(Legal.MGDB_G6); + RawDB.AddRange(Legal.MGDB_G7); + + RawDB = new List(RawDB.Where(mg => !mg.IsItem && mg.IsPokémon && mg.Species > 0).Distinct().Concat(Legal.MGDB_G3).OrderBy(mg => mg.Species)); + foreach (var mg in RawDB) + mg.GiftUsed = false; + BeginInvoke(new MethodInvoker(delegate + { + SetResults(RawDB); + PopulateComboBoxes(); + })); + } // IO Usage private void OpenDB(object sender, EventArgs e)