pk2pk Refactoring

Transition from modifying byte arrays to just having objects mediate the
conversion.

Farewell pk2pk!

The PK# objects can convert up to the next generation through their own
methods; doing this required having class structures for all generations
:)
This commit is contained in:
Kurt 2015-12-27 21:26:07 -08:00
parent 24703bfc22
commit 3147d8b1df
11 changed files with 1555 additions and 4480 deletions

View file

@ -49,7 +49,7 @@ namespace PKHeX
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)); }
get { return PKM.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; } }
@ -73,7 +73,7 @@ namespace PKHeX
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)); }
get { return PKM.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; } }

View file

@ -63,7 +63,7 @@ namespace PKHeX
if (!IsPokémon && Detail == 0)
{
PKM.OT_Name = "\x013A\x0135\x0132\x0149\x0142\xFFFF"; // PKHeX
PKM.OT_Name = "PKHeX";
PKM.TID = 12345;
PKM.SID = 54321;
PKM.OT_Gender = (int)(Util.rnd32()%2);
@ -80,7 +80,7 @@ namespace PKHeX
PKM.Ball = 4;
PKM.Version = 10; // Diamond
PKM.Language = 2; // English
PKM.Nickname = "\x0137\x012B\x0138\x012B\x013A\x0132\x0143\xFFFF"; // Manaphy
PKM.Nickname = "MANAPHY";
PKM.Egg_Location = 1; // Ranger (will be +3000 later)
}

250
Misc/PK3.cs Normal file
View file

@ -0,0 +1,250 @@
using System;
using System.Linq;
namespace PKHeX
{
public class PK3 // 3rd Generation PKM File
{
internal static readonly int SIZE_PARTY = 100;
internal static readonly int SIZE_STORED = 80;
internal static readonly int SIZE_BLOCK = 12;
public PK3(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
// 0x20 Intro
public uint PID { get { return BitConverter.ToUInt32(Data, 0x00); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x00); } }
public uint EID { get { return BitConverter.ToUInt32(Data, 0x04); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x04); } }
public ushort TID { get { return BitConverter.ToUInt16(Data, 0x04); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x04); } }
public ushort SID { get { return BitConverter.ToUInt16(Data, 0x06); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x06); } }
public string Nickname {
get { return PKM.getG3Str(Data.Skip(0x08).Take(10).ToArray(), Japanese); }
set { byte[] strdata = PKM.setG3Str(value, Japanese);
if (strdata.Length > 10)
Array.Resize(ref strdata, 10);
strdata.CopyTo(Data, 0x08); } }
public int Language { get { return BitConverter.ToUInt16(Data, 0x12); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x12); } }
public string OT_Name {
get { return PKM.getG3Str(Data.Skip(0x14).Take(7).ToArray(), Japanese); }
set { byte[] strdata = PKM.setG3Str(value, Japanese);
if (strdata.Length > 7)
Array.Resize(ref strdata, 7);
strdata.CopyTo(Data, 0x14); } }
private byte Markings { get { return Data[0x1B]; } set { Data[0x1B] = value; } }
public bool Circle { get { return (Markings & (1 << 0)) == 1 << 0; } set { Markings = (byte)(Markings & ~(1 << 0) | (value ? 1 << 0 : 0)); } }
public bool Square { get { return (Markings & (1 << 1)) == 1 << 1; } set { Markings = (byte)(Markings & ~(1 << 1) | (value ? 1 << 1 : 0)); } }
public bool Triangle { 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 ushort Checksum { get { return BitConverter.ToUInt16(Data, 0x1C); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x1C); } }
public ushort Sanity { get { return BitConverter.ToUInt16(Data, 0x1E); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x1E); } }
#region Block A
public int Species { get { return PKM.getG4Species(BitConverter.ToUInt16(Data, 0x20)); } set { BitConverter.GetBytes((ushort)(PKM.getG3Species(value))).CopyTo(Data, 0x20); } }
public ushort HeldItem { get { return BitConverter.ToUInt16(Data, 0x22); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x22); } }
public uint EXP { get { return BitConverter.ToUInt32(Data, 0x24); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x24); } }
private byte PPUps { get { return Data[0x28]; } set { Data[0x28] = value; } }
public int Move1_PPUps { get { return (PPUps >> 0) & 3; } set { PPUps = (byte)((PPUps & ~(3 << 0)) | value); } }
public int Move2_PPUps { get { return (PPUps >> 2) & 3; } set { PPUps = (byte)((PPUps & ~(3 << 2)) | value); } }
public int Move3_PPUps { get { return (PPUps >> 4) & 3; } set { PPUps = (byte)((PPUps & ~(3 << 4)) | value); } }
public int Move4_PPUps { get { return (PPUps >> 6) & 3; } set { PPUps = (byte)((PPUps & ~(3 << 6)) | value); } }
public int Friendship { get { return Data[0x29]; } set { Data[0x29] = (byte)value; } }
// Unused 0x2A 0x2B
#endregion
#region Block B
public int Move1 { get { return BitConverter.ToUInt16(Data, 0x2C); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x2C); } }
public int Move2 { get { return BitConverter.ToUInt16(Data, 0x2E); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x2E); } }
public int Move3 { get { return BitConverter.ToUInt16(Data, 0x30); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x30); } }
public int Move4 { get { return BitConverter.ToUInt16(Data, 0x32); } set { BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x32); } }
public int Move1_PP { get { return Data[0x34]; } set { Data[0x34] = (byte)value; } }
public int Move2_PP { get { return Data[0x35]; } set { Data[0x35] = (byte)value; } }
public int Move3_PP { get { return Data[0x36]; } set { Data[0x36] = (byte)value; } }
public int Move4_PP { get { return Data[0x37]; } set { Data[0x37] = (byte)value; } }
#endregion
#region Block C
public int EV_HP { get { return Data[0x38]; } set { Data[0x38] = (byte)value; } }
public int EV_ATK { get { return Data[0x39]; } set { Data[0x39] = (byte)value; } }
public int EV_DEF { get { return Data[0x3A]; } set { Data[0x3A] = (byte)value; } }
public int EV_SPE { get { return Data[0x3B]; } set { Data[0x3B] = (byte)value; } }
public int EV_SPA { get { return Data[0x3C]; } set { Data[0x3C] = (byte)value; } }
public int EV_SPD { get { return Data[0x3D]; } set { Data[0x3D] = (byte)value; } }
public int CNT_Cool { get { return Data[0x3E]; } set { Data[0x3E] = (byte)value; } }
public int CNT_Beauty { get { return Data[0x3F]; } set { Data[0x3F] = (byte)value; } }
public int CNT_Cute { get { return Data[0x40]; } set { Data[0x40] = (byte)value; } }
public int CNT_Smart { get { return Data[0x41]; } set { Data[0x41] = (byte)value; } }
public int CNT_Tough { get { return Data[0x42]; } set { Data[0x42] = (byte)value; } }
public int CNT_Sheen { get { return Data[0x43]; } set { Data[0x43] = (byte)value; } }
#endregion
#region Block D
private byte PKRS { get { return Data[0x44]; } set { Data[0x44] = 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 Met_Location { get { return Data[0x45]; } set { Data[0x45] = (byte)value; } }
// Origins
private ushort Origins { get { return BitConverter.ToUInt16(Data, 0x46); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x46); } }
public int Version { get { return (Origins >> 7) & 0xF; } set { Origins = (ushort)((Origins & ~0x780) | ((value & 0xF) << 7));} }
public int Pokéball { get { return (Origins >> 11) & 0xF; } set { Origins = (ushort)((Origins & ~0x7800) | ((value & 0xF) << 11)); } }
public int OT_Gender { get { return (Origins >> 15) & 1; } set { Origins = (ushort)(Origins & ~(1 << 15) | ((value & 1) << 15)); } }
public uint IV32 { get { return BitConverter.ToUInt32(Data, 0x48); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x48); } }
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 int Ability { get { return (int)((IV32 >> 31) & 1); } set { IV32 = ((IV32 & 0x7FFFFFFF) | (value == 1 ? 0x80000000 : 0)); } }
private uint RIB0 { get { return BitConverter.ToUInt32(Data, 0x4C); } set { BitConverter.GetBytes(value).CopyTo(Data, 0x4C); } }
public int Cool_Ribbons { get { return (int)(RIB0 >> 00) & 7; } set { RIB0 = (uint)((RIB0 & ~(7 << 00)) | (uint)(value & 7)); } }
public int Beauty_Ribbons{ get { return (int)(RIB0 >> 03) & 7; } set { RIB0 = (uint)((RIB0 & ~(7 << 03)) | (uint)(value & 7)); } }
public int Cute_Ribbons { get { return (int)(RIB0 >> 06) & 7; } set { RIB0 = (uint)((RIB0 & ~(7 << 06)) | (uint)(value & 7)); } }
public int Smart_Ribbons { get { return (int)(RIB0 >> 09) & 3; } set { RIB0 = (uint)((RIB0 & ~(7 << 09)) | (uint)(value & 7)); } }
public int Tough_Ribbons { get { return (int)(RIB0 >> 12) & 3; } set { RIB0 = (uint)((RIB0 & ~(7 << 12)) | (uint)(value & 7)); } }
public bool Champion { get { return (RIB0 & (1 << 15)) == 1 << 15; } set { RIB0 = (uint)(RIB0 & ~(1 << 15) | (uint)(value ? 1 << 0 : 0)); } }
public bool Winning { get { return (RIB0 & (1 << 16)) == 1 << 16; } set { RIB0 = (uint)(RIB0 & ~(1 << 16) | (uint)(value ? 1 << 0 : 0)); } }
public bool Victory { get { return (RIB0 & (1 << 17)) == 1 << 17; } set { RIB0 = (uint)(RIB0 & ~(1 << 17) | (uint)(value ? 1 << 0 : 0)); } }
public bool Artist { get { return (RIB0 & (1 << 18)) == 1 << 18; } set { RIB0 = (uint)(RIB0 & ~(1 << 18) | (uint)(value ? 1 << 0 : 0)); } }
public bool Effort { get { return (RIB0 & (1 << 19)) == 1 << 19; } set { RIB0 = (uint)(RIB0 & ~(1 << 19) | (uint)(value ? 1 << 0 : 0)); } }
public bool Special1 { get { return (RIB0 & (1 << 20)) == 1 << 20; } set { RIB0 = (uint)(RIB0 & ~(1 << 20) | (uint)(value ? 1 << 0 : 0)); } }
public bool Special2 { get { return (RIB0 & (1 << 21)) == 1 << 21; } set { RIB0 = (uint)(RIB0 & ~(1 << 21) | (uint)(value ? 1 << 0 : 0)); } }
public bool Special3 { get { return (RIB0 & (1 << 22)) == 1 << 22; } set { RIB0 = (uint)(RIB0 & ~(1 << 22) | (uint)(value ? 1 << 0 : 0)); } }
public bool Special4 { get { return (RIB0 & (1 << 23)) == 1 << 23; } set { RIB0 = (uint)(RIB0 & ~(1 << 23) | (uint)(value ? 1 << 0 : 0)); } }
public bool Special5 { get { return (RIB0 & (1 << 24)) == 1 << 24; } set { RIB0 = (uint)(RIB0 & ~(1 << 24) | (uint)(value ? 1 << 0 : 0)); } }
public bool Special6 { get { return (RIB0 & (1 << 25)) == 1 << 25; } set { RIB0 = (uint)(RIB0 & ~(1 << 25) | (uint)(value ? 1 << 0 : 0)); } }
public bool Special7 { get { return (RIB0 & (1 << 26)) == 1 << 26; } set { RIB0 = (uint)(RIB0 & ~(1 << 26) | (uint)(value ? 1 << 0 : 0)); } }
public bool Unused1 { get { return (RIB0 & (1 << 27)) == 1 << 27; } set { RIB0 = (uint)(RIB0 & ~(1 << 27) | (uint)(value ? 1 << 0 : 0)); } }
public bool Unused2 { get { return (RIB0 & (1 << 28)) == 1 << 28; } set { RIB0 = (uint)(RIB0 & ~(1 << 28) | (uint)(value ? 1 << 0 : 0)); } }
public bool Unused3 { get { return (RIB0 & (1 << 29)) == 1 << 29; } set { RIB0 = (uint)(RIB0 & ~(1 << 29) | (uint)(value ? 1 << 0 : 0)); } }
public bool Unused4 { get { return (RIB0 & (1 << 30)) == 1 << 30; } set { RIB0 = (uint)(RIB0 & ~(1 << 30) | (uint)(value ? 1 << 0 : 0)); } }
public bool Obedience { get { return (RIB0 & (1 << 31)) == 1 << 31; } set { RIB0 = ((RIB0 & ~(1 << 31)) | (uint)(value ? 1 << 0 : 0)); } }
#endregion
// Simple Generated Attributes
public bool Japanese { get { return Language == 1; } }
public bool Gen3 { get { return ((Version >= 1 && Version <= 5) || Version == 15); } }
public PK4 convertToPK4()
{
DateTime moment = DateTime.Now;
PK4 pk4 = new PK4 // Convert away!
{
PID = PID,
Species = Species,
TID = TID,
SID = SID,
EXP = IsEgg ? PKX.getEXP(5, Species) : EXP,
IsEgg = false,
Friendship = 40,
Circle = Circle,
Square = Square,
Triangle = Triangle,
Heart = Heart,
Language = Language,
EV_HP = EV_HP,
EV_ATK = EV_ATK,
EV_DEF = EV_DEF,
EV_SPA = EV_SPA,
EV_SPD = EV_SPD,
EV_SPE = EV_SPE,
CNT_Cool = CNT_Cool,
CNT_Beauty = CNT_Beauty,
CNT_Cute = CNT_Cute,
CNT_Smart = CNT_Smart,
CNT_Tough = CNT_Tough,
CNT_Sheen = CNT_Sheen,
FatefulEncounter = Obedience,
Move1 = Move1,
Move2 = Move2,
Move3 = Move3,
Move4 = Move4,
Move1_PPUps = Move1_PPUps,
Move2_PPUps = Move2_PPUps,
Move3_PPUps = Move3_PPUps,
Move4_PPUps = Move4_PPUps,
Move1_PP = PKX.getMovePP(Move1, Move1_PPUps),
Move2_PP = PKX.getMovePP(Move2, Move2_PPUps),
Move3_PP = PKX.getMovePP(Move3, Move3_PPUps),
Move4_PP = PKX.getMovePP(Move4, Move4_PPUps),
IV_HP = IV_HP,
IV_ATK = IV_ATK,
IV_DEF = IV_DEF,
IV_SPA = IV_SPA,
IV_SPD = IV_SPD,
IV_SPE = IV_SPE,
Ability = PKM.Gen3Abilities[Species][Ability],
Version = Version,
Ball = Pokéball,
PKRS_Strain = PKRS_Strain,
PKRS_Days = PKRS_Days,
OT_Gender = OT_Gender,
Met_Year = (moment.Year - 2000),
Met_Month = moment.Month,
Met_Day = moment.Day,
Met_Location = 0x37, // Pal Park
RIB6_4 = Champion,
RIB6_5 = Winning,
RIB6_6 = Victory,
RIB6_7 = Artist,
RIB7_0 = Effort,
RIB7_1 = Special1, // Battle Champion Ribbon
RIB7_2 = Special2, // Regional Champion Ribbon
RIB7_3 = Special3, // National Champion Ribbon
RIB7_4 = Special4, // Country Ribbon
RIB7_5 = Special5, // National Ribbon
RIB7_6 = Special6, // Earth Ribbon
RIB7_7 = Special7, // World Ribbon
};
// Remaining Ribbons
pk4.RIB4_0 |= Cool_Ribbons > 0;
pk4.RIB4_1 |= Cool_Ribbons > 1;
pk4.RIB4_2 |= Cool_Ribbons > 2;
pk4.RIB4_3 |= Cool_Ribbons > 3;
pk4.RIB4_4 |= Beauty_Ribbons > 0;
pk4.RIB4_5 |= Beauty_Ribbons > 1;
pk4.RIB4_6 |= Beauty_Ribbons > 2;
pk4.RIB4_7 |= Beauty_Ribbons > 3;
pk4.RIB5_0 |= Cute_Ribbons > 0;
pk4.RIB5_1 |= Cute_Ribbons > 1;
pk4.RIB5_2 |= Cute_Ribbons > 2;
pk4.RIB5_3 |= Cute_Ribbons > 3;
pk4.RIB5_4 |= Smart_Ribbons > 0;
pk4.RIB5_5 |= Smart_Ribbons > 1;
pk4.RIB5_6 |= Smart_Ribbons > 2;
pk4.RIB5_7 |= Smart_Ribbons > 3;
pk4.RIB6_0 |= Tough_Ribbons > 0;
pk4.RIB6_1 |= Tough_Ribbons > 1;
pk4.RIB6_2 |= Tough_Ribbons > 2;
pk4.RIB6_3 |= Tough_Ribbons > 3;
// Yay for reusing string buffers!
PKM.G4TransferTrashBytes[pk4.Language].CopyTo(pk4.Data, 0x48 + 4);
pk4.Nickname = IsEgg ? PKM.getSpeciesName(pk4.Species, pk4.Language) : Nickname;
Array.Copy(pk4.Data, 0x48, pk4.Data, 0x68, 0x10);
pk4.OT_Name = OT_Name;
// Ribbons
// Set Final Data
pk4.Met_Level = PKX.getLevel(pk4.Species, pk4.EXP);
pk4.Gender = PKM.getGender(pk4.Species, pk4.PID);
pk4.IsNicknamed |= (pk4.Nickname != PKM.getSpeciesName(pk4.Species, pk4.Language));
pk4.RefreshChecksum();
return pk4;
}
}
}

View file

@ -1,14 +1,13 @@
using System;
using System.Linq;
using System.Text;
namespace PKHeX
{
// 5th Generation PKM File
public class PK4
public class PK4 // 4th Generation PKM File
{
internal static readonly int SIZE_PARTY = 236;
internal static readonly int SIZE_STORED = 136;
internal static readonly int SIZE_BLOCK = 32;
public PK4(byte[] decryptedData = null, string ident = null)
{
@ -168,7 +167,7 @@ namespace PKHeX
{
get
{
return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x48, 22))
return PKM.array2strG4(Data.Skip(0x48).Take(22).ToArray())
.Replace("\uE08F", "\u2640") // nidoran
.Replace("\uE08E", "\u2642") // nidoran
.Replace("\u2019", "\u0027"); // farfetch'd
@ -180,9 +179,8 @@ namespace PKHeX
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);
.Replace("\u0027", "\u2019"); // farfetch'd
PKM.str2arrayG4(TempNick).CopyTo(Data, 0x48);
}
}
// 0x5E unused
@ -231,10 +229,10 @@ namespace PKHeX
{
get
{
return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x68, 16))
return PKM.array2strG4(Data.Skip(0x68).Take(16).ToArray())
.Replace("\uE08F", "\u2640") // Nidoran ♂
.Replace("\uE08E", "\u2642") // Nidoran ♀
.Replace("\u2019", "\u0027"); // farfetch'd
.Replace("\u2019", "\u0027"); // Farfetch'd
}
set
{
@ -243,9 +241,8 @@ namespace PKHeX
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);
.Replace("\u0027", "\u2019"); // Farfetch'd
PKM.str2arrayG4(TempNick).CopyTo(Data, 0x68);
}
}
public int Egg_Year { get { return Data[0x78]; } set { Data[0x78] = (byte)value; } }
@ -263,7 +260,8 @@ namespace PKHeX
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
public int HGSSBall { get { return Data[0x86]; } set { Data[0x86] = (byte)value; } }
// Unused 0x87
#endregion
// Simple Generated Attributes
@ -355,5 +353,56 @@ namespace PKHeX
return false;
}
public PK5 convertToPK5()
{
// Double Check Location Data to see if we're already a PK5
if (Data[0x5F] < 0x10 && BitConverter.ToUInt16(Data, 0x80) > 0x4000)
return new PK5(Data);
DateTime moment = DateTime.Now;
PK5 pk5 = new PK5(Data) // Convert away!
{
HeldItem = 0,
Friendship = 70,
// Apply new met date
Met_Year = moment.Year - 2000,
Met_Month = moment.Month,
Met_Day = moment.Day
};
// Fix PP
pk5.Move1_PP = PKX.getMovePP(pk5.Move1_PP, pk5.Move1_PPUps);
pk5.Move2_PP = PKX.getMovePP(pk5.Move2_PP, pk5.Move2_PPUps);
pk5.Move3_PP = PKX.getMovePP(pk5.Move3_PP, pk5.Move3_PPUps);
pk5.Move4_PP = PKX.getMovePP(pk5.Move4_PP, pk5.Move4_PPUps);
// Disassociate Nature and PID
pk5.Nature = (int)(pk5.PID % 0x19);
// Delete Platinum/HGSS Met Location Data
BitConverter.GetBytes((uint)0).CopyTo(pk5.Data, 0x44);
// Met / Crown Data Detection
pk5.Met_Location = pk5.FatefulEncounter && Array.IndexOf(new[] {251, 243, 244, 245}, pk5.Species) >= 0
? ((pk5.Species == 251) ? 30010 : 30012) // Celebi : Beast
: 30001; // Pokétransfer (not Crown)
// Delete HGSS Data
BitConverter.GetBytes((ushort)0).CopyTo(pk5.Data, 0x86);
if (HGSSBall > 0 && HGSSBall != 4)
pk5.Ball = HGSSBall;
// Transfer Nickname and OT Name
pk5.Nickname = Nickname;
pk5.OT_Name = OT_Name;
// Fix Level
pk5.Met_Level = PKX.getLevel(pk5.Species, pk5.EXP);
pk5.RefreshChecksum();
return pk5;
}
}
}

View file

@ -4,11 +4,11 @@ using System.Text;
namespace PKHeX
{
// 5th Generation PKM File
public class PK5
public class PK5 // 5th Generation PKM File
{
internal static readonly int SIZE_PARTY = 220;
internal static readonly int SIZE_STORED = 136;
internal static readonly int SIZE_BLOCK = 32;
public PK5(byte[] decryptedData = null, string ident = null)
{
@ -168,7 +168,7 @@ namespace PKHeX
{
get
{
return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x48, 22))
return PKM.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x48, 22))
.Replace("\uE08F", "\u2640") // nidoran
.Replace("\uE08E", "\u2642") // nidoran
.Replace("\u2019", "\u0027"); // farfetch'd
@ -231,7 +231,7 @@ namespace PKHeX
{
get
{
return Util.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x68, 16))
return PKM.TrimFromFFFF(Encoding.Unicode.GetString(Data, 0x68, 16))
.Replace("\uE08F", "\u2640") // Nidoran ♂
.Replace("\uE08E", "\u2642") // Nidoran ♀
.Replace("\u2019", "\u0027"); // farfetch'd
@ -354,5 +354,216 @@ namespace PKHeX
return true;
return false;
}
public PK6 convertToPK6()
{
PK6 pk6 = new PK6 // Convert away!
{
EncryptionConstant = PID,
Species = Species,
TID = TID,
SID = SID,
EXP = EXP,
PID = PID,
Ability = Ability
};
int abilnum = PKX.getAbilityNumber(Species, Ability, AltForm);
if (abilnum > 0) pk6.AbilityNumber = abilnum;
else // Fallback (shouldn't happen)
{
if (HiddenAbility) pk6.AbilityNumber = 4; // Hidden, else G5 or G3/4 correlation.
else pk6.AbilityNumber = Gen5 ? 1 << (int)(PID >> 16 & 1) : 1 << (int)(PID & 1);
}
pk6.Circle = Circle;
pk6.Square = Square;
pk6.Triangle = Triangle;
pk6.Heart = Heart;
pk6.Star = Star;
pk6.Diamond = Diamond;
pk6.Language = Language;
pk6.CNT_Cool = CNT_Cool;
pk6.CNT_Beauty = CNT_Beauty;
pk6.CNT_Cute = CNT_Cute;
pk6.CNT_Smart = CNT_Smart;
pk6.CNT_Tough = CNT_Tough;
// Cap EVs
pk6.EV_HP = EV_HP > 252 ? 252 : EV_HP;
pk6.EV_ATK = EV_ATK > 252 ? 252 : EV_ATK;
pk6.EV_DEF = EV_DEF > 252 ? 252 : EV_DEF;
pk6.EV_SPA = EV_SPA > 252 ? 252 : EV_SPA;
pk6.EV_SPD = EV_SPD > 252 ? 252 : EV_SPD;
pk6.EV_SPE = EV_SPE > 252 ? 252 : EV_SPE;
pk6.Move1 = Move1;
pk6.Move2 = Move2;
pk6.Move3 = Move3;
pk6.Move4 = Move4;
pk6.Move1_PP = PKX.getMovePP(Move1, Move1_PPUps);
pk6.Move2_PP = PKX.getMovePP(Move2, Move2_PPUps);
pk6.Move3_PP = PKX.getMovePP(Move3, Move3_PPUps);
pk6.Move4_PP = PKX.getMovePP(Move4, Move4_PPUps);
pk6.Move1_PPUps = Move1_PPUps;
pk6.Move2_PPUps = Move2_PPUps;
pk6.Move3_PPUps = Move3_PPUps;
pk6.Move4_PPUps = Move4_PPUps;
pk6.IV_HP = IV_HP;
pk6.IV_ATK = IV_ATK;
pk6.IV_DEF = IV_DEF;
pk6.IV_SPA = IV_SPA;
pk6.IV_SPD = IV_SPD;
pk6.IV_SPE = IV_SPE;
pk6.IsEgg = IsEgg;
pk6.IsNicknamed = IsNicknamed;
pk6.FatefulEncounter = FatefulEncounter;
pk6.Nature = Nature;
pk6.Nickname = Nickname.Length > 1 && !IsNicknamed
? Nickname[0] + Nickname.Substring(1).ToLower() // Decapitalize
: Nickname;
pk6.Version = Version;
pk6.OT_Name = OT_Name;
// Dates are kept upon transfer
pk6.Met_Year = Met_Year;
pk6.Met_Month = Met_Month;
pk6.Met_Day = Met_Day;
pk6.Egg_Year = Egg_Year;
pk6.Egg_Month = Egg_Month;
pk6.Egg_Day = Egg_Day;
// Locations are kept upon transfer
pk6.Met_Location = Met_Location;
pk6.Egg_Location = Egg_Location;
pk6.PKRS_Strain = PKRS_Strain;
pk6.PKRS_Days = PKRS_Days;
pk6.Ball = Ball;
// OT Gender & Encounter Level
pk6.Met_Level = Met_Level;
pk6.OT_Gender = OT_Gender;
pk6.EncounterType = EncounterType;
// Ribbon Decomposer (Contest & Battle)
byte contestribbons = 0;
byte battleribbons = 0;
// Contest Ribbon Counter
for (int i = 0; i < 8; i++) // Sinnoh 3, Hoenn 1
{
if (((Data[0x60] >> i) & 1) == 1) contestribbons++;
if (((Data[0x61] >> i) & 1) == 1) contestribbons++;
if (((Data[0x3C] >> i) & 1) == 1) contestribbons++;
if (((Data[0x3D] >> i) & 1) == 1) contestribbons++;
}
for (int i = 0; i < 4; i++) // Sinnoh 4, Hoenn 2
{
if (((Data[0x62] >> i) & 1) == 1) contestribbons++;
if (((Data[0x3E] >> i) & 1) == 1) contestribbons++;
}
// Battle Ribbon Counter
// Winning Ribbon
if ((Data[0x3E] & 0x20) >> 5 == 1) battleribbons++;
// Victory Ribbon
if ((Data[0x3E] & 0x40) >> 6 == 1) battleribbons++;
for (int i = 1; i < 7; i++) // Sinnoh Battle Ribbons
if (((Data[0x24] >> i) & 1) == 1) battleribbons++;
// Fill the Ribbon Counter Bytes
pk6.Memory_ContestCount = contestribbons;
pk6.Memory_BattleCount = battleribbons;
// Copy Ribbons to their new locations.
int bx30 = 0;
// bx30 |= 0; // Kalos Champ - New Kalos Ribbon
bx30 |= (((Data[0x3E] & 0x10) >> 4) << 1); // Hoenn Champion
bx30 |= (((Data[0x24] & 0x01) >> 0) << 2); // Sinnoh Champ
// bx30 |= 0; // Best Friend - New Kalos Ribbon
// bx30 |= 0; // Training - New Kalos Ribbon
// bx30 |= 0; // Skillful - New Kalos Ribbon
// bx30 |= 0; // Expert - New Kalos Ribbon
bx30 |= (((Data[0x3F] & 0x01) >> 0) << 7); // Effort Ribbon
pk6.Data[0x30] = (byte)bx30;
int bx31 = 0;
bx31 |= (((Data[0x24] & 0x80) >> 7) << 0); // Alert
bx31 |= (((Data[0x25] & 0x01) >> 0) << 1); // Shock
bx31 |= (((Data[0x25] & 0x02) >> 1) << 2); // Downcast
bx31 |= (((Data[0x25] & 0x04) >> 2) << 3); // Careless
bx31 |= (((Data[0x25] & 0x08) >> 3) << 4); // Relax
bx31 |= (((Data[0x25] & 0x10) >> 4) << 5); // Snooze
bx31 |= (((Data[0x25] & 0x20) >> 5) << 6); // Smile
bx31 |= (((Data[0x25] & 0x40) >> 6) << 7); // Gorgeous
pk6.Data[0x31] = (byte)bx31;
int bx32 = 0;
bx32 |= (((Data[0x25] & 0x80) >> 7) << 0); // Royal
bx32 |= (((Data[0x26] & 0x01) >> 0) << 1); // Gorgeous Royal
bx32 |= (((Data[0x3E] & 0x80) >> 7) << 2); // Artist
bx32 |= (((Data[0x26] & 0x02) >> 1) << 3); // Footprint
bx32 |= (((Data[0x26] & 0x04) >> 2) << 4); // Record
bx32 |= (((Data[0x26] & 0x10) >> 4) << 5); // Legend
bx32 |= (((Data[0x3F] & 0x10) >> 4) << 6); // Country
bx32 |= (((Data[0x3F] & 0x20) >> 5) << 7); // National
pk6.Data[0x32] = (byte)bx32;
int bx33 = 0;
bx33 |= (((Data[0x3F] & 0x40) >> 6) << 0); // Earth
bx33 |= (((Data[0x3F] & 0x80) >> 7) << 1); // World
bx33 |= (((Data[0x27] & 0x04) >> 2) << 2); // Classic
bx33 |= (((Data[0x27] & 0x08) >> 3) << 3); // Premier
bx33 |= (((Data[0x26] & 0x08) >> 3) << 4); // Event
bx33 |= (((Data[0x26] & 0x40) >> 6) << 5); // Birthday
bx33 |= (((Data[0x26] & 0x80) >> 7) << 6); // Special
bx33 |= (((Data[0x27] & 0x01) >> 0) << 7); // Souvenir
pk6.Data[0x33] = (byte)bx33;
int bx34 = 0;
bx34 |= (((Data[0x27] & 0x02) >> 1) << 0); // Wishing Ribbon
bx34 |= (((Data[0x3F] & 0x02) >> 1) << 1); // Battle Champion
bx34 |= (((Data[0x3F] & 0x04) >> 2) << 2); // Regional Champion
bx34 |= (((Data[0x3F] & 0x08) >> 3) << 3); // National Champion
bx34 |= (((Data[0x26] & 0x20) >> 5) << 4); // World Champion
pk6.Data[0x34] = (byte)bx34;
// Write Transfer Location - location is dependent on 3DS system that transfers.
pk6.Country = Converter.Country;
pk6.Region = Converter.Region;
pk6.ConsoleRegion = Converter.ConsoleRegion;
// Write the Memories, Friendship, and Origin!
pk6.CurrentHandler = 1;
pk6.HT_Name = Converter.OT_Name;
pk6.Geo1_Region = Converter.Region;
pk6.Geo1_Country = Converter.Country;
pk6.HT_Intensity = 1;
pk6.HT_Memory = 4;
pk6.HT_Feeling = (int)(Util.rnd32() % 10);
// When transferred, friendship gets reset.
pk6.OT_Friendship = pk6.HT_Friendship = PKX.getBaseFriendship(Species);
// Antishiny Mechanism
ushort LID = (ushort)(PID & 0xFFFF);
ushort HID = (ushort)(PID >> 0x10);
int XOR = TID ^ SID ^ LID ^ HID;
if (XOR >= 8 && XOR < 16) // If we get an illegal collision...
pk6.PID ^= 0x80000000;
// Fix Checksum
pk6.RefreshChecksum();
return pk6; // Done!
}
}
}

1006
Misc/PKM.cs Normal file

File diff suppressed because it is too large Load diff

View file

@ -264,6 +264,14 @@ namespace PKHeX
{
return Personal[Personal[species].FormeIndex(species, formnum)].Abilities;
}
internal static int getAbilityNumber(int species, int ability, int formnum)
{
byte[] spec_abilities = Personal[Personal[species].FormeIndex(species, formnum)].Abilities;
int abilval = Array.IndexOf(spec_abilities, ability);
if (abilval >= 0)
return 1 << abilval;
return -1;
}
internal static int getGender(string s)
{
if (s == null)

View file

@ -179,11 +179,6 @@ namespace PKHeX
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.

File diff suppressed because it is too large Load diff

View file

@ -69,11 +69,13 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Misc\PGT.cs" />
<Compile Include="Misc\PK3.cs" />
<Compile Include="Misc\PK4.cs" />
<Compile Include="Misc\Legal.cs" />
<Compile Include="Misc\PGF.cs" />
<Compile Include="Misc\PK5.cs" />
<Compile Include="Misc\PK6.cs" />
<Compile Include="Misc\PKM.cs" />
<Compile Include="Misc\QR.cs">
<SubType>Form</SubType>
</Compile>
@ -155,7 +157,6 @@
<Compile Include="SAV\frmReport.Designer.cs">
<DependentUpon>frmReport.cs</DependentUpon>
</Compile>
<Compile Include="Misc\pk2pk.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SAV\SAV_BerryField.cs">

View file

@ -141,7 +141,6 @@ namespace PKHeX
public static SAV6 SAV = new SAV6();
public static byte[] originalSAV; // original save for CyberGadget Codes
public static byte[] ramsav; // original ramsav for ramsav exporting
public static pk2pk Converter = new pk2pk();
public static string pathSDF;
public static string path3DS;
@ -504,7 +503,7 @@ namespace PKHeX
if (!PKX.verifychk(input)) Util.Error("Invalid File (Checksum Error)");
try // to convert g5pkm
{
byte[] data = Converter.ConvertPKM(input);
byte[] data = Converter.ConvertPKMtoPK6(input);
Array.Resize(ref data, PK6.SIZE_STORED);
populateFields(data);
}
@ -542,7 +541,7 @@ namespace PKHeX
}
setPKXBoxes();
Width = largeWidth;
Util.Alert("Box Binary loaded.");
Util.Alert("Box Binary loaded.");
}
#endregion
#region injectiondebug
@ -617,7 +616,7 @@ namespace PKHeX
(pk == null ? "Not a Pokémon PGF." : "Invalid species."));
return;
}
populateFields(Converter.ConvertPKM(pk.Data));
populateFields(Converter.ConvertPKMtoPK6(pk.Data));
}
else if ((input.Length == PGT.Size && ext == ".pgt"))
{
@ -629,7 +628,7 @@ namespace PKHeX
(pk == null ? "Not a Pokémon PGT." : "Invalid species."));
return;
}
populateFields(Converter.ConvertPKM(pk.Data));
populateFields(Converter.ConvertPKMtoPK6(pk.Data));
}
else if (input.Length == PCD.Size && ext == ".pcd")
{
@ -642,7 +641,7 @@ namespace PKHeX
(pk == null ? "Not a Pokémon PCD." : "Invalid species."));
return;
}
populateFields(Converter.ConvertPKM(pk.Data));
populateFields(Converter.ConvertPKMtoPK6(pk.Data));
}
#endregion
else
@ -2715,7 +2714,7 @@ namespace PKHeX
}
private void refreshTrainerInfo()
{
Converter.setG6TrainerInfo(SAV.SubRegion, SAV.Country, SAV.ConsoleRegion, SAV.OT, SAV.Gender);
Converter.updateConfig(SAV.SubRegion, SAV.Country, SAV.ConsoleRegion, SAV.OT, SAV.Gender);
}
private void clickExportParty(object sender, EventArgs e)
{
@ -2975,7 +2974,7 @@ namespace PKHeX
if (!PKX.verifychk(input)) continue;
{
try // to convert g5pkm
{ data = PKX.encryptArray(Converter.ConvertPKM(input)); pastctr++; }
{ data = PKX.encryptArray(Converter.ConvertPKMtoPK6(input)); pastctr++; }
catch { continue; }
}
}
@ -3362,7 +3361,7 @@ namespace PKHeX
if (!PKX.verifychk(input)) Util.Alert("Invalid File Loaded.", "Checksum is not valid.");
try // to convert past gen pkm
{
byte[] data = Converter.ConvertPKM(input);
byte[] data = Converter.ConvertPKMtoPK6(input);
SAV.setPK6Stored(new PK6(data), offset);
}
catch