2016-09-26 23:14:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-09-26 23:14:11 +00:00
|
|
|
|
{
|
|
|
|
|
public sealed class SAV4BR : SaveFile
|
|
|
|
|
{
|
2016-12-08 06:57:08 +00:00
|
|
|
|
public override string BAKName => $"{FileName} [{Version} #{SaveCount:0000}].bak";
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public override string Filter => "PbrSaveData|*";
|
|
|
|
|
public override string Extension => "";
|
|
|
|
|
|
2016-09-26 23:43:52 +00:00
|
|
|
|
private const int SAVE_COUNT = 4;
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public SAV4BR(byte[] data = null)
|
|
|
|
|
{
|
|
|
|
|
Data = data == null ? new byte[SaveUtil.SIZE_G4BR] : (byte[])data.Clone();
|
|
|
|
|
BAK = (byte[])Data.Clone();
|
|
|
|
|
Exportable = !Data.SequenceEqual(new byte[Data.Length]);
|
|
|
|
|
|
|
|
|
|
if (SaveUtil.getIsG4BRSAV(Data) != GameVersion.BATREV)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Data = DecryptPBRSaveData(data);
|
|
|
|
|
|
|
|
|
|
// Detect active save
|
2016-12-11 06:57:31 +00:00
|
|
|
|
SaveCount = Math.Max(BigEndian.ToUInt32(Data, 0x1C004C), BigEndian.ToUInt32(Data, 0x4C));
|
2016-09-26 23:43:52 +00:00
|
|
|
|
if (BigEndian.ToUInt32(Data, 0x1C004C) > BigEndian.ToUInt32(Data, 0x4C))
|
2016-09-26 23:14:11 +00:00
|
|
|
|
{
|
|
|
|
|
byte[] tempData = new byte[0x1C0000];
|
|
|
|
|
Array.Copy(Data, 0, tempData, 0, 0x1C0000);
|
|
|
|
|
Array.Copy(Data, 0x1C0000, Data, 0, 0x1C0000);
|
|
|
|
|
tempData.CopyTo(Data, 0x1C0000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SaveSlots = new List<int>();
|
2016-09-26 23:43:52 +00:00
|
|
|
|
SaveNames = new string[SAVE_COUNT];
|
|
|
|
|
for (int i = 0; i < SAVE_COUNT; i++)
|
2016-09-26 23:14:11 +00:00
|
|
|
|
{
|
|
|
|
|
if (BitConverter.ToUInt16(Data, 0x390 + 0x6FF00*i) != 0)
|
|
|
|
|
{
|
|
|
|
|
SaveSlots.Add(i);
|
|
|
|
|
SaveNames[i] = Encoding.BigEndianUnicode.GetString(Data, 0x390 + 0x6FF00*i, 0x10);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CurrentSlot = SaveSlots.First();
|
|
|
|
|
|
|
|
|
|
Personal = PersonalTable.DP;
|
|
|
|
|
HeldItems = Legal.HeldItems_DP;
|
|
|
|
|
|
|
|
|
|
if (!Exportable)
|
|
|
|
|
resetBoxes();
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 06:57:31 +00:00
|
|
|
|
private readonly uint SaveCount;
|
2016-09-26 23:43:52 +00:00
|
|
|
|
public override byte[] Write(bool DSV)
|
2016-09-26 23:14:11 +00:00
|
|
|
|
{
|
|
|
|
|
setChecksums();
|
|
|
|
|
return EncryptPBRSaveData(Data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configuration
|
2016-09-26 23:43:52 +00:00
|
|
|
|
public override SaveFile Clone() { return new SAV4BR(Write(DSV: false)); }
|
2016-09-26 23:14:11 +00:00
|
|
|
|
|
2016-09-26 23:43:52 +00:00
|
|
|
|
public readonly List<int> SaveSlots;
|
|
|
|
|
public readonly string[] SaveNames;
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public int CurrentSlot;
|
2016-09-26 23:43:52 +00:00
|
|
|
|
protected override int Box { // 4 save slots, data reading depends on current slot
|
2016-09-26 23:14:11 +00:00
|
|
|
|
get { return 0x978 + 0x6FF00*CurrentSlot; }
|
|
|
|
|
set { }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override int SIZE_STORED => PKX.SIZE_4STORED;
|
|
|
|
|
public override int SIZE_PARTY => PKX.SIZE_4PARTY - 0x10; // PBR has a party
|
|
|
|
|
public override PKM BlankPKM => new BK4();
|
|
|
|
|
public override Type PKMType => typeof(BK4);
|
|
|
|
|
|
|
|
|
|
public override int MaxMoveID => 467;
|
2016-10-24 04:59:27 +00:00
|
|
|
|
public override int MaxSpeciesID => Legal.MaxSpeciesID_4;
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public override int MaxAbilityID => 123;
|
|
|
|
|
public override int MaxItemID => 536;
|
|
|
|
|
public override int MaxBallID => 0x18;
|
|
|
|
|
public override int MaxGameID => 15;
|
|
|
|
|
|
|
|
|
|
public override int MaxEV => 255;
|
|
|
|
|
public override int Generation => 4;
|
|
|
|
|
protected override int GiftCountMax => 1;
|
2017-04-10 04:53:53 +00:00
|
|
|
|
public override int OTLength => 7;
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public override int NickLength => 10;
|
|
|
|
|
public override int MaxMoney => 999999;
|
|
|
|
|
|
|
|
|
|
public override int BoxCount => 18;
|
|
|
|
|
public override bool HasParty => false;
|
|
|
|
|
|
|
|
|
|
// Checksums
|
|
|
|
|
protected override void setChecksums()
|
|
|
|
|
{
|
|
|
|
|
SetChecksum(Data, 0, 0x100, 8);
|
|
|
|
|
SetChecksum(Data, 0, 0x1C0000, 0x1BFF80);
|
|
|
|
|
SetChecksum(Data, 0x1C0000, 0x100, 0x1C0008);
|
|
|
|
|
SetChecksum(Data, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000);
|
|
|
|
|
}
|
|
|
|
|
public override bool ChecksumsValid
|
|
|
|
|
{
|
|
|
|
|
get {
|
|
|
|
|
bool valid = VerifyChecksum(Data, 0, 0x1C0000, 0x1BFF80);
|
|
|
|
|
valid &= VerifyChecksum(Data, 0, 0x100, 8);
|
|
|
|
|
valid &= VerifyChecksum(Data, 0x1C0000, 0x1C0000, 0x1BFF80 + 0x1C0000);
|
|
|
|
|
valid &= VerifyChecksum(Data, 0x1C0000, 0x100, 0x1C0008);
|
|
|
|
|
return valid;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-26 23:43:52 +00:00
|
|
|
|
public override string ChecksumInfo => $"Checksums valid: {ChecksumsValid}.";
|
2016-09-26 23:14:11 +00:00
|
|
|
|
|
|
|
|
|
// Trainer Info
|
|
|
|
|
public override GameVersion Version { get { return GameVersion.BATREV; } protected set { } }
|
|
|
|
|
|
|
|
|
|
// Storage
|
2016-09-26 23:43:52 +00:00
|
|
|
|
public override int getPartyOffset(int slot) // TODO
|
2016-09-26 23:14:11 +00:00
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
public override int getBoxOffset(int box)
|
|
|
|
|
{
|
|
|
|
|
return Box + SIZE_STORED * box * 30;
|
|
|
|
|
}
|
2016-09-26 23:43:52 +00:00
|
|
|
|
|
|
|
|
|
// Save file does not have Box Name / Wallpaper info
|
|
|
|
|
public override string getBoxName(int box) { return $"BOX {box + 1}"; }
|
|
|
|
|
public override void setBoxName(int box, string value) { }
|
|
|
|
|
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public override PKM getPKM(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
byte[] pkm = data.Take(SIZE_STORED).ToArray();
|
|
|
|
|
PKM bk = new BK4(pkm);
|
|
|
|
|
return bk;
|
|
|
|
|
}
|
|
|
|
|
public override byte[] decryptPKM(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void setDex(PKM pkm) { }
|
|
|
|
|
|
|
|
|
|
public static byte[] DecryptPBRSaveData(byte[] input)
|
|
|
|
|
{
|
|
|
|
|
byte[] output = new byte[input.Length];
|
|
|
|
|
for (int base_ofs = 0; base_ofs < SaveUtil.SIZE_G4BR; base_ofs += 0x1C0000)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(input, base_ofs, output, base_ofs, 8);
|
|
|
|
|
|
|
|
|
|
ushort[] keys = new ushort[4];
|
|
|
|
|
for (int i = 0; i < keys.Length; i++)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
keys[i] = BigEndian.ToUInt16(input, base_ofs + i * 2);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
|
|
|
|
|
for (int ofs = base_ofs + 8; ofs < base_ofs + 0x1C0000; ofs += 8)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < keys.Length; i++)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
ushort val = BigEndian.ToUInt16(input, ofs + i*2);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
val -= keys[i];
|
2016-09-26 23:43:52 +00:00
|
|
|
|
BigEndian.GetBytes(val).CopyTo(output, ofs + i*2);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
2016-09-27 06:07:17 +00:00
|
|
|
|
keys = SaveUtil.AdvanceGCKeys(keys);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-26 23:43:52 +00:00
|
|
|
|
private static byte[] EncryptPBRSaveData(byte[] input)
|
2016-09-26 23:14:11 +00:00
|
|
|
|
{
|
|
|
|
|
byte[] output = new byte[input.Length];
|
|
|
|
|
for (int base_ofs = 0; base_ofs < SaveUtil.SIZE_G4BR; base_ofs += 0x1C0000)
|
|
|
|
|
{
|
|
|
|
|
Array.Copy(input, base_ofs, output, base_ofs, 8);
|
|
|
|
|
|
|
|
|
|
ushort[] keys = new ushort[4];
|
|
|
|
|
for (int i = 0; i < keys.Length; i++)
|
2016-09-26 23:43:52 +00:00
|
|
|
|
keys[i] = BigEndian.ToUInt16(input, base_ofs + i * 2);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
|
|
|
|
|
for (int ofs = base_ofs + 8; ofs < base_ofs + 0x1C0000; ofs += 8)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < keys.Length; i++)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
ushort val = BigEndian.ToUInt16(input, ofs + i * 2);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
val += keys[i];
|
2016-09-26 23:43:52 +00:00
|
|
|
|
BigEndian.GetBytes(val).CopyTo(output, ofs + i * 2);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
2016-09-27 06:07:17 +00:00
|
|
|
|
keys = SaveUtil.AdvanceGCKeys(keys);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool VerifyChecksum(byte[] input, int offset, int len, int checksum_offset)
|
|
|
|
|
{
|
|
|
|
|
uint[] storedChecksums = new uint[16];
|
|
|
|
|
for (int i = 0; i < storedChecksums.Length; i++)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
storedChecksums[i] = BigEndian.ToUInt32(input, checksum_offset + i*4);
|
|
|
|
|
BitConverter.GetBytes((uint)0).CopyTo(input, checksum_offset + i*4);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint[] checksums = new uint[16];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i += 2)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
ushort val = BigEndian.ToUInt16(input, offset + i);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
for (int j = 0; j < 16; j++)
|
|
|
|
|
{
|
|
|
|
|
checksums[j] += (uint)((val >> j) & 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < storedChecksums.Length; i++)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
BigEndian.GetBytes(storedChecksums[i]).CopyTo(input, checksum_offset + i*4);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return checksums.SequenceEqual(storedChecksums);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void SetChecksum(byte[] input, int offset, int len, int checksum_offset)
|
|
|
|
|
{
|
|
|
|
|
uint[] storedChecksums = new uint[16];
|
|
|
|
|
for (int i = 0; i < storedChecksums.Length; i++)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
storedChecksums[i] = BigEndian.ToUInt32(input, checksum_offset + i * 4);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
BitConverter.GetBytes((uint)0).CopyTo(input, checksum_offset + i * 4);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint[] checksums = new uint[16];
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < len; i += 2)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
ushort val = BigEndian.ToUInt16(input, offset + i);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
for (int j = 0; j < 16; j++)
|
|
|
|
|
{
|
|
|
|
|
checksums[j] += (uint)((val >> j) & 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < checksums.Length; i++)
|
|
|
|
|
{
|
2016-09-26 23:43:52 +00:00
|
|
|
|
BigEndian.GetBytes(checksums[i]).CopyTo(input, checksum_offset + i * 4);
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
|
|
|
|
public override string getString(int Offset, int Count) => PKX.getBEString4(Data, Offset, Count);
|
|
|
|
|
public override byte[] setString(string value, int maxLength, int PadToSize = 0, ushort PadWith = 0)
|
|
|
|
|
{
|
|
|
|
|
if (PadToSize == 0)
|
|
|
|
|
PadToSize = maxLength + 1;
|
|
|
|
|
return PKX.setBEString4(value, maxLength, PadToSize, PadWith);
|
|
|
|
|
}
|
2016-09-26 23:14:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|