From ed47e389d4464a1c176fb846f65b9f34144ae4e4 Mon Sep 17 00:00:00 2001 From: Kurt Date: Wed, 4 Jan 2017 22:22:50 -0800 Subject: [PATCH] Improve supported extension openfiledialog Existing code supported 3-x, wasn't updated for gen1/2. Allows gens1/2 to override to their own gen format filter, and allows gen7 to allow finding of pk1 files (not yet converted). Noticed the backup would overwrite an existing backup; thus we only back up if the target file exists AND the backup doesn't already exist. --- PKHeX/MainWindow/Main.cs | 17 +++++++---------- PKHeX/PKM/PKM.cs | 1 + PKHeX/PKM/PKX.cs | 22 ++++++++++++++++------ PKHeX/Saves/SAV1.cs | 11 +++++++---- PKHeX/Saves/SAV2.cs | 13 ++++++++----- PKHeX/Saves/SAV7.cs | 6 ++++++ PKHeX/Saves/SaveFile.cs | 7 ++++++- 7 files changed, 51 insertions(+), 26 deletions(-) diff --git a/PKHeX/MainWindow/Main.cs b/PKHeX/MainWindow/Main.cs index d5e6026f9..cbb91c96b 100644 --- a/PKHeX/MainWindow/Main.cs +++ b/PKHeX/MainWindow/Main.cs @@ -270,14 +270,7 @@ private void mainMenuOpen(object sender, EventArgs e) string pkx = pkm.Extension; string ekx = 'e' + pkx.Substring(1, pkx.Length-1); - string supported = "*.pkm;"; - for (int i = 3; i <= SAV.Generation; i++) - { - supported += $"*.pk{i}"; - if (i != pkm.Format) - supported += ";"; - } - + string supported = string.Join(";", SAV.PKMExtensions.Select(s => "*."+s).Concat(new[] {"*.pkm"})); OpenFileDialog ofd = new OpenFileDialog { Filter = $"Supported Files|main;*.sav;*.bin;*.{ekx};{supported};*.bak" + @@ -320,8 +313,12 @@ private void mainMenuSave(object sender, EventArgs e) if (File.Exists(path)) { // File already exists, save a .bak - byte[] backupfile = File.ReadAllBytes(path); - File.WriteAllBytes(path + ".bak", backupfile); + string bakpath = path + ".bak"; + if (!File.Exists(bakpath)) + { + byte[] backupfile = File.ReadAllBytes(path); + File.WriteAllBytes(bakpath, backupfile); + } } if (new[] {".ekx", "."+ekx, ".bin"}.Contains(ext)) diff --git a/PKHeX/PKM/PKM.cs b/PKHeX/PKM/PKM.cs index be1e0228e..eb3551b4f 100644 --- a/PKHeX/PKM/PKM.cs +++ b/PKHeX/PKM/PKM.cs @@ -6,6 +6,7 @@ namespace PKHeX { public abstract class PKM { + public static readonly string[] Extensions = PKX.getPKMExtensions(); public abstract int SIZE_PARTY { get; } public abstract int SIZE_STORED { get; } public virtual string Extension => "pk" + Format; diff --git a/PKHeX/PKM/PKX.cs b/PKHeX/PKM/PKX.cs index ec0771f65..3c11e31a9 100644 --- a/PKHeX/PKM/PKX.cs +++ b/PKHeX/PKM/PKX.cs @@ -1385,7 +1385,7 @@ public static int getGender(int species, uint PID) default: return (PID & 0xFF) <= genderratio ? 1 : 0; } } -#region Gen 3 Species Table + #region Gen 3 Species Table internal static int[] newindex => new[] { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30, @@ -1429,8 +1429,8 @@ public static int getGender(int species, uint PID) 388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408, 409,410,411, }; -#endregion -#region Gen 3/4 Character Tables (Val->Unicode) + #endregion + #region Gen 3/4 Character Tables (Val->Unicode) internal static readonly ushort[] G4Values = { @@ -1821,7 +1821,7 @@ public static int getGender(int species, uint PID) 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 289, // E 452, 355, 373, 379, 387, 405, 411 // F }; -#endregion + #endregion public static readonly byte[][] G4TransferTrashBytes = { new byte[] { }, // Unused @@ -1964,7 +1964,7 @@ public static bool isTransferrable34(ushort item) return item == ITEM_UNK && item > 0; } -#region Gen 1 Character Tables + #region Gen 1 Character Tables private static Dictionary RBY2U_U => new Dictionary{ {0x50, "\0"}, {0x5D, "[TRAINER]"}, @@ -2472,7 +2472,7 @@ public static bool isTransferrable34(ushort item) {0xFE, "8"}, {0xFF, "9"} }; -#endregion + #endregion public static int getG1Species(int raw_id) { @@ -2523,5 +2523,15 @@ public static byte[] setColoStr(string value, int length) .PadRight(value.Length + 1, (char)0); // Null Terminator return Encoding.BigEndianUnicode.GetBytes(TempNick); } + + public static string[] getPKMExtensions() + { + const int gens = 7; + var result = new List(); + result.AddRange(new [] {"ck3", "xk3", "bk4"}); // Special Cases + for (int i = 1; i < gens; i++) + result.Add("pk"+i); + return result.ToArray(); + } } } diff --git a/PKHeX/Saves/SAV1.cs b/PKHeX/Saves/SAV1.cs index df3f3a280..680f1e108 100644 --- a/PKHeX/Saves/SAV1.cs +++ b/PKHeX/Saves/SAV1.cs @@ -8,6 +8,11 @@ public sealed class SAV1 : SaveFile public override string BAKName => $"{FileName} [{OT} ({Version}) - {PlayTimeString}].bak"; public override string Filter => "SAV File|*.sav|All Files|*.*"; public override string Extension => ".sav"; + public override string[] PKMExtensions => PKM.Extensions.Where(f => + { + int gen = f.Last() - 0x30; + return 1 <= gen && gen <= 2; + }).ToArray(); public SAV1(byte[] data = null) { @@ -153,10 +158,8 @@ public override byte[] Write(bool DSV) public override int SIZE_STORED => Japanese ? PKX.SIZE_1JLIST : PKX.SIZE_1ULIST; public override int SIZE_PARTY => Japanese ? PKX.SIZE_1JLIST : PKX.SIZE_1ULIST; - - public int SIZE_BOX => BoxSlotCount*SIZE_STORED; - - public int SIZE_STOREDBOX => PokemonList1.GetDataLength(Japanese ? PokemonList1.CapacityType.StoredJP : PokemonList1.CapacityType.Stored, Japanese); + private int SIZE_BOX => BoxSlotCount*SIZE_STORED; + private int SIZE_STOREDBOX => PokemonList1.GetDataLength(Japanese ? PokemonList1.CapacityType.StoredJP : PokemonList1.CapacityType.Stored, Japanese); public override PKM BlankPKM => new PK1(null, null, Japanese); public override Type PKMType => typeof(PK1); diff --git a/PKHeX/Saves/SAV2.cs b/PKHeX/Saves/SAV2.cs index 0c03f4e06..c782bbb4f 100644 --- a/PKHeX/Saves/SAV2.cs +++ b/PKHeX/Saves/SAV2.cs @@ -8,6 +8,11 @@ public sealed class SAV2 : SaveFile public override string BAKName => $"{FileName} [{OT} ({Version}) - {PlayTimeString}].bak"; public override string Filter => "SAV File|*.sav|All Files|*.*"; public override string Extension => ".sav"; + public override string[] PKMExtensions => PKM.Extensions.Where(f => + { + int gen = f.Last() - 0x30; + return 1 <= gen && gen <= 2; + }).ToArray(); public SAV2(byte[] data = null) { @@ -202,14 +207,12 @@ private void getSAVOffsets() public override int SIZE_STORED => Japanese ? PKX.SIZE_2JLIST : PKX.SIZE_2ULIST; public override int SIZE_PARTY => Japanese ? PKX.SIZE_2JLIST : PKX.SIZE_2ULIST; - - public int SIZE_BOX => BoxSlotCount*SIZE_STORED; - - public int SIZE_STOREDBOX => PokemonList2.GetDataLength(Japanese ? PokemonList2.CapacityType.StoredJP : PokemonList2.CapacityType.Stored, Japanese); - public override PKM BlankPKM => new PK2(null, null, Japanese); public override Type PKMType => typeof(PK2); + private int SIZE_BOX => BoxSlotCount*SIZE_STORED; + private int SIZE_STOREDBOX => PokemonList2.GetDataLength(Japanese ? PokemonList2.CapacityType.StoredJP : PokemonList2.CapacityType.Stored, Japanese); + public override int MaxMoveID => 251; public override int MaxSpeciesID => Legal.MaxSpeciesID_2; public override int MaxAbilityID => 0; diff --git a/PKHeX/Saves/SAV7.cs b/PKHeX/Saves/SAV7.cs index 76447b19c..b0c4885dd 100644 --- a/PKHeX/Saves/SAV7.cs +++ b/PKHeX/Saves/SAV7.cs @@ -10,6 +10,12 @@ public sealed class SAV7 : SaveFile public override string BAKName => $"{FileName} [{OT} ({Version}) - {LastSavedTime}].bak"; public override string Filter => "Main SAV|*.*"; public override string Extension => ""; + public override string[] PKMExtensions => PKM.Extensions.Where(f => + { + int gen = f.Last() - 0x30; + return gen == 1 || (3 <= gen && gen <= 7); + }).ToArray(); + public SAV7(byte[] data = null) { Data = data == null ? new byte[SaveUtil.SIZE_G7SM] : (byte[])data.Clone(); diff --git a/PKHeX/Saves/SaveFile.cs b/PKHeX/Saves/SaveFile.cs index b59e09dbd..255743f4d 100644 --- a/PKHeX/Saves/SaveFile.cs +++ b/PKHeX/Saves/SaveFile.cs @@ -25,6 +25,12 @@ public abstract class SaveFile public virtual bool IndeterminateGame => false; public virtual bool IndeterminateLanguage => false; public virtual bool IndeterminateSubVersion => false; + public abstract string Extension { get; } + public virtual string[] PKMExtensions => PKM.Extensions.Where(f => + { + int gen = f.Last() - 0x30; + return 3 <= gen && gen <= 7; + }).ToArray(); // General PKM Properties public abstract Type PKMType { get; } @@ -322,7 +328,6 @@ public virtual MysteryGiftAlbum GiftAlbum public virtual uint Money { get; set; } public abstract int BoxCount { get; } public virtual int PartyCount { get; protected set; } - public abstract string Extension { get; } // Varied Methods protected abstract void setChecksums();