diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index ecd080132..4ad171a8c 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -707,7 +707,7 @@ private void openFile(byte[] input, string path, string ext) { openSAV(sav, path); } - else if ((temp = PKMConverter.getPKMfromBytes(input)) != null) + else if ((temp = PKMConverter.getPKMfromBytes(input, prefer: SAV.Generation)) != null) { PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); if (pk == null) @@ -1640,7 +1640,7 @@ private void clickQR(object sender, EventArgs e) if (ekx == null) return; - PKM pk = PKMConverter.getPKMfromBytes(ekx); + PKM pk = PKMConverter.getPKMfromBytes(ekx, prefer: SAV.Generation); if (pk == null) { WinFormsUtil.Alert("Decoded data not a valid PKM.", $"QR Data Size: {ekx.Length}"); } else { @@ -3860,7 +3860,7 @@ private void loadBoxesFromDB(string path) foreach (byte[] data in from file in filepaths where PKX.getIsPKM(new FileInfo(file).Length) select File.ReadAllBytes(file)) { string c; - PKM temp = PKMConverter.getPKMfromBytes(data); + PKM temp = PKMConverter.getPKMfromBytes(data, prefer: SAV.Generation); PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); if (pk != null) // Write to save @@ -4215,7 +4215,7 @@ private void pbBoxSlot_DragDrop(object sender, DragEventArgs e) byte[] data = File.ReadAllBytes(file); MysteryGift mg = MysteryGift.getMysteryGift(data, fi.Extension); - PKM temp = mg != null ? mg.convertToPKM(SAV) : PKMConverter.getPKMfromBytes(data); + PKM temp = mg?.convertToPKM(SAV) ?? PKMConverter.getPKMfromBytes(data, prefer: SAV.Generation); string c; PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); diff --git a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs index 174d8684f..509324407 100644 --- a/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs +++ b/PKHeX.WinForms/Subforms/PKM Editors/BatchEditor.cs @@ -14,7 +14,7 @@ public partial class BatchEditor : Form public BatchEditor(PKM pk) { InitializeComponent(); - pkm = pk; + pkmref = pk; DragDrop += tabMain_DragDrop; DragEnter += tabMain_DragEnter; @@ -47,7 +47,7 @@ private static string[][] getPropArray() return p1; } - private readonly PKM pkm; + private readonly PKM pkmref; private const string CONST_RAND = "$rand"; private const string CONST_SHINY = "$shiny"; private int currentFormat = -1; @@ -232,7 +232,7 @@ private void processFolder(string[] files, List Filters, List } byte[] data = File.ReadAllBytes(file); - var pkm = PKMConverter.getPKMfromBytes(data); + var pkm = PKMConverter.getPKMfromBytes(data, prefer: Main.SAV.Generation); if (!pkm.Valid) { @@ -316,8 +316,8 @@ private void tabMain_DragDrop(object sender, DragEventArgs e) private void CB_Property_SelectedIndexChanged(object sender, EventArgs e) { L_PropType.Text = getPropertyType(CB_Property.Text); - L_PropValue.Text = pkm.GetType().HasProperty(CB_Property.Text) - ? ReflectUtil.GetValue(pkm, CB_Property.Text).ToString() + L_PropValue.Text = pkmref.GetType().HasProperty(CB_Property.Text) + ? ReflectUtil.GetValue(pkmref, CB_Property.Text).ToString() : ""; } private string getPropertyType(string propertyName) diff --git a/PKHeX.WinForms/Subforms/SAV_Database.cs b/PKHeX.WinForms/Subforms/SAV_Database.cs index e7345e34b..ac5bf9d93 100644 --- a/PKHeX.WinForms/Subforms/SAV_Database.cs +++ b/PKHeX.WinForms/Subforms/SAV_Database.cs @@ -89,7 +89,7 @@ public SAV_Database(Main f1) { FileInfo fi = new FileInfo(file); if (!fi.Extension.Contains(".pk") || !PKX.getIsPKM(fi.Length)) return; - var pk = PKMConverter.getPKMfromBytes(File.ReadAllBytes(file), file); + var pk = PKMConverter.getPKMfromBytes(File.ReadAllBytes(file), file, prefer: Main.SAV.Generation); if (pk != null) dbTemp.Add(pk); }); diff --git a/PKHeX.WinForms/Subforms/Save Editors/SAV_BoxViewer.cs b/PKHeX.WinForms/Subforms/Save Editors/SAV_BoxViewer.cs index b95fff541..63bf1272a 100644 --- a/PKHeX.WinForms/Subforms/Save Editors/SAV_BoxViewer.cs +++ b/PKHeX.WinForms/Subforms/Save Editors/SAV_BoxViewer.cs @@ -262,7 +262,7 @@ private void pbBoxSlot_DragDrop(object sender, DragEventArgs e) byte[] data = File.ReadAllBytes(file); MysteryGift mg = MysteryGift.getMysteryGift(data, fi.Extension); - PKM temp = mg != null ? mg.convertToPKM(SAV) : PKMConverter.getPKMfromBytes(data); + PKM temp = mg != null ? mg.convertToPKM(SAV) : PKMConverter.getPKMfromBytes(data, prefer: SAV.Generation); string c; PKM pk = PKMConverter.convertToFormat(temp, SAV.PKMType, out c); diff --git a/PKHeX/PKM/PKMConverter.cs b/PKHeX/PKM/PKMConverter.cs index 58d6b2e67..f66fed09f 100644 --- a/PKHeX/PKM/PKMConverter.cs +++ b/PKHeX/PKM/PKMConverter.cs @@ -75,8 +75,9 @@ public static int getPKMDataFormat(byte[] data) /// /// Raw data of the Pokemon file. /// Optional identifier for the Pokemon. Usually the full path of the source file. + /// Optional identifier for the preferred generation. Usually the generation of the destination save file. /// An instance of created from the given , or null if is invalid. - public static PKM getPKMfromBytes(byte[] data, string ident = null) + public static PKM getPKMfromBytes(byte[] data, string ident = null, int prefer = 7) { checkEncrypted(ref data); switch (getPKMDataFormat(data)) @@ -110,7 +111,7 @@ public static PKM getPKMfromBytes(byte[] data, string ident = null) return new PK5(data, ident); case 6: var pkx = new PK6(data, ident); - return checkPKMFormat7(pkx); + return checkPKMFormat7(pkx, prefer); default: return null; } @@ -120,14 +121,16 @@ public static PKM getPKMfromBytes(byte[] data, string ident = null) /// Checks if the input PK6 file is really a PK7, if so, updates the object. /// /// PKM to check + /// Prefer a certain generation over another /// Updated PKM if actually PK7 - private static PKM checkPKMFormat7(PK6 pk) => checkPK6is7(pk) ? new PK7(pk.Data, pk.Identifier) : (PKM)pk; + private static PKM checkPKMFormat7(PK6 pk, int prefer) => checkPK6is7(pk, prefer) ? new PK7(pk.Data, pk.Identifier) : (PKM)pk; /// /// Checks if the input PK6 file is really a PK7. /// - /// PKM to check + /// PK6 to check + /// Prefer a certain generation over another /// Boolean is a PK7 - private static bool checkPK6is7(PK6 pk) + private static bool checkPK6is7(PK6 pk, int prefer) { if (pk.Version > Legal.MaxGameID_6) return true; @@ -146,11 +149,19 @@ private static bool checkPK6is7(PK6 pk) if (pk.HeldItem > Legal.MaxItemID_6_AO) return true; - int lvl = pk.CurrentLevel; - if (lvl < 100 && pk.EncounterType != 0) - return false; - if (pk.EncounterType > 24) - return true; + int et = pk.EncounterType; + if (et != 0) + { + if (pk.CurrentLevel < 100) // can't be hyper trained + return false; + + if (pk.GenNumber != 4) // can't have encounter type + return true; + if (et > 24) // invalid encountertype + return true; + if (prefer > 6) // preferential treatment + return true; + } return false; // 6 }