From 1bb07b5998ca1bc86a5c43301672b68386db4537 Mon Sep 17 00:00:00 2001 From: Kurt Date: Tue, 22 Dec 2015 21:14:50 -0800 Subject: [PATCH] Add pgf skeleton & supports Untested and not intended to be correct; just establishing the framework for now. --- Misc/PGF.cs | 229 ++++++++++++++++++++++++++++++++++++ Misc/PK5.cs | 326 +++++++++++++++++++++++++++++++++++++++++++++++++++ Misc/Util.cs | 5 + PKHeX.csproj | 2 + 4 files changed, 562 insertions(+) create mode 100644 Misc/PGF.cs create mode 100644 Misc/PK5.cs diff --git a/Misc/PGF.cs b/Misc/PGF.cs new file mode 100644 index 000000000..f53b5ce61 --- /dev/null +++ b/Misc/PGF.cs @@ -0,0 +1,229 @@ +using System; +using System.Text; + +namespace PKHeX +{ + public class PGF + { + internal static int Size = 0xCC; + + public byte[] Data; + public PGF(byte[] data = null) + { + Data = data ?? new byte[Size]; + } + + public ushort TID { get { return BitConverter.ToUInt16(Data, 0x00); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x00); } } + public ushort SID { get { return BitConverter.ToUInt16(Data, 0x02); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x02); } } + public int OriginGame { get { return Data[0x04]; } set { Data[0x04] = (byte)value; } } + + private byte RIB0 { get { return Data[0x74]; } set { Data[0x74] = value; } } + public bool RIB0_0 { get { return (RIB0 & (1 << 0)) == 1 << 0; } set { RIB0 = (byte)(RIB0 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } // Country Ribbon + public bool RIB0_1 { get { return (RIB0 & (1 << 1)) == 1 << 1; } set { RIB0 = (byte)(RIB0 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } // National Ribbon + public bool RIB0_2 { get { return (RIB0 & (1 << 2)) == 1 << 2; } set { RIB0 = (byte)(RIB0 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } // Earth Ribbon + public bool RIB0_3 { get { return (RIB0 & (1 << 3)) == 1 << 3; } set { RIB0 = (byte)(RIB0 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } // World Ribbon + public bool RIB0_4 { get { return (RIB0 & (1 << 4)) == 1 << 4; } set { RIB0 = (byte)(RIB0 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } // Classic Ribbon + public bool RIB0_5 { get { return (RIB0 & (1 << 5)) == 1 << 5; } set { RIB0 = (byte)(RIB0 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } // Premier Ribbon + public bool RIB0_6 { get { return (RIB0 & (1 << 6)) == 1 << 6; } set { RIB0 = (byte)(RIB0 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } // Event Ribbon + public bool RIB0_7 { get { return (RIB0 & (1 << 7)) == 1 << 7; } set { RIB0 = (byte)(RIB0 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } // Birthday Ribbon + private byte RIB1 { get { return Data[0x75]; } set { Data[0x75] = value; } } + public bool RIB1_0 { get { return (RIB1 & (1 << 0)) == 1 << 0; } set { RIB1 = (byte)(RIB1 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } // Special Ribbon + public bool RIB1_1 { get { return (RIB1 & (1 << 1)) == 1 << 1; } set { RIB1 = (byte)(RIB1 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } // Souvenir Ribbon + public bool RIB1_2 { get { return (RIB1 & (1 << 2)) == 1 << 2; } set { RIB1 = (byte)(RIB1 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } // Wishing Ribbon + public bool RIB1_3 { get { return (RIB1 & (1 << 3)) == 1 << 3; } set { RIB1 = (byte)(RIB1 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } // Battle Champ Ribbon + public bool RIB1_4 { get { return (RIB1 & (1 << 4)) == 1 << 4; } set { RIB1 = (byte)(RIB1 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } // Regional Champ Ribbon + public bool RIB1_5 { get { return (RIB1 & (1 << 5)) == 1 << 5; } set { RIB1 = (byte)(RIB1 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } // National Champ Ribbon + public bool RIB1_6 { get { return (RIB1 & (1 << 6)) == 1 << 6; } set { RIB1 = (byte)(RIB1 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } // World Champ Ribbon + public bool RIB1_7 { get { return (RIB1 & (1 << 7)) == 1 << 7; } set { RIB1 = (byte)(RIB1 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } // Empty + + public uint PID { get { return BitConverter.ToUInt32(Data, 0x08); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x08); } } + // Ribbons1 + // Ribbons2 + public int Pokéball { get { return Data[0x0E]; } set { Data[0x0E] = (byte)value; } } + public int HeldItem { get { return BitConverter.ToUInt16(Data, 0x10); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x10); } } + public int Move1 { get { return BitConverter.ToUInt16(Data, 0x12); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x12); } } + public int Move2 { get { return BitConverter.ToUInt16(Data, 0x14); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x14); } } + public int Move3 { get { return BitConverter.ToUInt16(Data, 0x16); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x16); } } + public int Move4 { get { return BitConverter.ToUInt16(Data, 0x18); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x18); } } + public int Species { get { return BitConverter.ToUInt16(Data, 0x1A); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x1A); } } + public int Form { get { return Data[0x1C]; } set { Data[0x1C] = (byte)value; } } + public int Language { get { return Data[0x1D]; } set { Data[0x1D] = (byte)value; } } + public string Nickname + { + get { return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x1E, 0x16)); } + set { Encoding.Unicode.GetBytes(value.PadRight(0xB, (char)0xFFFF)).CopyTo(Data, 0x1E); } + } + public int Nature { get { return Data[0x34]; } set { Data[0x34] = (byte)value; } } + public int Gender { get { return Data[0x35]; } set { Data[0x35] = (byte)value; } } + public int AbilityType { get { return Data[0x36]; } set { Data[0x36] = (byte)value; } } + public int PIDType { get { return Data[0x37]; } set { Data[0x37] = (byte)value; } } + public ushort EggLocation { get { return BitConverter.ToUInt16(Data, 0x38); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x38); } } + public ushort MetLocation { get { return BitConverter.ToUInt16(Data, 0x3A); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x3A); } } + public int MetLevel { get { return Data[0x3C]; } set { Data[0x3C] = (byte)value; } } + public int CNT_Cool { get { return Data[0x3D]; } set { Data[0x3D] = (byte)value; } } + public int CNT_Beauty { get { return Data[0x3E]; } set { Data[0x3E] = (byte)value; } } + public int CNT_Cute { get { return Data[0x3F]; } set { Data[0x3F] = (byte)value; } } + public int CNT_Smart { get { return Data[0x40]; } set { Data[0x40] = (byte)value; } } + public int CNT_Tough { get { return Data[0x41]; } set { Data[0x41] = (byte)value; } } + public int CNT_Sheen { get { return Data[0x42]; } set { Data[0x42] = (byte)value; } } + public int IV_HP { get { return Data[0x43]; } set { Data[0x43] = (byte)value; } } + public int IV_ATK { get { return Data[0x44]; } set { Data[0x44] = (byte)value; } } + public int IV_DEF { get { return Data[0x45]; } set { Data[0x45] = (byte)value; } } + public int IV_SPE { get { return Data[0x46]; } set { Data[0x46] = (byte)value; } } + public int IV_SPA { get { return Data[0x47]; } set { Data[0x47] = (byte)value; } } + public int IV_SPD { get { return Data[0x48]; } set { Data[0x48] = (byte)value; } } + // Unused 0x49 + public string OT { + get { return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x4A, 0x10)); } + set { Encoding.Unicode.GetBytes(value.PadRight(0x08, (char)0xFFFF)).CopyTo(Data, 0x4A); } } + public int OTGender { get { return Data[0x5A]; } set { Data[0x5A] = (byte)value; } } + public int Level { get { return Data[0x5B]; } set { Data[0x5C] = (byte)value; } } + public bool IsEgg { get { return Data[0x5C] == 1; } set { Data[0x5C] = (byte)(value ? 1 : 0); } } + // Unused 0x5D 0x5E 0x5F + + // Card Attributes + public ushort Item { get { return BitConverter.ToUInt16(Data, 0x00); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x00); } } + + public ushort Year { get { return BitConverter.ToUInt16(Data, 0xAE); } set { BitConverter.GetBytes(value).CopyTo(Data, 0xAE); } } + public byte Month { get { return Data[0xAD]; } set { Data[0xAD] = value; } } + public byte Day { get { return Data[0xAC]; } set { Data[0xAC] = value; } } + public int CardID + { + get { return BitConverter.ToUInt16(Data, 0xB0); } + set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0xB0); } + } + public int CardLocation { get { return Data[0xB2]; } set { Data[0xB2] = (byte)value; } } + public int CardType { get { return Data[0xB3]; } set { Data[0xB3] = (byte)value; } } + public bool GiftUsed { get { return Data[0xB4] >> 1 > 0; } set { Data[0xB4] = (byte)(Data[0xB4] & ~2 | (value ? 2 : 0)); } } + public bool MultiObtain { get { return Data[0xB4] == 1; } set { Data[0xB4] = (byte)(value ? 1 : 0); } } + + // Meta Accessible Properties + public int[] IVs { get { return new[] { IV_HP, IV_ATK, IV_DEF, IV_SPE, IV_SPA, IV_SPD }; } } + public bool IsNicknamed { get { return Nickname.Length > 0; } } + public int[] Moves + { + get { return new[] { Move1, Move2, Move3, Move4 }; } + set + { + if (value.Length > 0) Move1 = value[0]; + if (value.Length > 1) Move2 = value[1]; + if (value.Length > 2) Move3 = value[2]; + if (value.Length > 3) Move4 = value[3]; + } + } + public bool IsPokémon { get { return CardType == 1; } set { if (value) CardType = 1; } } + public bool IsItem { get { return CardType == 2; } set { if (value) CardType = 2; } } + public bool IsPower { get { return CardType == 3; } set { if (value) CardType = 3; } } + + public PK5 convertToPK5(SAV6 SAV) + { + if (!IsPokémon) + return null; + + int currentLevel = Level > 0 ? Level : (int)(Util.rnd32() % 100 + 1); + PK5 pk = new PK5 + { + Species = Species, + HeldItem = HeldItem, + TID = TID, + SID = SID, + Met_Level = currentLevel, + Nature = Nature != 0xFF ? Nature : (int)(Util.rnd32() % 25), + Gender = PKX.Personal[Species].Gender == 255 ? 2 : (Gender != 3 ? Gender : PKX.Personal[Species].RandomGender), + AltForm = Form, + Version = OriginGame == 0 ? new[] {20, 21, 22, 23}[Util.rnd32() & 0x3] : OriginGame, + Language = Language == 0 ? SAV.Language : Language, + Ball = Pokéball, + Move1 = Move1, + Move2 = Move2, + Move3 = Move3, + Move4 = Move4, + Move1_PP = PKX.getBasePP(Move1), + Move2_PP = PKX.getBasePP(Move2), + Move3_PP = PKX.getBasePP(Move3), + Move4_PP = PKX.getBasePP(Move4), + Met_Location = MetLocation, + Met_Day = Day, + Met_Month = Month, + Met_Year = Year - 2000, + Egg_Location = EggLocation, + CNT_Cool = CNT_Cool, + CNT_Beauty = CNT_Beauty, + CNT_Cute = CNT_Cute, + CNT_Smart = CNT_Smart, + CNT_Tough = CNT_Tough, + CNT_Sheen = CNT_Sheen, + + OT_Name = OT.Length > 0 ? OT : "PKHeX", + OT_Gender = OTGender != 3 ? OTGender % 2 : 1, + + EXP = PKX.getEXP(Level, Species), + + // Ribbons + // TBD + + Friendship = PKX.getBaseFriendship(Species), + FatefulEncounter = true, + }; + if (OTGender == 3) // User's + { + TID = 12345; + SID = 54321; + } + pk.IsNicknamed = IsNicknamed; + pk.Nickname = IsNicknamed ? Nickname : PKX.getSpeciesName(Species, pk.Language); + + // More 'complex' logic to determine final values + + // Dumb way to generate random IVs. + for (int i = 0; i < IVs.Length; i++) + if (IVs[i] == 0xFF) IVs[i] = (int)(Util.rnd32() & 0x1F); + pk.IVs = IVs; + + int av = 0; + switch (AbilityType) + { + case 00: // 0 - 0 + case 01: // 1 - 1 + case 02: // 2 - H + av = AbilityType; + break; + case 03: // 0/1 + case 04: // 0/1/H + av = (int)(Util.rnd32() % (AbilityType - 1)); + break; + } + if (av == 2) + pk.HiddenAbility = true; + pk.Ability = PKX.Personal[PKX.Personal[Species].FormeIndex(Species, pk.AltForm)].Abilities[av]; + + switch (PIDType) + { + case 00: // Specified + if (PID == 0) + do { pk.PID = Util.rnd32(); } while ((uint)(((TID ^ SID ^ (pk.PID & 0xFFFF)) << 8) + (pk.PID & 0xFFFF)) < 8); + else + pk.PID = PID; + break; + case 01: // Random + pk.PID = PID == 0 ? Util.rnd32() : PID; + break; + case 02: // Random Shiny + pk.PID = Util.rnd32(); + pk.PID = (uint)(((TID ^ SID ^ (pk.PID & 0xFFFF)) << 8) + (pk.PID & 0xFFFF)); + break; + } + + if (IsEgg) + { + pk.IsEgg = true; + pk.Egg_Day = Day; + pk.Egg_Month = Month; + pk.Egg_Year = Year; + } + + pk.RefreshChecksum(); + return pk; + } + } +} diff --git a/Misc/PK5.cs b/Misc/PK5.cs new file mode 100644 index 000000000..1992ee951 --- /dev/null +++ b/Misc/PK5.cs @@ -0,0 +1,326 @@ +using System; +using System.Linq; +using System.Text; + +namespace PKHeX +{ + // 5th Generation PKM File + public class PK5 + { + internal static readonly int SIZE_PARTY = 220; + internal static readonly int SIZE_STORED = 136; + + public PK5(byte[] decryptedData = null, string ident = null) + { + Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone(); + Identifier = ident; + if (Data.Length != SIZE_PARTY) + Array.Resize(ref Data, SIZE_PARTY); + } + + // Internal Attributes set on creation + public byte[] Data; // Raw Storage + public string Identifier; // User or Form Custom Attribute + + // Structure + public uint PID { get { return BitConverter.ToUInt32(Data, 0x00); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x00); } } + public ushort Sanity { get { return BitConverter.ToUInt16(Data, 0x04); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x04); } } + public ushort Checksum { get { return BitConverter.ToUInt16(Data, 0x06); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x06); } } + + #region Block A + public int Species { get { return BitConverter.ToUInt16(Data, 0x08); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x08); } } + public int HeldItem { get { return BitConverter.ToUInt16(Data, 0x0A); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x0A); } } + public int TID { get { return BitConverter.ToUInt16(Data, 0x0C); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x0C); } } + public int SID { get { return BitConverter.ToUInt16(Data, 0x0E); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x0E); } } + public uint EXP { get { return BitConverter.ToUInt32(Data, 0x10); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x10); } } + public int Friendship { get { return Data[0x14]; } set { Data[0x14] = (byte)value; } } + public int Ability { get { return Data[0x15]; } set { Data[0x15] = (byte)value; } } + public byte Markings { get { return Data[0x16]; } set { Data[0x16] = value; } } + public bool Circle { get { return (Markings & (1 << 0)) == 1 << 0; } set { Markings = (byte)(Markings & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool Triangle { get { return (Markings & (1 << 1)) == 1 << 1; } set { Markings = (byte)(Markings & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool Square { get { return (Markings & (1 << 2)) == 1 << 2; } set { Markings = (byte)(Markings & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool Heart { get { return (Markings & (1 << 3)) == 1 << 3; } set { Markings = (byte)(Markings & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool Star { get { return (Markings & (1 << 4)) == 1 << 4; } set { Markings = (byte)(Markings & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool Diamond { get { return (Markings & (1 << 5)) == 1 << 5; } set { Markings = (byte)(Markings & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public int Language { get { return Data[0x17]; } set { Data[0x17] = (byte)value; } } + public int EV_HP { get { return Data[0x18]; } set { Data[0x18] = (byte)value; } } + public int EV_ATK { get { return Data[0x19]; } set { Data[0x19] = (byte)value; } } + public int EV_DEF { get { return Data[0x1A]; } set { Data[0x1A] = (byte)value; } } + public int EV_SPE { get { return Data[0x1B]; } set { Data[0x1B] = (byte)value; } } + public int EV_SPA { get { return Data[0x1C]; } set { Data[0x1C] = (byte)value; } } + public int EV_SPD { get { return Data[0x1D]; } set { Data[0x1D] = (byte)value; } } + public int CNT_Cool { get { return Data[0x1E]; } set { Data[0x1E] = (byte)value; } } + public int CNT_Beauty { get { return Data[0x1F]; } set { Data[0x1F] = (byte)value; } } + public int CNT_Cute { get { return Data[0x20]; } set { Data[0x20] = (byte)value; } } + public int CNT_Smart { get { return Data[0x21]; } set { Data[0x21] = (byte)value; } } + public int CNT_Tough { get { return Data[0x22]; } set { Data[0x22] = (byte)value; } } + public int CNT_Sheen { get { return Data[0x23]; } set { Data[0x23] = (byte)value; } } + + private byte RIB0 { get { return Data[0x24]; } set { Data[0x24] = value; } } // Sinnoh 1 + public bool RIB0_0 { get { return (RIB0 & (1 << 0)) == 1 << 0; } set { RIB0 = (byte)(RIB0 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB0_1 { get { return (RIB0 & (1 << 1)) == 1 << 1; } set { RIB0 = (byte)(RIB0 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB0_2 { get { return (RIB0 & (1 << 2)) == 1 << 2; } set { RIB0 = (byte)(RIB0 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB0_3 { get { return (RIB0 & (1 << 3)) == 1 << 3; } set { RIB0 = (byte)(RIB0 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB0_4 { get { return (RIB0 & (1 << 4)) == 1 << 4; } set { RIB0 = (byte)(RIB0 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB0_5 { get { return (RIB0 & (1 << 5)) == 1 << 5; } set { RIB0 = (byte)(RIB0 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB0_6 { get { return (RIB0 & (1 << 6)) == 1 << 6; } set { RIB0 = (byte)(RIB0 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB0_7 { get { return (RIB0 & (1 << 7)) == 1 << 7; } set { RIB0 = (byte)(RIB0 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + private byte RIB1 { get { return Data[0x25]; } set { Data[0x25] = value; } } // Sinnoh 2 + public bool RIB1_0 { get { return (RIB1 & (1 << 0)) == 1 << 0; } set { RIB1 = (byte)(RIB1 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB1_1 { get { return (RIB1 & (1 << 1)) == 1 << 1; } set { RIB1 = (byte)(RIB1 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB1_2 { get { return (RIB1 & (1 << 2)) == 1 << 2; } set { RIB1 = (byte)(RIB1 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB1_3 { get { return (RIB1 & (1 << 3)) == 1 << 3; } set { RIB1 = (byte)(RIB1 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB1_4 { get { return (RIB1 & (1 << 4)) == 1 << 4; } set { RIB1 = (byte)(RIB1 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB1_5 { get { return (RIB1 & (1 << 5)) == 1 << 5; } set { RIB1 = (byte)(RIB1 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB1_6 { get { return (RIB1 & (1 << 6)) == 1 << 6; } set { RIB1 = (byte)(RIB1 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB1_7 { get { return (RIB1 & (1 << 7)) == 1 << 7; } set { RIB1 = (byte)(RIB1 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + + private byte RIB2 { get { return Data[0x26]; } set { Data[0x26] = value; } } // Unova 1 + public bool RIB2_0 { get { return (RIB2 & (1 << 0)) == 1 << 0; } set { RIB2 = (byte)(RIB2 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB2_1 { get { return (RIB2 & (1 << 1)) == 1 << 1; } set { RIB2 = (byte)(RIB2 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB2_2 { get { return (RIB2 & (1 << 2)) == 1 << 2; } set { RIB2 = (byte)(RIB2 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB2_3 { get { return (RIB2 & (1 << 3)) == 1 << 3; } set { RIB2 = (byte)(RIB2 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB2_4 { get { return (RIB2 & (1 << 4)) == 1 << 4; } set { RIB2 = (byte)(RIB2 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB2_5 { get { return (RIB2 & (1 << 5)) == 1 << 5; } set { RIB2 = (byte)(RIB2 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB2_6 { get { return (RIB2 & (1 << 6)) == 1 << 6; } set { RIB2 = (byte)(RIB2 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB2_7 { get { return (RIB2 & (1 << 7)) == 1 << 7; } set { RIB2 = (byte)(RIB2 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + private byte RIB3 { get { return Data[0x27]; } set { Data[0x27] = value; } } // Unova 2 + public bool RIB3_0 { get { return (RIB3 & (1 << 0)) == 1 << 0; } set { RIB3 = (byte)(RIB3 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB3_1 { get { return (RIB3 & (1 << 1)) == 1 << 1; } set { RIB3 = (byte)(RIB3 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB3_2 { get { return (RIB3 & (1 << 2)) == 1 << 2; } set { RIB3 = (byte)(RIB3 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB3_3 { get { return (RIB3 & (1 << 3)) == 1 << 3; } set { RIB3 = (byte)(RIB3 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB3_4 { get { return (RIB3 & (1 << 4)) == 1 << 4; } set { RIB3 = (byte)(RIB3 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB3_5 { get { return (RIB3 & (1 << 5)) == 1 << 5; } set { RIB3 = (byte)(RIB3 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB3_6 { get { return (RIB3 & (1 << 6)) == 1 << 6; } set { RIB3 = (byte)(RIB3 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB3_7 { get { return (RIB3 & (1 << 7)) == 1 << 7; } set { RIB3 = (byte)(RIB3 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + #endregion + + #region Block B + public int Move1 { get { return BitConverter.ToUInt16(Data, 0x28); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x28); } } + public int Move2 { get { return BitConverter.ToUInt16(Data, 0x2A); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x2A); } } + public int Move3 { get { return BitConverter.ToUInt16(Data, 0x2C); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x2C); } } + public int Move4 { get { return BitConverter.ToUInt16(Data, 0x2E); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x2E); } } + public int Move1_PP { get { return Data[0x30]; } set { Data[0x30] = (byte)value; } } + public int Move2_PP { get { return Data[0x31]; } set { Data[0x31] = (byte)value; } } + public int Move3_PP { get { return Data[0x32]; } set { Data[0x32] = (byte)value; } } + public int Move4_PP { get { return Data[0x33]; } set { Data[0x33] = (byte)value; } } + public int Move1_PPUps { get { return Data[0x34]; } set { Data[0x34] = (byte)value; } } + public int Move2_PPUps { get { return Data[0x35]; } set { Data[0x35] = (byte)value; } } + public int Move3_PPUps { get { return Data[0x36]; } set { Data[0x36] = (byte)value; } } + public int Move4_PPUps { get { return Data[0x37]; } set { Data[0x37] = (byte)value; } } + private uint IV32 { get { return BitConverter.ToUInt32(Data, 0x38); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x38); } } + public int IV_HP { get { return (int)(IV32 >> 00) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 00)) | (uint)((value > 31 ? 31 : value) << 00)); } } + public int IV_ATK { get { return (int)(IV32 >> 05) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 05)) | (uint)((value > 31 ? 31 : value) << 05)); } } + public int IV_DEF { get { return (int)(IV32 >> 10) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 10)) | (uint)((value > 31 ? 31 : value) << 10)); } } + public int IV_SPE { get { return (int)(IV32 >> 15) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 15)) | (uint)((value > 31 ? 31 : value) << 15)); } } + public int IV_SPA { get { return (int)(IV32 >> 20) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 20)) | (uint)((value > 31 ? 31 : value) << 20)); } } + public int IV_SPD { get { return (int)(IV32 >> 25) & 0x1F; } set { IV32 = (uint)((IV32 & ~(0x1F << 25)) | (uint)((value > 31 ? 31 : value) << 25)); } } + public bool IsEgg { get { return ((IV32 >> 30) & 1) == 1; } set { IV32 = (uint)((IV32 & ~0x40000000) | (uint)(value ? 0x40000000 : 0)); } } + public bool IsNicknamed { get { return ((IV32 >> 31) & 1) == 1; } set { IV32 = ((IV32 & ~0x80000000) | (value ? 0x80000000 : 0)); } } + + private byte RIB4 { get { return Data[0x26]; } set { Data[0x26] = value; } } // Hoenn 1 + public bool RIB4_0 { get { return (RIB4 & (1 << 0)) == 1 << 0; } set { RIB4 = (byte)(RIB4 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB4_1 { get { return (RIB4 & (1 << 1)) == 1 << 1; } set { RIB4 = (byte)(RIB4 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB4_2 { get { return (RIB4 & (1 << 2)) == 1 << 2; } set { RIB4 = (byte)(RIB4 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB4_3 { get { return (RIB4 & (1 << 3)) == 1 << 3; } set { RIB4 = (byte)(RIB4 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB4_4 { get { return (RIB4 & (1 << 4)) == 1 << 4; } set { RIB4 = (byte)(RIB4 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB4_5 { get { return (RIB4 & (1 << 5)) == 1 << 5; } set { RIB4 = (byte)(RIB4 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB4_6 { get { return (RIB4 & (1 << 6)) == 1 << 6; } set { RIB4 = (byte)(RIB4 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB4_7 { get { return (RIB4 & (1 << 7)) == 1 << 7; } set { RIB4 = (byte)(RIB4 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + private byte RIB5 { get { return Data[0x27]; } set { Data[0x27] = value; } } // Hoenn 2 + public bool RIB5_0 { get { return (RIB5 & (1 << 0)) == 1 << 0; } set { RIB5 = (byte)(RIB5 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB5_1 { get { return (RIB5 & (1 << 1)) == 1 << 1; } set { RIB5 = (byte)(RIB5 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB5_2 { get { return (RIB5 & (1 << 2)) == 1 << 2; } set { RIB5 = (byte)(RIB5 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB5_3 { get { return (RIB5 & (1 << 3)) == 1 << 3; } set { RIB5 = (byte)(RIB5 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB5_4 { get { return (RIB5 & (1 << 4)) == 1 << 4; } set { RIB5 = (byte)(RIB5 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB5_5 { get { return (RIB5 & (1 << 5)) == 1 << 5; } set { RIB5 = (byte)(RIB5 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB5_6 { get { return (RIB5 & (1 << 6)) == 1 << 6; } set { RIB5 = (byte)(RIB5 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB5_7 { get { return (RIB5 & (1 << 7)) == 1 << 7; } set { RIB5 = (byte)(RIB5 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + + public bool FatefulEncounter { get { return (Data[0x40] & 1) == 1; } set { Data[0x40] = (byte)(Data[0x40] & ~0x01 | (value ? 1 : 0)); } } + public int Gender { get { return (Data[0x40] >> 1) & 0x3; } set { Data[0x40] = (byte)(Data[0x40] & ~0x06 | (value << 1)); } } + public int AltForm { get { return Data[0x40] >> 3; } set { Data[0x40] = (byte)(Data[0x40] & 0x07 | (value << 3)); } } + public int Nature { get { return Data[0x41]; } set { Data[0x41] = (byte)value; } } + public bool HiddenAbility { get { return (Data[0x41] & 1) == 1; } set { Data[0x41] = (byte)(Data[0x41] & ~0x01 | (value ? 1 : 0)); } } + public bool NPokémon { get { return (Data[0x41] & 2) == 2; } set { Data[0x41] = (byte)(Data[0x41] & ~0x02 | (value ? 2 : 0)); } } + // 0x43-0x47 Unused + #endregion + + #region Block C + public string Nickname + { + get + { + return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x48, 22)) + .Replace("\uE08F", "\u2640") // nidoran + .Replace("\uE08E", "\u2642") // nidoran + .Replace("\u2019", "\u0027"); // farfetch'd + } + set + { + if (value.Length > 11) + value = value.Substring(0, 11); // Hard cap + string TempNick = value // Replace Special Characters and add Terminator + .Replace("\u2640", "\uE08F") // nidoran + .Replace("\u2642", "\uE08E") // nidoran + .Replace("\u0027", "\u2019") // farfetch'd + .PadRight(value.Length + 1, (char)0xFFFF); // Null Terminator + Encoding.Unicode.GetBytes(TempNick).CopyTo(Data, 0x48); + } + } + // 0x5E unused + public int Version { get { return Data[0x5F]; } set { Data[0x5F] = (byte)value; } } + private byte RIB6 { get { return Data[0x60]; } set { Data[0x60] = value; } } // Sinnoh 3 + public bool RIB6_0 { get { return (RIB6 & (1 << 0)) == 1 << 0; } set { RIB6 = (byte)(RIB6 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB6_1 { get { return (RIB6 & (1 << 1)) == 1 << 1; } set { RIB6 = (byte)(RIB6 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB6_2 { get { return (RIB6 & (1 << 2)) == 1 << 2; } set { RIB6 = (byte)(RIB6 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB6_3 { get { return (RIB6 & (1 << 3)) == 1 << 3; } set { RIB6 = (byte)(RIB6 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB6_4 { get { return (RIB6 & (1 << 4)) == 1 << 4; } set { RIB6 = (byte)(RIB6 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB6_5 { get { return (RIB6 & (1 << 5)) == 1 << 5; } set { RIB6 = (byte)(RIB6 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB6_6 { get { return (RIB6 & (1 << 6)) == 1 << 6; } set { RIB6 = (byte)(RIB6 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB6_7 { get { return (RIB6 & (1 << 7)) == 1 << 7; } set { RIB6 = (byte)(RIB6 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + private byte RIB7 { get { return Data[0x61]; } set { Data[0x61] = value; } } // Sinnoh 4 + public bool RIB7_0 { get { return (RIB7 & (1 << 0)) == 1 << 0; } set { RIB7 = (byte)(RIB7 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB7_1 { get { return (RIB7 & (1 << 1)) == 1 << 1; } set { RIB7 = (byte)(RIB7 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB7_2 { get { return (RIB7 & (1 << 2)) == 1 << 2; } set { RIB7 = (byte)(RIB7 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB7_3 { get { return (RIB7 & (1 << 3)) == 1 << 3; } set { RIB7 = (byte)(RIB7 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB7_4 { get { return (RIB7 & (1 << 4)) == 1 << 4; } set { RIB7 = (byte)(RIB7 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB7_5 { get { return (RIB7 & (1 << 5)) == 1 << 5; } set { RIB7 = (byte)(RIB7 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB7_6 { get { return (RIB7 & (1 << 6)) == 1 << 6; } set { RIB7 = (byte)(RIB7 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB7_7 { get { return (RIB7 & (1 << 7)) == 1 << 7; } set { RIB7 = (byte)(RIB7 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + private byte RIB8 { get { return Data[0x62]; } set { Data[0x62] = value; } } // Sinnoh 5 + public bool RIB8_0 { get { return (RIB8 & (1 << 0)) == 1 << 0; } set { RIB8 = (byte)(RIB8 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB8_1 { get { return (RIB8 & (1 << 1)) == 1 << 1; } set { RIB8 = (byte)(RIB8 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB8_2 { get { return (RIB8 & (1 << 2)) == 1 << 2; } set { RIB8 = (byte)(RIB8 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB8_3 { get { return (RIB8 & (1 << 3)) == 1 << 3; } set { RIB8 = (byte)(RIB8 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB8_4 { get { return (RIB8 & (1 << 4)) == 1 << 4; } set { RIB8 = (byte)(RIB8 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB8_5 { get { return (RIB8 & (1 << 5)) == 1 << 5; } set { RIB8 = (byte)(RIB8 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB8_6 { get { return (RIB8 & (1 << 6)) == 1 << 6; } set { RIB8 = (byte)(RIB8 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB8_7 { get { return (RIB8 & (1 << 7)) == 1 << 7; } set { RIB8 = (byte)(RIB8 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + private byte RIB9 { get { return Data[0x63]; } set { Data[0x63] = value; } } // Sinnoh 6 + public bool RIB9_0 { get { return (RIB9 & (1 << 0)) == 1 << 0; } set { RIB9 = (byte)(RIB9 & ~(1 << 0) | (value ? 1 << 0 : 0)); } } + public bool RIB9_1 { get { return (RIB9 & (1 << 1)) == 1 << 1; } set { RIB9 = (byte)(RIB9 & ~(1 << 1) | (value ? 1 << 1 : 0)); } } + public bool RIB9_2 { get { return (RIB9 & (1 << 2)) == 1 << 2; } set { RIB9 = (byte)(RIB9 & ~(1 << 2) | (value ? 1 << 2 : 0)); } } + public bool RIB9_3 { get { return (RIB9 & (1 << 3)) == 1 << 3; } set { RIB9 = (byte)(RIB9 & ~(1 << 3) | (value ? 1 << 3 : 0)); } } + public bool RIB9_4 { get { return (RIB9 & (1 << 4)) == 1 << 4; } set { RIB9 = (byte)(RIB9 & ~(1 << 4) | (value ? 1 << 4 : 0)); } } + public bool RIB9_5 { get { return (RIB9 & (1 << 5)) == 1 << 5; } set { RIB9 = (byte)(RIB9 & ~(1 << 5) | (value ? 1 << 5 : 0)); } } + public bool RIB9_6 { get { return (RIB9 & (1 << 6)) == 1 << 6; } set { RIB9 = (byte)(RIB9 & ~(1 << 6) | (value ? 1 << 6 : 0)); } } + public bool RIB9_7 { get { return (RIB9 & (1 << 7)) == 1 << 7; } set { RIB9 = (byte)(RIB9 & ~(1 << 7) | (value ? 1 << 7 : 0)); } } + // 0x64-0x67 Unused + #endregion + + #region Block D + public string OT_Name + { + get + { + return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x68, 16)) + .Replace("\uE08F", "\u2640") // Nidoran ♂ + .Replace("\uE08E", "\u2642") // Nidoran ♀ + .Replace("\u2019", "\u0027"); // farfetch'd + } + set + { + if (value.Length > 7) + value = value.Substring(0, 7); // Hard cap + string TempNick = value // Replace Special Characters and add Terminator + .Replace("\u2640", "\uE08F") // Nidoran ♂ + .Replace("\u2642", "\uE08E") // Nidoran ♀ + .Replace("\u0027", "\u2019") // Farfetch'd + .PadRight(value.Length + 1, (char)0xFFFF); // Null Terminator + Encoding.Unicode.GetBytes(TempNick).CopyTo(Data, 0x68); + } + } + public int Egg_Year { get { return Data[0x78]; } set { Data[0x78] = (byte)value; } } + public int Egg_Month { get { return Data[0x79]; } set { Data[0x79] = (byte)value; } } + public int Egg_Day { get { return Data[0x7A]; } set { Data[0x7A] = (byte)value; } } + public int Met_Year { get { return Data[0x7B]; } set { Data[0x7B] = (byte)value; } } + public int Met_Month { get { return Data[0x7C]; } set { Data[0x7C] = (byte)value; } } + public int Met_Day { get { return Data[0x7D]; } set { Data[0x7D] = (byte)value; } } + public int Egg_Location { get { return BitConverter.ToUInt16(Data, 0x7E); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x7E); } } + public int Met_Location { get { return BitConverter.ToUInt16(Data, 0x80); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x80); } } + private byte PKRS { get { return Data[0x82]; } set { Data[0x82] = value; } } + public int PKRS_Days { get { return PKRS & 0xF; } set { PKRS = (byte)(PKRS & ~0xF | value); } } + public int PKRS_Strain { get { return PKRS >> 4; } set { PKRS = (byte)(PKRS & 0xF | (value << 4)); } } + public int Ball { get { return Data[0x83]; } set { Data[0x83] = (byte)value; } } + public int Met_Level { get { return Data[0x84] & ~0x80; } set { Data[0x84] = (byte)((Data[0x84] & 0x80) | value); } } + public int OT_Gender { get { return Data[0x84] >> 7; } set { Data[0x84] = (byte)((Data[0x84] & ~0x80) | (value << 7)); } } + public int EncounterType { get { return Data[0x85]; } set { Data[0x85] = (byte)value; } } + // 0x86-0x87 Unused + #endregion + + // Simple Generated Attributes + public int[] IVs + { + get { return new[] { IV_HP, IV_ATK, IV_DEF, IV_SPE, IV_SPA, IV_SPD }; } + set + { + if (value == null || value.Length != 6) return; + IV_HP = value[0]; IV_ATK = value[1]; IV_DEF = value[2]; + IV_SPE = value[3]; IV_SPA = value[4]; IV_SPD = value[5]; + } + } + public int[] EVs { get { return new[] { EV_HP, EV_ATK, EV_DEF, EV_SPE, EV_SPA, EV_SPD }; } } + public int[] Moves { get { return new[] { Move1, Move2, Move3, Move4 }; } } + public int PSV { get { return (int)(((PID >> 16) ^ (PID & 0xFFFF)) >> 4); } } + public int TSV { get { return (TID ^ SID) >> 4; } } + public bool IsShiny { get { return TSV == PSV; } } + public bool PKRS_Infected { get { return PKRS_Strain > 0; } } + public bool PKRS_Cured { get { return PKRS_Days == 0 && PKRS_Strain > 0; } } + public bool Gen5 { get { return Version >= 20 && Version <= 23; } } + public bool Gen4 { get { return (Version >= 10 && Version < 12) || (Version >= 7 && Version <= 8); } } + public bool Gen3 { get { return ((Version >= 1 && Version <= 5) || Version == 15); } } + public bool GenU { get { return !(Gen5 || Gen4 || Gen3); } } + + // Complex Generated Attributes + public int Characteristic + { + get + { + // Characteristic with PID%6 + int pm6 = (int)(PID % 6); // PID MOD 6 + int maxIV = IVs.Max(); + int pm6stat = 0; + + for (int i = 0; i < 6; i++) + { + pm6stat = (pm6 + i) % 6; + if (IVs[pm6stat] == maxIV) + break; // P%6 is this stat + } + return pm6stat * 5 + maxIV % 5; + } + } + public int PotentialRating + { + get + { + int ivTotal = IVs.Sum(); + if (ivTotal <= 90) + return 0; + if (ivTotal <= 120) + return 1; + return ivTotal <= 150 ? 2 : 3; + } + } + + // Methods + public void RefreshChecksum() + { + Checksum = CalculateChecksum(); + } + public ushort CalculateChecksum() + { + ushort chk = 0; + for (int i = 8; i < SIZE_STORED; i += 2) // Loop through the entire PK6 + chk += BitConverter.ToUInt16(Data, i); + + return chk; + } + public byte[] Write() + { + RefreshChecksum(); + return Data; + } + } +} diff --git a/Misc/Util.cs b/Misc/Util.cs index 9568d8d2e..380ff2aad 100644 --- a/Misc/Util.cs +++ b/Misc/Util.cs @@ -179,6 +179,11 @@ internal static string TrimFromZero(string input) int index = input.IndexOf('\0'); return index < 0 ? input : input.Substring(0, index); } + internal static string TrimFromFFFF(string input) + { + int index = input.IndexOf((char)0xFFFF); + return index < 0 ? input : input.Substring(0, index); + } internal static string[] getStringList(string f, string l) { object txt = Properties.Resources.ResourceManager.GetObject("text_" + f + "_" + l); // Fetch File, \n to list. diff --git a/PKHeX.csproj b/PKHeX.csproj index 30a3173c4..f8b72db9f 100644 --- a/PKHeX.csproj +++ b/PKHeX.csproj @@ -73,6 +73,8 @@ + + Form