2017-04-02 14:53:46 +00:00
|
|
|
|
using System;
|
2018-08-10 04:53:39 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.Core
|
|
|
|
|
{
|
|
|
|
|
/* GameCube memory card format data, checksum and code to extract files based on Dolphin code, adapted from C++ to C#
|
|
|
|
|
* https://github.com/dolphin-emu/dolphin/
|
|
|
|
|
*/
|
|
|
|
|
|
2017-10-24 06:12:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Flags for indicating what data is present in the Memory Card
|
|
|
|
|
/// </summary>
|
2017-04-02 14:53:46 +00:00
|
|
|
|
public enum GCMemoryCardState
|
|
|
|
|
{
|
|
|
|
|
Invalid,
|
|
|
|
|
NoPkmSaveGame,
|
2017-04-02 20:42:42 +00:00
|
|
|
|
SaveGameCOLO,
|
|
|
|
|
SaveGameXD,
|
|
|
|
|
SaveGameRSBOX,
|
2017-04-02 14:53:46 +00:00
|
|
|
|
MultipleSaveGame,
|
2017-04-02 20:42:42 +00:00
|
|
|
|
DuplicateCOLO,
|
|
|
|
|
DuplicateXD,
|
|
|
|
|
DuplicateRSBOX,
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-24 06:12:58 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// GameCube save container which may or may not contain Generation 3 <see cref="SaveFile"/> objects.
|
|
|
|
|
/// </summary>
|
2017-04-02 14:53:46 +00:00
|
|
|
|
public sealed class SAV3GCMemoryCard
|
|
|
|
|
{
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private const int BLOCK_SIZE = 0x2000;
|
|
|
|
|
private const int MBIT_TO_BLOCKS = 0x10;
|
|
|
|
|
private const int DENTRY_STRLEN = 0x20;
|
|
|
|
|
private const int DENTRY_SIZE = 0x40;
|
2018-09-26 19:42:39 +00:00
|
|
|
|
private const int NumEntries_Directory = BLOCK_SIZE / DENTRY_SIZE;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2020-12-22 01:17:56 +00:00
|
|
|
|
private static readonly HashSet<int> ValidMemoryCardSizes = new()
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2018-08-10 04:53:39 +00:00
|
|
|
|
0x0080000, // 512KB 59 Blocks Memory Card
|
|
|
|
|
0x0100000, // 1MB
|
|
|
|
|
0x0200000, // 2MB
|
|
|
|
|
0x0400000, // 4MB 251 Blocks Memory Card
|
|
|
|
|
0x0800000, // 8MB
|
2017-04-02 20:42:42 +00:00
|
|
|
|
0x1000000, // 16MB 1019 Blocks Default Dolphin Memory Card
|
2018-08-10 04:53:39 +00:00
|
|
|
|
0x2000000, // 64MB
|
2018-09-26 19:42:39 +00:00
|
|
|
|
0x4000000, // 128MB
|
2017-04-02 14:53:46 +00:00
|
|
|
|
};
|
2018-08-10 04:53:39 +00:00
|
|
|
|
|
|
|
|
|
public static bool IsMemoryCardSize(long Size) => ValidMemoryCardSizes.Contains((int)Size);
|
2018-09-26 19:42:39 +00:00
|
|
|
|
|
|
|
|
|
public static bool IsMemoryCardSize(byte[] Data)
|
|
|
|
|
{
|
|
|
|
|
if (!IsMemoryCardSize(Data.Length))
|
|
|
|
|
return false; // bad size
|
|
|
|
|
if (BitConverter.ToUInt64(Data, 0) == ulong.MaxValue)
|
|
|
|
|
return false; // uninitialized
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static readonly byte[] RawEmpty_DEntry = { 0xFF, 0xFF, 0xFF, 0xFF };
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
|
|
|
|
// Control blocks
|
|
|
|
|
private const int Header_Block = 0;
|
|
|
|
|
private const int Directory_Block = 1;
|
|
|
|
|
private const int DirectoryBackup_Block = 2;
|
|
|
|
|
private const int BlockAlloc_Block = 3;
|
|
|
|
|
private const int BlockAllocBackup_Block = 4;
|
|
|
|
|
|
2018-09-26 19:42:39 +00:00
|
|
|
|
private const int Header = BLOCK_SIZE * Header_Block;
|
|
|
|
|
private const int Directory = BLOCK_SIZE * Directory_Block;
|
|
|
|
|
private const int DirectoryBAK = BLOCK_SIZE * DirectoryBackup_Block;
|
|
|
|
|
private const int BlockAlloc = BLOCK_SIZE * BlockAlloc_Block;
|
2019-03-08 01:30:50 +00:00
|
|
|
|
private const int BlockAllocBAK = BLOCK_SIZE * BlockAllocBackup_Block;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2018-08-18 06:23:44 +00:00
|
|
|
|
public SAV3GCMemoryCard(byte[] data) => Data = data;
|
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
// Checksums
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void GetChecksum(int block, int offset, int length, out ushort csum, out ushort inv_csum)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
|
|
|
|
csum = inv_csum = 0;
|
2018-08-10 04:53:39 +00:00
|
|
|
|
var ofs = (block * BLOCK_SIZE) + offset;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
for (int i = 0; i < length; i++)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2018-08-10 04:53:39 +00:00
|
|
|
|
var val = BigEndian.ToUInt16(Data, ofs + (i * 2));
|
2017-04-02 20:42:42 +00:00
|
|
|
|
csum += val;
|
2018-08-10 04:53:39 +00:00
|
|
|
|
inv_csum += (ushort)~val;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
if (csum == 0xffff)
|
|
|
|
|
csum = 0;
|
|
|
|
|
if (inv_csum == 0xffff)
|
|
|
|
|
inv_csum = 0;
|
|
|
|
|
}
|
2018-08-10 04:53:39 +00:00
|
|
|
|
|
2019-07-14 22:06:45 +00:00
|
|
|
|
private GCChecksumFlag VerifyChecksums()
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
GCChecksumFlag results = 0;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
GetChecksum(Header_Block, 0, 0xFE, out ushort csum, out ushort csum_inv);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
if (Header_Checksum != csum || (Header_Checksum_Inv != csum_inv))
|
2019-07-14 22:06:45 +00:00
|
|
|
|
results |= GCChecksumFlag.HeaderBad;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
GetChecksum(Directory_Block, 0, 0xFFE, out csum, out csum_inv);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
if (Directory_Checksum != csum || (Directory_Checksum_Inv != csum_inv))
|
2019-07-14 22:06:45 +00:00
|
|
|
|
results |= GCChecksumFlag.DirectoryBad;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
GetChecksum(DirectoryBackup_Block, 0, 0xFFE, out csum, out csum_inv);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
if (DirectoryBAK_Checksum != csum || (DirectoryBAK_Checksum_Inv != csum_inv))
|
2019-07-14 22:06:45 +00:00
|
|
|
|
results |= GCChecksumFlag.DirectoryBackupBad;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
GetChecksum(BlockAlloc_Block, 4, 0xFFE, out csum, out csum_inv);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
if (BlockAlloc_Checksum != csum || (BlockAlloc_Checksum_Inv != csum_inv))
|
2019-07-14 22:06:45 +00:00
|
|
|
|
results |= GCChecksumFlag.BlockAllocBad;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
GetChecksum(BlockAllocBackup_Block, 4, 0xFFE, out csum, out csum_inv);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
if ((BlockAllocBAK_Checksum != csum) || BlockAllocBAK_Checksum_Inv != csum_inv)
|
2019-07-14 22:06:45 +00:00
|
|
|
|
results |= GCChecksumFlag.BlockAllocBackupBad;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
|
|
|
|
return results;
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
// Structure
|
|
|
|
|
private int Header_Size => BigEndian.ToUInt16(Data, Header + 0x0022);
|
|
|
|
|
private ushort Header_Checksum => BigEndian.ToUInt16(Data, Header + 0x01fc);
|
|
|
|
|
private ushort Header_Checksum_Inv => BigEndian.ToUInt16(Data, Header + 0x01fe);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
// Encoding (Windows-1252 or Shift JIS)
|
|
|
|
|
private int Header_Encoding => BigEndian.ToUInt16(Data, Header + 0x0024);
|
|
|
|
|
private bool Header_Japanese => Header_Encoding == 1;
|
|
|
|
|
private Encoding EncodingType => Header_Japanese ? Encoding.GetEncoding(1252) : Encoding.GetEncoding(932);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private int Directory_UpdateCounter => BigEndian.ToUInt16(Data, Directory + 0x1ffa);
|
|
|
|
|
private int Directory_Checksum => BigEndian.ToUInt16(Data, Directory + 0x1ffc);
|
|
|
|
|
private int Directory_Checksum_Inv => BigEndian.ToUInt16(Data, Directory + 0x1ffe);
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private int DirectoryBAK_UpdateCounter => BigEndian.ToUInt16(Data, DirectoryBAK + 0x1ffa);
|
|
|
|
|
private int DirectoryBAK_Checksum => BigEndian.ToUInt16(Data, DirectoryBAK + 0x1ffc);
|
|
|
|
|
private int DirectoryBAK_Checksum_Inv => BigEndian.ToUInt16(Data, DirectoryBAK + 0x1ffe);
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private int BlockAlloc_Checksum => BigEndian.ToUInt16(Data, BlockAlloc + 0x0000);
|
|
|
|
|
private int BlockAlloc_Checksum_Inv => BigEndian.ToUInt16(Data, BlockAlloc + 0x0002);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private int BlockAllocBAK_Checksum => BigEndian.ToUInt16(Data, BlockAllocBAK + 0x0000);
|
|
|
|
|
private int BlockAllocBAK_Checksum_Inv => BigEndian.ToUInt16(Data, BlockAllocBAK + 0x0002);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private int DirectoryBlock_Used;
|
2018-08-10 04:53:39 +00:00
|
|
|
|
private int NumBlocks => (Data.Length/BLOCK_SIZE) - 5;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private int EntryCOLO = -1;
|
|
|
|
|
private int EntryXD = -1;
|
|
|
|
|
private int EntryRSBOX = -1;
|
|
|
|
|
private int EntrySelected = -1;
|
2019-07-14 22:06:45 +00:00
|
|
|
|
public bool HasCOLO => EntryCOLO >= 0;
|
|
|
|
|
public bool HasXD => EntryXD >= 0;
|
|
|
|
|
public bool HasRSBOX => EntryRSBOX >= 0;
|
2017-04-02 20:42:42 +00:00
|
|
|
|
public int SaveGameCount;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2019-07-14 22:06:45 +00:00
|
|
|
|
[Flags]
|
|
|
|
|
public enum GCChecksumFlag : uint
|
|
|
|
|
{
|
|
|
|
|
NoIssue = 0,
|
|
|
|
|
HeaderBad = 1 << 0,
|
|
|
|
|
DirectoryBad = 1 << 1,
|
|
|
|
|
DirectoryBackupBad = 1 << 2,
|
|
|
|
|
BlockAllocBad,
|
|
|
|
|
BlockAllocBackupBad
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 14:53:46 +00:00
|
|
|
|
private bool IsCorruptedMemoryCard()
|
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
var csums = VerifyChecksums();
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if ((csums & GCChecksumFlag.HeaderBad) != 0)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if ((csums & GCChecksumFlag.DirectoryBad) != 0)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if ((csums & GCChecksumFlag.DirectoryBackupBad) != 0) // backup is also wrong
|
2017-04-02 20:42:42 +00:00
|
|
|
|
return true; // Directory checksum and directory backup checksum failed
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2019-07-14 22:06:45 +00:00
|
|
|
|
// backup is correct, restore
|
|
|
|
|
RestoreBackup();
|
2017-06-18 01:37:19 +00:00
|
|
|
|
csums = VerifyChecksums(); // update checksums
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if ((csums & GCChecksumFlag.BlockAllocBad) != 0)
|
|
|
|
|
{
|
|
|
|
|
if ((csums & GCChecksumFlag.BlockAllocBackupBad) != 0) // backup is also wrong
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// backup is correct, restore
|
|
|
|
|
RestoreBackup();
|
|
|
|
|
}
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-08-10 04:53:39 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private void RestoreBackup()
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2017-04-02 20:42:42 +00:00
|
|
|
|
Array.Copy(Data, DirectoryBackup_Block*BLOCK_SIZE, Data, Directory_Block*BLOCK_SIZE, BLOCK_SIZE);
|
2017-04-04 02:59:29 +00:00
|
|
|
|
Array.Copy(Data, BlockAllocBackup_Block*BLOCK_SIZE, Data, BlockAlloc_Block*BLOCK_SIZE, BLOCK_SIZE);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-18 06:23:44 +00:00
|
|
|
|
public GCMemoryCardState GetMemoryCardState()
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2017-04-02 20:42:42 +00:00
|
|
|
|
if (!IsMemoryCardSize(Data))
|
2018-08-10 04:53:39 +00:00
|
|
|
|
return GCMemoryCardState.Invalid; // Invalid size
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
|
|
|
|
// Size in megabits, not megabytes
|
2017-04-02 20:42:42 +00:00
|
|
|
|
int m_sizeMb = Data.Length / BLOCK_SIZE / MBIT_TO_BLOCKS;
|
|
|
|
|
if (m_sizeMb != Header_Size) // Memory card file size does not match the header size
|
2017-04-02 14:53:46 +00:00
|
|
|
|
return GCMemoryCardState.Invalid;
|
|
|
|
|
|
|
|
|
|
if (IsCorruptedMemoryCard())
|
|
|
|
|
return GCMemoryCardState.Invalid;
|
|
|
|
|
|
|
|
|
|
// Use the most recent directory block
|
2018-05-12 15:13:39 +00:00
|
|
|
|
DirectoryBlock_Used = DirectoryBAK_UpdateCounter > Directory_UpdateCounter
|
|
|
|
|
? DirectoryBackup_Block
|
2017-04-02 20:42:42 +00:00
|
|
|
|
: Directory_Block;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
string Empty_DEntry = EncodingType.GetString(RawEmpty_DEntry, 0, 4);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
// Search for pokemon savegames in the directory
|
|
|
|
|
for (int i = 0; i < NumEntries_Directory; i++)
|
|
|
|
|
{
|
2018-08-10 04:53:39 +00:00
|
|
|
|
int offset = (DirectoryBlock_Used * BLOCK_SIZE) + (i * DENTRY_SIZE);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
string GameCode = EncodingType.GetString(Data, offset, 4);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
if (GameCode == Empty_DEntry)
|
|
|
|
|
continue;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
int FirstBlock = BigEndian.ToUInt16(Data, offset + 0x36);
|
|
|
|
|
int BlockCount = BigEndian.ToUInt16(Data, offset + 0x38);
|
|
|
|
|
|
2017-04-02 14:53:46 +00:00
|
|
|
|
// Memory card directory contains info for deleted files with boundaries beyond memory card size, ignore
|
|
|
|
|
if (FirstBlock + BlockCount > NumBlocks)
|
|
|
|
|
continue;
|
2017-04-02 20:42:42 +00:00
|
|
|
|
|
2021-01-06 23:46:43 +00:00
|
|
|
|
var ver = SaveHandlerGCI.GetGameCode(GameCode);
|
|
|
|
|
if (ver == GameVersion.COLO)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (HasCOLO) // another entry already exists
|
2017-04-02 20:42:42 +00:00
|
|
|
|
return GCMemoryCardState.DuplicateCOLO;
|
|
|
|
|
EntryCOLO = i;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
SaveGameCount++;
|
|
|
|
|
}
|
2021-01-06 23:46:43 +00:00
|
|
|
|
if (ver == GameVersion.XD)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (HasXD) // another entry already exists
|
2017-04-02 20:42:42 +00:00
|
|
|
|
return GCMemoryCardState.DuplicateXD;
|
|
|
|
|
EntryXD = i;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
SaveGameCount++;
|
|
|
|
|
}
|
2021-01-06 23:46:43 +00:00
|
|
|
|
if (ver == GameVersion.RSBOX)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (HasRSBOX) // another entry already exists
|
2017-04-02 20:42:42 +00:00
|
|
|
|
return GCMemoryCardState.DuplicateRSBOX;
|
|
|
|
|
EntryRSBOX = i;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
SaveGameCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (SaveGameCount == 0)
|
|
|
|
|
return GCMemoryCardState.NoPkmSaveGame;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2017-04-02 14:53:46 +00:00
|
|
|
|
if (SaveGameCount > 1)
|
|
|
|
|
return GCMemoryCardState.MultipleSaveGame;
|
2018-05-12 15:13:39 +00:00
|
|
|
|
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (HasCOLO)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2017-04-02 20:42:42 +00:00
|
|
|
|
EntrySelected = EntryCOLO;
|
|
|
|
|
return GCMemoryCardState.SaveGameCOLO;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (HasXD)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2017-04-02 20:42:42 +00:00
|
|
|
|
EntrySelected = EntryXD;
|
|
|
|
|
return GCMemoryCardState.SaveGameXD;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
2017-04-02 20:42:42 +00:00
|
|
|
|
EntrySelected = EntryRSBOX;
|
|
|
|
|
return GCMemoryCardState.SaveGameRSBOX;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public GameVersion SelectedGameVersion
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (EntrySelected < 0)
|
|
|
|
|
return GameVersion.Any;
|
|
|
|
|
|
|
|
|
|
if (EntrySelected == EntryCOLO)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
return GameVersion.COLO;
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (EntrySelected == EntryXD)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
return GameVersion.XD;
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (EntrySelected == EntryRSBOX)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
return GameVersion.RSBOX;
|
2017-04-02 20:42:42 +00:00
|
|
|
|
return GameVersion.Any; //Default for no game selected
|
2018-05-12 15:13:39 +00:00
|
|
|
|
}
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
2018-08-10 04:53:39 +00:00
|
|
|
|
|
2017-04-02 14:53:46 +00:00
|
|
|
|
public void SelectSaveGame(GameVersion Game)
|
|
|
|
|
{
|
2017-04-02 20:42:42 +00:00
|
|
|
|
switch (Game)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
case GameVersion.COLO: if (HasCOLO) EntrySelected = EntryCOLO; break;
|
|
|
|
|
case GameVersion.XD: if (HasXD) EntrySelected = EntryXD; break;
|
|
|
|
|
case GameVersion.RSBOX: if (HasRSBOX) EntrySelected = EntryRSBOX; break;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
public string GCISaveName => GCISaveGameName();
|
2017-05-13 03:32:36 +00:00
|
|
|
|
public byte[] SelectedSaveData { get => ReadSaveGameData(); set => WriteSaveGameData(value); }
|
2019-10-08 01:40:09 +00:00
|
|
|
|
public readonly byte[] Data;
|
2017-04-02 20:42:42 +00:00
|
|
|
|
|
2017-06-18 01:37:19 +00:00
|
|
|
|
private string GCISaveGameName()
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2018-08-10 04:53:39 +00:00
|
|
|
|
int offset = (DirectoryBlock_Used * BLOCK_SIZE) + (EntrySelected * DENTRY_SIZE);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
string GameCode = EncodingType.GetString(Data, offset, 4);
|
|
|
|
|
string Makercode = EncodingType.GetString(Data, offset + 0x04, 2);
|
|
|
|
|
string FileName = EncodingType.GetString(Data, offset + 0x08, DENTRY_STRLEN);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2019-02-08 05:40:20 +00:00
|
|
|
|
return $"{Makercode}-{GameCode}-{Util.TrimFromZero(FileName)}.gci";
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
2018-08-10 04:53:39 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private byte[] ReadSaveGameData()
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (EntrySelected < 0)
|
2019-02-02 07:26:43 +00:00
|
|
|
|
return Array.Empty<byte>(); // No entry selected
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2018-08-10 04:53:39 +00:00
|
|
|
|
int offset = (DirectoryBlock_Used * BLOCK_SIZE) + (EntrySelected * DENTRY_SIZE);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
int FirstBlock = BigEndian.ToUInt16(Data, offset + 0x36);
|
|
|
|
|
int BlockCount = BigEndian.ToUInt16(Data, offset + 0x38);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
|
|
|
|
byte[] SaveData = new byte[BlockCount * BLOCK_SIZE];
|
2017-04-02 20:42:42 +00:00
|
|
|
|
Array.Copy(Data, FirstBlock * BLOCK_SIZE, SaveData, 0, BlockCount * BLOCK_SIZE);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
|
|
|
|
return SaveData;
|
|
|
|
|
}
|
2018-08-10 04:53:39 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
private void WriteSaveGameData(byte[] SaveData)
|
2017-04-02 14:53:46 +00:00
|
|
|
|
{
|
2019-07-14 22:06:45 +00:00
|
|
|
|
if (EntrySelected < 0) // Can't write anywhere
|
2017-04-02 20:42:42 +00:00
|
|
|
|
return;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2018-08-10 04:53:39 +00:00
|
|
|
|
int offset = (DirectoryBlock_Used * BLOCK_SIZE) + (EntrySelected * DENTRY_SIZE);
|
2017-04-02 20:42:42 +00:00
|
|
|
|
int FirstBlock = BigEndian.ToUInt16(Data, offset + 0x36);
|
|
|
|
|
int BlockCount = BigEndian.ToUInt16(Data, offset + 0x38);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
if (SaveData.Length != BlockCount * BLOCK_SIZE) // Invalid File Size
|
|
|
|
|
return;
|
2017-04-02 14:53:46 +00:00
|
|
|
|
|
2017-04-02 20:42:42 +00:00
|
|
|
|
Array.Copy(SaveData, 0, Data, FirstBlock * BLOCK_SIZE, BlockCount * BLOCK_SIZE);
|
2017-04-02 14:53:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|