Expand pk6/pk7 checks

As per comments on 75d460e
This commit is contained in:
Kurt 2017-01-27 23:33:36 -08:00
parent 75d460e502
commit b86321c142
5 changed files with 32 additions and 21 deletions

View file

@ -707,7 +707,7 @@ namespace PKHeX.WinForms
{
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 @@ namespace PKHeX.WinForms
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 @@ namespace PKHeX.WinForms
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 @@ namespace PKHeX.WinForms
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);

View file

@ -14,7 +14,7 @@ namespace PKHeX.WinForms
public BatchEditor(PKM pk)
{
InitializeComponent();
pkm = pk;
pkmref = pk;
DragDrop += tabMain_DragDrop;
DragEnter += tabMain_DragEnter;
@ -47,7 +47,7 @@ namespace PKHeX.WinForms
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 @@ namespace PKHeX.WinForms
}
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 @@ namespace PKHeX.WinForms
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)

View file

@ -89,7 +89,7 @@ namespace PKHeX.WinForms
{
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);
});

View file

@ -262,7 +262,7 @@ namespace PKHeX.WinForms
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);

View file

@ -75,8 +75,9 @@ namespace PKHeX.Core
/// </summary>
/// <param name="data">Raw data of the Pokemon file.</param>
/// <param name="ident">Optional identifier for the Pokemon. Usually the full path of the source file.</param>
/// <param name="prefer">Optional identifier for the preferred generation. Usually the generation of the destination save file.</param>
/// <returns>An instance of <see cref="PKM"/> created from the given <paramref name="data"/>, or null if <paramref name="data"/> is invalid.</returns>
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 @@ namespace PKHeX.Core
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 @@ namespace PKHeX.Core
/// Checks if the input PK6 file is really a PK7, if so, updates the object.
/// </summary>
/// <param name="pk">PKM to check</param>
/// <param name="prefer">Prefer a certain generation over another</param>
/// <returns>Updated PKM if actually PK7</returns>
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;
/// <summary>
/// Checks if the input PK6 file is really a PK7.
/// </summary>
/// <param name="pk">PKM to check</param>
/// <param name="pk">PK6 to check</param>
/// <param name="prefer">Prefer a certain generation over another</param>
/// <returns>Boolean is a PK7</returns>
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 @@ namespace PKHeX.Core
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
}