Example #1
0
File: QR7.cs Project: kwsch/PKHeX
        public static byte[] GenerateQRData(PK7 pk7, int box = 0, int slot = 0, int num_copies = 1)
        {
            if (box > 31)
                box = 31;
            if (slot > 29)
                slot = 29;
            if (box < 0)
                box = 0;
            if (slot < 0)
                slot = 0;
            if (num_copies < 0)
                num_copies = 1;

            byte[] data = new byte[0x1A2];
            BitConverter.GetBytes(0x454B4F50).CopyTo(data, 0); // POKE magic
            data[0x4] = 0xFF; // QR Type
            BitConverter.GetBytes(box).CopyTo(data, 0x8);
            BitConverter.GetBytes(slot).CopyTo(data, 0xC);
            BitConverter.GetBytes(num_copies).CopyTo(data, 0x10); // No need to check max num_copies, payload parser handles it on-console.

            pk7.EncryptedPartyData.CopyTo(data, 0x30); // Copy in pokemon data
            GetRawQR(pk7.Species, pk7.AltForm, pk7.IsShiny, pk7.Gender).CopyTo(data, 0x140);
            BitConverter.GetBytes((ushort) SaveUtil.check16(data.Take(0x1A0).ToArray(), 0)).CopyTo(data, 0x1A0);
            return data;
        }
Example #2
0
        public PKM convertPK1toPK7()
        {
            if (Format != 1)
            {
                return(null);
            }
            if (Species > 151)
            {
                return(null);
            }

            var pk = new PK7();

            TransferPropertiesWithReflection(this, pk);
            pk.EVs    = new int[6];
            pk.Nature = IVs.Sum() % 25;
            pk.IVs    = new[] { 31, 31, 31, 31, 31, 31 };
            pk.RefreshChecksum();
            if (!IsNicknamed)
            {
                pk.Nickname = Nickname.ToLower();
            }
            pk.Version = -1;
            pk.Ability = PersonalTable.SM[Species].Abilities[0];
            do
            {
                PID = PKX.getRandomPID(Species, Gender, Version, Nature, AltForm, PID);
            } while (!IsShiny);
            return(pk);
        }
Example #3
0
        public static byte[] GenerateQRData(PK7 pk7, int box = 0, int slot = 0, int num_copies = 1)
        {
            if (box > 31)
            {
                box = 31;
            }
            if (slot > 29)
            {
                slot = 29;
            }
            if (box < 0)
            {
                box = 0;
            }
            if (slot < 0)
            {
                slot = 0;
            }
            if (num_copies < 0)
            {
                num_copies = 1;
            }

            byte[] data = new byte[0x1A2];
            BitConverter.GetBytes(0x454B4F50).CopyTo(data, 0); // POKE magic
            data[0x4] = 0xFF;                                  // QR Type
            BitConverter.GetBytes(box).CopyTo(data, 0x8);
            BitConverter.GetBytes(slot).CopyTo(data, 0xC);
            BitConverter.GetBytes(num_copies).CopyTo(data, 0x10); // No need to check max num_copies, payload parser handles it on-console.

            pk7.EncryptedPartyData.CopyTo(data, 0x30);            // Copy in pokemon data
            GetRawQR(pk7.Species, pk7.AltForm, pk7.IsShiny, pk7.Gender).CopyTo(data, 0x140);
            BitConverter.GetBytes(SaveUtil.CRC16(data, 0, 0x1A0)).CopyTo(data, 0x1A0);
            return(data);
        }
Example #4
0
 public static Bitmap GenerateQRCode7(PK7 pk7, int box = 0, int slot = 0, int num_copies = 1)
 {
     byte[] data = GenerateQRData(pk7, box, slot, num_copies);
     using (var generator = new QRCodeGenerator())
         using (var qr_data = generator.CreateQRCode(data))
             using (var qr_code = new QRCode(qr_data))
                 return(qr_code.GetGraphic(4));
 }
Example #5
0
        /// <summary>
        /// Creates an instance of <see cref="PKM"/> from the given data.
        /// </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>
        /// <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)
        {
            checkEncrypted(ref data);
            switch (getPKMDataFormat(data))
            {
            case 1:
                var PL1 = new PokemonList1(data, PokemonList1.CapacityType.Single, data.Length == PKX.SIZE_1JLIST);
                if (ident != null)
                {
                    PL1[0].Identifier = ident;
                }
                return(PL1[0]);

            case 2:
                var PL2 = new PokemonList2(data, PokemonList2.CapacityType.Single, data.Length == PKX.SIZE_2JLIST);
                if (ident != null)
                {
                    PL2[0].Identifier = ident;
                }
                return(PL2[0]);

            case 3:
                switch (data.Length)
                {
                case PKX.SIZE_3CSTORED: return(new CK3(data, ident));

                case PKX.SIZE_3XSTORED: return(new XK3(data, ident));

                default: return(new PK3(data, ident));
                }

            case 4:
                var pk = new PK4(data, ident);
                if (!pk.Valid || pk.Sanity != 0)
                {
                    var bk = new BK4(data, ident);
                    if (bk.Valid)
                    {
                        return(bk);
                    }
                }
                return(pk);

            case 5:
                return(new PK5(data, ident));

            case 6:
                PKM pkx = new PK6(data, ident);
                if (pkx.SM)
                {
                    pkx = new PK7(data, ident);
                }
                return(pkx);

            default:
                return(null);
            }
        }
Example #6
0
        public PKM[] GetTeam(int teamIndex)
        {
            var team = new PKM[6];
            var ofs  = offsets[teamIndex];

            for (int p = 0; p < 6; p++)
            {
                int offset = ofs + (PokeCrypto.SIZE_6PARTY * p);
                team[p] = new PK7(Data.Slice(offset, PokeCrypto.SIZE_6STORED));
            }

            return(team);
        }
Example #7
0
        public PK7 convertToPK7()
        {
            PK7 pk7 = new PK7(Data)
            {
                Markings = Markings,       // Clears old Super Training Bag & Hits Remaining
                Data     = { [0x2A] = 0 }, // Clears old Marking Value
            };

            switch (AbilityNumber)
            {
            case 1:
            case 2:
            case 4:                                           // Valid Ability Numbers
                int index = AbilityNumber >> 1;
                if (PersonalInfo.Abilities[index] == Ability) // correct pair
                {
                    pk7.Ability = pk7.PersonalInfo.Abilities[index];
                }
                break;
            }

            pk7.TradeMemory(Bank: true); // oh no, memories on gen7 pkm
            pk7.Geo1_Country = PKMConverter.Country;
            pk7.Geo1_Region  = PKMConverter.Region;

            // Bank-accurate data zeroing
            for (var i = 0x94; i < 0x9E; i++)
            {
                pk7.Data[i] = 0;                               /* Geolocations. */
            }
            for (var i = 0xAA; i < 0xB0; i++)
            {
                pk7.Data[i] = 0;                               /* Unused/Amie Fullness & Enjoyment. */
            }
            for (var i = 0xE4; i < 0xE8; i++)
            {
                pk7.Data[i] = 0;    /* Unused. */
            }
            pk7.Data[0x72] &= 0xFC; /* Clear lower two bits of Super training flags. */
            pk7.Data[0xDE]  = 0;    /* Gen IV encounter type. */

            // Fix Checksum
            pk7.RefreshChecksum();

            return(pk7); // Done!
        }
Example #8
0
        public override PKM convertToPKM(SaveFile SAV)
        {
            if (!IsPokémon)
            {
                return(null);
            }

            int currentLevel = Level > 0 ? Level : (int)(Util.rnd32() % 100 + 1);
            int metLevel     = MetLevel > 0 ? MetLevel : currentLevel;
            var pi           = PersonalTable.SM[Species];
            PK7 pk           = new PK7
            {
                Species            = Species,
                HeldItem           = HeldItem,
                TID                = TID,
                SID                = SID,
                Met_Level          = metLevel,
                Nature             = Nature != 0xFF ? Nature : (int)(Util.rnd32() % 25),
                Gender             = Gender != 3 ? Gender : pi.RandomGender,
                AltForm            = Form,
                EncryptionConstant = EncryptionConstant != 0 ? EncryptionConstant : Util.rnd32(),
                Version            = OriginGame != 0 ? OriginGame : SAV.Game,
                Language           = Language != 0 ? Language : SAV.Language,
                Ball               = Ball,
                Country            = SAV.Country,
                Region             = SAV.SubRegion,
                ConsoleRegion      = SAV.ConsoleRegion,
                Move1              = Move1, Move2 = Move2, Move3 = Move3, Move4 = Move4,
                RelearnMove1       = RelearnMove1, RelearnMove2 = RelearnMove2,
                RelearnMove3       = RelearnMove3, RelearnMove4 = RelearnMove4,
                Met_Location       = MetLocation,
                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 : SAV.OT,
                OT_Gender      = OTGender != 3 ? OTGender % 2 : SAV.Gender,
                HT_Name        = OT.Length > 0 ? SAV.OT : "",
                HT_Gender      = OT.Length > 0 ? SAV.Gender : 0,
                CurrentHandler = OT.Length > 0 ? 1 : 0,

                EXP = PKX.getEXP(currentLevel, Species),

                // Ribbons
                RibbonCountry  = RibbonCountry,
                RibbonNational = RibbonNational,

                RibbonEarth    = RibbonEarth,
                RibbonWorld    = RibbonWorld,
                RibbonClassic  = RibbonClassic,
                RibbonPremier  = RibbonPremier,
                RibbonEvent    = RibbonEvent,
                RibbonBirthday = RibbonBirthday,
                RibbonSpecial  = RibbonSpecial,
                RibbonSouvenir = RibbonSouvenir,

                RibbonWishing          = RibbonWishing,
                RibbonChampionBattle   = RibbonChampionBattle,
                RibbonChampionRegional = RibbonChampionRegional,
                RibbonChampionNational = RibbonChampionNational,
                RibbonChampionWorld    = RibbonChampionWorld,

                OT_Friendship    = pi.BaseFriendship,
                OT_Intensity     = OT_Intensity,
                OT_Memory        = OT_Memory,
                OT_TextVar       = OT_TextVar,
                OT_Feeling       = OT_Feeling,
                FatefulEncounter = true,

                EVs = EVs,
            };

            pk.Move1_PP = pk.getMovePP(Move1, 0);
            pk.Move2_PP = pk.getMovePP(Move2, 0);
            pk.Move3_PP = pk.getMovePP(Move3, 0);
            pk.Move4_PP = pk.getMovePP(Move4, 0);

            if (OTGender == 3)
            {
                pk.TID = SAV.TID;
                pk.SID = SAV.SID;
            }

            pk.MetDate = Date ?? DateTime.Now;

            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.
            int[] finalIVs = new int[6];
            switch (IVs[0])
            {
            case 0xFE:
                finalIVs[0] = 31;
                do       // 31 HP IV, 2 other 31s
                {
                    for (int i = 1; i < 6; i++)
                    {
                        finalIVs[i] = IVs[i] > 31 ? (int)(Util.rnd32() & 0x1F) : IVs[i];
                    }
                } while (finalIVs.Count(r => r == 31) < 3);     // 31 + 2*31
                break;

            case 0xFD:
                do       // 2 other 31s
                {
                    for (int i = 0; i < 6; i++)
                    {
                        finalIVs[i] = IVs[i] > 31 ? (int)(Util.rnd32() & 0x1F) : IVs[i];
                    }
                } while (finalIVs.Count(r => r == 31) < 2);     // 2*31
                break;

            default:     // Random IVs
                for (int i = 0; i < 6; i++)
                {
                    finalIVs[i] = IVs[i] > 31 ? (int)(Util.rnd32() & 0x1F) : IVs[i];
                }
                break;
            }
            pk.IVs = finalIVs;

            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;
            }
            pk.Ability       = PersonalTable.SM.getAbilities(Species, pk.AltForm)[av];
            pk.AbilityNumber = 1 << av;

            switch (PIDType)
            {
            case 00:     // Specified
                pk.PID = PID;
                break;

            case 01:     // Random
                pk.PID = Util.rnd32();
                break;

            case 02:     // Random Shiny
                pk.PID = Util.rnd32();
                pk.PID = (uint)(((TID ^ SID ^ (pk.PID & 0xFFFF)) << 16) + (pk.PID & 0xFFFF));
                break;

            case 03:     // Random Nonshiny
                do
                {
                    pk.PID = Util.rnd32();
                } while ((uint)(((TID ^ SID ^ (pk.PID & 0xFFFF)) << 16) + (pk.PID & 0xFFFF)) < 16);
                break;
            }

            if (IsEgg)
            {
                pk.IsEgg      = true;
                pk.EggMetDate = Date;
            }

            pk.RefreshChecksum();
            return(pk);
        }
Example #9
0
        public static PKM convertToFormat(PKM pk, Type PKMType, out string comment)
        {
            bool timeMachine = true;

            if (pk == null || pk.Species == 0)
            {
                comment = "Null input. Aborting.";
                return(null);
            }

            Type fromType   = pk.GetType();
            int  fromFormat = int.Parse(fromType.Name.Last().ToString());
            int  toFormat   = int.Parse(PKMType.Name.Last().ToString());

            Console.WriteLine($"Trying to convert {fromType.Name} to {PKMType.Name}.");

            PKM pkm = null;

            if (fromType == PKMType)
            {
                comment = "No need to convert, current format matches requested format.";
                return(pk);
            }
            if (fromFormat <= toFormat)
            {
                pkm = pk.Clone();
                if (pkm.IsEgg) // force hatch
                {
                    pkm.IsEgg = false;
                    if (pkm.AO)
                    {
                        pkm.Met_Location = 318; // Battle Resort
                    }
                    else if (pkm.XY)
                    {
                        pkm.Met_Location = 38; // Route 7
                    }
                    else if (pkm.Gen5)
                    {
                        pkm.Met_Location = 16; // Route 16
                    }
                    else
                    {
                        pkm.Met_Location = 30001; // Pokétransfer
                    }
                }
                switch (fromType.Name)
                {
                case nameof(PK1):
                    pkm = PKMType == typeof(PK2) ? ((PK1)pk).convertToPK2() : null;
                    if (toFormat == 2)
                    {
                        break;
                    }
                    pkm = ((PK2)pk).convertToPK3();
                    if (toFormat == 3)
                    {
                        break;
                    }
                    pkm = ((PK3)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        if (PKMType == typeof(BK4))
                        {
                            pkm = ((PK4)pkm).convertToBK4();
                        }
                        break;
                    }
                    pkm = ((PK4)pkm).convertToPK5();
                    if (toFormat == 5)
                    {
                        break;
                    }
                    pkm = ((PK5)pkm).convertToPK6();
                    if (toFormat == 6)
                    {
                        break;
                    }
                    pkm = new PK7(pkm.Data, pkm.Identifier);
                    break;

                //if (toFormat == 7)
                //    pkm = null; // pkm.convertPK1toPK7();
                //break;
                case nameof(PK2):
                    //if (PKMType == typeof(PK1))
                    //{
                    //    if (pk.Species > 151)
                    //    {
                    //        comment = $"Cannot convert a {PKX.getSpeciesName(pkm.Species, ((PK2)pkm).Japanese ? 1 : 2)} to {PKMType.Name}";
                    //        return null;
                    //    }
                    //    pkm = ((PK2)pk).convertToPK1();
                    //}
                    //else if (PKMType == typeof(PK3))
                    //{
                    //    pkm = ((PK2)pk).convertToPK3();
                    //}
                    //else
                    //    pkm = null;

                    pkm = ((PK2)pk).convertToPK3();
                    if (toFormat == 3)
                    {
                        break;
                    }
                    pkm = ((PK3)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        if (PKMType == typeof(BK4))
                        {
                            pkm = ((PK4)pkm).convertToBK4();
                        }
                        break;
                    }
                    pkm = ((PK4)pkm).convertToPK5();
                    if (toFormat == 5)
                    {
                        break;
                    }
                    pkm = ((PK5)pkm).convertToPK6();
                    if (toFormat == 6)
                    {
                        break;
                    }
                    pkm = new PK7(pkm.Data, pkm.Identifier);
                    break;

                case nameof(CK3):
                case nameof(XK3):
                    // interconverting C/XD needs to visit main series format
                    // ends up stripping purification/shadow etc stats
                    pkm = pkm.convertToPK3();
                    goto case nameof(PK3);     // fall through

                case nameof(PK3):
                    if (toFormat == 2)
                    {
                        pkm = ((PK3)pkm).convertToPK2();
                        break;
                    }
                    if (toFormat == 3)     // Gen3 Inter-trading
                    {
                        switch (PKMType.Name)
                        {
                        case nameof(CK3): pkm = pkm.convertToCK3(); break;

                        case nameof(XK3): pkm = pkm.convertToXK3(); break;

                        case nameof(PK3): pkm = pkm.convertToPK3(); break;         // already converted, instantly returns

                        default: throw new FormatException();
                        }
                        break;
                    }
                    if (fromType.Name != nameof(PK3))
                    {
                        pkm = pkm.convertToPK3();
                    }

                    pkm = ((PK3)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        break;
                    }
                    goto case nameof(PK4);

                case nameof(BK4):
                    pkm = ((BK4)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        break;
                    }
                    goto case nameof(PK4);

                case nameof(PK4):
                    if (PKMType == typeof(BK4))
                    {
                        pkm = ((PK4)pkm).convertToBK4();
                        break;
                    }
                    pkm = ((PK4)pkm).convertToPK5();
                    if (toFormat == 5)
                    {
                        break;
                    }
                    goto case nameof(PK5);

                case nameof(PK5):
                    pkm = ((PK5)pkm).convertToPK6();
                    if (toFormat == 6)
                    {
                        break;
                    }
                    goto case nameof(PK6);

                case nameof(PK6):
                    pkm = ((PK6)pkm).convertToPK7();
                    if (toFormat == 7)
                    {
                        break;
                    }
                    goto case nameof(PK7);

                case nameof(PK7):
                    break;
                }
            }

            else // fromFormat > toFormat - we're going backwards, normally unsupported - obviously not perfect, but its cool i guess
            {
                pkm = pk.Clone();
                if (pkm.IsEgg) // force hatch
                {
                    pkm.IsEgg = false;
                    if (pkm.AO)
                    {
                        pkm.Met_Location = 318; // Battle Resort
                    }
                    else if (pkm.XY)
                    {
                        pkm.Met_Location = 38; // Route 7
                    }
                    else if (pkm.Gen5)
                    {
                        pkm.Met_Location = 16; // Route 16
                    }
                    else
                    {
                        pkm.Met_Location = 30001; // Pokétransfer
                    }
                }

                switch (fromType.Name)
                {
                case "PK2":
                    if (PKMType == typeof(PK1))
                    {
                        if (pk.Species > 151)
                        {
                            comment = $"Cannot convert a {PKX.getSpeciesName(pkm.Species, ((PK2)pkm).Japanese ? 1 : 2)} to {PKMType.Name}";
                            return(null);
                        }
                        pkm = ((PK2)pk).convertToPK1();
                    }
                    else
                    {
                        pkm = null;
                    }
                    break;

                case "PK7":
                    if (
                        (toFormat == 6 && pk.Species > 721) ||
                        (toFormat == 5 && pk.Species > 649) ||
                        (toFormat == 4 && pk.Species > 493) ||
                        (toFormat == 3 && pk.Species > 386) ||
                        (toFormat == 2 && pk.Species > 251) ||
                        (toFormat == 1 && pk.Species > 151)
                        )
                    {
                        pkm = null; break;
                    }                          // how awkward im the worst

                    pkm = new PK6(pkm.Data, pkm.Identifier);
                    if (toFormat == 6)
                    {
                        break;
                    }
                    pkm = ((PK6)pkm).convertToPK5();
                    if (toFormat == 5)
                    {
                        break;
                    }
                    pkm = ((PK5)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        break;
                    }
                    pkm = ((PK4)pkm).convertToPK3();
                    if (toFormat == 3)
                    {
                        break;
                    }
                    pkm = ((PK3)pkm).convertToPK2();
                    if (toFormat == 2)
                    {
                        break;
                    }
                    pkm = ((PK2)pkm).convertToPK1();
                    break;

                case "PK6":
                    if (
                        (toFormat == 5 && pk.Species > 649) ||
                        (toFormat == 4 && pk.Species > 493) ||
                        (toFormat == 3 && pk.Species > 386) ||
                        (toFormat == 2 && pk.Species > 251) ||
                        (toFormat == 1 && pk.Species > 151)
                        )
                    {
                        pkm = null; break;
                    }                               // how awkward im the worst

                    pkm = ((PK6)pkm).convertToPK5();
                    if (toFormat == 5)
                    {
                        break;
                    }
                    pkm = ((PK5)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        break;
                    }
                    pkm = ((PK4)pkm).convertToPK3();
                    if (toFormat == 3)
                    {
                        break;
                    }
                    pkm = ((PK3)pkm).convertToPK2();
                    if (toFormat == 2)
                    {
                        break;
                    }
                    pkm = ((PK2)pkm).convertToPK1();
                    break;

                case "PK5":
                    if (
                        (toFormat == 4 && pk.Species > 493) ||
                        (toFormat == 3 && pk.Species > 386) ||
                        (toFormat == 2 && pk.Species > 251) ||
                        (toFormat == 1 && pk.Species > 151)
                        )
                    {
                        pkm = null; break;
                    }                               // how awkward im the worst

                    pkm = ((PK5)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        break;
                    }
                    pkm = ((PK4)pkm).convertToPK3();
                    if (toFormat == 3)
                    {
                        break;
                    }
                    pkm = ((PK3)pkm).convertToPK2();
                    if (toFormat == 2)
                    {
                        break;
                    }
                    pkm = ((PK2)pkm).convertToPK1();
                    break;

                case "PK4":
                    if (
                        (toFormat == 3 && pk.Species > 386) ||
                        (toFormat == 2 && pk.Species > 251) ||
                        (toFormat == 1 && pk.Species > 151)
                        )
                    {
                        pkm = null; break;
                    }                               // how awkward im the worst

                    pkm = ((PK4)pkm).convertToPK3();
                    if (toFormat == 3)
                    {
                        break;
                    }
                    pkm = ((PK3)pkm).convertToPK2();
                    if (toFormat == 2)
                    {
                        break;
                    }
                    pkm = ((PK2)pkm).convertToPK1();
                    break;

                case "CK3":
                case "XK3":
                    // interconverting C/XD needs to visit main series format
                    // ends up stripping purification/shadow etc stats
                    pkm = pkm.convertToPK3();
                    goto case "PK3";     // fall through

                case "PK3":
                    if (
                        (toFormat == 2 && pk.Species > 251) ||
                        (toFormat == 1 && pk.Species > 151)
                        )
                    {
                        pkm = null; break;
                    }                               // how awkward im the worst

                    pkm = ((PK3)pkm).convertToPK2();
                    if (toFormat == 2)
                    {
                        break;
                    }
                    pkm = ((PK2)pkm).convertToPK1();
                    break;
                }
            }

            comment = pkm == null
                ? $"Cannot convert a {fromType.Name} to a {PKMType.Name}."
                : $"Converted from {fromType.Name} to {PKMType.Name}.";

            return(pkm);
        }
Example #10
0
 /// <summary>
 /// Creates an instance of <see cref="PKM"/> from the given data.
 /// </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>
 /// <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)
 {
     checkEncrypted(ref data);
     switch (getPKMDataFormat(data))
     {
         case 1:
             var PL1 = new PokemonList1(data, PokemonList1.CapacityType.Single, data.Length == PKX.SIZE_1JLIST);
             if (ident != null)
                 PL1[0].Identifier = ident;
             return PL1[0];
         case 2:
             var PL2 = new PokemonList2(data, PokemonList2.CapacityType.Single, data.Length == PKX.SIZE_2JLIST);
             if (ident != null)
                 PL2[0].Identifier = ident;
             return PL2[0];
         case 3:
             switch (data.Length) { 
                 case PKX.SIZE_3CSTORED: return new CK3(data, ident);
                 case PKX.SIZE_3XSTORED: return new XK3(data, ident);
                 default: return new PK3(data, ident);
             }
         case 4:
             var pk = new PK4(data, ident);
             if (!pk.Valid || pk.Sanity != 0)
             {
                 var bk = new BK4(data, ident);
                 if (bk.Valid)
                     return bk;
             }
             return pk;
         case 5:
             return new PK5(data, ident);
         case 6:
             PKM pkx = new PK6(data, ident);
             if (pkx.SM)
                 pkx = new PK7(data, ident);
             return pkx;
         default:
             return null;
     }
 }
Example #11
0
        public static PKM convertToFormat(PKM pk, Type PKMType, out string comment)
        {
            if (pk == null || pk.Species == 0)
            {
                comment = "Null input. Aborting.";
                return null;
            }

            Type fromType = pk.GetType();
            int fromFormat = int.Parse(fromType.Name.Last().ToString());
            int toFormat = int.Parse(PKMType.Name.Last().ToString());
            Console.WriteLine($"Trying to convert {fromType.Name} to {PKMType.Name}.");

            PKM pkm = null;

            if (fromType == PKMType)
            {
                comment = "No need to convert, current format matches requested format.";
                return pk;
            }
            if (fromFormat <= toFormat || fromFormat == 2)
            {
                pkm = pk.Clone();
                if (pkm.IsEgg) // force hatch
                {
                    pkm.IsEgg = false;
                    if (pkm.AO)
                        pkm.Met_Location = 318; // Battle Resort
                    else if (pkm.XY)
                        pkm.Met_Location = 38; // Route 7
                    else if (pkm.Gen5)
                        pkm.Met_Location = 16; // Route 16
                    else
                        pkm.Met_Location = 30001; // Pokétransfer
                }
                switch (fromType.Name)
                {
                    case nameof(PK1):
                        if (toFormat == 2)
                        {
                            pkm = PKMType == typeof (PK2) ? ((PK1) pk).convertToPK2() : null;
                            break;
                        }
                        if (toFormat == 7)
                            pkm = null; // pkm.convertPK1toPK7();
                        break;
                    case nameof(PK2):
                        if (PKMType == typeof (PK1))
                        {
                            if (pk.Species > 151)
                            {
                                comment = $"Cannot convert a {PKX.getSpeciesName(pkm.Species, ((PK2)pkm).Japanese ? 1 : 2)} to {PKMType.Name}";
                                return null;
                            }
                            pkm = ((PK2) pk).convertToPK1();
                        }
                        else
                            pkm = null;
                        break;
                    case nameof(CK3):
                    case nameof(XK3):
                        // interconverting C/XD needs to visit main series format
                        // ends up stripping purification/shadow etc stats
                        pkm = pkm.convertToPK3();
                        goto case nameof(PK3); // fall through
                    case nameof(PK3):
                        if (toFormat == 3) // Gen3 Inter-trading
                        {
                            switch (PKMType.Name)
                            {
                                case nameof(CK3): pkm = pkm.convertToCK3(); break;
                                case nameof(XK3): pkm = pkm.convertToXK3(); break;
                                case nameof(PK3): pkm = pkm.convertToPK3(); break; // already converted, instantly returns
                                default: throw new FormatException();
                            }
                            break;
                        }
                        if (fromType.Name != nameof(PK3))
                            pkm = pkm.convertToPK3();

                        pkm = ((PK3)pkm).convertToPK4();
                        if (toFormat == 4)
                            break;
                        goto case nameof(PK4);
                    case nameof(BK4):
                        pkm = ((BK4)pkm).convertToPK4();
                        if (toFormat == 4)
                            break;
                        goto case nameof(PK4);
                    case nameof(PK4):
                        if (PKMType == typeof(BK4))
                        {
                            pkm = ((PK4)pkm).convertToBK4();
                            break;
                        }
                        pkm = ((PK4)pkm).convertToPK5();
                        if (toFormat == 5)
                            break;
                        goto case nameof(PK5);
                    case nameof(PK5):
                        pkm = ((PK5)pkm).convertToPK6();
                        if (toFormat == 6)
                            break;
                        goto case nameof(PK6);
                    case nameof(PK6):
                        pkm = new PK7(pkm.Data, pkm.Identifier);
                        if (toFormat == 7)
                            break;
                        goto case nameof(PK7);
                    case nameof(PK7):
                        break;
                }
            }

            comment = pkm == null
                ? $"Cannot convert a {fromType.Name} to a {PKMType.Name}." 
                : $"Converted from {fromType.Name} to {PKMType.Name}.";

            return pkm;
        }
Example #12
0
        public PK7 convertToPK7()
        {
            var pk7 = new PK7
            {
                EncryptionConstant = Util.rnd32(),
                Species            = Species,
                TID          = TID,
                CurrentLevel = CurrentLevel,
                EXP          = EXP,
                Met_Level    = CurrentLevel,
                Nature       = (int)(EXP % 25),
                PID          = Util.rnd32(),
                Ball         = 4,
                MetDate      = DateTime.Now,
                Version      = (int)GameVersion.RD, // Default to red, for now?
                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     = Move1_PP,
                Move2_PP     = Move2_PP,
                Move3_PP     = Move3_PP,
                Move4_PP     = Move4_PP,
                Met_Location = 30013, // "Kanto region", hardcoded.
                Gender       = PersonalTable.SM[Species].RandomGender,
                OT_Name      = PKX.getG1ConvertedString(otname, Japanese),
                IsNicknamed  = false,

                Country        = PKMConverter.Country,
                Region         = PKMConverter.Region,
                ConsoleRegion  = PKMConverter.ConsoleRegion,
                CurrentHandler = 1,
                HT_Name        = PKMConverter.OT_Name,
                HT_Gender      = PKMConverter.OT_Gender,
                Language       = PKMConverter.Language,
                Geo1_Country   = PKMConverter.Country,
                Geo1_Region    = PKMConverter.Region
            };

            pk7.Nickname = PKX.getSpeciesNameGeneration(pk7.Species, pk7.Language, pk7.Format);
            if (otname[0] == 0x5D) // Ingame Trade
            {
                var s = PKX.getG1Char(0x5D, Japanese);
                pk7.OT_Name = s.Substring(0, 1) + s.Substring(1).ToLower();
            }
            pk7.OT_Friendship = pk7.HT_Friendship = PersonalTable.SM[Species].BaseFriendship;

            // IVs
            var new_ivs  = new int[6];
            int flawless = Species == 151 ? 5 : 3;

            for (var i = 0; i < new_ivs.Length; i++)
            {
                new_ivs[i] = (int)(Util.rnd32() & 31);
            }
            for (var i = 0; i < flawless; i++)
            {
                new_ivs[i] = 31;
            }
            Util.Shuffle(new_ivs);
            pk7.IVs = new_ivs;

            // Really? :(
            if (IsShiny)
            {
                pk7.setShinyPID();
            }

            int abil = 2; // Hidden

            if (Legal.TransferSpeciesDefaultAbility_1.Contains(Species))
            {
                abil = 0;             // Reset
            }
            pk7.RefreshAbility(abil); // 0/1/2 (not 1/2/4)

            if (Species == 151)       // Mew gets special treatment.
            {
                pk7.FatefulEncounter = true;
            }
            else if (IsNicknamedBank)
            {
                pk7.IsNicknamed = true;
                pk7.Nickname    = PKX.getG1ConvertedString(nick, Japanese);
            }

            pk7.TradeMemory(Bank: true); // oh no, memories on gen7 pkm

            if (pk7.Species == 150)      // Pay Day Mewtwo
            {
                var moves = pk7.Moves;
                var index = Array.IndexOf(moves, 6);
                if (index != -1)
                {
                    moves[index] = 0;
                    pk7.Moves    = moves;
                    pk7.FixMoves();
                }
            }

            pk7.RefreshChecksum();
            return(pk7);
        }
Example #13
0
File: WC7.cs Project: kwsch/PKHeX
        public override PKM convertToPKM(SaveFile SAV)
        {
            if (!IsPokémon)
                return null;

            int currentLevel = Level > 0 ? Level : (int)(Util.rnd32()%100 + 1);
            int metLevel = MetLevel > 0 ? MetLevel : currentLevel;
            PK7 pk = new PK7
            {
                Species = Species,
                HeldItem = HeldItem,
                TID = TID,
                SID = SID,
                Met_Level = metLevel,
                Nature = Nature != 0xFF ? Nature : (int)(Util.rnd32() % 25),
                Gender = PersonalTable.AO[Species].Gender == 255 ? 2 : (Gender != 3 ? Gender : PersonalTable.AO[Species].RandomGender),
                AltForm = Form,
                EncryptionConstant = EncryptionConstant == 0 ? Util.rnd32() : EncryptionConstant,
                Version = OriginGame == 0 ? SAV.Game : OriginGame,
                Language = Language == 0 ? SAV.Language : Language,
                Ball = Ball,
                Country = SAV.Country,
                Region = SAV.SubRegion,
                ConsoleRegion = SAV.ConsoleRegion,
                Move1 = Move1, Move2 = Move2, Move3 = Move3, Move4 = Move4,
                RelearnMove1 = RelearnMove1, RelearnMove2 = RelearnMove2,
                RelearnMove3 = RelearnMove3, RelearnMove4 = RelearnMove4,
                Met_Location = MetLocation,
                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 : SAV.OT,
                OT_Gender = OTGender != 3 ? OTGender % 2 : SAV.Gender,
                HT_Name = OT.Length > 0 ? SAV.OT : "",
                HT_Gender = OT.Length > 0 ? SAV.Gender : 0,
                CurrentHandler = OT.Length > 0 ? 1 : 0,
                
                EXP = PKX.getEXP(currentLevel, Species),

                // Ribbons
                RibbonCountry = RibbonCountry,
                RibbonNational = RibbonNational,

                RibbonEarth = RibbonEarth,
                RibbonWorld = RibbonWorld,
                RibbonClassic = RibbonClassic,
                RibbonPremier = RibbonPremier,
                RibbonEvent = RibbonEvent,
                RibbonBirthday = RibbonBirthday,
                RibbonSpecial = RibbonSpecial,
                RibbonSouvenir = RibbonSouvenir,

                RibbonWishing = RibbonWishing,
                RibbonChampionBattle = RibbonChampionBattle,
                RibbonChampionRegional = RibbonChampionRegional,
                RibbonChampionNational = RibbonChampionNational,
                RibbonChampionWorld = RibbonChampionWorld,
                
                OT_Friendship = PersonalTable.AO[Species].BaseFriendship,
                OT_Intensity = OT_Intensity,
                OT_Memory = OT_Memory,
                OT_TextVar = OT_TextVar,
                OT_Feeling = OT_Feeling,
                FatefulEncounter = true,

                EVs = EVs,
            };
            pk.Move1_PP = pk.getMovePP(Move1, 0);
            pk.Move2_PP = pk.getMovePP(Move2, 0);
            pk.Move3_PP = pk.getMovePP(Move3, 0);
            pk.Move4_PP = pk.getMovePP(Move4, 0);

            if (OTGender == 3)
            {
                pk.TID = SAV.TID;
                pk.SID = SAV.SID;
            }

            pk.MetDate = Date ?? DateTime.Now;
            
            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.
            int[] finalIVs = new int[6];
            switch (IVs[0])
            {
                case 0xFE:
                    finalIVs[0] = 31;
                    do { // 31 HP IV, 2 other 31s
                    for (int i = 1; i < 6; i++)
                        finalIVs[i] = IVs[i] > 31 ? (int)(Util.rnd32() & 0x1F) : IVs[i];
                    } while (finalIVs.Count(r => r == 31) < 3); // 31 + 2*31
                    break;
                case 0xFD: 
                    do { // 2 other 31s
                    for (int i = 0; i < 6; i++)
                        finalIVs[i] = IVs[i] > 31 ? (int)(Util.rnd32() & 0x1F) : IVs[i];
                    } while (finalIVs.Count(r => r == 31) < 2); // 2*31
                    break;
                default: // Random IVs
                    for (int i = 0; i < 6; i++)
                        finalIVs[i] = IVs[i] > 31 ? (int)(Util.rnd32() & 0x1F) : IVs[i];
                    break;
            }
            pk.IVs = finalIVs;

            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;
            }
            pk.Ability = PersonalTable.SM.getAbilities(Species, pk.AltForm)[av];
            pk.AbilityNumber = 1 << av;

            switch (PIDType)
            {
                case 00: // Specified
                    pk.PID = PID;
                    break;
                case 01: // Random
                    pk.PID = Util.rnd32();
                    break;
                case 02: // Random Shiny
                    pk.PID = Util.rnd32();
                    pk.PID = (uint)(((TID ^ SID ^ (pk.PID & 0xFFFF)) << 16) + (pk.PID & 0xFFFF));
                    break;
                case 03: // Random Nonshiny
                    do { pk.PID = Util.rnd32(); } while ((uint)(((TID ^ SID ^ (pk.PID & 0xFFFF)) << 16) + (pk.PID & 0xFFFF)) < 16);
                    break;
            }

            if (IsEgg)
            {
                pk.IsEgg = true;
                pk.EggMetDate = Date;
            }

            pk.RefreshChecksum();
            return pk;
        }
Example #14
0
        public static PKM convertToFormat(PKM pk, Type PKMType, out string comment)
        {
            if (pk == null || pk.Species == 0)
            {
                comment = "Null input. Aborting.";
                return(null);
            }

            Type fromType   = pk.GetType();
            int  fromFormat = int.Parse(fromType.Name.Last().ToString());
            int  toFormat   = int.Parse(PKMType.Name.Last().ToString());

            Console.WriteLine($"Trying to convert {fromType.Name} to {PKMType.Name}.");

            PKM pkm = null;

            if (fromType == PKMType)
            {
                comment = "No need to convert, current format matches requested format.";
                return(pk);
            }
            if (fromFormat <= toFormat || fromFormat == 2)
            {
                pkm = pk.Clone();
                if (pkm.IsEgg) // force hatch
                {
                    pkm.IsEgg = false;
                    if (pkm.AO)
                    {
                        pkm.Met_Location = 318; // Battle Resort
                    }
                    else if (pkm.XY)
                    {
                        pkm.Met_Location = 38; // Route 7
                    }
                    else if (pkm.Gen5)
                    {
                        pkm.Met_Location = 16; // Route 16
                    }
                    else
                    {
                        pkm.Met_Location = 30001; // Pokétransfer
                    }
                }
                switch (fromType.Name)
                {
                case nameof(PK1):
                    if (toFormat == 2)
                    {
                        pkm = PKMType == typeof(PK2) ? ((PK1)pk).convertToPK2() : null;
                        break;
                    }
                    if (toFormat == 7)
                    {
                        pkm = null;     // pkm.convertPK1toPK7();
                    }
                    break;

                case nameof(PK2):
                    if (PKMType == typeof(PK1))
                    {
                        if (pk.Species > 151)
                        {
                            comment = $"Cannot convert a {PKX.getSpeciesName(pkm.Species, ((PK2)pkm).Japanese ? 1 : 2)} to {PKMType.Name}";
                            return(null);
                        }
                        pkm = ((PK2)pk).convertToPK1();
                    }
                    else
                    {
                        pkm = null;
                    }
                    break;

                case nameof(CK3):
                case nameof(XK3):
                    // interconverting C/XD needs to visit main series format
                    // ends up stripping purification/shadow etc stats
                    pkm = pkm.convertToPK3();
                    goto case nameof(PK3);     // fall through

                case nameof(PK3):
                    if (toFormat == 3)     // Gen3 Inter-trading
                    {
                        switch (PKMType.Name)
                        {
                        case nameof(CK3): pkm = pkm.convertToCK3(); break;

                        case nameof(XK3): pkm = pkm.convertToXK3(); break;

                        case nameof(PK3): pkm = pkm.convertToPK3(); break;         // already converted, instantly returns

                        default: throw new FormatException();
                        }
                        break;
                    }
                    if (fromType.Name != nameof(PK3))
                    {
                        pkm = pkm.convertToPK3();
                    }

                    pkm = ((PK3)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        break;
                    }
                    goto case nameof(PK4);

                case nameof(BK4):
                    pkm = ((BK4)pkm).convertToPK4();
                    if (toFormat == 4)
                    {
                        break;
                    }
                    goto case nameof(PK4);

                case nameof(PK4):
                    if (PKMType == typeof(BK4))
                    {
                        pkm = ((PK4)pkm).convertToBK4();
                        break;
                    }
                    pkm = ((PK4)pkm).convertToPK5();
                    if (toFormat == 5)
                    {
                        break;
                    }
                    goto case nameof(PK5);

                case nameof(PK5):
                    pkm = ((PK5)pkm).convertToPK6();
                    if (toFormat == 6)
                    {
                        break;
                    }
                    goto case nameof(PK6);

                case nameof(PK6):
                    pkm = new PK7(pkm.Data, pkm.Identifier);
                    if (toFormat == 7)
                    {
                        break;
                    }
                    goto case nameof(PK7);

                case nameof(PK7):
                    break;
                }
            }

            comment = pkm == null
                ? $"Cannot convert a {fromType.Name} to a {PKMType.Name}."
                : $"Converted from {fromType.Name} to {PKMType.Name}.";

            return(pkm);
        }
Example #15
0
File: QR.cs Project: kwsch/PKHeX
 // QR7 Utility
 public static Image GenerateQRCode7(PK7 pk7, int box = 0, int slot = 0, int num_copies = 1)
 {
     byte[] data = QR7.GenerateQRData(pk7, box, slot, num_copies);
     using (var generator = new QRCodeGenerator())
     using (var qr_data = generator.CreateQRCode(data))
     using (var qr_code = new QRCode(qr_data))
         return qr_code.GetGraphic(4);
 }
Example #16
0
File: PKM.cs Project: kwsch/PKHeX
        public PKM convertPK1toPK7()
        {
            if (Format != 1)
                return null;
            if (Species > 151)
                return null;

            var pk = new PK7();
            TransferPropertiesWithReflection(this, pk);
            pk.EVs = new int[6];
            pk.Nature = IVs.Sum() % 25;
            pk.IVs = new[] {31,31,31,31,31,31};
            pk.RefreshChecksum();
            if (!IsNicknamed)
                pk.Nickname = Nickname.ToLower();
            pk.Version = -1;
            pk.Ability = PersonalTable.SM[Species].Abilities[0];
            do PID = PKX.getRandomPID(Species, Gender, Version, Nature, AltForm, PID); while (!IsShiny);
            return pk;
        }