2016-09-19 05:47:31 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2017-01-08 07:54:09 +00:00
|
|
|
|
namespace PKHeX.Core
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
2017-10-24 06:12:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generation 3 <see cref="SaveFile"/> object for Pokémon Ruby Sapphire Box saves.
|
|
|
|
|
/// </summary>
|
2016-09-19 05:47:31 +00:00
|
|
|
|
public sealed class SAV3RSBox : SaveFile
|
|
|
|
|
{
|
2016-12-08 06:57:08 +00:00
|
|
|
|
public override string BAKName => $"{FileName} [{Version} #{SaveCount:0000}].bak";
|
2017-04-02 14:53:46 +00:00
|
|
|
|
public override string Filter
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (IsMemoryCardSave)
|
2017-04-02 15:36:50 +00:00
|
|
|
|
return "Memory Card Raw File|*.raw|Memory Card Binary File|*.bin|GameCube Save File|*.gci|All Files|*.*";
|
2017-04-02 14:53:46 +00:00
|
|
|
|
return "GameCube Save File|*.gci|All Files|*.*";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public override string Extension => IsMemoryCardSave ? ".raw" : ".gci";
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private readonly SAV3GCMemoryCard MC;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private bool IsMemoryCardSave => MC != null;
|
2017-04-02 20:42:42 +00:00
|
|
|
|
public SAV3RSBox(byte[] data, SAV3GCMemoryCard MC) : this(data) { this.MC = MC; BAK = MC.Data; }
|
2016-09-19 05:47:31 +00:00
|
|
|
|
public SAV3RSBox(byte[] data = null)
|
|
|
|
|
{
|
|
|
|
|
Data = data == null ? new byte[SaveUtil.SIZE_G3BOX] : (byte[])data.Clone();
|
|
|
|
|
BAK = (byte[])Data.Clone();
|
2017-10-31 16:24:54 +00:00
|
|
|
|
Exportable = !Data.All(z => z == 0);
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
if (SaveUtil.GetIsG3BOXSAV(Data) != GameVersion.RSBOX)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
Blocks = new RSBOX_Block[2*BLOCK_COUNT];
|
|
|
|
|
for (int i = 0; i < Blocks.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
int offset = BLOCK_SIZE + i* BLOCK_SIZE;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
Blocks[i] = new RSBOX_Block(GetData(offset, BLOCK_SIZE), offset);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Detect active save
|
|
|
|
|
int[] SaveCounts = Blocks.Select(block => (int)block.SaveCount).ToArray();
|
|
|
|
|
SaveCount = SaveCounts.Max();
|
|
|
|
|
int ActiveSAV = Array.IndexOf(SaveCounts, SaveCount) / BLOCK_COUNT;
|
|
|
|
|
Blocks = Blocks.Skip(ActiveSAV*BLOCK_COUNT).Take(BLOCK_COUNT).OrderBy(b => b.BlockNumber).ToArray();
|
|
|
|
|
|
|
|
|
|
// Set up PC data buffer beyond end of save file.
|
|
|
|
|
Box = Data.Length;
|
|
|
|
|
Array.Resize(ref Data, Data.Length + SIZE_RESERVED); // More than enough empty space.
|
|
|
|
|
|
|
|
|
|
// Copy block to the allocated location
|
|
|
|
|
foreach (RSBOX_Block b in Blocks)
|
2017-05-12 04:34:18 +00:00
|
|
|
|
Array.Copy(b.Data, 0xC, Data, (int)(Box + b.BlockNumber*(BLOCK_SIZE - 0x10)), b.Data.Length - 0x10);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
Personal = PersonalTable.RS;
|
|
|
|
|
HeldItems = Legal.HeldItems_RS;
|
|
|
|
|
|
|
|
|
|
if (!Exportable)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
ClearBoxes();
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private readonly RSBOX_Block[] Blocks;
|
|
|
|
|
private readonly int SaveCount;
|
|
|
|
|
private const int BLOCK_COUNT = 23;
|
|
|
|
|
private const int BLOCK_SIZE = 0x2000;
|
|
|
|
|
private const int SIZE_RESERVED = BLOCK_COUNT * BLOCK_SIZE; // unpacked box data
|
2017-04-21 02:38:48 +00:00
|
|
|
|
public override byte[] Write(bool DSV, bool GCI)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
|
|
|
|
// Copy Box data back to block
|
|
|
|
|
foreach (RSBOX_Block b in Blocks)
|
2017-05-12 04:34:18 +00:00
|
|
|
|
Array.Copy(Data, (int)(Box + b.BlockNumber * (BLOCK_SIZE - 0x10)), b.Data, 0xC, b.Data.Length - 0x10);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetChecksums();
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
// Set Data Back
|
|
|
|
|
foreach (RSBOX_Block b in Blocks)
|
|
|
|
|
b.Data.CopyTo(Data, b.Offset);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
byte[] newFile = GetData(0, Data.Length - SIZE_RESERVED);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
|
|
|
|
|
// Return the gci if Memory Card is not being exported
|
|
|
|
|
if (!IsMemoryCardSave || GCI)
|
|
|
|
|
return Header.Concat(newFile).ToArray();
|
|
|
|
|
|
|
|
|
|
MC.SelectedSaveData = newFile.ToArray();
|
|
|
|
|
return MC.Data;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Configuration
|
2016-12-27 23:05:06 +00:00
|
|
|
|
public override SaveFile Clone()
|
|
|
|
|
{
|
2017-04-21 02:38:48 +00:00
|
|
|
|
byte[] data = Write(DSV: false, GCI: true).Skip(Header.Length).ToArray();
|
2016-12-27 23:05:06 +00:00
|
|
|
|
var sav = new SAV3RSBox(data) {Header = (byte[]) Header.Clone()};
|
|
|
|
|
return sav;
|
|
|
|
|
}
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
public override int SIZE_STORED => PKX.SIZE_3STORED + 4;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override int SIZE_PARTY => PKX.SIZE_3PARTY; // unused
|
2016-09-19 05:47:31 +00:00
|
|
|
|
public override PKM BlankPKM => new PK3();
|
2016-09-26 23:14:11 +00:00
|
|
|
|
public override Type PKMType => typeof(PK3);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
public override int MaxMoveID => 354;
|
2016-10-24 04:59:27 +00:00
|
|
|
|
public override int MaxSpeciesID => Legal.MaxSpeciesID_3;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
public override int MaxAbilityID => 77;
|
|
|
|
|
public override int MaxItemID => 374;
|
|
|
|
|
public override int MaxBallID => 0xC;
|
|
|
|
|
public override int MaxGameID => 5;
|
|
|
|
|
|
2016-09-25 20:42:03 +00:00
|
|
|
|
public override int MaxEV => 255;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
public override int Generation => 3;
|
|
|
|
|
protected override int GiftCountMax => 1;
|
2017-04-10 04:53:53 +00:00
|
|
|
|
public override int OTLength => 7;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
public override int NickLength => 10;
|
|
|
|
|
public override int MaxMoney => 999999;
|
2017-01-03 06:16:15 +00:00
|
|
|
|
public override bool HasBoxWallpapers => false;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
public override int BoxCount => 50;
|
|
|
|
|
public override bool HasParty => false;
|
|
|
|
|
|
|
|
|
|
// Checksums
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override void SetChecksums()
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
|
|
|
|
foreach (RSBOX_Block b in Blocks)
|
|
|
|
|
b.SetChecksums();
|
|
|
|
|
}
|
|
|
|
|
public override bool ChecksumsValid
|
|
|
|
|
{
|
|
|
|
|
get { return Blocks.All(t => t.ChecksumsValid); }
|
|
|
|
|
}
|
|
|
|
|
public override string ChecksumInfo
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return string.Join(Environment.NewLine,
|
2016-12-08 06:57:08 +00:00
|
|
|
|
Blocks.Where(b => !b.ChecksumsValid).Select(b => $"Block {b.BlockNumber:00} invalid"));
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Trainer Info
|
2017-05-13 03:32:36 +00:00
|
|
|
|
public override GameVersion Version { get => GameVersion.RSBOX; protected set { } }
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
// Storage
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override int GetPartyOffset(int slot) => -1;
|
|
|
|
|
public override int GetBoxOffset(int box) => Box + 8 + SIZE_STORED * box * 30;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
public override int CurrentBox
|
|
|
|
|
{
|
2017-05-13 03:32:36 +00:00
|
|
|
|
get => Data[Box + 4] * 2;
|
|
|
|
|
set => Data[Box + 4] = (byte)(value / 2);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override int GetBoxWallpaperOffset(int box)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
|
|
|
|
// Box Wallpaper is directly after the Box Names
|
|
|
|
|
int offset = Box + 0x1ED19 + box/2;
|
2016-10-29 18:32:21 +00:00
|
|
|
|
return offset;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override string GetBoxName(int box)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
|
|
|
|
// Tweaked for the 1-30/31-60 box showing
|
2017-01-19 03:57:42 +00:00
|
|
|
|
int lo = 30*(box%2) + 1;
|
|
|
|
|
int hi = 30*(box%2 + 1);
|
|
|
|
|
string boxName = $"[{lo:00}-{hi:00}] ";
|
|
|
|
|
box /= 2;
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
int offset = Box + 0x1EC38 + 9 * box;
|
|
|
|
|
if (Data[offset] == 0 || Data[offset] == 0xFF)
|
|
|
|
|
boxName += $"BOX {box + 1}";
|
2017-06-18 01:37:19 +00:00
|
|
|
|
boxName += GetString(offset, 9);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
return boxName;
|
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetBoxName(int box, string value)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
|
|
|
|
int offset = Box + 0x1EC38 + 9 * box;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
byte[] data = value == $"BOX {box + 1}" ? new byte[9] : SetString(value, 8);
|
|
|
|
|
SetData(data, offset);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override PKM GetPKM(byte[] data)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
2017-06-11 07:23:04 +00:00
|
|
|
|
if (data.Length != PKX.SIZE_3STORED)
|
|
|
|
|
Array.Resize(ref data, PKX.SIZE_3STORED);
|
|
|
|
|
return new PK3(data);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override byte[] DecryptPKM(byte[] data)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
2017-06-11 07:23:04 +00:00
|
|
|
|
if (data.Length != PKX.SIZE_3STORED)
|
|
|
|
|
Array.Resize(ref data, PKX.SIZE_3STORED);
|
2017-06-18 01:37:19 +00:00
|
|
|
|
return PKX.DecryptArray3(data);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
protected override void SetDex(PKM pkm) { }
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public override void SetStoredSlot(PKM pkm, int offset, bool? trade = null, bool? dex = null)
|
2016-09-19 05:47:31 +00:00
|
|
|
|
{
|
|
|
|
|
if (pkm == null) return;
|
|
|
|
|
if (pkm.GetType() != PKMType)
|
|
|
|
|
throw new InvalidCastException($"PKM Format needs to be {PKMType} when setting to a Gen{Generation} Save File.");
|
|
|
|
|
if (trade ?? SetUpdatePKM)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetPKM(pkm);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
if (dex ?? SetUpdateDex)
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetDex(pkm);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
byte[] data = pkm.EncryptedBoxData;
|
2017-06-18 01:37:19 +00:00
|
|
|
|
SetData(data, offset);
|
2016-09-19 05:47:31 +00:00
|
|
|
|
|
|
|
|
|
BitConverter.GetBytes((ushort)pkm.TID).CopyTo(Data, offset + data.Length + 0);
|
|
|
|
|
BitConverter.GetBytes((ushort)pkm.SID).CopyTo(Data, offset + data.Length + 2);
|
|
|
|
|
Edited = true;
|
|
|
|
|
}
|
2017-04-09 21:06:50 +00:00
|
|
|
|
|
2017-08-01 06:03:51 +00:00
|
|
|
|
public override string GetString(int Offset, int Count) => StringConverter.GetString3(Data, Offset, Count, Japanese);
|
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.SetString3(value, maxLength, Japanese, PadToSize, PadWith);
|
2017-04-09 21:06:50 +00:00
|
|
|
|
}
|
2016-09-19 05:47:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|