Пример #1
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="prefer">Optional identifier for the preferred generation.  Usually the generation of the destination save 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, int prefer = 7)
        {
            int format = GetPKMDataFormat(data);

            switch (format)
            {
            case 1:
                var list1 = new PokeList1(data);
                return(list1[0]);

            case 2:
                if (data.Length == PokeCrypto.SIZE_2STADIUM)
                {
                    return(new SK2(data));
                }

                var list2 = new PokeList2(data);
                return(list2[0]);

            case 3:
                return(data.Length switch
                {
                    PokeCrypto.SIZE_3CSTORED => new CK3(data),
                    PokeCrypto.SIZE_3XSTORED => new XK3(data),
                    _ => new PK3(data)
                });
Пример #2
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>
        /// <param name="prefer">Optional identifier for the preferred generation.  Usually the generation of the destination save 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, int prefer = 7)
        {
            int format = GetPKMDataFormat(data);

            switch (format)
            {
            case 1:
                var PL1 = new PokeList1(data);
                if (ident != null)
                {
                    PL1[0].Identifier = ident;
                }
                return(PL1[0]);

            case 2:
                var PL2 = new PokeList2(data);
                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:
                var pkx = new PK6(data, ident);
                return(CheckPKMFormat7(pkx, prefer));

            default:
                return(null);
            }
        }
Пример #3
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="prefer">Optional identifier for the preferred generation.  Usually the generation of the destination save 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, int prefer = 7)
        {
            int format = GetPKMDataFormat(data);

            switch (format)
            {
            case 1:
                var PL1 = new PokeList1(data);
                return(PL1[0]);

            case 2:
                var PL2 = new PokeList2(data);
                return(PL2[0]);

            case 3:
                return(data.Length switch
                {
                    PokeCrypto.SIZE_3CSTORED => new CK3(data),
                    PokeCrypto.SIZE_3XSTORED => new XK3(data),
                    _ => new PK3(data)
                });
Пример #4
0
        public SAV2(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
        {
            Data       = data ?? new byte[SaveUtil.SIZE_G2RAW_U];
            BAK        = (byte[])Data.Clone();
            Exportable = !IsRangeEmpty(0, Data.Length);

            if (data == null)
            {
                Version = GameVersion.C;
            }
            else if (versionOverride != GameVersion.Any)
            {
                Version = versionOverride;
            }
            else
            {
                Version = SaveUtil.GetIsG2SAV(Data);
            }

            if (Version == GameVersion.Invalid)
            {
                return;
            }

            Japanese = SaveUtil.GetIsG2SAVJ(Data) != GameVersion.Invalid;
            if (!Japanese)
            {
                Korean = SaveUtil.GetIsG2SAVK(Data) != GameVersion.Invalid;
            }

            Box = Data.Length;
            Array.Resize(ref Data, Data.Length + SIZE_RESERVED);
            Party = GetPartyOffset(0);

            Personal = Version == GameVersion.GS ? PersonalTable.GS : PersonalTable.C;

            Offsets = new SAV2Offsets(this);

            LegalItems    = Legal.Pouch_Items_GSC;
            LegalBalls    = Legal.Pouch_Ball_GSC;
            LegalKeyItems = Version == GameVersion.C ? Legal.Pouch_Key_C : Legal.Pouch_Key_GS;
            LegalTMHMs    = Legal.Pouch_TMHM_GSC;
            HeldItems     = Legal.HeldItems_GSC;

            // Stash boxes after the save file's end.
            int splitAtIndex = (Japanese ? 6 : 7);
            int stored       = SIZE_STOREDBOX;
            int baseDest     = Data.Length - SIZE_RESERVED;
            var capacity     = Japanese ? PokeListType.StoredJP : PokeListType.Stored;

            for (int i = 0; i < BoxCount; i++)
            {
                int ofs     = GetBoxRawDataOffset(i, splitAtIndex);
                var box     = GetData(ofs, stored);
                var boxDest = baseDest + (i * SIZE_BOX);
                var boxPL   = new PokeList2(box, capacity, Japanese);
                for (int j = 0; j < boxPL.Pokemon.Length; j++)
                {
                    var dest  = boxDest + (j * SIZE_STORED);
                    var pkDat = (j < boxPL.Count)
                        ? new PokeList2(boxPL[j]).Write()
                        : new byte[PokeList2.GetDataLength(PokeListType.Single, Japanese)];
                    pkDat.CopyTo(Data, dest);
                }
            }

            var current  = GetData(Offsets.CurrentBox, stored);
            var curBoxPL = new PokeList2(current, capacity, Japanese);
            var curDest  = baseDest + (CurrentBox * SIZE_BOX);

            for (int i = 0; i < curBoxPL.Pokemon.Length; i++)
            {
                var dest  = curDest + (i * SIZE_STORED);
                var pkDat = i < curBoxPL.Count
                    ? new PokeList2(curBoxPL[i]).Write()
                    : new byte[PokeList2.GetDataLength(PokeListType.Single, Japanese)];
                pkDat.CopyTo(Data, dest);
            }

            var party   = GetData(Offsets.Party, SIZE_STOREDPARTY);
            var partyPL = new PokeList2(party, PokeListType.Party, Japanese);

            for (int i = 0; i < partyPL.Pokemon.Length; i++)
            {
                var dest  = GetPartyOffset(i);
                var pkDat = i < partyPL.Count
                    ? new PokeList2(partyPL[i]).Write()
                    : new byte[PokeList2.GetDataLength(PokeListType.Single, Japanese)];
                pkDat.CopyTo(Data, dest);
            }

            if (Offsets.Daycare >= 0)
            {
                int offset = Offsets.Daycare;

                DaycareFlags[0] = Data[offset]; offset++;
                var pk1      = ReadPKMFromOffset(offset);    // parent 1
                var daycare1 = new PokeList2(pk1);
                offset         += (StringLength * 2) + 0x20; // nick/ot/pkm
                DaycareFlags[1] = Data[offset]; offset++;
                byte steps = Data[offset]; offset++;
                byte BreedMotherOrNonDitto = Data[offset]; offset++;
                var  pk2      = ReadPKMFromOffset(offset);       // parent 2
                var  daycare2 = new PokeList2(pk2);
                offset += (StringLength * 2) + PKX.SIZE_2STORED; // nick/ot/pkm
                var pk3 = ReadPKMFromOffset(offset);             // egg!
                pk3.IsEgg = true;
                var daycare3 = new PokeList2(pk3);

                daycare1.Write().CopyTo(Data, GetPartyOffset(7 + (0 * 2)));
                daycare2.Write().CopyTo(Data, GetPartyOffset(7 + (1 * 2)));
                daycare3.Write().CopyTo(Data, GetPartyOffset(7 + (2 * 2)));
                Daycare = Offsets.Daycare;
            }

            // Enable Pokedex editing
            PokeDex   = 0;
            EventFlag = Offsets.EventFlag;

            if (!Exportable)
            {
                ClearBoxes();
            }
        }
Пример #5
0
        protected override byte[] GetFinalData()
        {
            int splitAtIndex = (Japanese ? 6 : 7);

            for (int i = 0; i < BoxCount; i++)
            {
                var boxPL = new PokeList2(Japanese ? PokeListType.StoredJP : PokeListType.Stored, Japanese);
                int slot  = 0;
                for (int j = 0; j < boxPL.Pokemon.Length; j++)
                {
                    PK2 boxPK = (PK2)GetPKM(GetData(GetBoxOffset(i) + (j * SIZE_STORED), SIZE_STORED));
                    if (boxPK.Species > 0)
                    {
                        boxPL[slot++] = boxPK;
                    }
                }

                int src = GetBoxRawDataOffset(i, splitAtIndex);
                boxPL.Write().CopyTo(Data, src);
                if (i == CurrentBox)
                {
                    boxPL.Write().CopyTo(Data, Offsets.CurrentBox);
                }
            }

            var partyPL = new PokeList2(PokeListType.Party, Japanese);
            int pSlot   = 0;

            for (int i = 0; i < 6; i++)
            {
                PK2 partyPK = (PK2)GetPKM(GetData(GetPartyOffset(i), SIZE_STORED));
                if (partyPK.Species > 0)
                {
                    partyPL[pSlot++] = partyPK;
                }
            }
            partyPL.Write().CopyTo(Data, Offsets.Party);

            SetChecksums();
            if (Japanese)
            {
                switch (Version)
                {
                case GameVersion.GS: Array.Copy(Data, Offsets.Trainer1, Data, 0x7209, 0xC83); break;

                case GameVersion.C:  Array.Copy(Data, Offsets.Trainer1, Data, 0x7209, 0xADA); break;
                }
            }
            else if (Korean)
            {
                // Calculate oddball checksum
                ushort     sum         = 0;
                ushort[][] offsetpairs =
                {
                    new ushort[] { 0x106B, 0x1533 },
                    new ushort[] { 0x1534, 0x1A12 },
                    new ushort[] { 0x1A13, 0x1C38 },
                    new ushort[] { 0x3DD8, 0x3F79 },
                    new ushort[] { 0x7E39, 0x7E6A },
                };
                foreach (ushort[] p in offsetpairs)
                {
                    for (int i = p[0]; i < p[1]; i++)
                    {
                        sum += Data[i];
                    }
                }
                BitConverter.GetBytes(sum).CopyTo(Data, 0x7E6B);
            }
            else
            {
                switch (Version)
                {
                case GameVersion.GS:
                    Array.Copy(Data, 0x2009, Data, 0x15C7, 0x222F - 0x2009);
                    Array.Copy(Data, 0x222F, Data, 0x3D69, 0x23D9 - 0x222F);
                    Array.Copy(Data, 0x23D9, Data, 0x0C6B, 0x2856 - 0x23D9);
                    Array.Copy(Data, 0x2856, Data, 0x7E39, 0x288A - 0x2856);
                    Array.Copy(Data, 0x288A, Data, 0x10E8, 0x2D69 - 0x288A);
                    break;

                case GameVersion.C:
                    Array.Copy(Data, 0x2009, Data, 0x1209, 0xB7A);
                    break;
                }
            }
            byte[] outData = new byte[Data.Length - SIZE_RESERVED];
            Array.Copy(Data, outData, outData.Length);
            return(outData);
        }
Пример #6
0
        private void Initialize()
        {
            Box = Data.Length;
            Array.Resize(ref Data, Data.Length + SIZE_RESERVED);
            Party = GetPartyOffset(0);

            // Stash boxes after the save file's end.
            int splitAtIndex = (Japanese ? 6 : 7);
            int stored       = SIZE_STOREDBOX;
            int baseDest     = Data.Length - SIZE_RESERVED;
            var capacity     = Japanese ? PokeListType.StoredJP : PokeListType.Stored;

            for (int i = 0; i < BoxCount; i++)
            {
                int ofs     = GetBoxRawDataOffset(i, splitAtIndex);
                var box     = GetData(ofs, stored);
                var boxDest = baseDest + (i * SIZE_BOX);
                var boxPL   = new PokeList2(box, capacity, Japanese);
                for (int j = 0; j < boxPL.Pokemon.Length; j++)
                {
                    var dest  = boxDest + (j * SIZE_STORED);
                    var pkDat = (j < boxPL.Count)
                        ? new PokeList2(boxPL[j]).Write()
                        : new byte[PokeList2.GetDataLength(PokeListType.Single, Japanese)];
                    pkDat.CopyTo(Data, dest);
                }
            }

            var current  = GetData(Offsets.CurrentBox, stored);
            var curBoxPL = new PokeList2(current, capacity, Japanese);
            var curDest  = baseDest + (CurrentBox * SIZE_BOX);

            for (int i = 0; i < curBoxPL.Pokemon.Length; i++)
            {
                var dest  = curDest + (i * SIZE_STORED);
                var pkDat = i < curBoxPL.Count
                    ? new PokeList2(curBoxPL[i]).Write()
                    : new byte[PokeList2.GetDataLength(PokeListType.Single, Japanese)];
                pkDat.CopyTo(Data, dest);
            }

            var party   = GetData(Offsets.Party, SIZE_STOREDPARTY);
            var partyPL = new PokeList2(party, PokeListType.Party, Japanese);

            for (int i = 0; i < partyPL.Pokemon.Length; i++)
            {
                var dest  = GetPartyOffset(i);
                var pkDat = i < partyPL.Count
                    ? new PokeList2(partyPL[i]).Write()
                    : new byte[PokeList2.GetDataLength(PokeListType.Single, Japanese)];
                pkDat.CopyTo(Data, dest);
            }

            if (Offsets.Daycare >= 0)
            {
                int offset = Offsets.Daycare;

                DaycareFlags[0] = Data[offset];
                offset++;
                var pk1      = ReadPKMFromOffset(offset);    // parent 1
                var daycare1 = new PokeList2(pk1);
                offset         += (StringLength * 2) + 0x20; // nick/ot/pkm
                DaycareFlags[1] = Data[offset];
                offset++;
                //byte steps = Data[offset];
                offset++;
                //byte BreedMotherOrNonDitto = Data[offset];
                offset++;
                var pk2      = ReadPKMFromOffset(offset);               // parent 2
                var daycare2 = new PokeList2(pk2);
                offset += (StringLength * 2) + PokeCrypto.SIZE_2STORED; // nick/ot/pkm
                var pk3 = ReadPKMFromOffset(offset);                    // egg!
                pk3.IsEgg = true;
                var daycare3 = new PokeList2(pk3);

                daycare1.Write().CopyTo(Data, GetPartyOffset(7 + (0 * 2)));
                daycare2.Write().CopyTo(Data, GetPartyOffset(7 + (1 * 2)));
                daycare3.Write().CopyTo(Data, GetPartyOffset(7 + (2 * 2)));
                DaycareOffset = Offsets.Daycare;
            }

            // Enable Pokedex editing
            PokeDex    = 0;
            EventFlag  = Offsets.EventFlag;
            EventConst = Offsets.EventConst;
        }