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
|
|
|
|
|
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>
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public abstract class SAV3 : SaveFile, ILangDeviantSave
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected internal sealed override string ShortSummary => $"{OT} ({Version}) - {PlayTimeString}";
|
|
|
|
|
public sealed override string Extension => ".sav";
|
2020-09-26 20:30:17 +00:00
|
|
|
|
|
|
|
|
|
public int SaveRevision => Japanese ? 0 : 1;
|
|
|
|
|
public string SaveRevisionString => Japanese ? "J" : "U";
|
|
|
|
|
public bool Japanese { get; }
|
|
|
|
|
public bool Korean => false;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
// Similar to future games, the Generation 3 Mainline save files are comprised of separate objects:
|
|
|
|
|
// Object 1 - Small, containing misc configuration data & the Pokédex.
|
|
|
|
|
// Object 2 - Large, containing everything else that isn't PC Storage system data.
|
|
|
|
|
// Object 3 - Storage, containing all the data for the PC storage system.
|
2021-03-08 07:22:07 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
// When the objects are serialized to the savedata, the game fragments each object and saves it to a sector.
|
|
|
|
|
// The main save data for a save file occupies 14 sectors; there are a total of two serialized main saves.
|
|
|
|
|
// After the serialized main save data, there is "extra data", for stuff like Hall of Fame and battle videos.
|
|
|
|
|
// Extra data is always at the same sector, while the main sectors rotate sectors within their region (on each successive save?).
|
2021-03-08 07:22:07 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
private const int SIZE_SECTOR = 0x1000;
|
|
|
|
|
private const int SIZE_SECTOR_USED = 0xF80;
|
|
|
|
|
private const int COUNT_MAIN = 14; // sectors worth of data
|
|
|
|
|
private const int SIZE_MAIN = COUNT_MAIN * SIZE_SECTOR;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
// There's no harm having buffers larger than their actual size (per format).
|
|
|
|
|
// A checksum consuming extra zeroes does not change the prior checksum result.
|
|
|
|
|
public readonly byte[] Small = new byte[1 * SIZE_SECTOR]; // [0x890 RS, 0xf24 FR/LG, 0xf2c E]
|
|
|
|
|
public readonly byte[] Large = new byte[4 * SIZE_SECTOR]; //3+[0xc40 RS, 0xee8 FR/LG, 0xf08 E]
|
|
|
|
|
public readonly byte[] Storage = new byte[9 * SIZE_SECTOR]; // [0x83D0]
|
2021-03-08 07:22:07 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
private readonly int ActiveSlot;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
protected SAV3(bool japanese) => Japanese = japanese;
|
2018-09-16 20:57:09 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected SAV3(byte[] data) : base(data)
|
2019-06-09 02:56:11 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
// Copy sector data to the allocated location
|
|
|
|
|
ReadSectors(data, ActiveSlot = GetActiveSlot(data));
|
2021-03-08 07:22:07 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
// OT name is the first 8 bytes of Small. The game fills any unused characters with 0xFF.
|
|
|
|
|
// Japanese games are limited to 5 character OT names; INT 7 characters. +1 0xFF terminator.
|
|
|
|
|
// Since JPN games don't touch the last 2 bytes (alignment), they end up as zeroes!
|
2021-03-08 07:22:07 +00:00
|
|
|
|
Japanese = BitConverter.ToInt16(Small, 0x6) == 0;
|
2019-06-09 02:56:11 +00:00
|
|
|
|
}
|
2017-04-29 01:03:20 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
private void ReadSectors(byte[] data, int group)
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
int start = group * SIZE_MAIN;
|
|
|
|
|
int end = start + SIZE_MAIN;
|
|
|
|
|
for (int ofs = start; ofs < end; ofs += SIZE_SECTOR)
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
var id = BitConverter.ToInt16(data, ofs + 0xFF4);
|
|
|
|
|
switch (id)
|
|
|
|
|
{
|
|
|
|
|
case >=5: Buffer.BlockCopy(data, ofs, Storage, (id - 5) * SIZE_SECTOR_USED, SIZE_SECTOR_USED); break;
|
|
|
|
|
case >=1: Buffer.BlockCopy(data, ofs, Large , (id - 1) * SIZE_SECTOR_USED, SIZE_SECTOR_USED); break;
|
|
|
|
|
default: Buffer.BlockCopy(data, ofs, Small , 0 , SIZE_SECTOR_USED); break;
|
|
|
|
|
}
|
2021-03-08 07:22:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
private void WriteSectors(byte[] data, int group)
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
int start = group * SIZE_MAIN;
|
|
|
|
|
int end = start + SIZE_MAIN;
|
|
|
|
|
for (int ofs = start; ofs < end; ofs += SIZE_SECTOR)
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
var id = BitConverter.ToInt16(data, ofs + 0xFF4);
|
|
|
|
|
switch (id)
|
|
|
|
|
{
|
|
|
|
|
case >=5: Buffer.BlockCopy(Storage, (id - 5) * SIZE_SECTOR_USED, data, ofs, SIZE_SECTOR_USED); break;
|
|
|
|
|
case >=1: Buffer.BlockCopy(Large , (id - 1) * SIZE_SECTOR_USED, data, ofs, SIZE_SECTOR_USED); break;
|
|
|
|
|
default: Buffer.BlockCopy(Small , 0 , data, ofs, SIZE_SECTOR_USED); break;
|
|
|
|
|
}
|
2021-03-08 07:22:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks the input data to see if all required sectors for the main save data are present for the <see cref="slot"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">Data to check</param>
|
|
|
|
|
/// <param name="slot">Which main to check (primary or secondary)</param>
|
|
|
|
|
/// <param name="sector0">Offset of the sector that has the small object data</param>
|
|
|
|
|
public static bool IsAllMainSectorsPresent(byte[] data, int slot, out int sector0)
|
|
|
|
|
{
|
|
|
|
|
System.Diagnostics.Debug.Assert(slot is 0 or 1);
|
|
|
|
|
int start = SIZE_MAIN * slot;
|
|
|
|
|
int end = start + SIZE_MAIN;
|
|
|
|
|
int bitTrack = 0;
|
|
|
|
|
sector0 = 0;
|
|
|
|
|
for (int ofs = 0; ofs < end; ofs += SIZE_SECTOR)
|
2019-06-09 02:56:11 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
var id = BitConverter.ToInt16(data, ofs + 0xFF4);
|
|
|
|
|
bitTrack |= (1 << id);
|
|
|
|
|
if (id == 0)
|
|
|
|
|
sector0 = ofs;
|
2019-06-09 02:56:11 +00:00
|
|
|
|
}
|
2021-03-17 06:32:16 +00:00
|
|
|
|
// all 14 fragments present
|
|
|
|
|
return bitTrack == 0b_0011_1111_1111_1111;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2016-07-04 05:21:45 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
private static int GetActiveSlot(byte[] data)
|
2018-09-15 05:37:47 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
if (data.Length == SaveUtil.SIZE_G3RAWHALF)
|
|
|
|
|
return 0;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
var v0 = IsAllMainSectorsPresent(data, 0, out var sectorZero0);
|
|
|
|
|
var v1 = IsAllMainSectorsPresent(data, 1, out var sectorZero1);
|
|
|
|
|
if (!v0)
|
|
|
|
|
return v1 ? 1 : 0;
|
|
|
|
|
if (!v1)
|
2018-09-15 05:37:47 +00:00
|
|
|
|
return 0;
|
2021-03-17 06:32:16 +00:00
|
|
|
|
|
|
|
|
|
var count0 = BitConverter.ToUInt32(data, sectorZero0 + 0x0FFC);
|
|
|
|
|
var count1 = BitConverter.ToUInt32(data, sectorZero1 + 0x0FFC);
|
|
|
|
|
// don't care about 32bit overflow. a 10 second save would take 1,000 years to overflow!
|
|
|
|
|
return count1 > count0 ? 1 : 0;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected sealed override byte[] GetFinalData()
|
2016-07-28 15:52:04 +00:00
|
|
|
|
{
|
|
|
|
|
// Copy Box data back
|
2021-03-17 06:32:16 +00:00
|
|
|
|
WriteSectors(Data, ActiveSlot);
|
2021-03-08 07:22:07 +00:00
|
|
|
|
return base.GetFinalData();
|
2016-07-28 15:52:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected sealed override int SIZE_STORED => PokeCrypto.SIZE_3STORED;
|
|
|
|
|
protected sealed override int SIZE_PARTY => PokeCrypto.SIZE_3PARTY;
|
|
|
|
|
public sealed override PKM BlankPKM => new PK3();
|
|
|
|
|
public sealed override Type PKMType => typeof(PK3);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int MaxMoveID => Legal.MaxMoveID_3;
|
|
|
|
|
public sealed override int MaxSpeciesID => Legal.MaxSpeciesID_3;
|
|
|
|
|
public sealed override int MaxAbilityID => Legal.MaxAbilityID_3;
|
|
|
|
|
public sealed override int MaxItemID => Legal.MaxItemID_3;
|
|
|
|
|
public sealed override int MaxBallID => Legal.MaxBallID_3;
|
|
|
|
|
public sealed override int MaxGameID => Legal.MaxGameID_3;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
public sealed override IReadOnlyList<ushort> HeldItems => Legal.HeldItems_RS;
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int BoxCount => 14;
|
|
|
|
|
public sealed override int MaxEV => 255;
|
|
|
|
|
public sealed override int Generation => 3;
|
|
|
|
|
protected sealed override int GiftCountMax => 1;
|
|
|
|
|
public sealed override int OTLength => 7;
|
|
|
|
|
public sealed override int NickLength => 10;
|
|
|
|
|
public sealed override int MaxMoney => 999999;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override bool HasParty => true;
|
2016-07-05 06:52:37 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override bool IsPKMPresent(byte[] data, int offset) => PKX.IsPKMPresentGBA(data, offset);
|
|
|
|
|
protected sealed override PKM GetPKM(byte[] data) => new PK3(data);
|
|
|
|
|
protected sealed override byte[] DecryptPKM(byte[] data) => PokeCrypto.DecryptArray3(data);
|
2018-03-18 23:22:21 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
protected sealed override byte[] BoxBuffer => Storage;
|
|
|
|
|
protected sealed override byte[] PartyBuffer => Large;
|
|
|
|
|
|
|
|
|
|
private const int COUNT_BOX = 14;
|
|
|
|
|
private const int COUNT_SLOTSPERBOX = 30;
|
|
|
|
|
|
2016-06-26 21:23:41 +00:00
|
|
|
|
// Checksums
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected sealed override void SetChecksums()
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
int start = ActiveSlot * SIZE_MAIN;
|
|
|
|
|
int end = start + SIZE_MAIN;
|
|
|
|
|
for (int ofs = start; ofs < end; ofs += SIZE_SECTOR)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
ushort chk = Checksums.CheckSum32(Data, ofs, SIZE_SECTOR_USED);
|
2017-12-05 00:13:18 +00:00
|
|
|
|
BitConverter.GetBytes(chk).CopyTo(Data, ofs + 0xFF6);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2019-03-02 17:16:03 +00:00
|
|
|
|
|
2020-12-05 13:36:23 +00:00
|
|
|
|
if (State.BAK.Length < SaveUtil.SIZE_G3RAW) // don't update HoF for half-sizes
|
2019-03-02 17:16:03 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Hall of Fame Checksums
|
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
ushort chk = Checksums.CheckSum32(Data, 0x1C000, SIZE_SECTOR_USED);
|
2019-03-02 17:16:03 +00:00
|
|
|
|
BitConverter.GetBytes(chk).CopyTo(Data, 0x1CFF4);
|
|
|
|
|
}
|
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
ushort chk = Checksums.CheckSum32(Data, 0x1D000, SIZE_SECTOR_USED);
|
2019-03-02 17:16:03 +00:00
|
|
|
|
BitConverter.GetBytes(chk).CopyTo(Data, 0x1DFF4);
|
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override bool ChecksumsValid
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
for (int i = 0; i < COUNT_MAIN; i++)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
if (!IsSectorValid(i))
|
2016-06-26 21:23:41 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-03-02 17:16:03 +00:00
|
|
|
|
|
2020-12-05 13:36:23 +00:00
|
|
|
|
if (State.BAK.Length < SaveUtil.SIZE_G3RAW) // don't check HoF for half-sizes
|
2019-03-02 17:16:03 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
if (!IsSectorValidExtra(0x1C000))
|
2019-03-02 17:16:03 +00:00
|
|
|
|
return false;
|
2021-03-17 06:32:16 +00:00
|
|
|
|
if (!IsSectorValidExtra(0x1D000))
|
2019-03-02 17:16:03 +00:00
|
|
|
|
return false;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
private bool IsSectorValidExtra(int ofs)
|
2019-03-02 17:16:03 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
ushort chk = Checksums.CheckSum32(Data, ofs, SIZE_SECTOR_USED);
|
2019-10-04 05:21:33 +00:00
|
|
|
|
return chk == BitConverter.ToUInt16(Data, ofs + 0xFF4);
|
2019-03-02 17:16:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 06:32:16 +00:00
|
|
|
|
private bool IsSectorValid(int sector)
|
2019-03-02 17:16:03 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
int start = ActiveSlot * SIZE_MAIN;
|
|
|
|
|
int ofs = start + (sector * SIZE_SECTOR);
|
|
|
|
|
ushort chk = Checksums.CheckSum32(Data, ofs, SIZE_SECTOR_USED);
|
2019-10-04 05:21:33 +00:00
|
|
|
|
return chk == BitConverter.ToUInt16(Data, ofs + 0xFF6);
|
2019-03-02 17:16:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override string ChecksumInfo
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2017-09-30 05:58:25 +00:00
|
|
|
|
var list = new List<string>();
|
2021-03-17 06:32:16 +00:00
|
|
|
|
for (int i = 0; i < COUNT_MAIN; i++)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
if (!IsSectorValid(i))
|
|
|
|
|
list.Add($"Sector {i} @ {i*SIZE_SECTOR:X5} invalid.");
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2019-03-02 17:16:03 +00:00
|
|
|
|
|
2020-12-05 13:36:23 +00:00
|
|
|
|
if (State.BAK.Length > SaveUtil.SIZE_G3RAW) // don't check HoF for half-sizes
|
2019-03-02 17:16:03 +00:00
|
|
|
|
{
|
2021-03-17 06:32:16 +00:00
|
|
|
|
if (!IsSectorValidExtra(0x1C000))
|
|
|
|
|
list.Add("HoF first sector invalid.");
|
|
|
|
|
if (!IsSectorValidExtra(0x1D000))
|
|
|
|
|
list.Add("HoF second sector invalid.");
|
2019-03-02 17:16:03 +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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public abstract uint SecurityKey { get; set; }
|
2016-06-26 21:23:41 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override string OT
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => GetString(Small, 0, 0x10);
|
2017-09-14 01:24:37 +00:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
int len = Japanese ? 5 : OTLength;
|
2021-03-08 07:22:07 +00:00
|
|
|
|
SetString(value, len, PadToSize: len, PadWith: 0xFF).CopyTo(Small, 0);
|
2017-09-14 01:24:37 +00:00
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int Gender
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[8];
|
|
|
|
|
set => Small[8] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int TID
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Small, 0xA);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Small, 0xA);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int SID
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Small, 0xC);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Small, 0xC);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int PlayedHours
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => BitConverter.ToUInt16(Small, 0xE);
|
|
|
|
|
set => BitConverter.GetBytes((ushort)value).CopyTo(Small, 0xE);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int PlayedMinutes
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[0x10];
|
|
|
|
|
set => Small[0x10] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int PlayedSeconds
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[0x11];
|
|
|
|
|
set => Small[0x11] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2016-06-26 21:23:41 +00:00
|
|
|
|
public int PlayedFrames
|
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[0x12];
|
|
|
|
|
set => Small[0x12] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override bool GetEventFlag(int flagNumber)
|
2018-09-27 04:05:06 +00:00
|
|
|
|
{
|
2018-09-28 03:01:34 +00:00
|
|
|
|
if (flagNumber >= EventFlagMax)
|
2018-09-27 04:05:06 +00:00
|
|
|
|
throw new ArgumentException($"Event Flag to get ({flagNumber}) is greater than max ({EventFlagMax}).");
|
|
|
|
|
|
|
|
|
|
var start = EventFlag;
|
|
|
|
|
return GetFlag(start + (flagNumber >> 3), flagNumber & 7);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override void SetEventFlag(int flagNumber, bool value)
|
2018-09-27 04:05:06 +00:00
|
|
|
|
{
|
2018-09-28 03:01:34 +00:00
|
|
|
|
if (flagNumber >= EventFlagMax)
|
2018-09-27 04:05:06 +00:00
|
|
|
|
throw new ArgumentException($"Event Flag to set ({flagNumber}) is greater than max ({EventFlagMax}).");
|
|
|
|
|
|
|
|
|
|
var start = EventFlag;
|
|
|
|
|
SetFlag(start + (flagNumber >> 3), flagNumber & 7, value);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override bool GetFlag(int offset, int bitIndex) => FlagUtil.GetFlag(Large, offset, bitIndex);
|
|
|
|
|
public sealed override void SetFlag(int offset, int bitIndex, bool value) => FlagUtil.SetFlag(Large, offset, bitIndex, value);
|
2021-03-08 07:22:07 +00:00
|
|
|
|
|
|
|
|
|
public ushort GetEventConst(int index) => BitConverter.ToUInt16(Large, EventConst + (index * 2));
|
|
|
|
|
public void SetEventConst(int index, ushort value) => BitConverter.GetBytes(value).CopyTo(Large, EventConst + (index * 2));
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override ushort[] GetEventConsts()
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
|
|
|
|
ushort[] Constants = new ushort[EventConstMax];
|
|
|
|
|
for (int i = 0; i < Constants.Length; i++)
|
|
|
|
|
Constants[i] = GetEventConst(i);
|
|
|
|
|
return Constants;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override void SetEventConsts(ushort[] value)
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
|
|
|
|
if (value.Length != EventConstMax)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < value.Length; i++)
|
|
|
|
|
SetEventConst(i, value[i]);
|
|
|
|
|
}
|
2021-01-10 07:27:22 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected abstract int BadgeFlagStart { get; }
|
|
|
|
|
public abstract uint Coin { get; set; }
|
|
|
|
|
|
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++)
|
2018-09-15 05:37:47 +00:00
|
|
|
|
{
|
2018-07-08 00:01:47 +00:00
|
|
|
|
if (GetEventFlag(startFlag + i))
|
|
|
|
|
val |= 1 << i;
|
2018-09-15 05:37:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-07-08 00:01:47 +00:00
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override IReadOnlyList<InventoryPouch> Inventory
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2021-03-16 06:51:58 +00:00
|
|
|
|
var pouch = GetItems();
|
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)
|
2021-03-16 06:51:58 +00:00
|
|
|
|
p.SecurityKey = SecurityKey;
|
2016-07-01 04:53:24 +00:00
|
|
|
|
}
|
2021-03-08 07:22:07 +00:00
|
|
|
|
return pouch.LoadAll(Large);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2021-03-08 07:22:07 +00:00
|
|
|
|
set => value.SaveAll(Large);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected abstract InventoryPouch3[] GetItems();
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected abstract int DaycareSlotSize { get; }
|
2018-07-08 18:00:50 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override uint? GetDaycareEXP(int loc, int slot) => BitConverter.ToUInt32(Large, GetDaycareEXPOffset(slot));
|
|
|
|
|
public sealed override void SetDaycareEXP(int loc, int slot, uint EXP) => BitConverter.GetBytes(EXP).CopyTo(Large, GetDaycareEXPOffset(slot));
|
|
|
|
|
public sealed override bool? IsDaycareOccupied(int loc, int slot) => IsPKMPresent(Large, GetDaycareSlotOffset(loc, slot));
|
|
|
|
|
public sealed override void SetDaycareOccupied(int loc, int slot, bool occupied) { /* todo */ }
|
|
|
|
|
public sealed override int GetDaycareSlotOffset(int loc, int slot) => DaycareOffset + (slot * DaycareSlotSize);
|
2018-07-08 18:00:50 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected abstract int EggEventFlag { get; }
|
|
|
|
|
public sealed override bool? IsDaycareHasEgg(int loc) => GetEventFlag(EggEventFlag);
|
|
|
|
|
public sealed override void SetDaycareHasEgg(int loc, bool hasEgg) => SetEventFlag(EggEventFlag, hasEgg);
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected abstract int GetDaycareEXPOffset(int slot);
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
#region Storage
|
|
|
|
|
public sealed override int GetBoxOffset(int box) => Box + 4 + (SIZE_STORED * box * COUNT_SLOTSPERBOX);
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int CurrentBox
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Storage[0];
|
|
|
|
|
set => Storage[0] = (byte)value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override int GetBoxWallpaper(int box)
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
|
|
|
|
if (box > COUNT_BOX)
|
|
|
|
|
return box;
|
|
|
|
|
int offset = GetBoxWallpaperOffset(box);
|
|
|
|
|
return Storage[offset];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const int COUNT_BOXNAME = 8 + 1;
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override void SetBoxWallpaper(int box, int value)
|
2021-03-08 07:22:07 +00:00
|
|
|
|
{
|
|
|
|
|
if (box > COUNT_BOX)
|
|
|
|
|
return;
|
|
|
|
|
int offset = GetBoxWallpaperOffset(box);
|
|
|
|
|
Storage[offset] = (byte)value;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected sealed override int GetBoxWallpaperOffset(int box)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
int offset = GetBoxOffset(COUNT_BOX);
|
|
|
|
|
offset += (COUNT_BOX * COUNT_BOXNAME) + box;
|
2016-10-29 18:32:21 +00:00
|
|
|
|
return offset;
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override string GetBoxName(int box)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
int offset = GetBoxOffset(COUNT_BOX);
|
|
|
|
|
return StringConverter3.GetString3(Storage, offset + (box * COUNT_BOXNAME), COUNT_BOXNAME, Japanese);
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override void SetBoxName(int box, string value)
|
2016-06-26 21:23:41 +00:00
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
int offset = GetBoxOffset(COUNT_BOX);
|
|
|
|
|
SetString(value, COUNT_BOXNAME - 1).CopyTo(Storage, offset + (box * COUNT_BOXNAME));
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|
2021-03-16 06:51:58 +00:00
|
|
|
|
#endregion
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
#region Pokédex
|
|
|
|
|
protected sealed 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)
|
|
|
|
|
{
|
2019-06-01 17:22:49 +00:00
|
|
|
|
case (int)Species.Unown when !GetSeen(species): // Unown
|
2018-07-13 05:07:44 +00:00
|
|
|
|
DexPIDUnown = pkm.PID;
|
|
|
|
|
break;
|
2019-06-01 17:22:49 +00:00
|
|
|
|
case (int)Species.Spinda when !GetSeen(species): // Spinda
|
2018-07-13 05:07:44 +00:00
|
|
|
|
DexPIDSpinda = pkm.PID;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
SetCaught(species, true);
|
|
|
|
|
SetSeen(species, true);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +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;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-08 07:22:07 +00:00
|
|
|
|
public uint DexPIDUnown { get => BitConverter.ToUInt32(Small, PokeDex + 0x4); set => BitConverter.GetBytes(value).CopyTo(Small, PokeDex + 0x4); }
|
|
|
|
|
public uint DexPIDSpinda { get => BitConverter.ToUInt32(Small, PokeDex + 0x8); set => BitConverter.GetBytes(value).CopyTo(Small, PokeDex + 0x8); }
|
2018-07-13 05:07:44 +00:00
|
|
|
|
public int DexUnownForm => PKX.GetUnownForm(DexPIDUnown);
|
2017-03-15 05:41:15 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed 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;
|
2021-03-08 07:22:07 +00:00
|
|
|
|
return FlagUtil.GetFlag(Small, caughtOffset + ofs, bit & 7);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed 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;
|
2021-03-08 07:22:07 +00:00
|
|
|
|
FlagUtil.SetFlag(Small, caughtOffset + ofs, bit & 7, caught);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2017-03-15 05:41:15 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed 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;
|
2021-03-08 07:22:07 +00:00
|
|
|
|
return FlagUtil.GetFlag(Small, seenOffset + ofs, bit & 7);
|
2017-03-06 00:40:57 +00:00
|
|
|
|
}
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected abstract int SeenOffset2 { get; }
|
|
|
|
|
protected abstract int SeenOffset3 { get; }
|
|
|
|
|
|
|
|
|
|
public sealed 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
|
|
|
|
|
2021-03-08 07:22:07 +00:00
|
|
|
|
int seenOffset = PokeDex + 0x44;
|
|
|
|
|
FlagUtil.SetFlag(Small, seenOffset + ofs, bit & 7, seen);
|
2021-03-16 06:51:58 +00:00
|
|
|
|
FlagUtil.SetFlag(Large, SeenOffset2 + ofs, bit & 7, seen);
|
|
|
|
|
FlagUtil.SetFlag(Large, SeenOffset3 + ofs, bit & 7, seen);
|
2016-07-04 05:21:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 07:27:22 +00:00
|
|
|
|
public byte PokedexSort
|
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[PokeDex + 0x01];
|
|
|
|
|
set => Small[PokeDex + 0x01] = value;
|
2021-01-10 07:27:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte PokedexMode
|
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[PokeDex + 0x01];
|
|
|
|
|
set => Small[PokeDex + 0x01] = value;
|
2021-01-10 07:27:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte PokedexNationalMagicRSE
|
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[PokeDex + 0x02];
|
|
|
|
|
set => Small[PokeDex + 0x02] = value;
|
2021-01-10 07:27:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public byte PokedexNationalMagicFRLG
|
|
|
|
|
{
|
2021-03-08 07:22:07 +00:00
|
|
|
|
get => Small[PokeDex + 0x03];
|
|
|
|
|
set => Small[PokeDex + 0x03] = value;
|
2021-01-10 07:27:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected const int PokedexNationalUnlockRSE = 0xDA;
|
|
|
|
|
protected const int PokedexNationalUnlockFRLG = 0xDA;
|
|
|
|
|
protected const ushort PokedexNationalUnlockWorkRSE = 0x0302;
|
|
|
|
|
protected const ushort PokedexNationalUnlockWorkFRLG = 0x6258;
|
2021-01-10 07:27:22 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public abstract bool NationalDex { get; set; }
|
|
|
|
|
#endregion
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed override string GetString(byte[] data, int offset, int length) => StringConverter3.GetString3(data, offset, length, Japanese);
|
2018-09-15 05:37:47 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public sealed 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;
|
2019-03-21 04:50:44 +00:00
|
|
|
|
return StringConverter3.SetString3(value, maxLength, Japanese, PadToSize, PadWith);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2017-04-10 22:59:16 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
protected abstract int MailOffset { get; }
|
|
|
|
|
public int GetMailOffset(int index) => (index * Mail3.SIZE) + MailOffset;
|
2018-07-08 04:31:41 +00:00
|
|
|
|
|
2021-03-16 06:51:58 +00:00
|
|
|
|
public abstract string EBerryName { get; }
|
|
|
|
|
public abstract bool IsEBerryEngima { get; }
|
2021-03-17 06:32:16 +00:00
|
|
|
|
|
|
|
|
|
public byte[] GetHallOfFameData()
|
|
|
|
|
{
|
|
|
|
|
// HoF Data is split across two sectors
|
|
|
|
|
byte[] data = new byte[SIZE_SECTOR_USED * 2];
|
|
|
|
|
Buffer.BlockCopy(Data, 0x1C000, data, 0 , SIZE_SECTOR_USED);
|
|
|
|
|
Buffer.BlockCopy(Data, 0x1D000, data, SIZE_SECTOR_USED, SIZE_SECTOR_USED);
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetHallOfFameData(byte[] value)
|
|
|
|
|
{
|
|
|
|
|
if (value.Length != SIZE_SECTOR_USED * 2)
|
|
|
|
|
throw new ArgumentException("Invalid size", nameof(value));
|
|
|
|
|
// HoF Data is split across two sav sectors
|
|
|
|
|
Buffer.BlockCopy(value, 0 , Data, 0x1C000, SIZE_SECTOR_USED);
|
|
|
|
|
Buffer.BlockCopy(value, SIZE_SECTOR_USED, Data, 0x1D000, SIZE_SECTOR_USED);
|
|
|
|
|
}
|
2019-10-26 19:58:55 +00:00
|
|
|
|
}
|
2016-06-26 21:23:41 +00:00
|
|
|
|
}
|