示例#1
0
        /// <summary>
        /// Gets a <see cref="PKM"/> from the provided <see cref="file"/> path, which is to be loaded to the <see cref="SaveFile"/>.
        /// </summary>
        /// <param name="file"><see cref="PKM"/> or <see cref="MysteryGift"/> file path.</param>
        /// <param name="sav">Generation Info</param>
        /// <returns>New <see cref="PKM"/> reference from the file.</returns>
        public static PKM?GetSingleFromPath(string file, ITrainerInfo sav)
        {
            var fi = new FileInfo(file);

            if (!fi.Exists)
            {
                return(null);
            }
            if (fi.Length == GP1.SIZE && TryGetGP1(File.ReadAllBytes(file), out var gp1))
            {
                return(gp1?.ConvertToPB7(sav));
            }
            if (!PKX.IsPKM(fi.Length) && !MysteryGift.IsMysteryGift(fi.Length))
            {
                return(null);
            }
            var data = File.ReadAllBytes(file);
            var ext  = fi.Extension;
            var mg   = MysteryGift.GetMysteryGift(data, ext);
            var gift = mg?.ConvertToPKM(sav);

            if (gift != null)
            {
                return(gift);
            }
            int prefer = PKX.GetPKMFormatFromExtension(ext, sav.Generation);

            return(PKMConverter.GetPKMfromBytes(data, prefer: prefer));
        }
示例#2
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);
        }
示例#3
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);
        }