PKHeX/PKHeX.Core/Saves/SAV1.cs

557 lines
19 KiB
C#
Raw Normal View History

2016-08-27 11:33:21 +00:00
using System;
using System.Linq;
namespace PKHeX.Core
2016-08-27 11:33:21 +00:00
{
/// <summary>
/// Generation 1 <see cref="SaveFile"/> object.
/// </summary>
2016-08-27 11:33:21 +00:00
public sealed class SAV1 : SaveFile
{
protected override string BAKText => $"{OT} ({Version}) - {PlayTimeString}";
public override string Filter => "SAV File|*.sav|All Files|*.*";
2016-08-27 11:33:21 +00:00
public override string Extension => ".sav";
public override string[] PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
return 1 <= gen && gen <= 2;
}).ToArray();
2016-08-27 11:33:21 +00:00
public SAV1(byte[] data = null, GameVersion versionOverride = GameVersion.Any)
2016-08-27 11:33:21 +00:00
{
Data = data ?? new byte[SaveUtil.SIZE_G1RAW];
2016-08-27 11:33:21 +00:00
BAK = (byte[])Data.Clone();
Exportable = !IsRangeEmpty(0, Data.Length);
2016-08-27 11:33:21 +00:00
if (versionOverride != GameVersion.Any)
Version = versionOverride;
else if(data == null)
Version = GameVersion.RBY;
else Version = SaveUtil.GetIsG1SAV(Data);
2016-08-27 11:33:21 +00:00
if (Version == GameVersion.Invalid)
return;
2018-08-15 03:12:10 +00:00
Japanese = SaveUtil.GetIsG1SAVJ(Data);
Offsets = Japanese ? SAV1Offsets.JPN : SAV1Offsets.INT;
// see if RBY can be differentiated
if (Starter != 0 && versionOverride != GameVersion.Any)
Version = Yellow ? GameVersion.YW : GameVersion.RB;
2016-08-27 11:33:21 +00:00
Box = Data.Length;
Array.Resize(ref Data, Data.Length + SIZE_RESERVED);
Party = GetPartyOffset(0);
2016-08-27 11:33:21 +00:00
Personal = Version == GameVersion.Y ? PersonalTable.Y : PersonalTable.RB;
2016-08-27 11:33:21 +00:00
// Stash boxes after the save file's end.
int stored = SIZE_STOREDBOX;
int baseDest = Data.Length - SIZE_RESERVED;
var capacity = Japanese ? PokeListType.StoredJP : PokeListType.Stored;
for (int i = 0; i < BoxCount; i++)
{
int ofs = GetBoxRawDataOffset(i);
var box = GetData(ofs, stored);
var boxDest = baseDest + (i * SIZE_BOX);
var boxPL = new PokeList1(box, capacity, Japanese);
for (int j = 0; j < boxPL.Pokemon.Length; j++)
{
var dest = boxDest + (j * SIZE_STORED);
var pkDat = (j < boxPL.Count)
? new PokeList1(boxPL[j]).Write()
: new byte[PokeList1.GetDataLength(PokeListType.Single, Japanese)];
pkDat.CopyTo(Data, dest);
}
}
var current = GetData(Offsets.CurrentBox, SIZE_STOREDBOX);
var curBoxPL = new PokeList1(current, capacity, Japanese);
for (int i = 0; i < curBoxPL.Pokemon.Length; i++)
{
var dest = Data.Length - SIZE_RESERVED + (CurrentBox * SIZE_BOX) + (i * SIZE_STORED);
var pkDat = i < curBoxPL.Count
? new PokeList1(curBoxPL[i]).Write()
: new byte[PokeList1.GetDataLength(PokeListType.Single, Japanese)];
pkDat.CopyTo(Data, dest);
}
var party = GetData(Offsets.Party, SIZE_STOREDPARTY);
var partyPL = new PokeList1(party, PokeListType.Party, Japanese);
for (int i = 0; i < partyPL.Pokemon.Length; i++)
{
var dest = GetPartyOffset(i);
var pkDat = i < partyPL.Count
? new PokeList1(partyPL[i]).Write()
: new byte[PokeList1.GetDataLength(PokeListType.Single, Japanese)];
pkDat.CopyTo(Data, dest);
}
2016-08-29 20:27:17 +00:00
byte[] rawDC = new byte[0x38];
Array.Copy(Data, Offsets.Daycare, rawDC, 0, rawDC.Length);
byte[] TempDaycare = new byte[PokeList1.GetDataLength(PokeListType.Single, Japanese)];
2016-08-29 20:27:17 +00:00
TempDaycare[0] = rawDC[0];
Array.Copy(rawDC, 1, TempDaycare, 2 + 1 + PKX.SIZE_1PARTY + StringLength, StringLength);
Array.Copy(rawDC, 1 + StringLength, TempDaycare, 2 + 1 + PKX.SIZE_1PARTY, StringLength);
Array.Copy(rawDC, 1 + (2 * StringLength), TempDaycare, 2 + 1, PKX.SIZE_1STORED);
PokeList1 daycareList = new PokeList1(TempDaycare, PokeListType.Single, Japanese);
daycareList.Write().CopyTo(Data, GetPartyOffset(7));
Daycare = GetPartyOffset(7);
EventFlag = Offsets.EventFlag;
2016-08-30 00:48:05 +00:00
// Enable Pokedex editing
PokeDex = 0;
2016-08-27 11:33:21 +00:00
if (!Exportable)
ClearBoxes();
2016-08-27 11:33:21 +00:00
}
private readonly SAV1Offsets Offsets;
// Event Flags
protected override int EventFlagMax => EventFlag > 0 ? 0xA00 : int.MinValue; // 320 * 8
protected override int EventConstMax => 0;
private const int SIZE_RESERVED = 0x8000; // unpacked box data
protected override byte[] Write(bool DSV)
2016-08-27 11:33:21 +00:00
{
var capacity = Japanese ? PokeListType.StoredJP : PokeListType.Stored;
2016-08-29 20:27:17 +00:00
for (int i = 0; i < BoxCount; i++)
{
var boxPL = new PokeList1(capacity, Japanese);
2016-08-29 20:27:17 +00:00
int slot = 0;
for (int j = 0; j < boxPL.Pokemon.Length; j++)
{
PK1 boxPK = (PK1)GetPKM(GetData(GetBoxOffset(i) + (j * SIZE_STORED), SIZE_STORED));
2016-08-29 20:27:17 +00:00
if (boxPK.Species > 0)
boxPL[slot++] = boxPK;
}
// copy to box location
var boxdata = boxPL.Write();
int ofs = GetBoxRawDataOffset(i);
SetData(boxdata, ofs);
// copy to active loc if current box
2016-08-29 20:27:17 +00:00
if (i == CurrentBox)
SetData(boxdata, Offsets.CurrentBox);
2016-08-29 20:27:17 +00:00
}
var partyPL = new PokeList1(PokeListType.Party, Japanese);
2016-08-29 20:27:17 +00:00
int pSlot = 0;
for (int i = 0; i < 6; i++)
{
PK1 partyPK = (PK1)GetPKM(GetData(GetPartyOffset(i), SIZE_STORED));
2016-08-29 20:27:17 +00:00
if (partyPK.Species > 0)
partyPL[pSlot++] = partyPK;
}
partyPL.Write().CopyTo(Data, Offsets.Party);
2016-08-29 20:27:17 +00:00
// Daycare is read-only, but in case it ever becomes editable, copy it back in.
byte[] rawDC = GetData(GetDaycareSlotOffset(loc: 0, slot: 0), SIZE_STORED);
byte[] dc = new byte[1 + (2 * StringLength) + PKX.SIZE_1STORED];
2016-08-29 20:27:17 +00:00
dc[0] = rawDC[0];
Array.Copy(rawDC, 2 + 1 + PKX.SIZE_1PARTY + StringLength, dc, 1, StringLength);
Array.Copy(rawDC, 2 + 1 + PKX.SIZE_1PARTY, dc, 1 + StringLength, StringLength);
Array.Copy(rawDC, 2 + 1, dc, 1 + (2 * StringLength), PKX.SIZE_1STORED);
dc.CopyTo(Data, Offsets.Daycare);
2016-08-29 20:27:17 +00:00
SetChecksums();
2016-08-29 20:27:17 +00:00
byte[] outData = new byte[Data.Length - SIZE_RESERVED];
Array.Copy(Data, outData, outData.Length);
return outData;
2016-08-27 11:33:21 +00:00
}
private int GetBoxRawDataOffset(int i)
{
if (i < BoxCount / 2)
return 0x4000 + (i * SIZE_STOREDBOX);
return 0x6000 + ((i - (BoxCount / 2)) * SIZE_STOREDBOX);
}
2016-08-27 11:33:21 +00:00
// Configuration
2016-09-11 21:54:20 +00:00
public override SaveFile Clone() { return new SAV1(Write(DSV: false)); }
2016-08-27 11:33:21 +00:00
public override int SIZE_STORED => Japanese ? PKX.SIZE_1JLIST : PKX.SIZE_1ULIST;
protected override int SIZE_PARTY => Japanese ? PKX.SIZE_1JLIST : PKX.SIZE_1ULIST;
private int SIZE_BOX => BoxSlotCount*SIZE_STORED;
private int SIZE_STOREDBOX => PokeList1.GetDataLength(Japanese ? PokeListType.StoredJP : PokeListType.Stored, Japanese);
private int SIZE_STOREDPARTY => PokeList1.GetDataLength(PokeListType.Party, Japanese);
2016-08-29 20:27:17 +00:00
public override PKM BlankPKM => new PK1(null, null, Japanese);
public override Type PKMType => typeof(PK1);
2016-08-27 11:33:21 +00:00
public override int MaxMoveID => Legal.MaxMoveID_1;
public override int MaxSpeciesID => Legal.MaxSpeciesID_1;
public override int MaxAbilityID => Legal.MaxAbilityID_1;
public override int MaxItemID => Legal.MaxItemID_1;
public override int MaxBallID => 0; // unused
public override int MaxGameID => 99; // unused
public override int MaxMoney => 999999;
public override int MaxCoins => 9999;
2016-08-27 11:33:21 +00:00
public override int BoxCount => Japanese ? 8 : 12;
public override int MaxEV => 65535;
public override int MaxIV => 15;
2016-08-27 11:33:21 +00:00
public override int Generation => 1;
protected override int GiftCountMax => 0;
2016-09-04 22:44:54 +00:00
public override int OTLength => Japanese ? 5 : 7;
2016-08-27 11:33:21 +00:00
public override int NickLength => Japanese ? 5 : 10;
public override int BoxSlotCount => Japanese ? 30 : 20;
2016-08-27 11:33:21 +00:00
public override bool HasParty => true;
private int StringLength => Japanese ? _K12.STRLEN_J : _K12.STRLEN_U;
2016-08-27 11:33:21 +00:00
public override bool IsPKMPresent(int Offset) => PKX.IsPKMPresentGB(Data, Offset);
2016-08-27 11:33:21 +00:00
// Checksums
protected override void SetChecksums() => Data[Offsets.ChecksumOfs] = GetRBYChecksum(Offsets.ChecksumOfs);
public override bool ChecksumsValid => Data[Offsets.ChecksumOfs] == GetRBYChecksum(Offsets.ChecksumOfs);
public override string ChecksumInfo => ChecksumsValid ? "Checksum valid." : "Checksum invalid";
2016-08-27 11:33:21 +00:00
private const int CHECKSUM_START = 0x2598;
2016-08-27 11:33:21 +00:00
private byte GetRBYChecksum(int end)
2016-08-27 11:33:21 +00:00
{
byte chksum = 0;
for (int i = CHECKSUM_START; i < end; i++)
chksum += Data[i];
chksum ^= 0xFF;
return chksum;
2016-08-27 11:33:21 +00:00
}
// Trainer Info
public override GameVersion Version { get; protected set; }
public override string OT
{
get => GetString(0x2598, OTLength);
set => SetString(value, OTLength).CopyTo(Data, 0x2598);
2016-08-27 11:33:21 +00:00
}
public byte[] OT_Trash { get => GetData(0x2598, StringLength); set { if (value?.Length == StringLength) SetData(value, 0x2598); } }
2016-08-27 11:33:21 +00:00
public override int Gender
{
get => 0;
2016-08-27 11:33:21 +00:00
set { }
}
public override int TID
2016-08-27 11:33:21 +00:00
{
get => BigEndian.ToUInt16(Data, Offsets.TID);
set => BigEndian.GetBytes((ushort)value).CopyTo(Data, Offsets.TID);
2016-08-27 11:33:21 +00:00
}
public override int SID { get => 0; set { } }
public bool Yellow => Starter == 0x54; // Pikachu
public int Starter => Data[Offsets.Starter];
public byte PikaFriendship
{
get => Data[Offsets.PikaFriendship];
set => Data[Offsets.PikaFriendship] = value;
}
public override string PlayTimeString => !PlayedMaximum ? base.PlayTimeString : $"{base.PlayTimeString} {SaveUtil.CRC16_CCITT(Data):X4}";
2016-08-27 11:33:21 +00:00
public override int PlayedHours
{
get => Data[Offsets.PlayTime + 0];
set
{
if (value >= byte.MaxValue) // Set 255:00:00.00 and flag
{
PlayedMaximum = true;
value = byte.MaxValue;
PlayedMinutes = PlayedSeconds = PlayedFrames = 0;
}
Data[Offsets.PlayTime + 0] = (byte) value;
}
}
public bool PlayedMaximum
{
get => Data[Offsets.PlayTime + 1] != 0;
set => Data[Offsets.PlayTime + 1] = (byte)(value ? 1 : 0);
2016-08-27 11:33:21 +00:00
}
2016-08-27 11:33:21 +00:00
public override int PlayedMinutes
{
get => Data[Offsets.PlayTime + 2];
set => Data[Offsets.PlayTime + 2] = (byte)value;
2016-08-27 11:33:21 +00:00
}
2016-08-27 11:33:21 +00:00
public override int PlayedSeconds
{
get => Data[Offsets.PlayTime + 3];
set => Data[Offsets.PlayTime + 3] = (byte)value;
}
public int PlayedFrames
{
get => Data[Offsets.PlayTime + 4];
set => Data[Offsets.PlayTime + 4] = (byte)value;
2016-08-27 11:33:21 +00:00
}
2016-08-30 00:48:05 +00:00
public int Badges
{
get => Data[Offsets.Badges];
set { if (value < 0) return; Data[Offsets.Badges] = (byte)value; }
2016-08-30 00:48:05 +00:00
}
2016-08-30 00:48:05 +00:00
private byte Options
{
get => Data[Offsets.Options];
set => Data[Offsets.Options] = value;
2016-08-30 00:48:05 +00:00
}
2016-08-30 00:48:05 +00:00
public bool BattleEffects
{
get => (Options & 0x80) == 0;
set => Options = (byte)((Options & 0x7F) | (value ? 0 : 0x80));
2016-08-30 00:48:05 +00:00
}
2016-08-30 00:48:05 +00:00
public bool BattleStyleSwitch
{
get => (Options & 0x40) == 0;
set => Options = (byte)((Options & 0xBF) | (value ? 0 : 0x40));
2016-08-30 00:48:05 +00:00
}
2016-08-30 00:48:05 +00:00
public int Sound
{
get => (Options & 0x30) >> 4;
2016-08-30 00:48:05 +00:00
set
{
var new_sound = value;
if (new_sound > 3)
new_sound = 3;
if (new_sound < 0)
new_sound = 0;
Options = (byte)((Options & 0xCF) | (new_sound << 4));
}
}
2016-08-30 00:48:05 +00:00
public int TextSpeed
{
get => Options & 0x7;
2016-08-30 00:48:05 +00:00
set
{
var new_speed = value;
if (new_speed > 7)
new_speed = 7;
if (new_speed < 0)
new_speed = 0;
Options = (byte)((Options & 0xF8) | new_speed);
}
}
2016-08-27 11:33:21 +00:00
public override uint Money
{
get => (uint)BigEndian.BCDToInt32(Data, Offsets.Money, 3);
2016-08-27 11:33:21 +00:00
set
{
value = (uint)Math.Min(value, MaxMoney);
BigEndian.Int32ToBCD((int)value, 3).CopyTo(Data, Offsets.Money);
2016-08-27 11:33:21 +00:00
}
}
2016-08-27 11:33:21 +00:00
public uint Coin
{
get => (uint)BigEndian.BCDToInt32(Data, Offsets.Coin, 2);
2016-08-27 11:33:21 +00:00
set
{
value = (ushort)Math.Min(value, MaxCoins);
BigEndian.Int32ToBCD((int)value, 2).CopyTo(Data, Offsets.Coin);
2016-08-27 11:33:21 +00:00
}
}
private readonly ushort[] LegalItems = Legal.Pouch_Items_RBY;
2016-08-27 11:33:21 +00:00
public override InventoryPouch[] Inventory
{
get
{
ushort[] legalItems = LegalItems;
InventoryPouch[] pouch =
{
new InventoryPouchGB(InventoryType.Items, legalItems, 99, Offsets.Items, 20),
new InventoryPouchGB(InventoryType.PCItems, legalItems, 99, Offsets.PCItems, 50)
2016-08-27 11:33:21 +00:00
};
foreach (var p in pouch)
p.GetPouch(Data);
2016-08-27 11:33:21 +00:00
return pouch;
}
set
{
foreach (var p in value)
p.SetPouch(Data);
2016-08-27 11:33:21 +00:00
}
}
public override int GetDaycareSlotOffset(int loc, int slot)
2016-08-27 11:33:21 +00:00
{
return Daycare;
}
public override uint? GetDaycareEXP(int loc, int slot)
2016-08-27 11:33:21 +00:00
{
return null;
}
public override bool? IsDaycareOccupied(int loc, int slot)
2016-08-27 11:33:21 +00:00
{
if (loc == 0 && slot == 0)
return Data[Offsets.Daycare] == 0x01;
else
return null;
2016-08-27 11:33:21 +00:00
}
2019-02-02 18:19:41 +00:00
public override void SetDaycareEXP(int loc, int slot, uint EXP)
{
// todo
}
2019-02-02 18:19:41 +00:00
public override void SetDaycareOccupied(int loc, int slot, bool occupied)
{
// todo
}
2016-08-27 11:33:21 +00:00
// Storage
public override int PartyCount
{
get => Data[Offsets.Party];
protected set => Data[Offsets.Party] = (byte)value;
2016-08-27 11:33:21 +00:00
}
public override int GetBoxOffset(int box)
2016-08-27 11:33:21 +00:00
{
return Data.Length - SIZE_RESERVED + (box * SIZE_BOX);
2016-08-27 11:33:21 +00:00
}
public override int GetPartyOffset(int slot)
2016-08-27 11:33:21 +00:00
{
return Data.Length - SIZE_RESERVED + (BoxCount * SIZE_BOX) + (slot * SIZE_STORED);
2016-08-27 11:33:21 +00:00
}
2016-08-27 11:33:21 +00:00
public override int CurrentBox
{
get => Data[Offsets.CurrentBoxIndex] & 0x7F;
set => Data[Offsets.CurrentBoxIndex] = (byte)((Data[Offsets.CurrentBoxIndex] & 0x80) | (value & 0x7F));
2016-08-27 11:33:21 +00:00
}
public override string GetBoxName(int box)
2016-08-27 11:33:21 +00:00
{
return $"BOX {box + 1}";
2016-08-27 11:33:21 +00:00
}
public override void SetBoxName(int box, string value)
2016-08-27 11:33:21 +00:00
{
// Don't allow for custom box names
2016-08-27 11:33:21 +00:00
}
public override PKM GetPKM(byte[] data)
2016-08-27 11:33:21 +00:00
{
if (data.Length == SIZE_STORED)
return new PokeList1(data, PokeListType.Single, Japanese)[0];
2016-08-27 11:33:21 +00:00
return new PK1(data);
}
public override byte[] DecryptPKM(byte[] data)
2016-08-27 11:33:21 +00:00
{
return data;
}
// Pokédex
protected override void SetDex(PKM pkm)
2016-08-30 00:48:05 +00:00
{
int species = pkm.Species;
if (!CanSetDex(species))
return;
2016-08-30 00:48:05 +00:00
SetCaught(pkm.Species, true);
SetSeen(pkm.Species, true);
2016-08-30 00:48:05 +00:00
}
private bool CanSetDex(int species)
2016-08-30 00:48:05 +00:00
{
if (species <= 0)
2016-08-30 00:48:05 +00:00
return false;
if (species > MaxSpeciesID)
2016-08-30 00:48:05 +00:00
return false;
if (Version == GameVersion.Invalid)
2016-08-30 00:48:05 +00:00
return false;
return true;
}
public override void SetSeen(int species, bool seen)
{
int bit = species - 1;
2016-08-30 00:48:05 +00:00
int ofs = bit >> 3;
SetFlag(Offsets.DexSeen + ofs, bit & 7, seen);
2016-08-30 00:48:05 +00:00
}
public override void SetCaught(int species, bool caught)
2016-08-27 11:33:21 +00:00
{
int bit = species - 1;
int ofs = bit >> 3;
SetFlag(Offsets.DexCaught + ofs, bit & 7, caught);
}
public override bool GetSeen(int species)
{
int bit = species - 1;
2016-08-27 11:33:21 +00:00
int ofs = bit >> 3;
return GetFlag(Offsets.DexSeen + ofs, bit & 7);
2016-08-30 00:48:05 +00:00
}
public override bool GetCaught(int species)
2016-08-30 00:48:05 +00:00
{
int bit = species - 1;
2016-08-30 00:48:05 +00:00
int ofs = bit >> 3;
return GetFlag(Offsets.DexCaught + ofs, bit & 7);
2016-08-27 11:33:21 +00:00
}
public override void SetStoredSlot(PKM pkm, int offset, bool? trade = null, bool? dex = null)
{
// pkm that have never been boxed have yet to save the 'current level' for box indication
// set this value at this time
((PK1)pkm).Stat_LevelBox = pkm.CurrentLevel;
base.SetStoredSlot(pkm, offset, trade, dex);
}
private const int SpawnFlagCount = 0xF0;
public bool[] EventSpawnFlags
{
get
{
// RB uses 0xE4 (0xE8) flags, Yellow uses 0xF0 flags. Just grab 0xF0
bool[] data = new bool[SpawnFlagCount];
for (int i = 0; i < data.Length; i++)
data[i] = GetFlag(Offsets.ObjectSpawnFlags + i >> 3, i & 7);
return data;
}
set
{
if (value?.Length != SpawnFlagCount)
return;
for (int i = 0; i < value.Length; i++)
SetFlag(Offsets.ObjectSpawnFlags + i >> 3, i & 7, value[i]);
}
}
2018-12-05 06:00:57 +00:00
public override string GetString(byte[] data, int offset, int length) => StringConverter.GetString1(data, offset, length, Japanese);
public override byte[] SetString(string value, int maxLength, int PadToSize = 0, ushort PadWith = 0)
{
if (PadToSize == 0)
PadToSize = maxLength + 1;
return StringConverter.SetString1(value, maxLength, Japanese, PadToSize, PadWith);
}
2016-08-27 11:33:21 +00:00
}
}