Revise Stadium savefile logic to read from boxes

Add a helper to spit out teams
Alias hard-coded numbers to something to easy names.
Make them exportable, update checksums for box data
This commit is contained in:
Kurt 2020-09-30 18:00:25 -07:00
parent af4fa8078e
commit ee207a3fda
4 changed files with 182 additions and 34 deletions

View file

@ -5,7 +5,7 @@ namespace PKHeX.Core
{
public sealed class SAV1Stadium : SaveFile, ILangDeviantSave
{
protected override string BAKText => $"{OT} ({Version}) - {PlayTimeString}";
protected override string BAKText => $"{OT} ({Version})";
public override string Filter => "SAV File|*.sav|All Files|*.*";
public override string Extension => ".sav";
@ -21,8 +21,7 @@ namespace PKHeX.Core
public override SaveFile Clone() => new SAV1Stadium((byte[])Data.Clone(), Japanese);
public override bool ChecksumsValid => true;
public override string ChecksumInfo => string.Empty;
public override string ChecksumInfo => ChecksumsValid ? "Checksum valid." : "Checksum invalid";
public override int Generation => 1;
public override string GetString(byte[] data, int offset, int length) => StringConverter12.GetString1(data, offset, length, Japanese);
@ -34,11 +33,13 @@ namespace PKHeX.Core
return StringConverter12.SetString1(value, maxLength, Japanese, PadToSize, PadWith);
}
private int StringLength => Japanese ? 5 : 10;
private int StringLength => Japanese ? StringLengthJ : StringLengthU;
private const int StringLengthJ = 6;
private const int StringLengthU = 11;
public override int OTLength => StringLength;
public override int NickLength => StringLength;
public override int BoxCount => 84;
public override int BoxSlotCount => 6;
public override int BoxCount => Japanese ? 8 : 12;
public override int BoxSlotCount => Japanese ? 30 : 20;
public override int MaxMoveID => Legal.MaxMoveID_1;
public override int MaxSpeciesID => Legal.MaxSpeciesID_1;
@ -50,44 +51,100 @@ namespace PKHeX.Core
public override int MaxCoins => 9999;
public override int GetPartyOffset(int slot) => -1;
protected override void SetChecksums() { } // todo
public override bool ChecksumsValid => GetBoxChecksumsValid();
protected override void SetChecksums() => SetBoxChecksums();
private bool GetBoxChecksumsValid()
{
for (int i = 0; i < BoxCount; i++)
{
var boxOfs = GetBoxOffset(i) - ListHeaderSize;
var size = BoxSize - 2;
var chk = Checksums.CheckSum16(Data, boxOfs, size);
var actual = BigEndian.ToUInt16(Data, boxOfs + size);
if (chk != actual)
return false;
}
return true;
}
private void SetBoxChecksums()
{
for (int i = 0; i < BoxCount; i++)
{
var boxOfs = GetBoxOffset(i) - ListHeaderSize;
var size = BoxSize - 2;
var chk = Checksums.CheckSum16(Data, boxOfs, size);
BigEndian.GetBytes(chk).CopyTo(Data, boxOfs + size);
}
}
public override Type PKMType => typeof(PK1);
protected override PKM GetPKM(byte[] data)
{
int len = StringLength;
var nick = data.Slice(0x21, len);
var ot = data.Slice(0x21 + len, len);
var nick = data.Slice(PokeCrypto.SIZE_1STORED, len);
var ot = data.Slice(PokeCrypto.SIZE_1STORED + len, len);
data = data.Slice(0, PokeCrypto.SIZE_1STORED);
return new PK1(data, Japanese) {OT_Trash = ot, Nickname_Trash = nick};
}
protected override byte[] DecryptPKM(byte[] data) => data;
public override PKM BlankPKM => new PK1(Japanese);
protected override int SIZE_STORED => Japanese ? 0x2D : 0x37;
protected override int SIZE_PARTY => Japanese ? 0x2D : 0x37;
private const int SIZE_PK1J = PokeCrypto.SIZE_1STORED + (2 * StringLengthJ); // 0x2D
private const int SIZE_PK1U = PokeCrypto.SIZE_1STORED + (2 * StringLengthU); // 0x37
protected override int SIZE_STORED => Japanese ? SIZE_PK1J : SIZE_PK1U;
protected override int SIZE_PARTY => Japanese ? SIZE_PK1J : SIZE_PK1U;
public SAV1Stadium(byte[] data) : this(data, IsStadiumJ(data)) { }
public SAV1Stadium(byte[] data, bool japanese) : base(data, false)
public SAV1Stadium(byte[] data, bool japanese) : base(data)
{
Japanese = japanese;
Box = 0;
Box = 0xC000;
}
public SAV1Stadium(bool japanese = false) : base(SaveUtil.SIZE_G1STAD)
{
Japanese = japanese;
Box = 0;
Box = 0xC000;
ClearBoxes();
}
private int ListHeaderSize => Japanese ? 0x0C : 0x10;
private const int ListFooterSize = 6; // POKE + 2byte checksum
private const int TeamCount = 86; // todo
private int TeamSize => Japanese ? TeamSizeJ : TeamSizeU;
private const int TeamSizeU = 0x160;
private const int TeamSizeJ = 0x128;
public override int GetBoxOffset(int box) => (box * TeamSize) + 0x10;
public override string GetBoxName(int box) => $"Team {box + 1}";
private const int TeamSizeJ = 0x0C + (SIZE_PK1J * 6) + ListFooterSize; // 0x120
private const int TeamSizeU = 0x10 + (SIZE_PK1U * 6) + ListFooterSize; // 0x160
public int GetTeamOffset(int team) => 0 + ListHeaderSize + (team * TeamSize);
public static string GetTeamName(int team) => $"Team {team + 1}";
public BattleTeam<PK1> GetTeam(int team)
{
if ((uint)team >= TeamCount)
throw new ArgumentOutOfRangeException(nameof(team));
var name = GetTeamName(team);
var members = new PK1[6];
var ofs = GetTeamOffset(team);
for (int i = 0; i < 6; i++)
{
var rel = ofs + (i * SIZE_STORED);
members[i] = (PK1)GetStoredSlot(Data, rel);
}
return new BattleTeam<PK1>(name, members);
}
private int BoxSize => Japanese ? BoxSizeJ : BoxSizeU;
private const int BoxSizeJ = 0x0C + (SIZE_PK1J * 30) + ListFooterSize; // 0x558
private const int BoxSizeU = 0x10 + (SIZE_PK1U * 20) + 6 + ListFooterSize; // 0x468 (6 bytes alignment)
public override int GetBoxOffset(int box) => Box + ListHeaderSize + (box * BoxSize);
public override string GetBoxName(int box) => $"Box {box + 1}";
public override void SetBoxName(int box, string value) { }
public override void WriteSlotFormatStored(PKM pkm, byte[] data, int offset)
@ -106,14 +163,17 @@ namespace PKHeX.Core
base.WriteBoxSlot(pkm, Data, offset);
}
private const int MAGIC_POKE = 0x454B4F50;
public static bool IsStadiumU(byte[] data)
{
if (data.Length != SaveUtil.SIZE_G1STAD)
return false;
// Check footers of first few teams to see if the magic value is there.
for (int i = 0; i < 10; i++)
{
if (BitConverter.ToUInt32(data, 0x15A + (i * TeamSizeU)) != 0x454B4F50) // POKE
if (BitConverter.ToUInt32(data, TeamSizeU - ListFooterSize + (i * TeamSizeU)) != MAGIC_POKE) // POKE
return false;
}
return true;
@ -124,12 +184,25 @@ namespace PKHeX.Core
if (data.Length != SaveUtil.SIZE_G1STAD)
return false;
// Check footers of first few teams to see if the magic value is there.
for (int i = 0; i < 10; i++)
{
if (BitConverter.ToUInt32(data, 0x122 + (i * TeamSizeJ)) != 0x454B4F50) // POKE
if (BitConverter.ToUInt32(data, TeamSizeJ - ListFooterSize + (i * TeamSizeJ)) != MAGIC_POKE) // POKE
return false;
}
return true;
}
}
public class BattleTeam<T> where T : PKM
{
public readonly string TeamName;
public readonly T[] Team;
public BattleTeam(string name, T[] team)
{
TeamName = name;
Team = team;
}
}
}

View file

@ -6,12 +6,18 @@ namespace PKHeX.Core
/// <summary>
/// Pocket Monsters Stadium
/// </summary>
public class SAV1StadiumJ : SaveFile
public class SAV1StadiumJ : SaveFile, ILangDeviantSave
{
protected override string BAKText => $"{OT} ({Version}) - {PlayTimeString}";
protected override string BAKText => $"{OT} ({Version})";
public override string Filter => "SAV File|*.sav|All Files|*.*";
public override string Extension => ".sav";
// Required since PK1 logic comparing a save file assumes the save file can be U/J
public int SaveRevision => 0;
public string SaveRevisionString => string.Empty;
public bool Japanese => true;
public bool Korean => false;
public override PersonalTable Personal => PersonalTable.Y;
public override int MaxEV => ushort.MaxValue;
public override IReadOnlyList<ushort> HeldItems => Array.Empty<ushort>();
@ -19,8 +25,7 @@ namespace PKHeX.Core
public override SaveFile Clone() => new SAV1StadiumJ((byte[])Data.Clone());
public override bool ChecksumsValid => true;
public override string ChecksumInfo => string.Empty;
public override string ChecksumInfo => ChecksumsValid ? "Checksum valid." : "Checksum invalid";
public override int Generation => 1;
public override string GetString(byte[] data, int offset, int length) => StringConverter12.GetString1(data, offset, length, true);
@ -32,7 +37,7 @@ namespace PKHeX.Core
return StringConverter12.SetString1(value, maxLength, true, PadToSize, PadWith);
}
private int StringLength => 5;
private const int StringLength = 6; // Japanese Only
public override int OTLength => StringLength;
public override int NickLength => StringLength;
public override int BoxCount => 8;
@ -48,25 +53,54 @@ namespace PKHeX.Core
public override int MaxCoins => 9999;
public override int GetPartyOffset(int slot) => -1;
protected override void SetChecksums() { } // todo
public override bool ChecksumsValid => GetBoxChecksumsValid();
protected override void SetChecksums() => SetBoxChecksums();
private bool GetBoxChecksumsValid()
{
for (int i = 0; i < BoxCount; i++)
{
var boxOfs = GetBoxOffset(i) - ListHeaderSize;
const int size = BoxSizeJ - 2;
var chk = Checksums.CheckSum16(Data, boxOfs, size);
var actual = BigEndian.ToUInt16(Data, boxOfs + size);
if (chk != actual)
return false;
}
return true;
}
private void SetBoxChecksums()
{
for (int i = 0; i < BoxCount; i++)
{
var boxOfs = GetBoxOffset(i) - ListHeaderSize;
const int size = BoxSizeJ - 2;
var chk = Checksums.CheckSum16(Data, boxOfs, size);
BigEndian.GetBytes(chk).CopyTo(Data, boxOfs + size);
}
}
public override Type PKMType => typeof(PK1);
protected override PKM GetPKM(byte[] data)
{
int len = StringLength;
const int len = StringLength;
var nick = data.Slice(0x21, len);
var ot = data.Slice(0x21 + len, len);
data = data.Slice(0, 0x21);
return new PK1(data, true) { OT_Trash = ot, Nickname_Trash = nick };
}
protected override byte[] DecryptPKM(byte[] data) => data;
public override PKM BlankPKM => new PK1(true);
protected override int SIZE_STORED => 0x2D;
protected override int SIZE_PARTY => 0x2D;
private const int SIZE_PK1J = PokeCrypto.SIZE_1STORED + (2 * StringLength); // 0x2D
protected override int SIZE_STORED => SIZE_PK1J;
protected override int SIZE_PARTY => SIZE_PK1J;
public SAV1StadiumJ(byte[] data) : base(data, false)
public SAV1StadiumJ(byte[] data) : base(data)
{
Box = 0x2500;
}
@ -77,9 +111,32 @@ namespace PKHeX.Core
ClearBoxes();
}
private const int TeamSizeJ = 0x128;
private const int ListHeaderSize = 0x14;
private const int ListFooterSize = 6; // POKE + 2byte checksum
private const int TeamCount = 86; // todo
private const int TeamSizeJ = 0x14 + (SIZE_PK1J * 6) + ListFooterSize; // 0x128
public static int GetTeamOffset(int team) => 0 + ListHeaderSize + (team * TeamSizeJ);
public static string GetTeamName(int team) => $"Team {team + 1}";
public BattleTeam<PK1> GetTeam(int team)
{
if ((uint)team >= TeamCount)
throw new ArgumentOutOfRangeException(nameof(team));
var name = GetTeamName(team);
var members = new PK1[6];
var ofs = GetTeamOffset(team);
for (int i = 0; i < 6; i++)
{
var rel = ofs + (i * SIZE_STORED);
members[i] = (PK1)GetStoredSlot(Data, rel);
}
return new BattleTeam<PK1>(name, members);
}
private const int BoxSizeJ = 0x560;
public override int GetBoxOffset(int box) => (box * BoxSizeJ) + 0x14;
public override int GetBoxOffset(int box) => Box + ListHeaderSize + (box * BoxSizeJ);
public override string GetBoxName(int box) => $"Box {box + 1}";
public override void SetBoxName(int box, string value) { }
@ -99,14 +156,17 @@ namespace PKHeX.Core
base.WriteBoxSlot(pkm, Data, offset);
}
private const int MAGIC_POKE = 0x454B4F50;
public static bool IsStadiumJ(byte[] data)
{
if (data.Length != SaveUtil.SIZE_G1STAD)
if (data.Length != SaveUtil.SIZE_G1STADJ)
return false;
// Check footers of first few teams to see if the magic value is there.
for (int i = 0; i < 10; i++)
{
if (BitConverter.ToUInt32(data, 0x11A + (i * TeamSizeJ)) != 0x454B4F50) // POKE
if (BitConverter.ToUInt32(data, TeamSizeJ - ListFooterSize + (i * TeamSizeJ)) != MAGIC_POKE) // POKE
return false;
}
return true;

View file

@ -110,6 +110,20 @@ namespace PKHeX.Core
return (ushort)(val + (val >> 16));
}
/// <summary>Calculates the 16bit checksum over an input byte array. Used in N64 Stadium save files.</summary>
/// <param name="data">Input byte array</param>
/// <param name="start">Offset to start checksum at</param>
/// <param name="length">Length of array to checksum</param>
/// <param name="initial">Initial value for checksum</param>
/// <returns>Checksum</returns>
public static ushort CheckSum16(byte[] data, int start, int length, ushort initial = 0)
{
ushort acc = initial;
for (int i = 0; i < length; i++)
acc += data[start + i];
return acc;
}
/// <summary>Calculates the 32bit checksum over an input byte array. Used in GBA save files.</summary>
/// <param name="data">Input byte array</param>
/// <param name="initial">Initial value for checksum</param>

View file

@ -48,6 +48,7 @@ namespace PKHeX.Core
public const int SIZE_G2BAT_J = 0x1002C;
public const int SIZE_G2EMU_J = 0x10030;
public const int SIZE_G1STAD = 0x20000; // same as G3RAW_U
public const int SIZE_G1STADJ = 0x8000; // same as G1RAW
public const int SIZE_G1RAW = 0x8000;
public const int SIZE_G1BAT = 0x802C;