2016-06-20 04:22:43 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-10-24 06:12:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generation 5 <see cref="SaveFile"/> object.
|
|
|
|
|
/// </summary>
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public sealed class SAV5 : SaveFile
|
|
|
|
|
{
|
|
|
|
|
// Save Data Attributes
|
2018-07-30 04:51:45 +00:00
|
|
|
|
protected override string BAKText => $"{OT} ({(GameVersion)Game}) - {PlayTimeString}";
|
2016-10-17 14:49:40 +00:00
|
|
|
|
public override string Filter => (Footer.Length > 0 ? "DeSmuME DSV|*.dsv|" : "") + "SAV File|*.sav|All Files|*.*";
|
2016-06-20 05:11:53 +00:00
|
|
|
|
public override string Extension => ".sav";
|
2016-06-28 06:03:57 +00:00
|
|
|
|
public SAV5(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-12-27 05:38:19 +00:00
|
|
|
|
Data = data ?? new byte[SaveUtil.SIZE_G5RAW];
|
2016-06-20 04:22:43 +00:00
|
|
|
|
BAK = (byte[])Data.Clone();
|
2018-03-18 23:22:21 +00:00
|
|
|
|
Exportable = !IsRangeEmpty(0, Data.Length);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
// Get Version
|
2016-06-30 00:21:59 +00:00
|
|
|
|
if (data == null)
|
|
|
|
|
Version = GameVersion.B2W2;
|
|
|
|
|
else if (versionOverride != GameVersion.Any)
|
|
|
|
|
Version = versionOverride;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
else Version = SaveUtil.GetIsG5SAV(Data);
|
2016-06-28 06:03:57 +00:00
|
|
|
|
if (Version == GameVersion.Invalid)
|
|
|
|
|
return;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
// First blocks are always the same position/size
|
|
|
|
|
PCLayout = 0x0;
|
|
|
|
|
Box = 0x400;
|
|
|
|
|
Party = 0x18E00;
|
|
|
|
|
Trainer1 = 0x19400;
|
|
|
|
|
WondercardData = 0x1C800;
|
|
|
|
|
AdventureInfo = 0x1D900;
|
|
|
|
|
|
|
|
|
|
// Different Offsets for later blocks
|
2016-06-28 06:03:57 +00:00
|
|
|
|
switch (Version)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-06-28 06:03:57 +00:00
|
|
|
|
case GameVersion.BW:
|
2016-06-20 04:22:43 +00:00
|
|
|
|
BattleBox = 0x20A00;
|
|
|
|
|
Trainer2 = 0x21200;
|
2017-05-26 09:05:58 +00:00
|
|
|
|
EventConst = 0x20100;
|
|
|
|
|
EventFlag = EventConst + 0x27C;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
Daycare = 0x20E00;
|
2016-07-04 18:56:30 +00:00
|
|
|
|
PokeDex = 0x21600;
|
|
|
|
|
PokeDexLanguageFlags = PokeDex + 0x320;
|
2017-11-02 04:26:38 +00:00
|
|
|
|
BattleSubway = 0x21D00;
|
2016-08-08 05:46:07 +00:00
|
|
|
|
CGearInfoOffset = 0x1C000;
|
|
|
|
|
CGearDataOffset = 0x52000;
|
2018-05-28 15:26:52 +00:00
|
|
|
|
EntreeForestOffset = 0x22C00;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
// Inventory offsets are the same for each game.
|
|
|
|
|
OFS_PouchHeldItem = 0x18400; // 0x188D7
|
|
|
|
|
OFS_PouchKeyItem = 0x188D8; // 0x18A23
|
|
|
|
|
OFS_PouchTMHM = 0x18A24; // 0x18BD7
|
|
|
|
|
OFS_PouchMedicine = 0x18BD8; // 0x18C97
|
|
|
|
|
OFS_PouchBerry = 0x18C98; // 0x18DBF
|
|
|
|
|
LegalItems = Legal.Pouch_Items_BW;
|
|
|
|
|
LegalKeyItems = Legal.Pouch_Key_BW;
|
|
|
|
|
LegalTMHMs = Legal.Pouch_TMHM_BW;
|
|
|
|
|
LegalMedicine = Legal.Pouch_Medicine_BW;
|
|
|
|
|
LegalBerries = Legal.Pouch_Berries_BW;
|
2016-07-03 03:24:17 +00:00
|
|
|
|
|
2016-07-18 05:39:18 +00:00
|
|
|
|
Personal = PersonalTable.BW;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
break;
|
2016-06-28 06:03:57 +00:00
|
|
|
|
case GameVersion.B2W2: // B2W2
|
2016-06-20 04:22:43 +00:00
|
|
|
|
BattleBox = 0x20900;
|
|
|
|
|
Trainer2 = 0x21100;
|
|
|
|
|
EventConst = 0x1FF00;
|
|
|
|
|
EventFlag = EventConst + 0x35E;
|
|
|
|
|
Daycare = 0x20D00;
|
2016-07-04 18:56:30 +00:00
|
|
|
|
PokeDex = 0x21400;
|
|
|
|
|
PokeDexLanguageFlags = PokeDex + 0x328; // forme flags size is + 8 from bw with new formes (therians)
|
2017-11-02 04:26:38 +00:00
|
|
|
|
BattleSubway = 0x21B00;
|
2016-08-08 05:46:07 +00:00
|
|
|
|
CGearInfoOffset = 0x1C000;
|
|
|
|
|
CGearDataOffset = 0x52800;
|
2018-05-28 15:26:52 +00:00
|
|
|
|
EntreeForestOffset = 0x22A00;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
// Inventory offsets are the same for each game.
|
|
|
|
|
OFS_PouchHeldItem = 0x18400; // 0x188D7
|
|
|
|
|
OFS_PouchKeyItem = 0x188D8; // 0x18A23
|
|
|
|
|
OFS_PouchTMHM = 0x18A24; // 0x18BD7
|
|
|
|
|
OFS_PouchMedicine = 0x18BD8; // 0x18C97
|
|
|
|
|
OFS_PouchBerry = 0x18C98; // 0x18DBF
|
|
|
|
|
LegalItems = Legal.Pouch_Items_BW;
|
|
|
|
|
LegalKeyItems = Legal.Pouch_Key_B2W2;
|
|
|
|
|
LegalTMHMs = Legal.Pouch_TMHM_BW;
|
|
|
|
|
LegalMedicine = Legal.Pouch_Medicine_BW;
|
|
|
|
|
LegalBerries = Legal.Pouch_Berries_BW;
|
2016-07-03 03:24:17 +00:00
|
|
|
|
|
2016-07-18 05:39:18 +00:00
|
|
|
|
Personal = PersonalTable.B2W2;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
HeldItems = Legal.HeldItems_BW;
|
2018-02-19 19:33:37 +00:00
|
|
|
|
Blocks = Version == GameVersion.BW ? BlockInfoNDS.BlocksBW : BlockInfoNDS.BlocksB2W2;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
if (!Exportable)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ClearBoxes();
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configuration
|
2018-01-18 22:05:34 +00:00
|
|
|
|
public override SaveFile Clone() { return new SAV5((byte[])Data.Clone(), Version); }
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
public override int SIZE_STORED => PKX.SIZE_5STORED;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override int SIZE_PARTY => PKX.SIZE_5PARTY;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public override PKM BlankPKM => new PK5();
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public override Type PKMType => typeof(PK5);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
|
|
|
|
|
public override int BoxCount => 24;
|
|
|
|
|
public override int MaxEV => 255;
|
|
|
|
|
public override int Generation => 5;
|
2017-04-10 04:53:53 +00:00
|
|
|
|
public override int OTLength => 7;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
public override int NickLength => 10;
|
2017-05-26 09:05:58 +00:00
|
|
|
|
protected override int EventConstMax => (Version == GameVersion.BW ? 0x27C : 0x35E) >> 1;
|
|
|
|
|
protected override int EventFlagMax => (Version == GameVersion.BW ? 0x16C : 0x17F) << 3;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
protected override int GiftCountMax => 12;
|
|
|
|
|
|
2017-01-27 03:18:20 +00:00
|
|
|
|
public override int MaxMoveID => Legal.MaxMoveID_5;
|
2016-10-24 04:59:27 +00:00
|
|
|
|
public override int MaxSpeciesID => Legal.MaxSpeciesID_5;
|
2017-01-27 03:18:20 +00:00
|
|
|
|
public override int MaxItemID => Version == GameVersion.BW ? Legal.MaxItemID_5_BW : Legal.MaxItemID_5_B2W2;
|
|
|
|
|
public override int MaxAbilityID => Legal.MaxAbilityID_5;
|
|
|
|
|
public override int MaxBallID => Legal.MaxBallID_5;
|
|
|
|
|
public override int MaxGameID => Legal.MaxGameID_5; // B2
|
2016-06-21 01:58:06 +00:00
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
// Blocks & Offsets
|
2018-02-19 19:33:37 +00:00
|
|
|
|
private readonly BlockInfoNDS[] Blocks;
|
|
|
|
|
protected override void SetChecksums() => Blocks.SetChecksums(Data);
|
|
|
|
|
public override bool ChecksumsValid => Blocks.GetChecksumsValid(Data);
|
|
|
|
|
public override string ChecksumInfo => Blocks.GetChecksumInfo(Data);
|
2017-09-30 05:58:25 +00:00
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
private const int wcSeed = 0x1D290;
|
|
|
|
|
|
2016-08-08 05:46:07 +00:00
|
|
|
|
public readonly int CGearInfoOffset, CGearDataOffset;
|
2018-05-28 15:26:52 +00:00
|
|
|
|
private readonly int EntreeForestOffset;
|
2017-11-02 04:26:38 +00:00
|
|
|
|
private readonly int Trainer2, AdventureInfo, BattleSubway;
|
2017-03-06 01:34:30 +00:00
|
|
|
|
public readonly int PokeDexLanguageFlags;
|
2016-07-04 18:56:30 +00:00
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
// Daycare
|
|
|
|
|
public override int DaycareSeedSize => 16;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override int GetDaycareSlotOffset(int loc, int slot)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
return Daycare + 4 + 0xE4 * slot;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override string GetDaycareRNGSeed(int loc)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-06-28 06:03:57 +00:00
|
|
|
|
if (Version != GameVersion.B2W2)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
return null;
|
2016-10-30 02:11:47 +00:00
|
|
|
|
var data = Data.Skip(Daycare + 0x1CC).Take(DaycareSeedSize/2).Reverse().ToArray();
|
|
|
|
|
return BitConverter.ToString(data).Replace("-", "");
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override uint? GetDaycareEXP(int loc, int slot)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
return BitConverter.ToUInt32(Data, Daycare + 4 + 0xDC + slot * 0xE4);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override bool? IsDaycareOccupied(int loc, int slot)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
return BitConverter.ToUInt32(Data, Daycare + 0xE4*slot) == 1;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetDaycareEXP(int loc, int slot, uint EXP)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
BitConverter.GetBytes(EXP).CopyTo(Data, Daycare + 4 + 0xDC + slot * 0xE4);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetDaycareOccupied(int loc, int slot, bool occupied)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
BitConverter.GetBytes((uint)(occupied ? 1 : 0)).CopyTo(Data, Daycare + 0x1CC);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetDaycareRNGSeed(int loc, string seed)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-06-28 06:03:57 +00:00
|
|
|
|
if (Version != GameVersion.B2W2)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
return;
|
2016-10-30 02:11:47 +00:00
|
|
|
|
Enumerable.Range(0, seed.Length)
|
|
|
|
|
.Where(x => x % 2 == 0)
|
|
|
|
|
.Select(x => Convert.ToByte(seed.Substring(x, 2), 16))
|
|
|
|
|
.Reverse().ToArray().CopyTo(Data, Daycare + 0x1CC);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Inventory
|
|
|
|
|
private readonly ushort[] LegalItems, LegalKeyItems, LegalTMHMs, LegalMedicine, LegalBerries;
|
|
|
|
|
public override InventoryPouch[] Inventory
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
InventoryPouch[] pouch =
|
|
|
|
|
{
|
2018-07-29 20:55:07 +00:00
|
|
|
|
new InventoryPouch4(InventoryType.Items, LegalItems, 999, OFS_PouchHeldItem),
|
|
|
|
|
new InventoryPouch4(InventoryType.KeyItems, LegalKeyItems, 1, OFS_PouchKeyItem),
|
|
|
|
|
new InventoryPouch4(InventoryType.TMHMs, LegalTMHMs, 1, OFS_PouchTMHM),
|
|
|
|
|
new InventoryPouch4(InventoryType.Medicine, LegalMedicine, 999, OFS_PouchMedicine),
|
|
|
|
|
new InventoryPouch4(InventoryType.Berries, LegalBerries, 999, OFS_PouchBerry),
|
2016-06-20 04:22:43 +00:00
|
|
|
|
};
|
|
|
|
|
foreach (var p in pouch)
|
2017-12-28 00:36:24 +00:00
|
|
|
|
p.GetPouch(Data);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
return pouch;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
foreach (var p in value)
|
2017-12-28 00:36:24 +00:00
|
|
|
|
p.SetPouch(Data);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
|
public override int PartyCount
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Party + 4];
|
|
|
|
|
protected set => Data[Party + 4] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override int GetBoxOffset(int box)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
return Box + SIZE_STORED * box * 30 + box * 0x10;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override int GetPartyOffset(int slot)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
return Party + 8 + SIZE_PARTY*slot;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override string GetBoxName(int box)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
if (box >= BoxCount)
|
|
|
|
|
return "";
|
2017-08-01 06:03:51 +00:00
|
|
|
|
return StringConverter.TrimFromFFFF(Encoding.Unicode.GetString(Data, PCLayout + 0x28 * box + 4, 0x28));
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
public override void SetBoxName(int box, string value)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2018-05-12 15:13:39 +00:00
|
|
|
|
if (value.Length > 38)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
return;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
value += '\uFFFF';
|
|
|
|
|
Encoding.Unicode.GetBytes(value.PadRight(0x14, '\0')).CopyTo(Data, PCLayout + 0x28 * box + 4);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
Edited = true;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override int GetBoxWallpaperOffset(int box)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2016-10-29 18:32:21 +00:00
|
|
|
|
return PCLayout + 0x3C4 + box;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public override int CurrentBox
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[PCLayout];
|
|
|
|
|
set => Data[PCLayout] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-05-13 03:32:36 +00:00
|
|
|
|
public override bool BattleBoxLocked
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BattleBox >= 0 && Data[BattleBox + 0x358] != 0; // wifi/live
|
2016-06-20 04:22:43 +00:00
|
|
|
|
set { }
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override PKM GetPKM(byte[] data)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
|
|
|
|
return new PK5(data);
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override byte[] DecryptPKM(byte[] data)
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return PKX.DecryptArray45(data);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2018-03-22 04:10:23 +00:00
|
|
|
|
protected override void SetPKM(PKM pkm)
|
|
|
|
|
{
|
|
|
|
|
var pk5 = (PK5)pkm;
|
|
|
|
|
// Apply to this Save File
|
|
|
|
|
DateTime Date = DateTime.Now;
|
|
|
|
|
if (pk5.Trade(OT, TID, SID, Gender, Date.Day, Date.Month, Date.Year))
|
|
|
|
|
pkm.RefreshChecksum();
|
|
|
|
|
}
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
// Mystery Gift
|
|
|
|
|
public override MysteryGiftAlbum GiftAlbum
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
uint seed = BitConverter.ToUInt32(Data, wcSeed);
|
|
|
|
|
MysteryGiftAlbum Info = new MysteryGiftAlbum { Seed = seed };
|
2018-02-19 19:33:37 +00:00
|
|
|
|
byte[] wcData = GetData(WondercardData, 0xA90); // Encrypted, Decrypt
|
2016-06-20 04:22:43 +00:00
|
|
|
|
for (int i = 0; i < wcData.Length; i += 2)
|
|
|
|
|
BitConverter.GetBytes((ushort)(BitConverter.ToUInt16(wcData, i) ^ PKX.LCRNG(ref seed) >> 16)).CopyTo(wcData, i);
|
|
|
|
|
|
|
|
|
|
Info.Flags = new bool[GiftFlagMax];
|
|
|
|
|
Info.Gifts = new MysteryGift[GiftCountMax];
|
|
|
|
|
// 0x100 Bytes for Used Flags
|
|
|
|
|
for (int i = 0; i < GiftFlagMax; i++)
|
|
|
|
|
Info.Flags[i] = (wcData[i/8] >> i%8 & 0x1) == 1;
|
|
|
|
|
// 12 PGFs
|
|
|
|
|
for (int i = 0; i < Info.Gifts.Length; i++)
|
|
|
|
|
Info.Gifts[i] = new PGF(wcData.Skip(0x100 + i*PGF.Size).Take(PGF.Size).ToArray());
|
|
|
|
|
|
|
|
|
|
return Info;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
byte[] wcData = new byte[0xA90];
|
|
|
|
|
|
|
|
|
|
// Toss back into byte[]
|
|
|
|
|
for (int i = 0; i < value.Flags.Length; i++)
|
|
|
|
|
if (value.Flags[i])
|
|
|
|
|
wcData[i/8] |= (byte)(1 << (i & 7));
|
|
|
|
|
for (int i = 0; i < value.Gifts.Length; i++)
|
|
|
|
|
value.Gifts[i].Data.CopyTo(wcData, 0x100 + i*PGF.Size);
|
|
|
|
|
|
|
|
|
|
// Decrypted, Encrypt
|
|
|
|
|
uint seed = value.Seed;
|
|
|
|
|
for (int i = 0; i < wcData.Length; i += 2)
|
|
|
|
|
BitConverter.GetBytes((ushort)(BitConverter.ToUInt16(wcData, i) ^ PKX.LCRNG(ref seed) >> 16)).CopyTo(wcData, i);
|
|
|
|
|
|
|
|
|
|
// Write Back
|
|
|
|
|
wcData.CopyTo(Data, WondercardData);
|
|
|
|
|
BitConverter.GetBytes(value.Seed).CopyTo(Data, wcSeed);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-13 03:32:36 +00:00
|
|
|
|
protected override bool[] MysteryGiftReceivedFlags { get => null; set { } }
|
2018-08-03 03:11:42 +00:00
|
|
|
|
protected override MysteryGift[] MysteryGiftCards { get => Array.Empty<MysteryGift>(); set { } }
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2016-06-20 04:22:43 +00:00
|
|
|
|
// Trainer Info
|
|
|
|
|
public override string OT
|
|
|
|
|
{
|
2017-06-18 01:37:19 +00:00
|
|
|
|
get => GetString(Trainer1 + 0x4, 16);
|
|
|
|
|
set => SetString(value, OTLength).CopyTo(Data, Trainer1 + 0x4);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2018-04-28 18:06:58 +00:00
|
|
|
|
public override int TID
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, Trainer1 + 0x14 + 0);
|
2018-04-28 18:06:58 +00:00
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, Trainer1 + 0x14 + 0);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2018-04-28 18:06:58 +00:00
|
|
|
|
public override int SID
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, Trainer1 + 0x14 + 2);
|
2018-04-28 18:06:58 +00:00
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, Trainer1 + 0x14 + 2);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2016-06-22 00:35:12 +00:00
|
|
|
|
public override uint Money
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt32(Data, Trainer2);
|
|
|
|
|
set => BitConverter.GetBytes(value).CopyTo(Data, Trainer2);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public override int Gender
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Trainer1 + 0x21];
|
|
|
|
|
set => Data[Trainer1 + 0x21] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public override int Language
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Trainer1 + 0x1E];
|
|
|
|
|
set => Data[Trainer1 + 0x1E] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public override int Game
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Trainer1 + 0x1F];
|
|
|
|
|
set => Data[Trainer1 + 0x1F] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public int Badges
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Trainer2 + 0x4];
|
|
|
|
|
set => Data[Trainer2 + 0x4] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public int M
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToInt32(Data, Trainer1 + 0x180);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, Trainer1 + 0x180);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public int X
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, Trainer1 + 0x186);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, Trainer1 + 0x186);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public int Z
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, Trainer1 + 0x18A);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, Trainer1 + 0x18A);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
public int Y
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, Trainer1 + 0x18E);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, Trainer1 + 0x18E);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-06-22 00:35:12 +00:00
|
|
|
|
public override int PlayedHours
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Data, Trainer1 + 0x24);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, Trainer1 + 0x24);
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2016-06-22 00:35:12 +00:00
|
|
|
|
public override int PlayedMinutes
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Trainer1 + 0x24 + 2];
|
|
|
|
|
set => Data[Trainer1 + 0x24 + 2] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2016-06-22 00:35:12 +00:00
|
|
|
|
public override int PlayedSeconds
|
2016-06-20 04:22:43 +00:00
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Trainer1 + 0x24 + 3];
|
|
|
|
|
set => Data[Trainer1 + 0x24 + 3] = (byte)value;
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
2017-05-13 03:32:36 +00:00
|
|
|
|
public override int SecondsToStart { get => BitConverter.ToInt32(Data, AdventureInfo + 0x34); set => BitConverter.GetBytes(value).CopyTo(Data, AdventureInfo + 0x34); }
|
|
|
|
|
public override int SecondsToFame { get => BitConverter.ToInt32(Data, AdventureInfo + 0x3C); set => BitConverter.GetBytes(value).CopyTo(Data, AdventureInfo + 0x3C); }
|
2016-07-04 18:56:30 +00:00
|
|
|
|
|
2017-11-02 04:26:38 +00:00
|
|
|
|
public int BP
|
|
|
|
|
{
|
|
|
|
|
get => BitConverter.ToUInt16(Data, BattleSubway);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Data, BattleSubway);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-14 20:34:03 +00:00
|
|
|
|
public ushort GetPWTRecord(int id) => GetPWTRecord((PWTRecordID) id);
|
|
|
|
|
public ushort GetPWTRecord(PWTRecordID id)
|
|
|
|
|
{
|
|
|
|
|
if (id < PWTRecordID.Normal || id > PWTRecordID.MixMaster)
|
|
|
|
|
throw new ArgumentException(nameof(id));
|
|
|
|
|
int ofs = 0x2375C + (int)id * 2;
|
|
|
|
|
return BitConverter.ToUInt16(Data, ofs);
|
|
|
|
|
}
|
|
|
|
|
public void SetPWTRecord(int id, ushort value) => SetPWTRecord((PWTRecordID) id, value);
|
|
|
|
|
public void SetPWTRecord(PWTRecordID id, ushort value)
|
|
|
|
|
{
|
|
|
|
|
if (id < PWTRecordID.Normal || id > PWTRecordID.MixMaster)
|
|
|
|
|
throw new ArgumentException(nameof(id));
|
|
|
|
|
int ofs = 0x2375C + (int)id * 2;
|
|
|
|
|
SetData(BitConverter.GetBytes(value), ofs);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override void SetDex(PKM pkm)
|
2016-07-04 18:56:30 +00:00
|
|
|
|
{
|
|
|
|
|
if (pkm.Species == 0)
|
|
|
|
|
return;
|
|
|
|
|
if (pkm.Species > MaxSpeciesID)
|
|
|
|
|
return;
|
2018-03-12 01:07:48 +00:00
|
|
|
|
if (Version == GameVersion.Invalid)
|
2016-07-04 18:56:30 +00:00
|
|
|
|
return;
|
|
|
|
|
if (PokeDex < 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const int brSize = 0x54;
|
|
|
|
|
int bit = pkm.Species - 1;
|
|
|
|
|
int gender = pkm.Gender % 2; // genderless -> male
|
|
|
|
|
int shiny = pkm.IsShiny ? 1 : 0;
|
2017-03-06 01:34:30 +00:00
|
|
|
|
int shift = shiny*2 + gender + 1;
|
2016-08-10 05:52:59 +00:00
|
|
|
|
int shiftoff = shiny * brSize * 2 + gender * brSize + brSize;
|
2017-03-06 01:34:30 +00:00
|
|
|
|
int ofs = PokeDex + 0x8 + (bit >> 3);
|
2016-07-04 18:56:30 +00:00
|
|
|
|
|
|
|
|
|
// Set the Species Owned Flag
|
2017-03-06 01:34:30 +00:00
|
|
|
|
Data[ofs + brSize*0] |= (byte)(1 << (bit % 8));
|
2016-07-04 18:56:30 +00:00
|
|
|
|
|
|
|
|
|
// Set the [Species/Gender/Shiny] Seen Flag
|
2017-03-06 01:34:30 +00:00
|
|
|
|
Data[PokeDex + 0x8 + shiftoff + bit / 8] |= (byte)(1 << (bit&7));
|
2016-07-04 18:56:30 +00:00
|
|
|
|
|
|
|
|
|
// Set the Display flag if none are set
|
|
|
|
|
bool Displayed = false;
|
2017-03-06 01:34:30 +00:00
|
|
|
|
Displayed |= (Data[ofs + brSize*5] & (byte)(1 << (bit&7))) != 0;
|
|
|
|
|
Displayed |= (Data[ofs + brSize*6] & (byte)(1 << (bit&7))) != 0;
|
|
|
|
|
Displayed |= (Data[ofs + brSize*7] & (byte)(1 << (bit&7))) != 0;
|
|
|
|
|
Displayed |= (Data[ofs + brSize*8] & (byte)(1 << (bit&7))) != 0;
|
2016-08-27 20:06:01 +00:00
|
|
|
|
if (!Displayed) // offset is already biased by brSize, reuse shiftoff but for the display flags.
|
2017-03-06 01:34:30 +00:00
|
|
|
|
Data[ofs + brSize*(shift + 4)] |= (byte)(1 << (bit&7));
|
2016-07-04 18:56:30 +00:00
|
|
|
|
|
|
|
|
|
// Set the Language
|
2017-08-28 03:37:57 +00:00
|
|
|
|
if (bit < 493) // shifted by 1, Gen5 species do not have international language bits
|
2017-08-21 00:52:10 +00:00
|
|
|
|
{
|
|
|
|
|
int lang = pkm.Language - 1; if (lang > 5) lang--; // 0-6 language vals
|
|
|
|
|
if (lang < 0) lang = 1;
|
|
|
|
|
Data[PokeDexLanguageFlags + ((bit*7 + lang)>>3)] |= (byte)(1 << ((bit*7 + lang) & 7));
|
|
|
|
|
}
|
2016-07-04 18:56:30 +00:00
|
|
|
|
|
2016-08-27 20:06:01 +00:00
|
|
|
|
// Formes
|
|
|
|
|
int fc = Personal[pkm.Species].FormeCount;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
int f = B2W2 ? SaveUtil.GetDexFormIndexB2W2(pkm.Species, fc) : SaveUtil.GetDexFormIndexBW(pkm.Species, fc);
|
2016-08-27 20:06:01 +00:00
|
|
|
|
if (f < 0) return;
|
|
|
|
|
|
|
|
|
|
int FormLen = B2W2 ? 0xB : 0x9;
|
|
|
|
|
int FormDex = PokeDex + 0x8 + brSize*9;
|
|
|
|
|
bit = f + pkm.AltForm;
|
|
|
|
|
|
|
|
|
|
// Set Form Seen Flag
|
2017-03-06 01:34:30 +00:00
|
|
|
|
Data[FormDex + FormLen*shiny + (bit>>3)] |= (byte)(1 << (bit&7));
|
2016-08-27 20:06:01 +00:00
|
|
|
|
|
|
|
|
|
// Set Displayed Flag if necessary, check all flags
|
|
|
|
|
for (int i = 0; i < fc; i++)
|
|
|
|
|
{
|
|
|
|
|
bit = f + i;
|
2017-03-06 01:34:30 +00:00
|
|
|
|
if ((Data[FormDex + FormLen*2 + (bit>>3)] & (byte)(1 << (bit&7))) != 0) // Nonshiny
|
2016-08-27 20:06:01 +00:00
|
|
|
|
return; // already set
|
2017-03-06 01:34:30 +00:00
|
|
|
|
if ((Data[FormDex + FormLen*3 + (bit>>3)] & (byte)(1 << (bit&7))) != 0) // Shiny
|
2016-08-27 20:06:01 +00:00
|
|
|
|
return; // already set
|
|
|
|
|
}
|
|
|
|
|
bit = f + pkm.AltForm;
|
2017-03-06 01:34:30 +00:00
|
|
|
|
Data[FormDex + FormLen * (2 + shiny) + (bit>>3)] |= (byte)(1 << (bit&7));
|
2016-07-04 18:56:30 +00:00
|
|
|
|
}
|
2017-03-15 05:41:15 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override bool GetCaught(int species)
|
2017-03-15 05:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
int bit = species - 1;
|
|
|
|
|
int bd = bit >> 3; // div8
|
|
|
|
|
int bm = bit & 7; // mod8
|
|
|
|
|
int ofs = PokeDex // Raw Offset
|
|
|
|
|
+ 0x08; // Magic + Flags
|
|
|
|
|
return (1 << bm & Data[ofs + bd]) != 0;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override bool GetSeen(int species)
|
2017-03-15 05:41:15 +00:00
|
|
|
|
{
|
|
|
|
|
const int brSize = 0x54;
|
|
|
|
|
|
|
|
|
|
int bit = species - 1;
|
|
|
|
|
int bd = bit >> 3; // div8
|
|
|
|
|
int bm = bit & 7; // mod8
|
|
|
|
|
int ofs = PokeDex // Raw Offset
|
|
|
|
|
+ 0x08; // Magic + Flags
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= 4; i++)
|
|
|
|
|
if ((1 << bm & Data[ofs + bd + i * brSize]) != 0)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2018-05-12 19:28:48 +00:00
|
|
|
|
public override string GetString(int Offset, int Length) => StringConverter.GetString5(Data, Offset, Length);
|
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.SetString5(value, maxLength, PadToSize, PadWith);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2018-01-14 00:32:57 +00:00
|
|
|
|
|
|
|
|
|
// DLC
|
|
|
|
|
private int CGearSkinInfoOffset => CGearInfoOffset + (B2W2 ? 0x10 : 0) + 0x24;
|
|
|
|
|
private bool CGearSkinPresent
|
|
|
|
|
{
|
|
|
|
|
get => Data[CGearSkinInfoOffset + 2] == 1;
|
|
|
|
|
set => Data[CGearSkinInfoOffset + 2] = Data[Trainer1 + (B2W2 ? 0x6C : 0x54)] = (byte) (value ? 1 : 0);
|
|
|
|
|
}
|
|
|
|
|
public byte[] CGearSkinData
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
byte[] data = new byte[0x2600];
|
|
|
|
|
if (CGearSkinPresent)
|
|
|
|
|
Array.Copy(Data, CGearDataOffset, data, 0, data.Length);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value == null)
|
|
|
|
|
return; // no clearing
|
|
|
|
|
byte[] dlcfooter = { 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x27, 0x00, 0x00, 0x27, 0x35, 0x05, 0x31, 0x00, 0x00 };
|
|
|
|
|
|
|
|
|
|
byte[] bgdata = value;
|
|
|
|
|
SetData(bgdata, CGearDataOffset);
|
|
|
|
|
|
|
|
|
|
ushort chk = SaveUtil.CRC16_CCITT(bgdata);
|
|
|
|
|
var chkbytes = BitConverter.GetBytes(chk);
|
|
|
|
|
int footer = CGearDataOffset + bgdata.Length;
|
|
|
|
|
|
|
|
|
|
BitConverter.GetBytes((ushort)1).CopyTo(Data, footer); // block updated once
|
|
|
|
|
chkbytes.CopyTo(Data, footer + 2); // checksum
|
|
|
|
|
chkbytes.CopyTo(Data, footer + 0x100); // second checksum
|
|
|
|
|
dlcfooter.CopyTo(Data, footer + 0x102);
|
|
|
|
|
ushort skinchkval = SaveUtil.CRC16_CCITT(Data, footer + 0x100, 4);
|
|
|
|
|
BitConverter.GetBytes(skinchkval).CopyTo(Data, footer + 0x112);
|
|
|
|
|
|
|
|
|
|
// Indicate in the save file that data is present
|
|
|
|
|
BitConverter.GetBytes((ushort)0xC21E).CopyTo(Data, 0x19438);
|
|
|
|
|
|
|
|
|
|
chkbytes.CopyTo(Data, CGearSkinInfoOffset);
|
|
|
|
|
CGearSkinPresent = true;
|
|
|
|
|
|
|
|
|
|
Edited = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-05-28 15:26:52 +00:00
|
|
|
|
|
|
|
|
|
public EntreeForest EntreeData
|
|
|
|
|
{
|
|
|
|
|
get => new EntreeForest(GetData(EntreeForestOffset, 0x850));
|
|
|
|
|
set => SetData(value.Write(), EntreeForestOffset);
|
|
|
|
|
}
|
2016-06-20 04:22:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|