Beispiel #1
0
 public PK5(byte[] decryptedData = null, string ident = null)
 {
     Data = (byte[])(decryptedData ?? new byte[SIZE_PARTY]).Clone();
     PKMConverter.checkEncrypted(ref Data);
     Identifier = ident;
     if (Data.Length != SIZE_PARTY)
     {
         Array.Resize(ref Data, SIZE_PARTY);
     }
 }
Beispiel #2
0
 public PK7(byte[] decryptedData, string ident = null)
 {
     Data = decryptedData;
     PKMConverter.CheckEncrypted(ref Data, Format);
     Identifier = ident;
     if (Data.Length != SIZE_PARTY)
     {
         Array.Resize(ref Data, SIZE_PARTY);
     }
 }
Beispiel #3
0
        public static IEnumerable <IEncounterable> GetLearn(int spec, int[] moveIDs)
        {
            var blank = PKMConverter.GetBlank(PKX.Generation);

            blank.Species = spec;

            var vers = GameUtil.GameVersions;

            return(EncounterMovesetGenerator.GenerateEncounters(blank, moveIDs, vers));
        }
Beispiel #4
0
 public PB7(byte[] decryptedData = null, string ident = null)
 {
     Data = decryptedData ?? new byte[SIZE];
     PKMConverter.CheckEncrypted(ref Data, 7);
     Identifier = ident;
     if (Data.Length != SIZE)
     {
         Array.Resize(ref Data, SIZE);
     }
 }
Beispiel #5
0
        /// <summary>
        /// Gets the <see cref="PKM"/> data from the message that is encoded in a QR.
        /// </summary>
        /// <param name="message">QR Message</param>
        /// <param name="format">Preferred <see cref="PKM.Format"/> to expect.</param>
        /// <returns>Decoded <see cref="PKM"/> object, null if invalid.</returns>
        public static PKM?GetPKM(string message, int format)
        {
            var data = DecodeMessagePKM(message);

            if (data == null)
            {
                return(null);
            }
            return(PKMConverter.GetPKMfromBytes(data, format));
        }
Beispiel #6
0
        protected BulkStorage(byte[] data, Type t, int start, int slotsPerBox = 30) : base(data)
        {
            Box         = start;
            SlotsPerBox = slotsPerBox;

            blank = PKMConverter.GetBlank(t);
            var slots = (Data.Length - Box) / blank.SIZE_STORED;

            BoxCount = slots / SlotsPerBox;

            GetIsPKMPresent = PKX.GetFuncIsPKMPresent(blank);
        }
Beispiel #7
0
        /// <summary>
        /// Tries to get an <see cref="PKM"/> object from the input parameters.
        /// </summary>
        /// <param name="data">Binary data</param>
        /// <param name="pk">Output result</param>
        /// <param name="ext">Format hint</param>
        /// <param name="sav">Reference savefile used for PC Binary compatibility checks.</param>
        /// <returns>True if file object reference is valid, false if none found.</returns>
        public static bool TryGetPKM(byte[] data, out PKM?pk, string ext, ITrainerInfo?sav = null)
        {
            if (ext == ".pgt") // size collision with pk6
            {
                pk = null;
                return(false);
            }
            var format = PKX.GetPKMFormatFromExtension(ext, sav?.Generation ?? 6);

            pk = PKMConverter.GetPKMfromBytes(data, prefer: format);
            return(pk != null);
        }
Beispiel #8
0
        public PKM ConvertToPKM(ITrainerInfo sav, EncounterCriteria criteria)
        {
            int gen     = Generation;
            var version = Version;
            var pk      = PKMConverter.GetBlank(gen, version);

            sav.ApplyTo(pk);

            pk.Species       = Species;
            pk.Nickname      = SpeciesName.GetSpeciesNameGeneration(Species, sav.Language, gen);
            pk.CurrentLevel  = Level;
            pk.Version       = (int)version;
            pk.Ball          = (int)Ball.Poke;
            pk.OT_Friendship = pk.PersonalInfo.BaseFriendship;

            int[] moves = SetEncounterMoves(pk, version);
            SetPINGA(pk, criteria);

            if (gen <= 2 && version != GameVersion.C)
            {
                return(pk);
            }

            SetMetData(pk);

            if (gen < 3)
            {
                return(pk);
            }

            if (gen >= 4)
            {
                pk.SetEggMetData(version, (GameVersion)sav.Game);
            }

            if (gen < 6)
            {
                return(pk);
            }
            if (gen == 6)
            {
                pk.SetHatchMemory6();
            }

            SetAltForm(pk, sav);

            pk.SetRandomEC();
            pk.RelearnMoves = moves;

            return(pk);
        }
Beispiel #9
0
        /// <summary>
        /// Checks a <see cref="PKM"/> file for compatibility to the <see cref="SaveFile"/>.
        /// </summary>
        /// <param name="sav"><see cref="SaveFile"/> that is being checked.</param>
        /// <param name="pk"><see cref="PKM"/> that is being tested for compatibility.</param>
        public static bool IsCompatiblePKM(this SaveFile sav, PKM pk)
        {
            if (sav.PKMType != pk.GetType())
            {
                return(false);
            }

            if (sav is ILangDeviantSave il && PKMConverter.IsIncompatibleGB(pk, il.Japanese, pk.Japanese))
            {
                return(false);
            }

            return(true);
        }
Beispiel #10
0
        public static IEnumerable <PKM> GetPKMsFromPaths(IEnumerable <string> files, int generation)
        {
            var result = files
                         .Where(file => PKX.IsPKM(new FileInfo(file).Length))
                         .Select(File.ReadAllBytes)
                         .Select(data => PKMConverter.GetPKMfromBytes(data, prefer: generation));

            foreach (var pkm in result)
            {
                if (pkm?.Species is > 0)
                {
                    yield return(pkm);
                }
            }
        }
Beispiel #11
0
        protected BulkStorage(byte[] data, Type t, int start, int slotsPerBox = 30)
        {
            Box         = start;
            Data        = data;
            SlotsPerBox = slotsPerBox;

            blank = PKMConverter.GetBlank(t);
            var slots = (Data.Length - Box) / blank.SIZE_STORED;

            BoxCount = slots / SlotsPerBox;

            Exportable = !IsRangeEmpty(0, Data.Length);
            BAK        = (byte[])Data.Clone();

            GetIsPKMPresent = PKX.GetFuncIsPKMPresent(blank);
        }
Beispiel #12
0
 /// <summary>
 /// Gets a compatible <see cref="PKM"/> for editing with a new <see cref="SaveFile"/>.
 /// </summary>
 /// <param name="sav">SaveFile to receive the compatible <see cref="pk"/></param>
 /// <param name="pk">Current Pokémon being edited</param>
 /// <returns>Current Pokémon, assuming conversion is possible. If conversion is not possible, a blank <see cref="PKM"/> will be obtained from the <see cref="sav"/>.</returns>
 public static PKM GetCompatiblePKM(this SaveFile sav, PKM pk)
 {
     if (pk.Format >= 3 || sav.Generation >= 7)
     {
         return(PKMConverter.ConvertToType(pk, sav.PKMType, out _) ?? sav.BlankPKM);
     }
     // gen1-2 compatibility check
     if (pk.Japanese != ((ILangDeviantSave)sav).Japanese)
     {
         return(sav.BlankPKM);
     }
     if (sav is SAV2 s2 && s2.Korean != pk.Korean)
     {
         return(sav.BlankPKM);
     }
     return(PKMConverter.ConvertToType(pk, sav.PKMType, out _) ?? sav.BlankPKM);
 }
Beispiel #13
0
        /// <summary>
        /// Gets all encounters where a <see cref="species"/> can learn all input <see cref="moves"/>.
        /// </summary>
        public static IEnumerable <IEncounterable> GetLearn(int species, int[] moves)
        {
            if (species <= 0)
            {
                return(Array.Empty <IEncounterable>());
            }
            if (moves.Any(z => z < 0))
            {
                return(Array.Empty <IEncounterable>());
            }

            var blank = PKMConverter.GetBlank(PKX.Generation);

            blank.Species = species;

            var vers = GameUtil.GameVersions;

            return(EncounterMovesetGenerator.GenerateEncounters(blank, moves, vers));
        }
Beispiel #14
0
 /// <summary>
 /// Gets a compatible <see cref="PKM"/> for editing with a new <see cref="SaveFile"/>.
 /// </summary>
 /// <param name="sav">SaveFile to receive the compatible <see cref="pk"/></param>
 /// <param name="pk">Current Pokémon being edited</param>
 /// <returns>Current Pokémon, assuming conversion is possible. If conversion is not possible, a blank <see cref="PKM"/> will be obtained from the <see cref="sav"/>.</returns>
 public static PKM GetCompatiblePKM(this SaveFile sav, PKM pk = null)
 {
     if (pk == null)
     {
         return(sav.BlankPKM);
     }
     if (pk.Format < 3 && sav.Generation < 7)
     {
         // gen1-2 compatibility check
         if (pk.Japanese != sav.Japanese)
         {
             return(sav.BlankPKM);
         }
         if (sav is SAV2 s2 && s2.Korean != pk.Korean)
         {
             return(sav.BlankPKM);
         }
     }
     return(PKMConverter.ConvertToType(pk, sav.PKMType, out _) ?? sav.BlankPKM);
 }
Beispiel #15
0
        public static List <PKM> GetLivingDex(ITrainerInfo tr, IEnumerable <int> speciesToGenerate, PKM blank)
        {
            var result   = new List <PKM>();
            var destType = blank.GetType();

            foreach (var s in speciesToGenerate)
            {
                var pk = blank.Clone();
                pk.Species = s;
                pk.Gender  = pk.GetSaneGender();

                var pi = pk.PersonalInfo;
                for (int i = 0; i < pi.FormeCount; i++)
                {
                    pk.AltForm = i;
                    if (s == (int)Species.Indeedee || s == (int)Species.Meowstic)
                    {
                        pk.Gender = i;
                    }

                    var f = EncounterMovesetGenerator.GeneratePKMs(pk, tr).FirstOrDefault();
                    if (f == null)
                    {
                        continue;
                    }
                    var converted = PKMConverter.ConvertToType(f, destType, out _);
                    if (converted == null)
                    {
                        continue;
                    }

                    converted.CurrentLevel = 100;
                    converted.Species      = s;
                    converted.AltForm      = i;

                    result.Add(converted);
                }
            }

            return(result);
        }
Beispiel #16
0
        /// <summary>
        /// Gets a blank file for the save file. If the template path exists, a template load will be attempted.
        /// </summary>
        /// <param name="sav">Save File to fetch a template for</param>
        /// <param name="templatePath">Path to look for a template in</param>
        /// <returns>Template if it exists, or a blank <see cref="PKM"/> from the <see cref="sav"/></returns>
        public static PKM LoadTemplate(this SaveFile sav, string templatePath = null)
        {
            var blank = sav.BlankPKM;

            if (!Directory.Exists(templatePath))
            {
                return(blank);
            }

            var    di   = new DirectoryInfo(templatePath);
            string path = Path.Combine(templatePath, $"{di.Name}.{blank.Extension}");

            if (!File.Exists(path) || !PKX.IsPKM(new FileInfo(path).Length))
            {
                return(blank);
            }

            var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(path), prefer: blank.Format);

            return(PKMConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out path) ?? blank); // no sneaky plz; reuse string
        }
Beispiel #17
0
        public static void AddFromLocalFile(string file, ConcurrentBag <SlotCache> db, ITrainerInfo dest, ICollection <string> validExtensions)
        {
            var fi = new FileInfo(file);

            if (!validExtensions.Contains(fi.Extension) || !PKX.IsPKM(fi.Length))
            {
                return;
            }

            var data   = File.ReadAllBytes(file);
            var prefer = PKX.GetPKMFormatFromExtension(fi.Extension, dest.Generation);
            var pk     = PKMConverter.GetPKMfromBytes(data, prefer);

            if (pk?.Species is not > 0)
            {
                return;
            }

            var info  = new SlotInfoFile(file);
            var entry = new SlotCache(info, pk);

            db.Add(entry);
        }
Beispiel #18
0
        /// <summary>
        /// Gets a blank file for the save file. If the template path exists, a template load will be attempted.
        /// </summary>
        /// <param name="sav">Save File to fetch a template for</param>
        /// <param name="templatePath">Path to look for a template in</param>
        /// <returns>Template if it exists, or a blank <see cref="PKM"/> from the <see cref="sav"/></returns>
        public static PKM LoadTemplate(this SaveFile sav, string templatePath)
        {
            if (!Directory.Exists(templatePath))
            {
                return(LoadTemplate(sav));
            }

            var    di   = new DirectoryInfo(templatePath);
            string path = Path.Combine(templatePath, $"{di.Name}.{sav.PKMType.Name.ToLower()}");

            if (!File.Exists(path) || !PKX.IsPKM(new FileInfo(path).Length))
            {
                return(LoadTemplate(sav));
            }

            var pk = PKMConverter.GetPKMfromBytes(File.ReadAllBytes(path), prefer: sav.Generation);

            if (pk == null)
            {
                return(LoadTemplate(sav));
            }

            return(PKMConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out _) ?? LoadTemplate(sav));
        }
Beispiel #19
0
 /// <summary>
 /// Checks if the <see cref="PKM"/> is compatible with the input <see cref="SaveFile"/>, and makes any necessary modifications to force compatibility.
 /// </summary>
 /// <remarks>Should only be used when forcing a backwards conversion to sanitize the PKM fields to the target format.
 /// If the PKM is compatible, some properties may be forced to sanitized values.</remarks>
 /// <param name="SAV">Save File target that the PKM will be injected.</param>
 /// <param name="pk">PKM input that is to be injected into the Save File.</param>
 /// <returns>Indication whether or not the PKM is compatible.</returns>
 public static bool IsPKMCompatibleWithModifications(this SaveFile SAV, PKM pk) => PKMConverter.IsPKMCompatibleWithModifications(pk, SAV);
Beispiel #20
0
 protected override PKM GetPKM(byte[] data) => PKMConverter.GetPKMfromBytes(data, prefer: Generation);
Beispiel #21
0
        public PKM ConvertToPKM(ITrainerInfo SAV)
        {
            int gen = Math.Max(2, Version.GetGeneration());
            var pk  = PKMConverter.GetBlank(gen);

            SAV.ApplyToPKM(pk);

            pk.Species      = Species;
            pk.Nickname     = PKX.GetSpeciesNameGeneration(Species, SAV.Language, gen);
            pk.CurrentLevel = Level;
            pk.Version      = (int)Version;

            var moves = Legal.GetEggMoves(pk, Species, pk.AltForm, Version);

            if (moves.Length == 0)
            {
                moves = Legal.GetEncounterMoves(pk, Level, Version);
            }
            pk.Moves = moves;
            pk.SetMaximumPPCurrent(moves);
            pk.OT_Friendship = pk.PersonalInfo.BaseFriendship;

            pk.SetRandomIVs(flawless: 3);

            if (pk.Format <= 2 && Version != GameVersion.C)
            {
                return(pk);
            }

            pk.Met_Level    = EncounterSuggestion.GetSuggestedEncounterEggMetLevel(pk);
            pk.Met_Location = Math.Max(0, EncounterSuggestion.GetSuggestedEggMetLocation(pk));

            if (pk.Format < 3)
            {
                return(pk);
            }

            pk.Ball = 4;

            int gender = Util.Rand.Next(2);
            int nature = Util.Rand.Next(25);

            gender = pk.GetSaneGender(gender);
            if (pk.Format <= 5)
            {
                pk.SetPIDGender(gender);
                pk.SetPIDNature(nature);
                pk.RefreshAbility(pk.PIDAbility);
            }
            else
            {
                pk.PID    = Util.Rand32();
                pk.Nature = nature;
                pk.Gender = gender;
                pk.RefreshAbility(Util.Rand.Next(2));
            }

            bool traded = (int)Version == SAV.Game;
            var  today  = pk.MetDate = DateTime.Today;

            if (pk.GenNumber >= 4)
            {
                pk.Egg_Location = EncounterSuggestion.GetSuggestedEncounterEggLocationEgg(pk, traded);
                pk.EggMetDate   = today;
            }

            if (pk.Format < 6)
            {
                return(pk);
            }
            if (pk.Gen6)
            {
                pk.SetHatchMemory6();
            }

            pk.SetRandomEC();
            pk.RelearnMoves = moves;

            return(pk);
        }
Beispiel #22
0
        public PKM ConvertToPKM(ITrainerInfo SAV)
        {
            int gen = Version.GetGeneration();

            if (gen < 2)
            {
                gen     = 2;
                Version = GameVersion.C;
            }
            var pk = PKMConverter.GetBlank(gen);

            SAV.ApplyToPKM(pk);

            pk.Species      = Species;
            pk.Nickname     = PKX.GetSpeciesNameGeneration(Species, SAV.Language, gen);
            pk.CurrentLevel = Level;
            pk.Version      = (int)Version;
            int[] moves = GetCurrentEggMoves(pk);
            pk.Moves = moves;
            pk.SetMaximumPPCurrent(moves);
            pk.OT_Friendship = pk.PersonalInfo.BaseFriendship;

            pk.SetRandomIVs(flawless: 3);

            if (pk.Format <= 2 && Version != GameVersion.C)
            {
                return(pk);
            }

            pk.Met_Level    = EncounterSuggestion.GetSuggestedEncounterEggMetLevel(pk);
            pk.Met_Location = Math.Max(0, EncounterSuggestion.GetSuggestedEggMetLocation(pk));

            if (pk.Format < 3)
            {
                return(pk);
            }

            pk.Ball = 4;

            int gender = pk.PersonalInfo.RandomGender;
            int nature = Util.Rand.Next(25);

            if (pk.Format <= 5)
            {
                pk.SetPIDGender(gender);
                pk.Gender = gender;
                pk.SetPIDNature(nature);
                pk.Nature = nature;
                pk.RefreshAbility(pk.PIDAbility);
            }
            else
            {
                pk.PID    = Util.Rand32();
                pk.Nature = nature;
                pk.Gender = gender;
                pk.RefreshAbility(Util.Rand.Next(2));
            }

            bool traded = (int)Version == SAV.Game;
            var  today  = pk.MetDate = DateTime.Today;

            if (pk.GenNumber >= 4)
            {
                pk.Egg_Location = EncounterSuggestion.GetSuggestedEncounterEggLocationEgg(pk, traded);
                pk.EggMetDate   = today;
            }

            if (pk.Format < 6)
            {
                return(pk);
            }
            if (pk.Gen6)
            {
                pk.SetHatchMemory6();
            }

            switch (Species)
            {
            case 774:     // Minior
                pk.AltForm = Util.Rand.Next(7, 14);
                break;

            case 664:     // Scatterbug
                pk.AltForm = Legal.GetVivillonPattern(SAV.Country, SAV.SubRegion);
                break;
            }

            pk.SetRandomEC();
            pk.RelearnMoves = moves;

            return(pk);
        }