From c5ebbbbe15064acc0ea3bfcced34986ccd608877 Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 10 Sep 2019 22:07:50 -0700 Subject: [PATCH] Minor clean Magic number -> const usage default(T) -> default (c#7.x feature) remove some unused stuff fix indentation in some spots --- PKHeX.Core/Legality/Analysis.cs | 4 +- PKHeX.Core/Legality/RNG/Frame/SeedInfo.cs | 8 +- PKHeX.Core/MysteryGifts/PGT.cs | 2 +- PKHeX.Core/MysteryGifts/WC6.cs | 4 +- PKHeX.Core/MysteryGifts/WC7.cs | 4 +- PKHeX.Core/PKM/Util/PKX.cs | 2 +- PKHeX.Core/Saves/MemeCrypto/MemeKey.cs | 80 +++----- .../Saves/Substructures/PokeDex/Zukan.cs | 4 +- PKHeX.Core/Util/BigEndian.cs | 8 +- PKHeX.Core/Util/DataUtil.cs | 17 +- PKHeX.Core/Util/FileUtil.cs | 2 +- .../Controls/SAV Editor/BitmapAnimator.cs | 7 +- .../Controls/SAV Editor/SlotIndex.cs | 30 +-- PKHeX.WinForms/MainWindow/Main.cs | 4 +- .../PKM Editors/BatchEditor.Designer.cs | 30 +-- .../Subforms/PKM Editors/BatchEditor.cs | 8 +- .../Subforms/PKM Editors/BatchEditor.resx | 3 + .../Subforms/SAV_Database.Designer.cs | 34 ++++ PKHeX.WinForms/Subforms/SAV_Database.cs | 176 +++++++++--------- PKHeX.WinForms/Subforms/SAV_Database.resx | 6 + .../Save Editors/Gen6/SAV_Pokepuff.cs | 2 - .../Save Editors/Gen7/SAV_Trainer7.cs | 2 +- PKHeX.WinForms/Util/WinFormsUtil.cs | 13 +- Tests/PKHeX.Core.Tests/PKM/PKMTests.cs | 10 +- 24 files changed, 223 insertions(+), 237 deletions(-) diff --git a/PKHeX.Core/Legality/Analysis.cs b/PKHeX.Core/Legality/Analysis.cs index 911197bbc..7a66b9903 100644 --- a/PKHeX.Core/Legality/Analysis.cs +++ b/PKHeX.Core/Legality/Analysis.cs @@ -121,7 +121,7 @@ namespace PKHeX.Core PersonalInfo = table?.GetFormeEntry(pkm.Species, pkm.AltForm) ?? pkm.PersonalInfo; ParseLegality(); - if (Parse.Count <= 0) + if (Parse.Count == 0) return; Valid = Parse.All(chk => chk.Valid) @@ -356,7 +356,7 @@ namespace PKHeX.Core for (int i = 0; i < 4; i++) { if (vRelearn[i].Valid) - lines.Add(vRelearn[i].Format(L_F0_RM_1_2, i + 1)); + lines.Add(vRelearn[i].Format(L_F0_RM_1_2, i + 1)); } } diff --git a/PKHeX.Core/Legality/RNG/Frame/SeedInfo.cs b/PKHeX.Core/Legality/RNG/Frame/SeedInfo.cs index cb6e5313d..4e00db210 100644 --- a/PKHeX.Core/Legality/RNG/Frame/SeedInfo.cs +++ b/PKHeX.Core/Legality/RNG/Frame/SeedInfo.cs @@ -69,10 +69,12 @@ namespace PKHeX.Core // Process Conditions if (PKX.GetUnownForm(pid) == form) // matches form, does it match nature? - switch (VerifyPIDCriteria(pid, info)) { - case LockInfo.Pass: // yes - yield break; + switch (VerifyPIDCriteria(pid, info)) + { + case LockInfo.Pass: // yes + yield break; + } } s1 = pidiv.RNG.Prev(s2); diff --git a/PKHeX.Core/MysteryGifts/PGT.cs b/PKHeX.Core/MysteryGifts/PGT.cs index 181480723..924e8adb1 100644 --- a/PKHeX.Core/MysteryGifts/PGT.cs +++ b/PKHeX.Core/MysteryGifts/PGT.cs @@ -205,7 +205,7 @@ namespace PKHeX.Core if (pk4.IV32 == 0) { uint iv1 = ((seed = RNG.LCRNG.Next(seed)) >> 16) & 0x7FFF; - uint iv2 = ((_ = RNG.LCRNG.Next(seed)) >> 16) & 0x7FFF; + uint iv2 = ((RNG.LCRNG.Next(seed)) >> 16) & 0x7FFF; pk4.IV32 = iv1 | iv2 << 15; } } diff --git a/PKHeX.Core/MysteryGifts/WC6.cs b/PKHeX.Core/MysteryGifts/WC6.cs index b42f7ff1e..b1a4af720 100644 --- a/PKHeX.Core/MysteryGifts/WC6.cs +++ b/PKHeX.Core/MysteryGifts/WC6.cs @@ -36,8 +36,8 @@ namespace PKHeX.Core Data = new byte[Data.Length]; // Invalidate } - public int RestrictLanguage { get; set; } = 0; // None - public byte RestrictVersion { get; set; } = 0; // Permit All + public int RestrictLanguage { get; set; } // None + public byte RestrictVersion { get; set; } // Permit All public bool CanBeReceivedByVersion(int v) { diff --git a/PKHeX.Core/MysteryGifts/WC7.cs b/PKHeX.Core/MysteryGifts/WC7.cs index a0f9a57ba..c942742f6 100644 --- a/PKHeX.Core/MysteryGifts/WC7.cs +++ b/PKHeX.Core/MysteryGifts/WC7.cs @@ -34,8 +34,8 @@ namespace PKHeX.Core RawDate = SetDate((uint) now.Year, (uint) now.Month, (uint) now.Day); } - public int RestrictLanguage { get; set; } = 0; // None - public byte RestrictVersion { get; set; } = 0; // Permit All + public int RestrictLanguage { get; set; } // None + public byte RestrictVersion { get; set; } // Permit All public bool CanBeReceivedByVersion(int v) { diff --git a/PKHeX.Core/PKM/Util/PKX.cs b/PKHeX.Core/PKM/Util/PKX.cs index b732ccc9e..9e9913603 100644 --- a/PKHeX.Core/PKM/Util/PKX.cs +++ b/PKHeX.Core/PKM/Util/PKX.cs @@ -660,7 +660,7 @@ namespace PKHeX.Core byte[] ekm = ShuffleArray3(pkm, blockPositionInvert[PID%24]); byte[] xorkey = BitConverter.GetBytes(seed); - for (int i = 32; i < 80; i++) + for (int i = 32; i < SIZE_3STORED; i++) ekm[i] ^= xorkey[i & 3]; return ekm; } diff --git a/PKHeX.Core/Saves/MemeCrypto/MemeKey.cs b/PKHeX.Core/Saves/MemeCrypto/MemeKey.cs index 5b92e3225..a9b066c5a 100644 --- a/PKHeX.Core/Saves/MemeCrypto/MemeKey.cs +++ b/PKHeX.Core/Saves/MemeCrypto/MemeKey.cs @@ -23,19 +23,19 @@ namespace PKHeX.Core // Constructor public MemeKey(MemeKeyIndex key) { - GetMemeData(key, out byte[] d, out byte[] der); - DER = der; + DER = GetMemeData(key); var _N = new byte[0x61]; var _E = new byte[0x3]; - Array.Copy(der, 0x18, _N, 0, 0x61); - Array.Copy(der, 0x7B, _E, 0, 3); + Array.Copy(DER, 0x18, _N, 0, 0x61); + Array.Copy(DER, 0x7B, _E, 0, 3); Array.Reverse(_N); N = new BigInteger(_N); Array.Reverse(_E); E = new BigInteger(_E); - if (d != null) + + if (key == MemeKeyIndex.PokedexAndSaveFile) { - var _D = (byte[])d.Clone(); + var _D = (byte[])D_3.Clone(); Array.Reverse(_D); D = new BigInteger(_D); } @@ -59,6 +59,7 @@ namespace PKHeX.Core { if (data.Length < 0x60) throw new ArgumentException("Memebuffers must be atleast 0x60 bytes long!"); + var key = new byte[0x10]; var buffer = new byte[DER.Length + data.Length - 0x60]; Array.Copy(DER, 0, buffer, 0, DER.Length); @@ -232,59 +233,26 @@ namespace PKHeX.Core Array.Copy(rawSig, outSig, 0x60); return outSig; } - // Helper Method to retrieve data for loading - private static void GetMemeData(MemeKeyIndex key, out byte[] d, out byte[] der) - { - d = null; + private static byte[] GetMemeData(MemeKeyIndex key) + { switch (key) { - case MemeKeyIndex.LocalWireless: - der = DER_LW; - break; - case MemeKeyIndex.FriendlyCompetition: - der = DER_0; - break; - case MemeKeyIndex.LiveCompetition: - der = DER_1; - break; - case MemeKeyIndex.RentalTeam: - der = DER_2; - break; - case MemeKeyIndex.PokedexAndSaveFile: - der = DER_3; - d = D_3; - break; - case MemeKeyIndex.GaOle: - der = DER_4; - break; - case MemeKeyIndex.MagearnaEvent: - der = DER_5; - break; - case MemeKeyIndex.MoncolleGet: - der = DER_6; - break; - case MemeKeyIndex.IslandScanEventSpecial: - der = DER_7; - break; - case MemeKeyIndex.TvTokyoDataBroadcasting: - der = DER_8; - break; - case MemeKeyIndex.CapPikachuEvent: - der = DER_9; - break; - case MemeKeyIndex.Unknown10: - der = DER_A; - break; - case MemeKeyIndex.Unknown11: - der = DER_B; - break; - case MemeKeyIndex.Unknown12: - der = DER_C; - break; - case MemeKeyIndex.Unknown13: - der = DER_D; - break; + case MemeKeyIndex.LocalWireless: return DER_LW; + case MemeKeyIndex.FriendlyCompetition: return DER_0; + case MemeKeyIndex.LiveCompetition: return DER_1; + case MemeKeyIndex.RentalTeam: return DER_2; + case MemeKeyIndex.PokedexAndSaveFile: return DER_3; + case MemeKeyIndex.GaOle: return DER_4; + case MemeKeyIndex.MagearnaEvent: return DER_5; + case MemeKeyIndex.MoncolleGet: return DER_6; + case MemeKeyIndex.IslandScanEventSpecial: return DER_7; + case MemeKeyIndex.TvTokyoDataBroadcasting: return DER_8; + case MemeKeyIndex.CapPikachuEvent: return DER_9; + case MemeKeyIndex.Unknown10: return DER_A; + case MemeKeyIndex.Unknown11: return DER_B; + case MemeKeyIndex.Unknown12: return DER_C; + case MemeKeyIndex.Unknown13: return DER_D; default: throw new ArgumentOutOfRangeException(nameof(key), key, null); } diff --git a/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs b/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs index e98aa103a..b00b8f8b5 100644 --- a/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs +++ b/PKHeX.Core/Saves/Substructures/PokeDex/Zukan.cs @@ -214,13 +214,13 @@ namespace PKHeX.Core { SetAllDexSeenFlags(baseBit, f, 0, false, seen); if (shinyToo) - SetAllDexSeenFlags(baseBit, f, 0, true, seen); + SetAllDexSeenFlags(baseBit, f, 0, true, seen); } if (!entry.OnlyMale && !entry.Genderless) { SetAllDexSeenFlags(baseBit, f, 1, false, seen); if (shinyToo) - SetAllDexSeenFlags(baseBit, f, 1, true, seen); + SetAllDexSeenFlags(baseBit, f, 1, true, seen); } } } diff --git a/PKHeX.Core/Util/BigEndian.cs b/PKHeX.Core/Util/BigEndian.cs index bad6829d3..e99eaa373 100644 --- a/PKHeX.Core/Util/BigEndian.cs +++ b/PKHeX.Core/Util/BigEndian.cs @@ -10,7 +10,7 @@ namespace PKHeX.Core val |= data[offset + 0] << 24; val |= data[offset + 1] << 16; val |= data[offset + 2] << 8; - val |= data[offset + 3] << 0; + val |= data[offset + 3]; return (uint)val; } @@ -18,7 +18,7 @@ namespace PKHeX.Core { int val = 0; val |= data[offset + 0] << 8; - val |= data[offset + 1] << 0; + val |= data[offset + 1]; return (ushort)val; } @@ -28,7 +28,7 @@ namespace PKHeX.Core val |= data[offset + 0] << 24; val |= data[offset + 1] << 16; val |= data[offset + 2] << 8; - val |= data[offset + 3] << 0; + val |= data[offset + 3]; return val; } @@ -36,7 +36,7 @@ namespace PKHeX.Core { int val = 0; val |= data[offset + 0] << 8; - val |= data[offset + 1] << 0; + val |= data[offset + 1]; return (short)val; } diff --git a/PKHeX.Core/Util/DataUtil.cs b/PKHeX.Core/Util/DataUtil.cs index bd8dc68ec..049c86c4b 100644 --- a/PKHeX.Core/Util/DataUtil.cs +++ b/PKHeX.Core/Util/DataUtil.cs @@ -116,17 +116,16 @@ namespace PKHeX.Core public static string GetStringResource(string name) { - if (!resourceNameMap.ContainsKey(name)) + if (!resourceNameMap.TryGetValue(name, out var resname)) { bool Match(string x) => x.StartsWith("PKHeX.Core.Resources.text.") && x.EndsWith($"{name}.txt", StringComparison.OrdinalIgnoreCase); - var resname = Array.Find(manifestResourceNames, Match); + resname = Array.Find(manifestResourceNames, Match); + if (resname == null) + return null; resourceNameMap.Add(name, resname); } - if (resourceNameMap[name] == null) - return null; - - using (var resource = thisAssembly.GetManifestResourceStream(resourceNameMap[name])) + using (var resource = thisAssembly.GetManifestResourceStream(resname)) using (var reader = new StreamReader(resource)) return reader.ReadToEnd(); } @@ -318,9 +317,8 @@ namespace PKHeX.Core public static void AddCBWithOffset(List cbList, IReadOnlyList inStrings, int offset, params int[] allowed) { int beginCount = cbList.Count; - for (int i = 0; i < allowed.Length; i++) + foreach (var index in allowed) { - int index = allowed[i]; var item = new ComboItem(inStrings[index - offset], index); cbList.Add(item); } @@ -330,9 +328,8 @@ namespace PKHeX.Core public static void AddCB(List cbList, IReadOnlyList inStrings, int[] allowed) { int beginCount = cbList.Count; - for (int i = 0; i < allowed.Length; i++) + foreach (var index in allowed) { - int index = allowed[i]; var item = new ComboItem(inStrings[index], index); cbList.Add(item); } diff --git a/PKHeX.Core/Util/FileUtil.cs b/PKHeX.Core/Util/FileUtil.cs index 3149c096a..6f2f193ab 100644 --- a/PKHeX.Core/Util/FileUtil.cs +++ b/PKHeX.Core/Util/FileUtil.cs @@ -134,7 +134,7 @@ namespace PKHeX.Core { if (ext == ".pgt") // size collision with pk6 { - pk = default(PKM); + pk = default; return false; } var format = PKX.GetPKMFormatFromExtension(ext, sav?.Generation ?? 6); diff --git a/PKHeX.WinForms/Controls/SAV Editor/BitmapAnimator.cs b/PKHeX.WinForms/Controls/SAV Editor/BitmapAnimator.cs index 1f53d99ce..3d8ff4d67 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/BitmapAnimator.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/BitmapAnimator.cs @@ -55,12 +55,15 @@ namespace PKHeX.WinForms.Controls imgWidth = baseImage.Width; imgHeight = baseImage.Height; GlowData = glowData; - pb = pbox; GlowCounter = 0; - OriginalBackground = original; GlowCache = new Image[GlowFps]; GlowInterval = 1000 / GlowFps; Interval = GlowInterval; + lock (Lock) + { + pb = pbox; + OriginalBackground = original; + } Enabled = true; } diff --git a/PKHeX.WinForms/Controls/SAV Editor/SlotIndex.cs b/PKHeX.WinForms/Controls/SAV Editor/SlotIndex.cs index 389d7528d..17ed170ce 100644 --- a/PKHeX.WinForms/Controls/SAV Editor/SlotIndex.cs +++ b/PKHeX.WinForms/Controls/SAV Editor/SlotIndex.cs @@ -1,7 +1,4 @@ -using System; -using PKHeX.Core; - -namespace PKHeX.WinForms.Controls +namespace PKHeX.WinForms.Controls { internal enum SlotIndex { @@ -9,29 +6,4 @@ namespace PKHeX.WinForms.Controls BattleBox = 6, Daycare = 12, } - - public static partial class Extensions - { - internal static bool IsEditable(this SlotIndex type) => type == SlotIndex.Party; - internal static bool IsParty(this SlotIndex type, int format) => type < SlotIndex.BattleBox || (format == 5 && type == SlotIndex.BattleBox); - - internal static SlotIndex GetMiscSlotType(int slot) - { - if (slot < (int)SlotIndex.BattleBox) return SlotIndex.Party; - if (slot < (int)SlotIndex.Daycare) return SlotIndex.BattleBox; - return SlotIndex.Daycare; - } - - internal static StorageSlotType GetMiscSlotType(this SlotIndex type) - { - switch (type) - { - case SlotIndex.Party: return StorageSlotType.Party; - case SlotIndex.Daycare: return StorageSlotType.Daycare; - case SlotIndex.BattleBox: return StorageSlotType.BattleBox; - default: - throw new ArgumentOutOfRangeException(nameof(type), type, null); - } - } - } } diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 0ea5096cd..40ed8f02c 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -1105,10 +1105,8 @@ namespace PKHeX.WinForms return; OpenQuick(files[0]); e.Effect = DragDropEffects.Copy; - - Cursor = DefaultCursor; } - // Decrypted Export + private void Dragout_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && (ModifierKeys == Keys.Alt || ModifierKeys == Keys.Shift)) diff --git a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.Designer.cs b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.Designer.cs index 003ae4b2b..e68024a0a 100644 --- a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.Designer.cs +++ b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.Designer.cs @@ -32,6 +32,7 @@ this.RB_Boxes = new System.Windows.Forms.RadioButton(); this.RB_Path = new System.Windows.Forms.RadioButton(); this.FLP_RB = new System.Windows.Forms.FlowLayoutPanel(); + this.RB_Party = new System.Windows.Forms.RadioButton(); this.TB_Folder = new System.Windows.Forms.TextBox(); this.RTB_Instructions = new System.Windows.Forms.RichTextBox(); this.B_Go = new System.Windows.Forms.Button(); @@ -42,7 +43,7 @@ this.B_Add = new System.Windows.Forms.Button(); this.L_PropType = new System.Windows.Forms.Label(); this.L_PropValue = new System.Windows.Forms.Label(); - this.RB_Party = new System.Windows.Forms.RadioButton(); + this.b = new System.ComponentModel.BackgroundWorker(); this.FLP_RB.SuspendLayout(); this.SuspendLayout(); // @@ -89,6 +90,19 @@ this.FLP_RB.Size = new System.Drawing.Size(370, 24); this.FLP_RB.TabIndex = 2; // + // RB_Party + // + this.RB_Party.Anchor = System.Windows.Forms.AnchorStyles.Left; + this.RB_Party.Appearance = System.Windows.Forms.Appearance.Button; + this.RB_Party.AutoSize = true; + this.RB_Party.Location = new System.Drawing.Point(46, 0); + this.RB_Party.Margin = new System.Windows.Forms.Padding(0); + this.RB_Party.Name = "RB_Party"; + this.RB_Party.Size = new System.Drawing.Size(41, 23); + this.RB_Party.TabIndex = 5; + this.RB_Party.Text = "Party"; + this.RB_Party.UseVisualStyleBackColor = true; + // // TB_Folder // this.TB_Folder.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) @@ -199,18 +213,9 @@ this.L_PropValue.TabIndex = 13; this.L_PropValue.Text = "PropertyValue"; // - // RB_Party + // b // - this.RB_Party.Anchor = System.Windows.Forms.AnchorStyles.Left; - this.RB_Party.Appearance = System.Windows.Forms.Appearance.Button; - this.RB_Party.AutoSize = true; - this.RB_Party.Location = new System.Drawing.Point(46, 0); - this.RB_Party.Margin = new System.Windows.Forms.Padding(0); - this.RB_Party.Name = "RB_Party"; - this.RB_Party.Size = new System.Drawing.Size(41, 23); - this.RB_Party.TabIndex = 5; - this.RB_Party.Text = "Party"; - this.RB_Party.UseVisualStyleBackColor = true; + this.b.WorkerReportsProgress = true; // // BatchEditor // @@ -258,5 +263,6 @@ private System.Windows.Forms.Label L_PropType; private System.Windows.Forms.Label L_PropValue; private System.Windows.Forms.RadioButton RB_Party; + private System.ComponentModel.BackgroundWorker b; } } \ No newline at end of file diff --git a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs index 422433524..635f8149e 100644 --- a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs +++ b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.ComponentModel; using System.Drawing; using System.IO; using System.Linq; @@ -119,8 +118,6 @@ namespace PKHeX.WinForms RB_Path.Checked = true; } - private BackgroundWorker b; - private void RunBackgroundWorker() { if (RTB_Instructions.Lines.Any(line => line.Length == 0)) @@ -167,7 +164,6 @@ namespace PKHeX.WinForms private void RunBatchEdit(StringInstructionSet[] sets, string source, string destination) { editor = new Core.BatchEditor(); - b = new BackgroundWorker { WorkerReportsProgress = true }; b.DoWork += (sender, e) => { if (RB_Boxes.Checked) @@ -193,7 +189,7 @@ namespace PKHeX.WinForms var files = Directory.GetFiles(source, "*", SearchOption.AllDirectories); SetupProgressBar(files.Length * sets.Count); foreach (var set in sets) - ProcessFolder(files, set.Filters, set.Instructions, destination); + ProcessFolder(files, set.Filters, set.Instructions, destination); } private void RunBatchEditSaveFile(IList sets, bool boxes = false, bool party = false) @@ -207,7 +203,7 @@ namespace PKHeX.WinForms { SetupProgressBar(d.Count * sets.Count); foreach (var set in sets) - ProcessSAV(d, set.Filters, set.Instructions); + ProcessSAV(d, set.Filters, set.Instructions); return d.Count != 0; } } diff --git a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.resx b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.resx index 603f660be..392f88ff6 100644 --- a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.resx +++ b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.resx @@ -117,6 +117,9 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 17, 17 + diff --git a/PKHeX.WinForms/Subforms/SAV_Database.Designer.cs b/PKHeX.WinForms/Subforms/SAV_Database.Designer.cs index 776f9ae4f..111140f67 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.Designer.cs +++ b/PKHeX.WinForms/Subforms/SAV_Database.Designer.cs @@ -28,6 +28,7 @@ /// private void InitializeComponent() { + this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SAV_Database)); this.SCR_Box = new System.Windows.Forms.VScrollBar(); this.bpkx30 = new System.Windows.Forms.PictureBox(); @@ -159,6 +160,10 @@ this.L_Format = new System.Windows.Forms.Label(); this.FLP_Level = new System.Windows.Forms.FlowLayoutPanel(); this.RTB_Instructions = new System.Windows.Forms.RichTextBox(); + this.mnu = new System.Windows.Forms.ContextMenuStrip(this.components); + this.mnuView = new System.Windows.Forms.ToolStripMenuItem(); + this.mnuDelete = new System.Windows.Forms.ToolStripMenuItem(); + this.hover = new System.Windows.Forms.ToolTip(this.components); ((System.ComponentModel.ISupportInitialize)(this.bpkx30)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.bpkx29)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.bpkx28)).BeginInit(); @@ -232,6 +237,7 @@ this.TLP_Filters.SuspendLayout(); this.FLP_Format.SuspendLayout(); this.FLP_Level.SuspendLayout(); + this.mnu.SuspendLayout(); this.SuspendLayout(); // // SCR_Box @@ -1625,6 +1631,7 @@ this.L_Viewed.TabIndex = 117; this.L_Viewed.Text = "Last Viewed: {0}"; this.L_Viewed.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; + this.L_Viewed.MouseEnter += new System.EventHandler(this.L_Viewed_MouseEnter); // // FLP_Egg // @@ -1845,6 +1852,28 @@ this.RTB_Instructions.TabIndex = 119; this.RTB_Instructions.Text = ""; // + // mnu + // + this.mnu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { + this.mnuView, + this.mnuDelete}); + this.mnu.Name = "mnu"; + this.mnu.Size = new System.Drawing.Size(108, 48); + this.mnu.Click += new System.EventHandler(this.ClickView); + // + // mnuView + // + this.mnuView.Name = "mnuView"; + this.mnuView.Size = new System.Drawing.Size(107, 22); + this.mnuView.Text = "View"; + // + // mnuDelete + // + this.mnuDelete.Name = "mnuDelete"; + this.mnuDelete.Size = new System.Drawing.Size(107, 22); + this.mnuDelete.Text = "Delete"; + this.mnuDelete.Click += new System.EventHandler(this.ClickDelete); + // // SAV_Database // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -1944,6 +1973,7 @@ this.FLP_Format.ResumeLayout(false); this.FLP_Level.ResumeLayout(false); this.FLP_Level.PerformLayout(); + this.mnu.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); @@ -2081,5 +2111,9 @@ private System.Windows.Forms.ToolStripMenuItem Menu_SearchClones; private System.Windows.Forms.ToolStripMenuItem Menu_DeleteClones; private System.Windows.Forms.ToolStripMenuItem Menu_Import; + private System.Windows.Forms.ContextMenuStrip mnu; + private System.Windows.Forms.ToolStripMenuItem mnuView; + private System.Windows.Forms.ToolStripMenuItem mnuDelete; + private System.Windows.Forms.ToolTip hover; } } \ No newline at end of file diff --git a/PKHeX.WinForms/Subforms/SAV_Database.cs b/PKHeX.WinForms/Subforms/SAV_Database.cs index 970985961..c38b40d76 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.cs +++ b/PKHeX.WinForms/Subforms/SAV_Database.cs @@ -1,5 +1,4 @@ -#define LOADALL -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; @@ -26,11 +25,7 @@ namespace PKHeX.WinForms { InitializeComponent(); - ToolStripMenuItem mnuView = new ToolStripMenuItem {Name = "mnuView", Text = "View"}; - ToolStripMenuItem mnuDelete = new ToolStripMenuItem {Name = "mnuDelete", Text = "Delete" }; - WinFormsUtil.TranslateInterface(this, Main.CurrentLanguage); - ContextMenuStrip mnu = new ContextMenuStrip(); mnu.Items.AddRange(new ToolStripItem[] { mnuView, mnuDelete }); SAV = saveditor.SAV; @@ -62,48 +57,23 @@ namespace PKHeX.WinForms // Enable Click slot.MouseClick += (sender, e) => { - if (ModifierKeys == Keys.Control) - ClickView(sender, e); - else if (ModifierKeys == Keys.Alt) - ClickDelete(sender, e); - else if (ModifierKeys == Keys.Shift) - ClickSet(sender, e); + switch (ModifierKeys) + { + case Keys.Control: ClickView(sender, e); break; + case Keys.Alt: ClickDelete(sender, e); break; + case Keys.Shift: ClickSet(sender, e); break; + } }; if (Settings.Default.HoverSlotShowText) - { - slot.MouseEnter += (sender, e) => - { - int index = Array.IndexOf(PKXBOXES, slot); - if (!GetShiftedIndex(ref index)) - return; - - var pk = Results[index]; - if (pk.Species == 0) - { - ShowSet.RemoveAll(); - return; - } - - var text = ShowdownSet.GetLocalizedPreviewText(pk, Settings.Default.Language); - ShowSet.SetToolTip(slot, text); - }; - } + slot.MouseEnter += ShowHoverTextForSlot; } Counter = L_Count.Text; Viewed = L_Viewed.Text; - L_Viewed.Text = string.Empty; // invis for now - var hover = new ToolTip(); - L_Viewed.MouseEnter += (sender, e) => hover.SetToolTip(L_Viewed, L_Viewed.Text); + L_Viewed.Text = string.Empty; // invisible for now PopulateComboBoxes(); - // Assign event handlers - mnuView.Click += ClickView; - mnuDelete.Click += ClickDelete; - - // Add to main context menu - // Assign to datagridview foreach (PictureBox p in PKXBOXES) p.ContextMenuStrip = mnu; @@ -134,7 +104,7 @@ namespace PKHeX.WinForms private readonly string Viewed; private const int MAXFORMAT = PKX.Generation; private readonly string EXTERNAL_SAV = new DirectoryInfo(Main.BackupPath).Name + Path.DirectorySeparatorChar; - private readonly ToolTip ShowSet = new ToolTip {InitialDelay = 200, IsBalloon = false}; + private readonly SummaryPreviewer ShowSet = new SummaryPreviewer(); // Important Events private void ClickView(object sender, EventArgs e) @@ -149,7 +119,7 @@ namespace PKHeX.WinForms PKME_Tabs.PopulateFields(Results[index], false); slotSelected = index; - slotColor = Properties.Resources.slotView; + slotColor = Resources.slotView; FillPKXBoxes(SCR_Box.Value); L_Viewed.Text = string.Format(Viewed, Results[index].Identifier); } @@ -236,7 +206,7 @@ namespace PKHeX.WinForms // Refresh database view. L_Count.Text = string.Format(Counter, Results.Count); slotSelected = Results.Count - 1; - slotColor = Properties.Resources.slotSet; + slotColor = Resources.slotSet; if ((SCR_Box.Maximum+1)*6 < Results.Count) SCR_Box.Maximum++; SCR_Box.Value = Math.Max(0, SCR_Box.Maximum - (PKXBOXES.Length/6) + 1); @@ -331,6 +301,14 @@ namespace PKHeX.WinForms if (WinFormsUtil.Prompt(MessageBoxButtons.YesNo, MsgDBCreateReportPrompt, MsgDBCreateReportWarning) != DialogResult.Yes) return; + + var form = WinFormsUtil.FirstFormOfType(); + if (form != null) + { + form.BringToFront(); + form.CenterToForm(this); + } + ReportGrid reportGrid = new ReportGrid(); reportGrid.Show(); reportGrid.PopulateData(Results.ToArray()); @@ -357,64 +335,70 @@ namespace PKHeX.WinForms private static List LoadPKMSaves(string pkmdb, string savdb, string EXTERNAL_SAV, SaveFile SAV) { var dbTemp = new ConcurrentBag(); - var files = Directory.EnumerateFiles(pkmdb, "*", SearchOption.AllDirectories); var extensions = new HashSet(PKM.Extensions.Select(z => $".{z}")); - Parallel.ForEach(files, file => - { - var fi = new FileInfo(file); - if (!extensions.Contains(fi.Extension) || !PKX.IsPKM(fi.Length)) return; - var data = File.ReadAllBytes(file); - var prefer = PKX.GetPKMFormatFromExtension(fi.Extension, SAV.Generation); - var pk = PKMConverter.GetPKMfromBytes(data, prefer); - if (!(pk?.Species > 0)) - return; - pk.Identifier = file; - dbTemp.Add(pk); - }); -#if LOADALL + var files = Directory.EnumerateFiles(pkmdb, "*", SearchOption.AllDirectories); + Parallel.ForEach(files, file => TryAddPKMsFromFolder(dbTemp, file, SAV, extensions)); + if (SaveUtil.GetSavesFromFolder(savdb, false, out IEnumerable result)) - { - Parallel.ForEach(result, file => - { - try { - var sav = SaveUtil.GetVariantSAV(file); - if (sav == null) - { - Console.WriteLine("Unable to load SaveFile: " + file); - return; // bad backup - } - var path = EXTERNAL_SAV + Path.GetFileName(file); - 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); - } - } - catch (Exception ex) - { - Console.WriteLine("ERROR: Unable to load SaveFile: " + file); - Console.WriteLine(ex.Message); - } - }); - } -#endif + Parallel.ForEach(result, file => TryAddPKMsFromSaveFilePath(dbTemp, file, EXTERNAL_SAV)); + // Fetch from save file var savpkm = SAV.BoxData.Where(pk => pk.Species != 0); var bakpkm = dbTemp.Where(pk => pk.Species != 0).OrderBy(pk => pk.Identifier); var db = bakpkm.Concat(savpkm).Where(pk => pk.ChecksumValid && pk.Sanity == 0); - // Prepare Database + // Finalize the Database return new List(db); } + private static void TryAddPKMsFromFolder(ConcurrentBag dbTemp, string file, ITrainerInfo dest, ICollection validExtensions) + { + var fi = new FileInfo(file); + if (!validExtensions.Contains(fi.Extension) || !PKX.IsPKM(fi.Length)) + return; + + var data = File.ReadAllBytes(file); + var prefer = PKX.GetPKMFormatFromExtension(fi.Extension, dest.Generation); + var pk = PKMConverter.GetPKMfromBytes(data, prefer); + if (!(pk?.Species > 0)) + return; + pk.Identifier = file; + dbTemp.Add(pk); + } + + private static void TryAddPKMsFromSaveFilePath(ConcurrentBag dbTemp, string file, string externalFilePrefix) + { + try + { + var sav = SaveUtil.GetVariantSAV(file); + if (sav == null) + { + Console.WriteLine("Unable to load SaveFile: " + file); + return; + } + + var path = externalFilePrefix + Path.GetFileName(file); + 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); + } + } + catch (Exception ex) + { + Console.WriteLine("ERROR: Unable to load SaveFile: " + file); + Console.WriteLine(ex.Message); + } + } + // IO Usage private void OpenDB(object sender, EventArgs e) { @@ -596,9 +580,9 @@ namespace PKHeX.WinForms PKXBOXES[i].Image = null; for (int i = 0; i < RES_MAX; i++) - PKXBOXES[i].BackgroundImage = Properties.Resources.slotTrans; + PKXBOXES[i].BackgroundImage = Resources.slotTrans; if (slotSelected != -1 && slotSelected >= begin && slotSelected < begin + RES_MAX) - PKXBOXES[slotSelected - begin].BackgroundImage = slotColor ?? Properties.Resources.slotView; + PKXBOXES[slotSelected - begin].BackgroundImage = slotColor ?? Resources.slotView; } // Misc Update Methods @@ -691,5 +675,17 @@ namespace PKHeX.WinForms WinFormsUtil.Alert(string.Format(MsgFileDeleteCount, deleted), MsgWindowClose); Close(); } + + private void L_Viewed_MouseEnter(object sender, EventArgs e) => hover.SetToolTip(L_Viewed, L_Viewed.Text); + + private void ShowHoverTextForSlot(object sender, EventArgs e) + { + var pb = (PictureBox)sender; + int index = Array.IndexOf(PKXBOXES, pb); + if (!GetShiftedIndex(ref index)) + return; + + ShowSet.Show(pb, Results[index]); + } } } diff --git a/PKHeX.WinForms/Subforms/SAV_Database.resx b/PKHeX.WinForms/Subforms/SAV_Database.resx index 2f3d5fa66..159583385 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.resx +++ b/PKHeX.WinForms/Subforms/SAV_Database.resx @@ -321,6 +321,12 @@ True + + 132, 17 + + + 208, 17 + diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Pokepuff.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Pokepuff.cs index 3de538586..aca96f503 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Pokepuff.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen6/SAV_Pokepuff.cs @@ -24,7 +24,6 @@ namespace PKHeX.WinForms } private readonly string[] pfa = GameInfo.Strings.puffs; - private int PuffCount { get; set; } private void Setup(int rowCount) { @@ -58,7 +57,6 @@ namespace PKHeX.WinForms private void LoadPuffs(byte[] Puffs) { - PuffCount = Puffs.Length; for (int i = 0; i < Puffs.Length; i++) { dgv.Rows[i].Cells[0].Value = (i + 1).ToString(); diff --git a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs index 514865904..7e9c6a885 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/Gen7/SAV_Trainer7.cs @@ -329,7 +329,7 @@ namespace PKHeX.WinForms SAV.ConsoleRegion = WinFormsUtil.GetIndex(CB_3DSReg); SAV.Language = WinFormsUtil.GetIndex(CB_Language); if (CB_AlolaTime.Enabled) - SAV.GameTime.AlolaTime = (ulong)WinFormsUtil.GetIndex(CB_AlolaTime); + SAV.GameTime.AlolaTime = (ulong)WinFormsUtil.GetIndex(CB_AlolaTime); SAV.OT = TB_OTName.Text; diff --git a/PKHeX.WinForms/Util/WinFormsUtil.cs b/PKHeX.WinForms/Util/WinFormsUtil.cs index cabdb3518..5e7718bde 100644 --- a/PKHeX.WinForms/Util/WinFormsUtil.cs +++ b/PKHeX.WinForms/Util/WinFormsUtil.cs @@ -297,9 +297,16 @@ namespace PKHeX.WinForms } catch (Exception x) { - if (x is UnauthorizedAccessException || x is FileNotFoundException || x is IOException) - Error(MsgFileWriteFail + Environment.NewLine + x.Message, MsgFileWriteProtectedAdvice); - else throw; + switch (x) + { + case UnauthorizedAccessException _: + case FileNotFoundException _: + case IOException _: + Error(MsgFileWriteFail + Environment.NewLine + x.Message, MsgFileWriteProtectedAdvice); + break; + default: + throw; + } } } diff --git a/Tests/PKHeX.Core.Tests/PKM/PKMTests.cs b/Tests/PKHeX.Core.Tests/PKM/PKMTests.cs index d03ec077a..d3d68c946 100644 --- a/Tests/PKHeX.Core.Tests/PKM/PKMTests.cs +++ b/Tests/PKHeX.Core.Tests/PKM/PKMTests.cs @@ -46,15 +46,15 @@ namespace PKHeX.Tests.PKM CheckStringGetSet(nameof(pkm.Nickname), name_nidoran, pkm.Nickname, byte_nidoran, pkm.Nickname_Trash); } - private static void CheckStringGetSet(string check, string instr, string outstr, byte[] indata, byte[] outdata) + private static void CheckStringGetSet(string check, string instr, string outstr, byte[] indata, + byte[] outdata) { - Assert.Equal(instr, outstr); + instr.Should().BeEquivalentTo(outstr); outdata = outdata.Take(indata.Length).ToArray(); - Assert.True(indata.SequenceEqual(outdata), - $"{check} did not set properly." - + Environment.NewLine + string.Join(", ", outdata.Select(z => $"{z:X2}"))); + indata.SequenceEqual(outdata).Should() + .BeTrue($"expected {check} to set properly, instead got {string.Join(", ", outdata.Select(z => $"{z:X2}"))}"); } }