2016-06-26 21:23:41 +00:00
|
|
|
|
using System;
|
2017-09-30 05:58:25 +00:00
|
|
|
|
using System.Collections.Generic;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-10-24 06:12:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generation 3 <see cref="SaveFile"/> object.
|
|
|
|
|
/// </summary>
|
2016-06-26 21:23:41 +00:00
|
|
|
|
public sealed class SAV3 : SaveFile
|
|
|
|
|
{
|
2018-07-30 04:51:45 +00:00
|
|
|
|
protected override string BAKText => $"{OT} ({Version}) - {PlayTimeString}";
|
2016-10-17 14:49:40 +00:00
|
|
|
|
public override string Filter => "SAV File|*.sav|All Files|*.*";
|
2016-06-28 06:03:57 +00:00
|
|
|
|
public override string Extension => ".sav";
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
|
|
|
|
/* SAV3 Structure:
|
|
|
|
|
* 0xE000 per save file
|
|
|
|
|
* 14 blocks @ 0x1000 each.
|
|
|
|
|
* Blocks do not use all 0x1000 bytes allocated.
|
|
|
|
|
* Via: http://bulbapedia.bulbagarden.net/wiki/Save_data_structure_in_Generation_III
|
|
|
|
|
*/
|
2017-04-29 01:03:20 +00:00
|
|
|
|
private const int SIZE_BLOCK = 0x1000;
|
|
|
|
|
private const int BLOCK_COUNT = 14;
|
|
|
|
|
private const int SIZE_RESERVED = 0x10000; // unpacked box data will start after the save data
|
2018-07-08 16:56:01 +00:00
|
|
|
|
private static readonly int[] chunkLength =
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2018-07-08 16:56:01 +00:00
|
|
|
|
0xf2c, // 0 | Small Block (Trainer Info)
|
|
|
|
|
0xf80, // 1 | Large Block Part 1
|
|
|
|
|
0xf80, // 2 | Large Block Part 2
|
|
|
|
|
0xf80, // 3 | Large Block Part 3
|
|
|
|
|
0xf08, // 4 | Large Block Part 4
|
2016-06-26 21:23:41 +00:00
|
|
|
|
0xf80, // 5 | PC Block 0
|
|
|
|
|
0xf80, // 6 | PC Block 1
|
|
|
|
|
0xf80, // 7 | PC Block 2
|
|
|
|
|
0xf80, // 8 | PC Block 3
|
|
|
|
|
0xf80, // 9 | PC Block 4
|
|
|
|
|
0xf80, // A | PC Block 5
|
|
|
|
|
0xf80, // B | PC Block 6
|
|
|
|
|
0xf80, // C | PC Block 7
|
|
|
|
|
0x7d0 // D | PC Block 8
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-08 16:56:01 +00:00
|
|
|
|
public static void GetLargeBlockOffset(int contiguousOffset, out int chunk, out int chunkOffset)
|
|
|
|
|
{
|
|
|
|
|
for (chunk = 1; chunk <= 4; chunk++)
|
|
|
|
|
{
|
|
|
|
|
int chunkSize = chunkLength[chunk];
|
|
|
|
|
if (chunkSize > contiguousOffset)
|
|
|
|
|
break;
|
|
|
|
|
contiguousOffset -= chunkSize;
|
|
|
|
|
}
|
|
|
|
|
chunkOffset = contiguousOffset;
|
|
|
|
|
}
|
|
|
|
|
public static int GetLargeBlockOffset(int chunk, int chunkOffset)
|
|
|
|
|
{
|
|
|
|
|
if (chunk == 1)
|
|
|
|
|
return chunkOffset;
|
|
|
|
|
for (int i = 1; i <= 4; i++)
|
|
|
|
|
{
|
|
|
|
|
if (chunk == i)
|
|
|
|
|
break;
|
|
|
|
|
chunkOffset += chunkLength[i];
|
|
|
|
|
}
|
|
|
|
|
return chunkOffset;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-28 06:03:57 +00:00
|
|
|
|
public SAV3(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-12-27 05:38:19 +00:00
|
|
|
|
Data = data ?? new byte[SaveUtil.SIZE_G3RAW];
|
2016-06-26 21:23:41 +00:00
|
|
|
|
BAK = (byte[])Data.Clone();
|
2018-03-18 23:22:21 +00:00
|
|
|
|
Exportable = !IsRangeEmpty(0, Data.Length);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2016-06-30 00:21:59 +00:00
|
|
|
|
if (data == null)
|
|
|
|
|
Version = GameVersion.FRLG;
|
|
|
|
|
else if (versionOverride != GameVersion.Any)
|
|
|
|
|
Version = versionOverride;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
else Version = SaveUtil.GetIsG3SAV(Data);
|
2016-06-28 06:03:57 +00:00
|
|
|
|
if (Version == GameVersion.Invalid)
|
|
|
|
|
return;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-29 01:03:20 +00:00
|
|
|
|
int[] BlockOrder1 = new int[BLOCK_COUNT];
|
|
|
|
|
for (int i = 0; i < BLOCK_COUNT; i++)
|
|
|
|
|
BlockOrder1[i] = BitConverter.ToInt16(Data, i*SIZE_BLOCK + 0xFF4);
|
2016-07-29 22:47:56 +00:00
|
|
|
|
int zeroBlock1 = Array.IndexOf(BlockOrder1, 0);
|
|
|
|
|
|
2017-01-15 00:43:16 +00:00
|
|
|
|
if (Data.Length > SaveUtil.SIZE_G3RAWHALF)
|
2016-07-29 22:47:56 +00:00
|
|
|
|
{
|
2017-04-29 01:03:20 +00:00
|
|
|
|
int[] BlockOrder2 = new int[BLOCK_COUNT];
|
|
|
|
|
for (int i = 0; i < BLOCK_COUNT; i++)
|
|
|
|
|
BlockOrder2[i] = BitConverter.ToInt16(Data, 0xE000 + i*SIZE_BLOCK + 0xFF4);
|
2016-07-29 22:47:56 +00:00
|
|
|
|
int zeroBlock2 = Array.IndexOf(BlockOrder2, 0);
|
|
|
|
|
|
2016-10-04 04:57:50 +00:00
|
|
|
|
if (zeroBlock2 < 0)
|
|
|
|
|
ActiveSAV = 0;
|
|
|
|
|
else if (zeroBlock1 < 0)
|
|
|
|
|
ActiveSAV = 1;
|
|
|
|
|
else
|
2017-04-29 01:03:20 +00:00
|
|
|
|
ActiveSAV = BitConverter.ToUInt32(Data, zeroBlock1*SIZE_BLOCK + 0xFFC) >
|
|
|
|
|
BitConverter.ToUInt32(Data, zeroBlock2*SIZE_BLOCK + 0xEFFC)
|
2016-07-29 22:47:56 +00:00
|
|
|
|
? 0
|
|
|
|
|
: 1;
|
|
|
|
|
BlockOrder = ActiveSAV == 0 ? BlockOrder1 : BlockOrder2;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ActiveSAV = 0;
|
|
|
|
|
BlockOrder = BlockOrder1;
|
|
|
|
|
}
|
2016-06-28 06:03:57 +00:00
|
|
|
|
|
2017-04-29 01:03:20 +00:00
|
|
|
|
BlockOfs = new int[BLOCK_COUNT];
|
|
|
|
|
for (int i = 0; i < BLOCK_COUNT; i++)
|
|
|
|
|
{
|
|
|
|
|
int index = Array.IndexOf(BlockOrder, i);
|
|
|
|
|
BlockOfs[i] = index < 0 ? int.MinValue : index*SIZE_BLOCK + ABO;
|
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
|
|
|
|
// Set up PC data buffer beyond end of save file.
|
|
|
|
|
Box = Data.Length;
|
2016-07-28 15:52:04 +00:00
|
|
|
|
Array.Resize(ref Data, Data.Length + SIZE_RESERVED); // More than enough empty space.
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
|
|
|
|
// Copy chunk to the allocated location
|
2017-04-29 01:03:20 +00:00
|
|
|
|
for (int i = 5; i < BLOCK_COUNT; i++)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
int blockIndex = Array.IndexOf(BlockOrder, i);
|
|
|
|
|
if (blockIndex == -1) // block empty
|
|
|
|
|
continue;
|
2017-04-29 01:03:20 +00:00
|
|
|
|
Array.Copy(Data, blockIndex * SIZE_BLOCK + ABO, Data, Box + (i - 5)*0xF80, chunkLength[i]);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-29 17:33:27 +00:00
|
|
|
|
// Japanese games are limited to 5 character OT names; any unused characters are 0xFF.
|
|
|
|
|
// 5 for JP, 7 for INT. There's always 1 terminator, thus we can check 0x6-0x7 being 0xFFFF = INT
|
|
|
|
|
// OT name is stored at the top of the first block.
|
2018-03-11 21:42:56 +00:00
|
|
|
|
Japanese = BitConverter.ToInt16(Data, BlockOfs[0] + 0x6) == 0;
|
2017-05-29 17:33:27 +00:00
|
|
|
|
|
2018-07-13 05:07:44 +00:00
|
|
|
|
PokeDex = BlockOfs[0] + 0x18;
|
2016-07-01 04:53:24 +00:00
|
|
|
|
switch (Version)
|
|
|
|
|
{
|
|
|
|
|
case GameVersion.RS:
|
|
|
|
|
LegalKeyItems = Legal.Pouch_Key_RS;
|
2017-02-27 06:38:15 +00:00
|
|
|
|
OFS_PCItem = BlockOfs[1] + 0x0498;
|
2016-07-04 05:21:45 +00:00
|
|
|
|
OFS_PouchHeldItem = BlockOfs[1] + 0x0560;
|
|
|
|
|
OFS_PouchKeyItem = BlockOfs[1] + 0x05B0;
|
|
|
|
|
OFS_PouchBalls = BlockOfs[1] + 0x0600;
|
|
|
|
|
OFS_PouchTMHM = BlockOfs[1] + 0x0640;
|
|
|
|
|
OFS_PouchBerry = BlockOfs[1] + 0x0740;
|
2018-07-13 05:07:44 +00:00
|
|
|
|
SeenFlagOffsets = new[] {PokeDex + 0x44, BlockOfs[1] + 0x938, BlockOfs[4] + 0xC0C};
|
2017-07-14 06:05:56 +00:00
|
|
|
|
EventFlag = BlockOfs[2] + 0x2A0;
|
|
|
|
|
EventConst = EventFlag + EventFlagMax / 8;
|
2018-07-08 18:00:50 +00:00
|
|
|
|
Daycare = BlockOfs[4] + 0x11C;
|
2016-07-01 04:53:24 +00:00
|
|
|
|
break;
|
|
|
|
|
case GameVersion.E:
|
|
|
|
|
LegalKeyItems = Legal.Pouch_Key_E;
|
2017-02-27 06:38:15 +00:00
|
|
|
|
OFS_PCItem = BlockOfs[1] + 0x0498;
|
2016-07-04 05:21:45 +00:00
|
|
|
|
OFS_PouchHeldItem = BlockOfs[1] + 0x0560;
|
|
|
|
|
OFS_PouchKeyItem = BlockOfs[1] + 0x05D8;
|
|
|
|
|
OFS_PouchBalls = BlockOfs[1] + 0x0650;
|
|
|
|
|
OFS_PouchTMHM = BlockOfs[1] + 0x0690;
|
|
|
|
|
OFS_PouchBerry = BlockOfs[1] + 0x0790;
|
2018-07-13 05:07:44 +00:00
|
|
|
|
SeenFlagOffsets = new[] {PokeDex + 0x44, BlockOfs[1] + 0x988, BlockOfs[4] + 0xCA4};
|
2017-07-14 06:05:56 +00:00
|
|
|
|
EventFlag = BlockOfs[2] + 0x2F0;
|
|
|
|
|
EventConst = EventFlag + EventFlagMax / 8;
|
2018-07-08 18:00:50 +00:00
|
|
|
|
Daycare = BlockOfs[4] + 0x1B0;
|
2017-03-09 19:12:57 +00:00
|
|
|
|
break;
|
|
|
|
|
case GameVersion.FRLG:
|
|
|
|
|
LegalKeyItems = Legal.Pouch_Key_FRLG;
|
|
|
|
|
OFS_PCItem = BlockOfs[1] + 0x0298;
|
|
|
|
|
OFS_PouchHeldItem = BlockOfs[1] + 0x0310;
|
|
|
|
|
OFS_PouchKeyItem = BlockOfs[1] + 0x03B8;
|
|
|
|
|
OFS_PouchBalls = BlockOfs[1] + 0x0430;
|
|
|
|
|
OFS_PouchTMHM = BlockOfs[1] + 0x0464;
|
|
|
|
|
OFS_PouchBerry = BlockOfs[1] + 0x054C;
|
2018-07-13 05:07:44 +00:00
|
|
|
|
SeenFlagOffsets = new[] {PokeDex + 0x44, BlockOfs[1] + 0x5F8, BlockOfs[4] + 0xB98};
|
2017-07-14 06:05:56 +00:00
|
|
|
|
EventFlag = BlockOfs[2] + 0x000;
|
|
|
|
|
EventConst = EventFlag + EventFlagMax / 8;
|
2018-07-08 18:00:50 +00:00
|
|
|
|
Daycare = BlockOfs[4] + 0x100;
|
2016-07-01 04:53:24 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2018-09-03 01:02:25 +00:00
|
|
|
|
Personal = SaveUtil.GetG3Personal(Version);
|
2017-04-10 22:59:16 +00:00
|
|
|
|
LoadEReaderBerryData();
|
2016-07-01 04:53:24 +00:00
|
|
|
|
LegalItems = Legal.Pouch_Items_RS;
|
|
|
|
|
LegalBalls = Legal.Pouch_Ball_RS;
|
|
|
|
|
LegalTMHMs = Legal.Pouch_TMHM_RS;
|
|
|
|
|
LegalBerries = Legal.Pouch_Berries_RS;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
HeldItems = Legal.HeldItems_RS;
|
|
|
|
|
|
2017-04-29 01:03:20 +00:00
|
|
|
|
// Sanity Check SeenFlagOffsets -- early saves may not have block 4 initialized yet
|
2017-06-10 06:13:41 +00:00
|
|
|
|
SeenFlagOffsets = SeenFlagOffsets?.Where(z => z >= 0).ToArray();
|
2017-04-29 01:03:20 +00:00
|
|
|
|
|
2016-06-26 21:23:41 +00:00
|
|
|
|
if (!Exportable)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ClearBoxes();
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2016-07-04 05:21:45 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override byte[] Write(bool DSV)
|
2016-07-28 15:52:04 +00:00
|
|
|
|
{
|
|
|
|
|
// Copy Box data back
|
2017-04-29 01:03:20 +00:00
|
|
|
|
for (int i = 5; i < BLOCK_COUNT; i++)
|
2016-07-28 15:52:04 +00:00
|
|
|
|
{
|
|
|
|
|
int blockIndex = Array.IndexOf(BlockOrder, i);
|
|
|
|
|
if (blockIndex == -1) // block empty
|
|
|
|
|
continue;
|
2017-04-29 01:03:20 +00:00
|
|
|
|
Array.Copy(Data, Box + (i - 5) * 0xF80, Data, blockIndex * SIZE_BLOCK + ABO, chunkLength[i]);
|
2016-07-28 15:52:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetChecksums();
|
2016-07-28 15:52:04 +00:00
|
|
|
|
return Data.Take(Data.Length - SIZE_RESERVED).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-26 21:23:41 +00:00
|
|
|
|
private readonly int ActiveSAV;
|
2017-04-29 01:03:20 +00:00
|
|
|
|
private int ABO => ActiveSAV*SIZE_BLOCK*0xE;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
private readonly int[] BlockOrder;
|
2016-07-05 06:52:37 +00:00
|
|
|
|
private readonly int[] BlockOfs;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public int GetBlockOffset(int block) => BlockOfs[block];
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
|
|
|
|
// Configuration
|
2017-04-13 01:19:31 +00:00
|
|
|
|
public override SaveFile Clone() { return new SAV3(Write(DSV:false), Version) {Japanese = Japanese}; }
|
2016-10-12 02:11:24 +00:00
|
|
|
|
public override bool IndeterminateSubVersion => Version == GameVersion.FRLG;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
|
|
|
|
public override int SIZE_STORED => PKX.SIZE_3STORED;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override int SIZE_PARTY => PKX.SIZE_3PARTY;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
public override PKM BlankPKM => new PK3();
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public override Type PKMType => typeof(PK3);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2017-01-27 03:18:20 +00:00
|
|
|
|
public override int MaxMoveID => Legal.MaxMoveID_3;
|
2016-10-24 04:59:27 +00:00
|
|
|
|
public override int MaxSpeciesID => Legal.MaxSpeciesID_3;
|
2017-01-27 03:18:20 +00:00
|
|
|
|
public override int MaxAbilityID => Legal.MaxAbilityID_3;
|
|
|
|
|
public override int MaxItemID => Legal.MaxItemID_3;
|
|
|
|
|
public override int MaxBallID => Legal.MaxBallID_3;
|
2017-12-28 00:36:24 +00:00
|
|
|
|
public override int MaxGameID => Legal.MaxGameID_3;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
|
|
|
|
public override int BoxCount => 14;
|
2016-09-25 20:42:03 +00:00
|
|
|
|
public override int MaxEV => 255;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
public override int Generation => 3;
|
|
|
|
|
protected override int GiftCountMax => 1;
|
2017-04-10 03:07:24 +00:00
|
|
|
|
public override int OTLength => 7;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
public override int NickLength => 10;
|
2016-09-09 03:20:32 +00:00
|
|
|
|
public override int MaxMoney => 999999;
|
2018-07-08 01:03:52 +00:00
|
|
|
|
protected override int EventFlagMax => 8 * (RS ? 288 : 300); // 0x900 RS, else 0x960
|
2017-07-14 06:05:56 +00:00
|
|
|
|
protected override int EventConstMax => EventConst > 0 ? 0x100 : int.MinValue;
|
2016-07-05 06:52:37 +00:00
|
|
|
|
|
|
|
|
|
public override bool HasParty => true;
|
|
|
|
|
|
2018-03-18 23:22:21 +00:00
|
|
|
|
public override bool IsPKMPresent(int Offset) => PKX.IsPKMPresentGBA(Data, Offset);
|
|
|
|
|
|
2016-06-26 21:23:41 +00:00
|
|
|
|
// Checksums
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override void SetChecksums()
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-04-29 01:03:20 +00:00
|
|
|
|
for (int i = 0; i < BLOCK_COUNT; i++)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-12-05 00:13:18 +00:00
|
|
|
|
int ofs = ABO + i * SIZE_BLOCK;
|
|
|
|
|
int len = chunkLength[BlockOrder[i]];
|
|
|
|
|
ushort chk = SaveUtil.CRC32(Data, ofs, len);
|
|
|
|
|
BitConverter.GetBytes(chk).CopyTo(Data, ofs + 0xFF6);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public override bool ChecksumsValid
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-04-29 01:03:20 +00:00
|
|
|
|
for (int i = 0; i < BLOCK_COUNT; i++)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-12-05 00:13:18 +00:00
|
|
|
|
int ofs = ABO + i * SIZE_BLOCK;
|
|
|
|
|
int len = chunkLength[BlockOrder[i]];
|
|
|
|
|
ushort chk = SaveUtil.CRC32(Data, ofs, len);
|
|
|
|
|
if (chk != BitConverter.ToUInt16(Data, ofs + 0xFF6))
|
2016-06-26 21:23:41 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public override string ChecksumInfo
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-09-30 05:58:25 +00:00
|
|
|
|
var list = new List<string>();
|
2017-04-29 01:03:20 +00:00
|
|
|
|
for (int i = 0; i < BLOCK_COUNT; i++)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-12-05 00:13:18 +00:00
|
|
|
|
int ofs = ABO + i * SIZE_BLOCK;
|
|
|
|
|
int len = chunkLength[BlockOrder[i]];
|
|
|
|
|
ushort chk = SaveUtil.CRC32(Data, ofs, len);
|
|
|
|
|
if (chk != BitConverter.ToUInt16(Data, ofs + 0xFF6))
|
2017-09-30 05:58:25 +00:00
|
|
|
|
list.Add($"Block {BlockOrder[i]:00} @ {i*SIZE_BLOCK:X5} invalid.");
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2017-12-05 04:16:54 +00:00
|
|
|
|
return list.Count != 0 ? string.Join(Environment.NewLine, list) : "Checksums are valid.";
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Trainer Info
|
2016-06-28 06:03:57 +00:00
|
|
|
|
public override GameVersion Version { get; protected set; }
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
|
|
|
|
private uint SecurityKey
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
switch (Version)
|
|
|
|
|
{
|
2016-07-04 05:21:45 +00:00
|
|
|
|
case GameVersion.E: return BitConverter.ToUInt32(Data, BlockOfs[0] + 0xAC);
|
2018-05-12 02:55:40 +00:00
|
|
|
|
case GameVersion.FRLG: return BitConverter.ToUInt32(Data, BlockOfs[0] + 0xF20);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public override string OT
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
get => GetString(BlockOfs[0], 0x10);
|
2017-09-14 01:24:37 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
int len = Japanese ? 5 : OTLength;
|
2018-05-12 15:41:23 +00:00
|
|
|
|
SetString(value, len, PadToSize: len, PadWith: 0xFF).CopyTo(Data, BlockOfs[0]);
|
2017-09-14 01:24:37 +00:00
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
public override int Gender
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[BlockOfs[0] + 8];
|
|
|
|
|
set => Data[BlockOfs[0] + 8] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-04-28 18:06:58 +00:00
|
|
|
|
public override int TID
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, BlockOfs[0] + 0xA + 0);
|
2018-04-28 18:06:58 +00:00
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, BlockOfs[0] + 0xA + 0);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-04-28 18:06:58 +00:00
|
|
|
|
public override int SID
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, BlockOfs[0] + 0xC);
|
2018-04-28 18:06:58 +00:00
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, BlockOfs[0] + 0xC);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
public override int PlayedHours
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, BlockOfs[0] + 0xE);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, BlockOfs[0] + 0xE);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
public override int PlayedMinutes
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[BlockOfs[0] + 0x10];
|
|
|
|
|
set => Data[BlockOfs[0] + 0x10] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
public override int PlayedSeconds
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[BlockOfs[0] + 0x11];
|
|
|
|
|
set => Data[BlockOfs[0] + 0x11] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
public int PlayedFrames
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[BlockOfs[0] + 0x12];
|
|
|
|
|
set => Data[BlockOfs[0] + 0x12] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2017-04-16 06:14:07 +00:00
|
|
|
|
public int Badges
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2018-07-08 00:01:47 +00:00
|
|
|
|
int startFlag = BadgeFlagStart;
|
|
|
|
|
int val = 0;
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
|
if (GetEventFlag(startFlag + i))
|
|
|
|
|
val |= 1 << i;
|
|
|
|
|
return val;
|
2017-04-16 06:14:07 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2018-07-08 00:01:47 +00:00
|
|
|
|
int startFlag = BadgeFlagStart;
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
|
SetEventFlag(startFlag + i, (value & (1 << i)) != 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int BadgeFlagStart
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Version == GameVersion.FRLG)
|
|
|
|
|
return 800; // dec
|
|
|
|
|
if (Version == GameVersion.RS)
|
|
|
|
|
return 0x807; // hex
|
|
|
|
|
return 0x867; // emerald
|
2017-04-16 06:14:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
public override uint Money
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
switch (Version)
|
|
|
|
|
{
|
|
|
|
|
case GameVersion.RS:
|
2016-07-04 05:21:45 +00:00
|
|
|
|
case GameVersion.E: return BitConverter.ToUInt32(Data, BlockOfs[1] + 0x0490) ^ SecurityKey;
|
|
|
|
|
case GameVersion.FRLG: return BitConverter.ToUInt32(Data, BlockOfs[1] + 0x0290) ^ SecurityKey;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
switch (Version)
|
|
|
|
|
{
|
|
|
|
|
case GameVersion.RS:
|
2016-07-04 05:21:45 +00:00
|
|
|
|
case GameVersion.E: BitConverter.GetBytes(value ^ SecurityKey).CopyTo(Data, BlockOfs[1] + 0x0490); break;
|
|
|
|
|
case GameVersion.FRLG: BitConverter.GetBytes(value ^ SecurityKey).CopyTo(Data, BlockOfs[1] + 0x0290); break;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public uint Coin
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
switch (Version)
|
|
|
|
|
{
|
|
|
|
|
case GameVersion.RS:
|
2017-03-10 03:38:57 +00:00
|
|
|
|
case GameVersion.E: return (ushort)(BitConverter.ToUInt16(Data, BlockOfs[1] + 0x0494) ^ SecurityKey);
|
|
|
|
|
case GameVersion.FRLG: return (ushort)(BitConverter.ToUInt16(Data, BlockOfs[1] + 0x0294) ^ SecurityKey);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
default: return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-03-10 03:38:57 +00:00
|
|
|
|
if (value > 9999)
|
|
|
|
|
value = 9999;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
switch (Version)
|
|
|
|
|
{
|
|
|
|
|
case GameVersion.RS:
|
2017-03-10 03:38:57 +00:00
|
|
|
|
case GameVersion.E: BitConverter.GetBytes((ushort)(value ^ SecurityKey)).CopyTo(Data, BlockOfs[1] + 0x0494); break;
|
|
|
|
|
case GameVersion.FRLG: BitConverter.GetBytes((ushort)(value ^ SecurityKey)).CopyTo(Data, BlockOfs[1] + 0x0294); break;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-03-10 03:38:57 +00:00
|
|
|
|
public uint BP
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, BlockOfs[0] + 0xEB8);
|
2017-03-10 03:38:57 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value > 9999)
|
|
|
|
|
value = 9999;
|
|
|
|
|
BitConverter.GetBytes((ushort)value).CopyTo(Data, BlockOfs[0] + 0xEB8);
|
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-05-12 02:55:40 +00:00
|
|
|
|
public uint BerryPowder
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Version != GameVersion.FRLG)
|
|
|
|
|
return 0;
|
|
|
|
|
return BitConverter.ToUInt32(Data, BlockOfs[0] + 0xAF8) ^ SecurityKey;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (Version != GameVersion.FRLG)
|
|
|
|
|
return;
|
|
|
|
|
SetData(BitConverter.GetBytes(value ^ SecurityKey), BlockOfs[0] + 0xAF8);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2016-07-01 04:53:24 +00:00
|
|
|
|
private readonly ushort[] LegalItems, LegalKeyItems, LegalBalls, LegalTMHMs, LegalBerries;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
public override InventoryPouch[] Inventory
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-06-15 01:43:28 +00:00
|
|
|
|
int max = Version == GameVersion.FRLG ? 999 : 99;
|
2017-02-27 06:49:08 +00:00
|
|
|
|
var PCItems = new [] {LegalItems, LegalKeyItems, LegalKeyItems, LegalBalls, LegalTMHMs, LegalBerries}.SelectMany(a => a).ToArray();
|
2016-07-01 04:53:24 +00:00
|
|
|
|
InventoryPouch[] pouch =
|
|
|
|
|
{
|
2018-07-29 20:55:07 +00:00
|
|
|
|
new InventoryPouch3(InventoryType.Items, LegalItems, max, OFS_PouchHeldItem, (OFS_PouchKeyItem - OFS_PouchHeldItem)/4),
|
|
|
|
|
new InventoryPouch3(InventoryType.KeyItems, LegalKeyItems, 1, OFS_PouchKeyItem, (OFS_PouchBalls - OFS_PouchKeyItem)/4),
|
|
|
|
|
new InventoryPouch3(InventoryType.Balls, LegalBalls, max, OFS_PouchBalls, (OFS_PouchTMHM - OFS_PouchBalls)/4),
|
|
|
|
|
new InventoryPouch3(InventoryType.TMHMs, LegalTMHMs, max, OFS_PouchTMHM, (OFS_PouchBerry - OFS_PouchTMHM)/4),
|
|
|
|
|
new InventoryPouch3(InventoryType.Berries, LegalBerries, max, OFS_PouchBerry, Version == GameVersion.FRLG ? 43 : 46),
|
|
|
|
|
new InventoryPouch3(InventoryType.PCItems, PCItems, max, OFS_PCItem, (OFS_PouchHeldItem - OFS_PCItem)/4),
|
2016-07-01 04:53:24 +00:00
|
|
|
|
};
|
|
|
|
|
foreach (var p in pouch)
|
|
|
|
|
{
|
2017-02-27 06:38:15 +00:00
|
|
|
|
if (p.Type != InventoryType.PCItems)
|
2018-07-29 20:55:07 +00:00
|
|
|
|
((InventoryPouch3)p).SecurityKey = SecurityKey;
|
2017-12-28 00:36:24 +00:00
|
|
|
|
p.GetPouch(Data);
|
2016-07-01 04:53:24 +00:00
|
|
|
|
}
|
|
|
|
|
return pouch;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
foreach (var p in value)
|
2017-12-28 00:36:24 +00:00
|
|
|
|
p.SetPouch(Data);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-08 18:00:50 +00:00
|
|
|
|
private int DaycareSlotSize => RS ? SIZE_STORED : SIZE_STORED + 0x3C; // 0x38 mail + 4 exp
|
|
|
|
|
public override int DaycareSeedSize => E ? 8 : 4; // 32bit, 16bit
|
|
|
|
|
public override uint? GetDaycareEXP(int loc, int slot) => BitConverter.ToUInt32(Data, GetDaycareEXPOffset(slot));
|
|
|
|
|
public override void SetDaycareEXP(int loc, int slot, uint EXP) => BitConverter.GetBytes(EXP).CopyTo(Data, GetDaycareEXPOffset(slot));
|
|
|
|
|
public override bool? IsDaycareOccupied(int loc, int slot) => IsPKMPresent(GetDaycareSlotOffset(loc, slot));
|
|
|
|
|
public override void SetDaycareOccupied(int loc, int slot, bool occupied) { }
|
|
|
|
|
public override int GetDaycareSlotOffset(int loc, int slot) => Daycare + (slot * DaycareSlotSize);
|
|
|
|
|
public override bool? IsDaycareHasEgg(int loc) => GetDaycareRNGSeed(loc).Any(z => z != '0');
|
|
|
|
|
public override void SetDaycareHasEgg(int loc, bool hasEgg)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2018-07-08 18:00:50 +00:00
|
|
|
|
SetDaycareRNGSeed(loc, E ? Util.Rand32().ToString("X8") : Util.Rand.Next(0x10000).ToString("X4"));
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-07-08 18:00:50 +00:00
|
|
|
|
|
|
|
|
|
private int GetDaycareEXPOffset(int slot)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2018-07-08 18:00:50 +00:00
|
|
|
|
if (Version == GameVersion.RS)
|
|
|
|
|
return GetDaycareSlotOffset(0, 2) + (2 * 0x38) + (4 * slot); // consecutive vals, after both consecutive slots & 2 mail
|
|
|
|
|
return GetDaycareSlotOffset(0, slot + 1) - 4; // @ end of each pkm slot
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-07-08 18:00:50 +00:00
|
|
|
|
|
|
|
|
|
public override string GetDaycareRNGSeed(int loc)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2018-07-08 18:00:50 +00:00
|
|
|
|
if (Version == GameVersion.E)
|
|
|
|
|
return BitConverter.ToUInt32(Data, GetDaycareSlotOffset(0, 2)).ToString("X8"); // after the 2 slots, before the step counter
|
|
|
|
|
return BitConverter.ToUInt16(Data, GetDaycareEXPOffset(2)).ToString("X4"); // after the 2nd slot EXP, before the step counter
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-07-08 18:00:50 +00:00
|
|
|
|
public override void SetDaycareRNGSeed(int loc, string seed)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2018-07-08 18:00:50 +00:00
|
|
|
|
if (Version == GameVersion.E) // egg pid
|
|
|
|
|
{
|
|
|
|
|
var val = Util.GetHexValue(seed);
|
|
|
|
|
BitConverter.GetBytes(val).CopyTo(Data, GetDaycareSlotOffset(0, 2));
|
|
|
|
|
}
|
|
|
|
|
// egg pid half
|
|
|
|
|
{
|
|
|
|
|
var val = (ushort)Util.GetHexValue(seed);
|
|
|
|
|
BitConverter.GetBytes(val).CopyTo(Data, GetDaycareEXPOffset(2));
|
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
|
public override int PartyCount
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
int ofs = 0x34;
|
|
|
|
|
if (GameVersion.FRLG != Version)
|
|
|
|
|
ofs += 0x200;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
return Data[BlockOfs[1] + ofs];
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
protected set
|
|
|
|
|
{
|
|
|
|
|
int ofs = 0x34;
|
|
|
|
|
if (GameVersion.FRLG != Version)
|
|
|
|
|
ofs += 0x200;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
Data[BlockOfs[1] + ofs] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override int GetBoxOffset(int box)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
return Box + 4 + SIZE_STORED * box * 30;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override int GetPartyOffset(int slot)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
int ofs = 0x38;
|
|
|
|
|
if (GameVersion.FRLG != Version)
|
|
|
|
|
ofs += 0x200;
|
2016-07-04 05:21:45 +00:00
|
|
|
|
return BlockOfs[1] + ofs + SIZE_PARTY * slot;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
public override int CurrentBox
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Box];
|
|
|
|
|
set => Data[Box] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override int GetBoxWallpaperOffset(int box)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int offset = GetBoxOffset(BoxCount);
|
2016-07-26 05:40:05 +00:00
|
|
|
|
offset += BoxCount * 0x9 + box;
|
2016-10-29 18:32:21 +00:00
|
|
|
|
return offset;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override string GetBoxName(int box)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int offset = GetBoxOffset(BoxCount);
|
2017-08-01 06:03:51 +00:00
|
|
|
|
return StringConverter.GetString3(Data, offset + box * 9, 9, Japanese);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetBoxName(int box, string value)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int offset = GetBoxOffset(BoxCount);
|
|
|
|
|
SetString(value, 8).CopyTo(Data, offset + box * 9);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override PKM GetPKM(byte[] data)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
return new PK3(data);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override byte[] DecryptPKM(byte[] data)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return PKX.DecryptArray3(data);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2016-07-04 05:21:45 +00:00
|
|
|
|
|
2017-03-06 00:40:57 +00:00
|
|
|
|
// Pokédex
|
|
|
|
|
private readonly int[] SeenFlagOffsets;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override void SetDex(PKM pkm)
|
2016-07-04 05:21:45 +00:00
|
|
|
|
{
|
2017-03-06 00:40:57 +00:00
|
|
|
|
int species = pkm.Species;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (!CanSetDex(species))
|
2016-07-04 05:21:45 +00:00
|
|
|
|
return;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2018-07-13 05:07:44 +00:00
|
|
|
|
switch (species)
|
|
|
|
|
{
|
|
|
|
|
case 201 when !GetSeen(species): // Unown
|
|
|
|
|
DexPIDUnown = pkm.PID;
|
|
|
|
|
break;
|
|
|
|
|
case 327 when !GetSeen(species): // Spinda
|
|
|
|
|
DexPIDSpinda = pkm.PID;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
SetCaught(species, true);
|
|
|
|
|
SetSeen(species, true);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private bool CanSetDex(int species)
|
2017-03-06 00:40:57 +00:00
|
|
|
|
{
|
2017-03-15 05:41:15 +00:00
|
|
|
|
if (species <= 0)
|
2017-03-06 00:40:57 +00:00
|
|
|
|
return false;
|
|
|
|
|
if (species > MaxSpeciesID)
|
|
|
|
|
return false;
|
2018-03-12 01:07:48 +00:00
|
|
|
|
if (Version == GameVersion.Invalid)
|
2017-03-06 00:40:57 +00:00
|
|
|
|
return false;
|
2016-07-04 05:21:45 +00:00
|
|
|
|
if (BlockOfs.Any(z => z < 0))
|
2017-03-06 00:40:57 +00:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-07-13 05:07:44 +00:00
|
|
|
|
public uint DexPIDUnown { get => BitConverter.ToUInt32(Data, PokeDex + 0x4); set => BitConverter.GetBytes(value).CopyTo(Data, PokeDex + 0x4); }
|
|
|
|
|
public uint DexPIDSpinda { get => BitConverter.ToUInt32(Data, PokeDex + 0x8); set => BitConverter.GetBytes(value).CopyTo(Data, PokeDex + 0x8); }
|
|
|
|
|
public int DexUnownForm => PKX.GetUnownForm(DexPIDUnown);
|
2017-03-15 05:41:15 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override bool GetCaught(int species)
|
2017-03-06 00:40:57 +00:00
|
|
|
|
{
|
|
|
|
|
int bit = species - 1;
|
2017-03-15 05:41:15 +00:00
|
|
|
|
int ofs = bit >> 3;
|
2018-07-13 05:07:44 +00:00
|
|
|
|
int caughtOffset = PokeDex + 0x10;
|
2017-10-19 04:43:42 +00:00
|
|
|
|
return GetFlag(caughtOffset + ofs, bit & 7);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetCaught(int species, bool caught)
|
2017-03-06 00:40:57 +00:00
|
|
|
|
{
|
|
|
|
|
int bit = species - 1;
|
2017-11-26 16:36:59 +00:00
|
|
|
|
int ofs = bit >> 3;
|
2018-07-13 05:07:44 +00:00
|
|
|
|
int caughtOffset = PokeDex + 0x10;
|
2017-10-19 04:43:42 +00:00
|
|
|
|
SetFlag(caughtOffset + ofs, bit & 7, caught);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2017-03-15 05:41:15 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override bool GetSeen(int species)
|
2017-03-06 00:40:57 +00:00
|
|
|
|
{
|
|
|
|
|
int bit = species - 1;
|
2017-03-15 05:41:15 +00:00
|
|
|
|
int ofs = bit >> 3;
|
2018-07-13 05:07:44 +00:00
|
|
|
|
int seenOffset = PokeDex + 0x44;
|
2017-10-19 04:43:42 +00:00
|
|
|
|
return GetFlag(seenOffset + ofs, bit & 7);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetSeen(int species, bool seen)
|
2017-03-06 00:40:57 +00:00
|
|
|
|
{
|
|
|
|
|
int bit = species - 1;
|
2017-11-26 16:36:59 +00:00
|
|
|
|
int ofs = bit >> 3;
|
2017-03-06 00:40:57 +00:00
|
|
|
|
|
2017-10-19 04:43:42 +00:00
|
|
|
|
foreach (int o in SeenFlagOffsets)
|
|
|
|
|
SetFlag(o + ofs, bit & 7, seen);
|
2016-07-04 05:21:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool NationalDex
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (BlockOfs.Any(z => z < 0))
|
|
|
|
|
return false;
|
|
|
|
|
switch (Version) // only check natdex status in Block0
|
|
|
|
|
{
|
|
|
|
|
case GameVersion.RS:
|
|
|
|
|
case GameVersion.E:
|
2018-07-13 06:23:36 +00:00
|
|
|
|
return Data[PokeDex + 2] == 0xDA; // enable nat dex option magic value
|
2016-07-04 05:21:45 +00:00
|
|
|
|
case GameVersion.FRLG:
|
2018-07-13 06:23:36 +00:00
|
|
|
|
return Data[PokeDex + 3] == 0xB9;
|
2016-07-04 05:21:45 +00:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (BlockOfs.Any(z => z < 0))
|
|
|
|
|
return;
|
|
|
|
|
switch (Version)
|
|
|
|
|
{
|
|
|
|
|
case GameVersion.RS:
|
2018-07-13 06:23:36 +00:00
|
|
|
|
Data[PokeDex + 1] = (byte)(value ? 1 : 0); // mode
|
|
|
|
|
Data[PokeDex + 2] = (byte)(value ? 0xDA : 0); // magic
|
2016-07-04 05:21:45 +00:00
|
|
|
|
Data[BlockOfs[2] + 0x3A6] &= 0xBF;
|
|
|
|
|
Data[BlockOfs[2] + 0x3A6] |= (byte)(value ? 1 << 6 : 0); // B
|
|
|
|
|
BitConverter.GetBytes((ushort)(value ? 0x0302 : 0)).CopyTo(Data, BlockOfs[2] + 0x44C); // C
|
|
|
|
|
break;
|
|
|
|
|
case GameVersion.E:
|
2018-07-13 06:23:36 +00:00
|
|
|
|
Data[PokeDex + 1] = (byte)(value ? 1 : 0); // mode
|
|
|
|
|
Data[PokeDex + 2] = (byte)(value ? 0xDA : 0); // magic
|
2016-07-04 05:21:45 +00:00
|
|
|
|
Data[BlockOfs[2] + 0x402] &= 0xBF; // Bit6
|
|
|
|
|
Data[BlockOfs[2] + 0x402] |= (byte)(value ? 1 << 6 : 0); // B
|
|
|
|
|
BitConverter.GetBytes((ushort)(value ? 0x6258 : 0)).CopyTo(Data, BlockOfs[2] + 0x4A8); // C
|
|
|
|
|
break;
|
|
|
|
|
case GameVersion.FRLG:
|
2018-07-13 06:23:36 +00:00
|
|
|
|
Data[PokeDex + 2] = (byte)(value ? 0xDA : 0); // magic
|
|
|
|
|
Data[PokeDex + 3] = (byte)(value ? 0xB9 : 0); // magic
|
2016-07-04 05:21:45 +00:00
|
|
|
|
Data[BlockOfs[2] + 0x68] &= 0xFE;
|
|
|
|
|
Data[BlockOfs[2] + 0x68] |= (byte)(value ? 1 : 0); // B
|
|
|
|
|
BitConverter.GetBytes((ushort)(value ? 0x6258 : 0)).CopyTo(Data, BlockOfs[2] + 0x11C); // C
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
public override string GetString(int Offset, int Length) => StringConverter.GetString3(Data, Offset, Length, Japanese);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override byte[] SetString(string value, int maxLength, int PadToSize = 0, ushort PadWith = 0)
|
2017-04-09 21:06:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (PadToSize == 0)
|
|
|
|
|
PadToSize = maxLength + 1;
|
2017-08-01 06:03:51 +00:00
|
|
|
|
return StringConverter.SetString3(value, maxLength, Japanese, PadToSize, PadWith);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2017-04-10 22:59:16 +00:00
|
|
|
|
|
|
|
|
|
#region eBerry
|
|
|
|
|
// Offset and checksum code based from
|
|
|
|
|
// https://github.com/suloku/wc-tool by Suloku
|
|
|
|
|
private const int SIZE_EBERRY = 0x530;
|
|
|
|
|
private const int OFFSET_EBERRY = 0x2E0;
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private uint EBerryChecksum => BitConverter.ToUInt32(Data, BlockOfs[4] + OFFSET_EBERRY + SIZE_EBERRY - 4);
|
|
|
|
|
private bool IsEBerryChecksumValid { get; set; }
|
2017-04-10 22:59:16 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override string EBerryName
|
2017-04-10 22:59:16 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (!GameVersion.RS.Contains(Version) || !IsEBerryChecksumValid)
|
2017-04-10 22:59:16 +00:00
|
|
|
|
return string.Empty;
|
2017-08-01 06:03:51 +00:00
|
|
|
|
return StringConverter.GetString3(Data, BlockOfs[4] + OFFSET_EBERRY, 7, Japanese).Trim();
|
2017-04-10 22:59:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override bool IsEBerryIsEnigma => string.IsNullOrEmpty(EBerryName.Trim());
|
2017-04-10 22:59:16 +00:00
|
|
|
|
|
|
|
|
|
private void LoadEReaderBerryData()
|
|
|
|
|
{
|
|
|
|
|
if (!GameVersion.RS.Contains(Version))
|
|
|
|
|
return;
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
byte[] data = GetData(BlockOfs[4] + OFFSET_EBERRY, SIZE_EBERRY - 4);
|
2017-04-10 22:59:16 +00:00
|
|
|
|
|
2017-04-10 23:47:08 +00:00
|
|
|
|
// 8 bytes are 0x00 for chk calculation
|
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
|
|
|
data[0xC + i] = 0x00;
|
|
|
|
|
uint chk = (uint)data.Sum(z => z);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
IsEBerryChecksumValid = EBerryChecksum == chk;
|
2017-04-10 22:59:16 +00:00
|
|
|
|
}
|
|
|
|
|
#endregion
|
2017-04-14 02:11:01 +00:00
|
|
|
|
|
|
|
|
|
// RTC
|
|
|
|
|
public class RTC3
|
|
|
|
|
{
|
|
|
|
|
public readonly byte[] Data;
|
|
|
|
|
private const int Size = 8;
|
|
|
|
|
public RTC3(byte[] data = null)
|
|
|
|
|
{
|
|
|
|
|
if (data == null || data.Length != Size)
|
|
|
|
|
data = new byte[8];
|
|
|
|
|
Data = data;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-13 03:32:36 +00:00
|
|
|
|
public int Day { get => BitConverter.ToUInt16(Data, 0x00); set => BitConverter.GetBytes((ushort)value).CopyTo(Data, 0x00); }
|
|
|
|
|
public int Hour { get => Data[2]; set => Data[2] = (byte)value; }
|
|
|
|
|
public int Minute { get => Data[3]; set => Data[3] = (byte)value; }
|
|
|
|
|
public int Second { get => Data[4]; set => Data[4] = (byte)value; }
|
2017-04-14 02:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
public RTC3 ClockInitial
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (FRLG)
|
|
|
|
|
return null;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int block0 = GetBlockOffset(0);
|
|
|
|
|
return new RTC3(GetData(block0 + 0x98, 8));
|
2017-04-14 02:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value?.Data == null || FRLG)
|
|
|
|
|
return;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int block0 = GetBlockOffset(0);
|
|
|
|
|
SetData(value.Data, block0 + 0x98);
|
2017-04-14 02:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public RTC3 ClockElapsed
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (FRLG)
|
|
|
|
|
return null;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int block0 = GetBlockOffset(0);
|
|
|
|
|
return new RTC3(GetData(block0 + 0xA0, 8));
|
2017-04-14 02:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value?.Data == null || FRLG)
|
|
|
|
|
return;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int block0 = GetBlockOffset(0);
|
|
|
|
|
SetData(value.Data, block0 + 0xA0);
|
2017-04-14 02:11:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-08 04:31:41 +00:00
|
|
|
|
|
|
|
|
|
public PokeBlock3Case PokeBlocks
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
var ofs = PokeBlockOffset;
|
|
|
|
|
if (ofs < 0)
|
|
|
|
|
throw new Exception($"Game does not support {nameof(PokeBlocks)}.");
|
|
|
|
|
return new PokeBlock3Case(Data, ofs);
|
|
|
|
|
}
|
|
|
|
|
set => SetData(value.Write(), PokeBlockOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int PokeBlockOffset
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (Version == GameVersion.E)
|
|
|
|
|
return BlockOfs[1] + 0x848;
|
|
|
|
|
if (Version == GameVersion.RS)
|
|
|
|
|
return BlockOfs[1] + 0x7F8;
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
}
|