From ed14ab743677835b3a357cbf3327e8802fea7e97 Mon Sep 17 00:00:00 2001 From: Kaphotics Date: Sun, 23 Oct 2016 22:01:39 -0700 Subject: [PATCH] Add personalinfo references per PKM format Fix possible hang in pk6/pk7 if country/region is unset (continues loop). PersonalInfo references are not to be used when calculating stats (use the savefile table instead) --- PKHeX/PKM/BK4.cs | 1 + PKHeX/PKM/CK3.cs | 3 ++- PKHeX/PKM/PK1.cs | 1 + PKHeX/PKM/PK2.cs | 1 + PKHeX/PKM/PK3.cs | 2 ++ PKHeX/PKM/PK4.cs | 2 ++ PKHeX/PKM/PK5.cs | 2 ++ PKHeX/PKM/PK6.cs | 4 ++++ PKHeX/PKM/PK7.cs | 4 ++++ PKHeX/PKM/PKM.cs | 24 +++++++++++++++++++++++- PKHeX/PKM/XK3.cs | 1 + Tests/PKHeX.Tests/PKM/DateTestPKM.cs | 1 + 12 files changed, 44 insertions(+), 2 deletions(-) diff --git a/PKHeX/PKM/BK4.cs b/PKHeX/PKM/BK4.cs index 1dde82c15..42503f726 100644 --- a/PKHeX/PKM/BK4.cs +++ b/PKHeX/PKM/BK4.cs @@ -12,6 +12,7 @@ public class BK4 : PKM // Big Endian 4th Generation PKM File public sealed override int SIZE_PARTY => PKX.SIZE_4PARTY; public override int SIZE_STORED => PKX.SIZE_4STORED; public override int Format => 4; + public override PersonalInfo PersonalInfo => PersonalTable.HGSS[Species]; public override byte[] DecryptedBoxData => EncryptedBoxData; diff --git a/PKHeX/PKM/CK3.cs b/PKHeX/PKM/CK3.cs index 8eec11b56..653305d68 100644 --- a/PKHeX/PKM/CK3.cs +++ b/PKHeX/PKM/CK3.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; namespace PKHeX { @@ -16,6 +15,8 @@ public class CK3 : PKM // 3rd Generation PKM File public sealed override int SIZE_PARTY => PKX.SIZE_3CSTORED; public override int SIZE_STORED => PKX.SIZE_3CSTORED; public override int Format => 3; + public override PersonalInfo PersonalInfo => PersonalTable.RS[Species]; + public CK3(byte[] decryptedData = null, string ident = null) { Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); diff --git a/PKHeX/PKM/PK1.cs b/PKHeX/PKM/PK1.cs index b97f61b44..1509207f9 100644 --- a/PKHeX/PKM/PK1.cs +++ b/PKHeX/PKM/PK1.cs @@ -8,6 +8,7 @@ public class PK1 : PKM // Internal use only protected internal byte[] otname; protected internal byte[] nick; + public override PersonalInfo PersonalInfo => PersonalTable.RBY[Species]; public byte[] OT_Name_Raw => (byte[])otname.Clone(); public byte[] Nickname_Raw => (byte[])nick.Clone(); diff --git a/PKHeX/PKM/PK2.cs b/PKHeX/PKM/PK2.cs index 76f9cfb74..28ce5c418 100644 --- a/PKHeX/PKM/PK2.cs +++ b/PKHeX/PKM/PK2.cs @@ -8,6 +8,7 @@ public class PK2 : PKM // Internal use only protected internal byte[] otname; protected internal byte[] nick; + public override PersonalInfo PersonalInfo => PersonalTable.C[Species]; public byte[] OT_Name_Raw => (byte[])otname.Clone(); public byte[] Nickname_Raw => (byte[])nick.Clone(); diff --git a/PKHeX/PKM/PK3.cs b/PKHeX/PKM/PK3.cs index b6c357084..eb81b087c 100644 --- a/PKHeX/PKM/PK3.cs +++ b/PKHeX/PKM/PK3.cs @@ -12,6 +12,8 @@ public class PK3 : PKM // 3rd Generation PKM File public sealed override int SIZE_PARTY => PKX.SIZE_3PARTY; public override int SIZE_STORED => PKX.SIZE_3STORED; public override int Format => 3; + public override PersonalInfo PersonalInfo => PersonalTable.RS[Species]; + public PK3(byte[] decryptedData = null, string ident = null) { Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); diff --git a/PKHeX/PKM/PK4.cs b/PKHeX/PKM/PK4.cs index baebcb292..cc2ff7f98 100644 --- a/PKHeX/PKM/PK4.cs +++ b/PKHeX/PKM/PK4.cs @@ -12,6 +12,8 @@ public class PK4 : PKM // 4th Generation PKM File public sealed override int SIZE_PARTY => PKX.SIZE_4PARTY; public override int SIZE_STORED => PKX.SIZE_4STORED; public override int Format => 4; + public override PersonalInfo PersonalInfo => PersonalTable.HGSS.getFormeEntry(Species, AltForm); + public PK4(byte[] decryptedData = null, string ident = null) { Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); diff --git a/PKHeX/PKM/PK5.cs b/PKHeX/PKM/PK5.cs index 3f2cbc39b..349a777d8 100644 --- a/PKHeX/PKM/PK5.cs +++ b/PKHeX/PKM/PK5.cs @@ -13,6 +13,8 @@ public class PK5 : PKM // 5th Generation PKM File public sealed override int SIZE_PARTY => PKX.SIZE_5PARTY; public override int SIZE_STORED => PKX.SIZE_5STORED; public override int Format => 5; + public override PersonalInfo PersonalInfo => PersonalTable.B2W2.getFormeEntry(Species, AltForm); + public PK5(byte[] decryptedData = null, string ident = null) { Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); diff --git a/PKHeX/PKM/PK6.cs b/PKHeX/PKM/PK6.cs index 3be0df10d..f3b65672e 100644 --- a/PKHeX/PKM/PK6.cs +++ b/PKHeX/PKM/PK6.cs @@ -14,6 +14,8 @@ public class PK6 : PKM public sealed override int SIZE_PARTY => PKX.SIZE_6PARTY; public override int SIZE_STORED => PKX.SIZE_6STORED; public override int Format => 6; + public override PersonalInfo PersonalInfo => (AO ? PersonalTable.AO : PersonalTable.XY).getFormeEntry(Species, AltForm); + public PK6(byte[] decryptedData = null, string ident = null) { Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); @@ -530,6 +532,8 @@ public void FixMemories() } if (Geo1_Country == 0 && !IsUntraded && !IsUntradedEvent6) { + if ((Country | Region) == 0) + break; // Traded Non-Eggs/Events need to have a current location. Geo1_Country = Country; Geo1_Region = Region; diff --git a/PKHeX/PKM/PK7.cs b/PKHeX/PKM/PK7.cs index c53616973..7f43c6575 100644 --- a/PKHeX/PKM/PK7.cs +++ b/PKHeX/PKM/PK7.cs @@ -14,6 +14,8 @@ public class PK7 : PKM public sealed override int SIZE_PARTY => PKX.SIZE_6PARTY; public override int SIZE_STORED => PKX.SIZE_6STORED; public override int Format => 7; + public override PersonalInfo PersonalInfo => PersonalTable.SM.getFormeEntry(Species, AltForm); + public PK7(byte[] decryptedData = null, string ident = null) { Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); @@ -538,6 +540,8 @@ public void FixMemories() } if (Geo1_Country == 0 && !IsUntraded && !IsUntradedEvent6) { + if ((Country | Region) == 0) + break; // Traded Non-Eggs/Events need to have a current location. Geo1_Country = Country; Geo1_Region = Region; diff --git a/PKHeX/PKM/PKM.cs b/PKHeX/PKM/PKM.cs index 19c6fc801..34230468e 100644 --- a/PKHeX/PKM/PKM.cs +++ b/PKHeX/PKM/PKM.cs @@ -9,6 +9,7 @@ public abstract class PKM public abstract int SIZE_PARTY { get; } public abstract int SIZE_STORED { get; } public virtual string Extension => "pk" + Format; + public abstract PersonalInfo PersonalInfo { get; } // Internal Attributes set on creation public byte[] Data; // Raw Storage @@ -388,10 +389,31 @@ public virtual int HPType public virtual bool WasEventEgg => ((Egg_Location > 40000 && Egg_Location < 50000) || (FatefulEncounter && Egg_Location > 0)) && Met_Level == 1; public virtual bool WasTradedEgg => Egg_Location == 30002; public virtual bool WasIngameTrade => Met_Location == 30001; - public virtual bool IsUntraded => string.IsNullOrWhiteSpace(HT_Name); + public virtual bool IsUntraded => string.IsNullOrWhiteSpace(HT_Name) && GenNumber == Format; public virtual bool SecretSuperTrainingUnlocked { get { return false; } set { } } public virtual bool SecretSuperTrainingComplete { get { return false; } set { } } + public bool InhabitedGeneration(int Generation) + { + if (Format < Generation) + return false; // Future + if (GenNumber > Generation) + return false; // Past + + switch (Generation) // Sanity Check Species ID + { + case 1: return Species <= Legal.MaxSpeciesID_1; + case 2: return Species <= Legal.MaxSpeciesID_2; + case 3: return Species <= Legal.MaxSpeciesID_3; + case 4: return Species <= Legal.MaxSpeciesID_4; + case 5: return Species <= Legal.MaxSpeciesID_5; + case 6: return Species <= Legal.MaxSpeciesID_6; + case 7: return Species <= Legal.MaxSpeciesID_7; + default: + return false; + } + } + // Methods public abstract bool getGenderIsValid(); public void RefreshChecksum() { Checksum = CalculateChecksum(); } diff --git a/PKHeX/PKM/XK3.cs b/PKHeX/PKM/XK3.cs index bd2870b25..ec34d300a 100644 --- a/PKHeX/PKM/XK3.cs +++ b/PKHeX/PKM/XK3.cs @@ -15,6 +15,7 @@ public class XK3 : PKM // 3rd Generation PKM File public sealed override int SIZE_PARTY => PKX.SIZE_3XSTORED; public override int SIZE_STORED => PKX.SIZE_3XSTORED; public override int Format => 3; + public override PersonalInfo PersonalInfo => PersonalTable.RS[Species]; public XK3(byte[] decryptedData = null, string ident = null) { Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); diff --git a/Tests/PKHeX.Tests/PKM/DateTestPKM.cs b/Tests/PKHeX.Tests/PKM/DateTestPKM.cs index 8617f8ab0..220bfc3c0 100644 --- a/Tests/PKHeX.Tests/PKM/DateTestPKM.cs +++ b/Tests/PKHeX.Tests/PKM/DateTestPKM.cs @@ -13,6 +13,7 @@ class DateTestPKM : PKHeX.PKM public int EggMetYear { get; set; } public int EggMetMonth { get; set; } public int EggMetDay { get; set; } + public override PersonalInfo PersonalInfo => null; protected override int Met_Year {