Merge pull request #7616 from AdmiralCurtiss/memcard-cleanup

Clean up GCMemcard a bit.
This commit is contained in:
Léo Lam 2019-01-16 21:56:40 +01:00 committed by GitHub
commit fd3ef7ebc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 499 additions and 419 deletions

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <array>
#include <string> #include <string>
#include <vector> #include <vector>
@ -92,42 +93,68 @@ protected:
struct GCMBlock struct GCMBlock
{ {
GCMBlock() { Erase(); } GCMBlock() { Erase(); }
void Erase() { memset(block, 0xFF, BLOCK_SIZE); } void Erase() { memset(m_block.data(), 0xFF, m_block.size()); }
u8 block[BLOCK_SIZE]; std::array<u8, BLOCK_SIZE> m_block;
}; };
void calc_checksumsBE(const u16* buf, u32 length, u16* csum, u16* inv_csum); void calc_checksumsBE(const u16* buf, u32 length, u16* csum, u16* inv_csum);
#pragma pack(push, 1) #pragma pack(push, 1)
struct Header // Offset Size Description struct Header
{ {
// Serial in libogc // NOTE: libogc refers to 'Serial' as the first 0x20 bytes of the header,
u8 serial[12]; // 0x0000 12 ? // so the data from m_serial until m_unknown_2 (inclusive)
u64 formatTime; // 0x000c 8 Time of format (OSTime value)
u32 SramBias; // 0x0014 4 SRAM bias at time of format // 12 bytes at 0x0000
u32 SramLang; // 0x0018 4 SRAM language std::array<u8, 12> m_serial;
u8 Unk2[4]; // 0x001c 4 ? almost always 0
// end Serial in libogc // 8 bytes at 0x000c: Time of format (OSTime value)
u8 deviceID[2]; // 0x0020 2 0 if formated in slot A 1 if formated in slot B Common::BigEndianValue<u64> m_format_time;
u8 SizeMb[2]; // 0x0022 2 Size of memcard in Mbits
u16 Encoding; // 0x0024 2 Encoding (Windows-1252 or Shift JIS) // 4 bytes at 0x0014; SRAM bias at time of format
u8 Unused1[468]; // 0x0026 468 Unused (0xff) u32 m_sram_bias;
u16 UpdateCounter; // 0x01fa 2 Update Counter (?, probably unused)
u16 Checksum; // 0x01fc 2 Additive Checksum // 4 bytes at 0x0018: SRAM language
u16 Checksum_Inv; // 0x01fe 2 Inverse Checksum Common::BigEndianValue<u32> m_sram_language;
u8 Unused2[7680]; // 0x0200 0x1e00 Unused (0xff)
// 4 bytes at 0x001c: ? almost always 0
std::array<u8, 4> m_unknown_2;
// 2 bytes at 0x0020: 0 if formated in slot A, 1 if formated in slot B
Common::BigEndianValue<u16> m_device_id;
// 2 bytes at 0x0022: Size of memcard in Mbits
Common::BigEndianValue<u16> m_size_mb;
// 2 bytes at 0x0024: Encoding (Windows-1252 or Shift JIS)
Common::BigEndianValue<u16> m_encoding;
// 468 bytes at 0x0026: Unused (0xff)
std::array<u8, 468> m_unused_1;
// 2 bytes at 0x01fa: Update Counter (?, probably unused)
u16 m_update_counter;
// 2 bytes at 0x01fc: Additive Checksum
u16 m_checksum;
// 2 bytes at 0x01fe: Inverse Checksum
u16 m_checksum_inv;
// 0x1e00 bytes at 0x0200: Unused (0xff)
std::array<u8, 7680> m_unused_2;
void CARD_GetSerialNo(u32* serial1, u32* serial2) const void CARD_GetSerialNo(u32* serial1, u32* serial2) const
{ {
u32 _serial[8]; u32 serial[8];
for (int i = 0; i < 8; i++) for (int i = 0; i < 8; i++)
{ {
memcpy(&_serial[i], (u8*)this + (i * 4), 4); memcpy(&serial[i], (u8*)this + (i * 4), 4);
} }
*serial1 = _serial[0] ^ _serial[2] ^ _serial[4] ^ _serial[6]; *serial1 = serial[0] ^ serial[2] ^ serial[4] ^ serial[6];
*serial2 = _serial[1] ^ _serial[3] ^ _serial[5] ^ _serial[7]; *serial2 = serial[1] ^ serial[3] ^ serial[5] ^ serial[7];
} }
// Nintendo format algorithm. // Nintendo format algorithm.
@ -136,41 +163,53 @@ struct Header // Offset Size Description
explicit Header(int slot = 0, u16 sizeMb = MemCard2043Mb, bool shift_jis = false) explicit Header(int slot = 0, u16 sizeMb = MemCard2043Mb, bool shift_jis = false)
{ {
memset(this, 0xFF, BLOCK_SIZE); memset(this, 0xFF, BLOCK_SIZE);
*(u16*)SizeMb = BE16(sizeMb); m_size_mb = sizeMb;
Encoding = BE16(shift_jis ? 1 : 0); m_encoding = shift_jis ? 1 : 0;
u64 rand = Common::Timer::GetLocalTimeSinceJan1970() - ExpansionInterface::CEXIIPL::GC_EPOCH; u64 rand = Common::Timer::GetLocalTimeSinceJan1970() - ExpansionInterface::CEXIIPL::GC_EPOCH;
formatTime = Common::swap64(rand); m_format_time = rand;
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
{ {
rand = (((rand * (u64)0x0000000041c64e6dULL) + (u64)0x0000000000003039ULL) >> 16); rand = (((rand * (u64)0x0000000041c64e6dULL) + (u64)0x0000000000003039ULL) >> 16);
serial[i] = (u8)(g_SRAM.settings_ex.flash_id[slot][i] + (u32)rand); m_serial[i] = (u8)(g_SRAM.settings_ex.flash_id[slot][i] + (u32)rand);
rand = (((rand * (u64)0x0000000041c64e6dULL) + (u64)0x0000000000003039ULL) >> 16); rand = (((rand * (u64)0x0000000041c64e6dULL) + (u64)0x0000000000003039ULL) >> 16);
rand &= (u64)0x0000000000007fffULL; rand &= (u64)0x0000000000007fffULL;
} }
SramBias = g_SRAM.settings.rtc_bias; m_sram_bias = g_SRAM.settings.rtc_bias;
SramLang = BE32(g_SRAM.settings.language); m_sram_language = static_cast<u32>(g_SRAM.settings.language);
// TODO: determine the purpose of Unk2 1 works for slot A, 0 works for both slot A and slot B // TODO: determine the purpose of m_unknown_2
*(u32*)&Unk2 = 0; // = _viReg[55]; static vu16* const _viReg = (u16*)0xCC002000; // 1 works for slot A, 0 works for both slot A and slot B
*(u16*)&deviceID = 0; memset(m_unknown_2.data(), 0,
calc_checksumsBE((u16*)this, 0xFE, &Checksum, &Checksum_Inv); m_unknown_2.size()); // = _viReg[55]; static vu16* const _viReg = (u16*)0xCC002000;
m_device_id = 0;
calc_checksumsBE((u16*)this, 0xFE, &m_checksum, &m_checksum_inv);
} }
}; };
static_assert(sizeof(Header) == BLOCK_SIZE);
struct DEntry struct DEntry
{ {
static const u8 DENTRY_SIZE = 0x40;
DEntry() { memset(this, 0xFF, DENTRY_SIZE); } DEntry() { memset(this, 0xFF, DENTRY_SIZE); }
std::string GCI_FileName() const std::string GCI_FileName() const
{ {
std::string filename = std::string((char*)Makercode, 2) + '-' + std::string filename =
std::string((char*)Gamecode, 4) + '-' + (char*)Filename + ".gci"; std::string(reinterpret_cast<const char*>(m_makercode.data()), m_makercode.size()) + '-' +
std::string(reinterpret_cast<const char*>(m_gamecode.data()), m_gamecode.size()) + '-' +
reinterpret_cast<const char*>(m_filename.data()) + ".gci";
return Common::EscapeFileName(filename); return Common::EscapeFileName(filename);
} }
u8 Gamecode[4]; // 0x00 0x04 Gamecode static constexpr std::array<u8, 4> UNINITIALIZED_GAMECODE{{0xFF, 0xFF, 0xFF, 0xFF}};
u8 Makercode[2]; // 0x04 0x02 Makercode
u8 Unused1; // 0x06 0x01 reserved/unused (always 0xff, has no effect) // 4 bytes at 0x00: Gamecode
u8 BIFlags; // 0x07 0x01 banner gfx format and icon animation (Image Key) std::array<u8, 4> m_gamecode;
// 2 bytes at 0x04: Makercode
std::array<u8, 2> m_makercode;
// 1 byte at 0x06: reserved/unused (always 0xff, has no effect)
u8 m_unused_1;
// 1 byte at 0x07: banner gfx format and icon animation (Image Key)
// Bit(s) Description // Bit(s) Description
// 2 Icon Animation 0: forward 1: ping-pong // 2 Icon Animation 0: forward 1: ping-pong
// 1 [--0: No Banner 1: Banner present--] WRONG! YAGCD LIES! // 1 [--0: No Banner 1: Banner present--] WRONG! YAGCD LIES!
@ -180,98 +219,133 @@ struct DEntry
// 01 CI8 banner // 01 CI8 banner
// 10 RGB5A3 banner // 10 RGB5A3 banner
// 11 ? maybe ==00? Time Splitters 2 and 3 have it and don't have banner // 11 ? maybe ==00? Time Splitters 2 and 3 have it and don't have banner
// u8 m_banner_and_icon_flags;
u8 Filename[DENTRY_STRLEN]; // 0x08 0x20 Filename
u8 ModTime[4]; // 0x28 0x04 Time of file's last modification in seconds since 12am, // 0x20 bytes at 0x08: Filename
// January 1st, 2000 std::array<u8, DENTRY_STRLEN> m_filename;
u8 ImageOffset[4]; // 0x2c 0x04 image data offset
u8 IconFmt[2]; // 0x30 0x02 icon gfx format (2bits per icon) // 4 bytes at 0x28: Time of file's last modification in seconds since 12am, January 1st, 2000
Common::BigEndianValue<u32> m_modification_time;
// 4 bytes at 0x2c: image data offset
Common::BigEndianValue<u32> m_image_offset;
// 2 bytes at 0x30: icon gfx format (2bits per icon)
// Bits Description // Bits Description
// 00 No icon // 00 No icon
// 01 CI8 with a shared color palette after the last frame // 01 CI8 with a shared color palette after the last frame
// 10 RGB5A3 // 10 RGB5A3
// 11 CI8 with a unique color palette after itself // 11 CI8 with a unique color palette after itself
// Common::BigEndianValue<u16> m_icon_format;
u8 AnimSpeed[2]; // 0x32 0x02 Animation speed (2bits per icon) (*1)
// 2 bytes at 0x32: Animation speed (2bits per icon)
// Bits Description // Bits Description
// 00 No icon // 00 No icon
// 01 Icon lasts for 4 frames // 01 Icon lasts for 4 frames
// 10 Icon lasts for 8 frames // 10 Icon lasts for 8 frames
// 11 Icon lasts for 12 frames // 11 Icon lasts for 12 frames
// Common::BigEndianValue<u16> m_animation_speed;
u8 Permissions; // 0x34 0x01 File-permissions
// 1 byte at 0x34: File-permissions
// Bit Permission Description // Bit Permission Description
// 4 no move File cannot be moved by the IPL // 4 no move File cannot be moved by the IPL
// 3 no copy File cannot be copied by the IPL // 3 no copy File cannot be copied by the IPL
// 2 public Can be read by any game // 2 public Can be read by any game
// u8 m_file_permissions;
u8 CopyCounter; // 0x35 0x01 Copy counter (*2)
u8 FirstBlock[2]; // 0x36 0x02 Block no of first block of file (0 == offset 0) // 1 byte at 0x35: Copy counter
u8 BlockCount[2]; // 0x38 0x02 File-length (number of blocks in file) u8 m_copy_counter;
u8 Unused2[2]; // 0x3a 0x02 Reserved/unused (always 0xffff, has no effect)
u8 CommentsAddr[4]; // 0x3c 0x04 Address of the two comments within the file data (*3) // 2 bytes at 0x36: Block number of first block of file (0 == offset 0)
Common::BigEndianValue<u16> m_first_block;
// 2 bytes at 0x38: File-length (number of blocks in file)
Common::BigEndianValue<u16> m_block_count;
// 2 bytes at 0x3a: Reserved/unused (always 0xffff, has no effect)
std::array<u8, 2> m_unused_2;
// 4 bytes at 0x3c: Address of the two comments within the file data
Common::BigEndianValue<u32> m_comments_address;
}; };
static_assert(sizeof(DEntry) == DENTRY_SIZE);
struct Directory struct Directory
{ {
DEntry Dir[DIRLEN]; // 0x0000 Directory Entries (max 127) std::array<DEntry, DIRLEN> m_dir_entries; // 0x0000 Directory Entries (max 127)
u8 Padding[0x3a]; std::array<u8, 0x3a> m_padding;
u16 UpdateCounter; // 0x1ffa 2 Update Counter Common::BigEndianValue<u16> m_update_counter; // 0x1ffa 2 Update Counter
u16 Checksum; // 0x1ffc 2 Additive Checksum u16 m_checksum; // 0x1ffc 2 Additive Checksum
u16 Checksum_Inv; // 0x1ffe 2 Inverse Checksum u16 m_checksum_inv; // 0x1ffe 2 Inverse Checksum
Directory() Directory()
{ {
memset(this, 0xFF, BLOCK_SIZE); memset(this, 0xFF, BLOCK_SIZE);
UpdateCounter = 0; m_update_counter = 0;
Checksum = BE16(0xF003); m_checksum = BE16(0xF003);
Checksum_Inv = 0; m_checksum_inv = 0;
} }
void Replace(DEntry d, int idx) void Replace(DEntry d, int idx)
{ {
Dir[idx] = d; m_dir_entries[idx] = d;
fixChecksums(); fixChecksums();
} }
void fixChecksums() { calc_checksumsBE((u16*)this, 0xFFE, &Checksum, &Checksum_Inv); } void fixChecksums() { calc_checksumsBE((u16*)this, 0xFFE, &m_checksum, &m_checksum_inv); }
}; };
static_assert(sizeof(Directory) == BLOCK_SIZE);
struct BlockAlloc struct BlockAlloc
{ {
u16 Checksum; // 0x0000 2 Additive Checksum // 2 bytes at 0x0000: Additive Checksum
u16 Checksum_Inv; // 0x0002 2 Inverse Checksum u16 m_checksum;
u16 UpdateCounter; // 0x0004 2 Update Counter
u16 FreeBlocks; // 0x0006 2 Free Blocks // 2 bytes at 0x0002: Inverse Checksum
u16 LastAllocated; // 0x0008 2 Last allocated Block u16 m_checksum_inv;
u16 Map[BAT_SIZE]; // 0x000a 0x1ff8 Map of allocated Blocks
// 2 bytes at 0x0004: Update Counter
Common::BigEndianValue<u16> m_update_counter;
// 2 bytes at 0x0006: Free Blocks
Common::BigEndianValue<u16> m_free_blocks;
// 2 bytes at 0x0008: Last allocated Block
Common::BigEndianValue<u16> m_last_allocated_block;
// 0x1ff8 bytes at 0x000a: Map of allocated Blocks
std::array<Common::BigEndianValue<u16>, BAT_SIZE> m_map;
u16 GetNextBlock(u16 Block) const; u16 GetNextBlock(u16 Block) const;
u16 NextFreeBlock(u16 MaxBlock, u16 StartingBlock = MC_FST_BLOCKS) const; u16 NextFreeBlock(u16 MaxBlock, u16 StartingBlock = MC_FST_BLOCKS) const;
bool ClearBlocks(u16 StartingBlock, u16 Length); bool ClearBlocks(u16 StartingBlock, u16 Length);
void fixChecksums() { calc_checksumsBE((u16*)&UpdateCounter, 0xFFE, &Checksum, &Checksum_Inv); } void fixChecksums()
{
calc_checksumsBE((u16*)&m_update_counter, 0xFFE, &m_checksum, &m_checksum_inv);
}
explicit BlockAlloc(u16 sizeMb = MemCard2043Mb) explicit BlockAlloc(u16 sizeMb = MemCard2043Mb)
{ {
memset(this, 0, BLOCK_SIZE); memset(this, 0, BLOCK_SIZE);
// UpdateCounter = 0; m_free_blocks = (sizeMb * MBIT_TO_BLOCKS) - MC_FST_BLOCKS;
FreeBlocks = BE16((sizeMb * MBIT_TO_BLOCKS) - MC_FST_BLOCKS); m_last_allocated_block = 4;
LastAllocated = BE16(4);
fixChecksums(); fixChecksums();
} }
u16 AssignBlocksContiguous(u16 length) u16 AssignBlocksContiguous(u16 length)
{ {
u16 starting = BE16(LastAllocated) + 1; u16 starting = m_last_allocated_block + 1;
if (length > BE16(FreeBlocks)) if (length > m_free_blocks)
return 0xFFFF; return 0xFFFF;
u16 current = starting; u16 current = starting;
while ((current - starting + 1) < length) while ((current - starting + 1) < length)
{ {
Map[current - 5] = BE16(current + 1); m_map[current - 5] = current + 1;
current++; current++;
} }
Map[current - 5] = 0xFFFF; m_map[current - 5] = 0xFFFF;
LastAllocated = BE16(current); m_last_allocated_block = current;
FreeBlocks = BE16(BE16(FreeBlocks) - length); m_free_blocks = m_free_blocks - length;
fixChecksums(); fixChecksums();
return BE16(starting); return starting;
} }
}; };
static_assert(sizeof(BlockAlloc) == BLOCK_SIZE);
#pragma pack(pop) #pragma pack(pop)
class GCIFile class GCIFile
@ -280,9 +354,11 @@ public:
bool LoadSaveBlocks(); bool LoadSaveBlocks();
bool HasCopyProtection() const bool HasCopyProtection() const
{ {
if ((strcmp((char*)m_gci_header.Filename, "PSO_SYSTEM") == 0) || if ((strcmp(reinterpret_cast<const char*>(m_gci_header.m_filename.data()), "PSO_SYSTEM") ==
(strcmp((char*)m_gci_header.Filename, "PSO3_SYSTEM") == 0) || 0) ||
(strcmp((char*)m_gci_header.Filename, "f_zero.dat") == 0)) (strcmp(reinterpret_cast<const char*>(m_gci_header.m_filename.data()), "PSO3_SYSTEM") ==
0) ||
(strcmp(reinterpret_cast<const char*>(m_gci_header.m_filename.data()), "f_zero.dat") == 0))
return true; return true;
return false; return false;
} }
@ -300,20 +376,28 @@ class GCMemcard
{ {
private: private:
bool m_valid; bool m_valid;
std::string m_fileName; std::string m_filename;
u32 maxBlock; u32 m_size_blocks;
u16 m_sizeMb; u16 m_size_mb;
Header hdr; Header m_header_block;
Directory dir, dir_backup, *CurrentDir, *PreviousDir; std::array<Directory, 2> m_directory_blocks;
BlockAlloc bat, bat_backup, *CurrentBat, *PreviousBat; std::array<BlockAlloc, 2> m_bat_blocks;
std::vector<GCMBlock> m_data_blocks;
std::vector<GCMBlock> mc_data_blocks; int m_active_directory;
int m_active_bat;
u32 ImportGciInternal(File::IOFile&& gci, const std::string& inputFile, u32 ImportGciInternal(File::IOFile&& gci, const std::string& inputFile,
const std::string& outputFile); const std::string& outputFile);
void InitDirBatPointers(); void InitActiveDirBat();
const Directory& GetActiveDirectory() const;
const BlockAlloc& GetActiveBat() const;
void UpdateDirectory(const Directory& directory);
void UpdateBat(const BlockAlloc& bat);
public: public:
explicit GCMemcard(const std::string& fileName, bool forceCreation = false, explicit GCMemcard(const std::string& fileName, bool forceCreation = false,
@ -366,8 +450,9 @@ public:
u32 DEntry_CommentsAddress(u8 index) const; u32 DEntry_CommentsAddress(u8 index) const;
std::string GetSaveComment1(u8 index) const; std::string GetSaveComment1(u8 index) const;
std::string GetSaveComment2(u8 index) const; std::string GetSaveComment2(u8 index) const;
// Copies a DEntry from u8 index to DEntry& data
bool GetDEntry(u8 index, DEntry& dest) const; // Fetches a DEntry from the given file index.
std::optional<DEntry> GetDEntry(u8 index) const;
u32 GetSaveData(u8 index, std::vector<GCMBlock>& saveBlocks) const; u32 GetSaveData(u8 index, std::vector<GCMBlock>& saveBlocks) const;

View File

@ -58,7 +58,7 @@ int GCMemcardDirectory::LoadGCI(const std::string& file_name, bool current_game_
} }
} }
u16 num_blocks = BE16(gci.m_gci_header.BlockCount); u16 num_blocks = gci.m_gci_header.m_block_count;
// largest number of free blocks on a memory card // largest number of free blocks on a memory card
// in reality, there are not likely any valid gci files > 251 blocks // in reality, there are not likely any valid gci files > 251 blocks
if (num_blocks > 2043) if (num_blocks > 2043)
@ -79,7 +79,7 @@ int GCMemcardDirectory::LoadGCI(const std::string& file_name, bool current_game_
return NO_INDEX; return NO_INDEX;
} }
if (m_game_id == BE32(gci.m_gci_header.Gamecode)) if (m_game_id == BE32(gci.m_gci_header.m_gamecode.data()))
{ {
gci.LoadSaveBlocks(); gci.LoadSaveBlocks();
} }
@ -89,8 +89,8 @@ int GCMemcardDirectory::LoadGCI(const std::string& file_name, bool current_game_
{ {
return NO_INDEX; return NO_INDEX;
} }
int total_blocks = BE16(m_hdr.SizeMb) * MBIT_TO_BLOCKS - MC_FST_BLOCKS; int total_blocks = m_hdr.m_size_mb * MBIT_TO_BLOCKS - MC_FST_BLOCKS;
int free_blocks = BE16(m_bat1.FreeBlocks); int free_blocks = m_bat1.m_free_blocks;
if (total_blocks > free_blocks * 10) if (total_blocks > free_blocks * 10)
{ {
PanicAlertT("%s\nwas not loaded because there is less than 10%% free blocks available on " PanicAlertT("%s\nwas not loaded because there is less than 10%% free blocks available on "
@ -108,7 +108,7 @@ int GCMemcardDirectory::LoadGCI(const std::string& file_name, bool current_game_
file_name.c_str()); file_name.c_str());
return NO_INDEX; return NO_INDEX;
} }
*(u16*)&gci.m_gci_header.FirstBlock = first_block; gci.m_gci_header.m_first_block = first_block;
if (gci.HasCopyProtection() && gci.LoadSaveBlocks()) if (gci.HasCopyProtection() && gci.LoadSaveBlocks())
{ {
GCMemcard::PSO_MakeSaveGameValid(m_hdr, gci.m_gci_header, gci.m_save_data); GCMemcard::PSO_MakeSaveGameValid(m_hdr, gci.m_gci_header, gci.m_save_data);
@ -151,7 +151,7 @@ std::vector<std::string> GCMemcardDirectory::GetFileNamesForGameID(const std::st
if (std::find(loaded_saves.begin(), loaded_saves.end(), gci_filename) != loaded_saves.end()) if (std::find(loaded_saves.begin(), loaded_saves.end(), gci_filename) != loaded_saves.end())
continue; continue;
const u16 num_blocks = BE16(gci.m_gci_header.BlockCount); const u16 num_blocks = gci.m_gci_header.m_block_count;
// largest number of free blocks on a memory card // largest number of free blocks on a memory card
// in reality, there are not likely any valid gci files > 251 blocks // in reality, there are not likely any valid gci files > 251 blocks
if (num_blocks > 2043) if (num_blocks > 2043)
@ -166,7 +166,7 @@ std::vector<std::string> GCMemcardDirectory::GetFileNamesForGameID(const std::st
// card (see above method), but since we're only loading the saves for one GameID here, we're // card (see above method), but since we're only loading the saves for one GameID here, we're
// definitely not going to run out of space. // definitely not going to run out of space.
if (game_code == BE32(gci.m_gci_header.Gamecode)) if (game_code == BE32(gci.m_gci_header.m_gamecode.data()))
{ {
loaded_saves.push_back(gci_filename); loaded_saves.push_back(gci_filename);
filenames.push_back(file_name); filenames.push_back(file_name);
@ -429,16 +429,17 @@ inline void GCMemcardDirectory::SyncSaves()
{ {
Directory* current = &m_dir2; Directory* current = &m_dir2;
if (BE16(m_dir1.UpdateCounter) > BE16(m_dir2.UpdateCounter)) if (m_dir1.m_update_counter > m_dir2.m_update_counter)
{ {
current = &m_dir1; current = &m_dir1;
} }
for (u32 i = 0; i < DIRLEN; ++i) for (u32 i = 0; i < DIRLEN; ++i)
{ {
if (BE32(current->Dir[i].Gamecode) != 0xFFFFFFFF) if (current->m_dir_entries[i].m_gamecode != DEntry::UNINITIALIZED_GAMECODE)
{ {
INFO_LOG(EXPANSIONINTERFACE, "Syncing save 0x%x", *(u32*)&(current->Dir[i].Gamecode)); INFO_LOG(EXPANSIONINTERFACE, "Syncing save 0x%x",
BE32(current->m_dir_entries[i].m_gamecode.data()));
bool added = false; bool added = false;
while (i >= m_saves.size()) while (i >= m_saves.size())
{ {
@ -447,20 +448,22 @@ inline void GCMemcardDirectory::SyncSaves()
added = true; added = true;
} }
if (added || memcmp((u8*)&(m_saves[i].m_gci_header), (u8*)&(current->Dir[i]), DENTRY_SIZE)) if (added ||
memcmp((u8*)&(m_saves[i].m_gci_header), (u8*)&(current->m_dir_entries[i]), DENTRY_SIZE))
{ {
m_saves[i].m_dirty = true; m_saves[i].m_dirty = true;
u32 gamecode = BE32(m_saves[i].m_gci_header.Gamecode); u32 gamecode = BE32(m_saves[i].m_gci_header.m_gamecode.data());
u32 new_gamecode = BE32(current->Dir[i].Gamecode); u32 new_gamecode = BE32(current->m_dir_entries[i].m_gamecode.data());
u32 old_start = BE16(m_saves[i].m_gci_header.FirstBlock); u32 old_start = m_saves[i].m_gci_header.m_first_block;
u32 new_start = BE16(current->Dir[i].FirstBlock); u32 new_start = current->m_dir_entries[i].m_first_block;
if ((gamecode != 0xFFFFFFFF) && (gamecode != new_gamecode)) if ((gamecode != 0xFFFFFFFF) && (gamecode != new_gamecode))
{ {
PanicAlertT("Game overwrote with another games save. Data corruption ahead 0x%x, 0x%x", PanicAlertT("Game overwrote with another games save. Data corruption ahead 0x%x, 0x%x",
BE32(m_saves[i].m_gci_header.Gamecode), BE32(current->Dir[i].Gamecode)); BE32(m_saves[i].m_gci_header.m_gamecode.data()),
BE32(current->m_dir_entries[i].m_gamecode.data()));
} }
memcpy((u8*)&(m_saves[i].m_gci_header), (u8*)&(current->Dir[i]), DENTRY_SIZE); memcpy((u8*)&(m_saves[i].m_gci_header), (u8*)&(current->m_dir_entries[i]), DENTRY_SIZE);
if (old_start != new_start) if (old_start != new_start)
{ {
INFO_LOG(EXPANSIONINTERFACE, "Save moved from 0x%x to 0x%x", old_start, new_start); INFO_LOG(EXPANSIONINTERFACE, "Save moved from 0x%x to 0x%x", old_start, new_start);
@ -476,8 +479,8 @@ inline void GCMemcardDirectory::SyncSaves()
else if ((i < m_saves.size()) && (*(u32*)&(m_saves[i].m_gci_header) != 0xFFFFFFFF)) else if ((i < m_saves.size()) && (*(u32*)&(m_saves[i].m_gci_header) != 0xFFFFFFFF))
{ {
INFO_LOG(EXPANSIONINTERFACE, "Clearing and/or deleting save 0x%x", INFO_LOG(EXPANSIONINTERFACE, "Clearing and/or deleting save 0x%x",
BE32(m_saves[i].m_gci_header.Gamecode)); BE32(m_saves[i].m_gci_header.m_gamecode.data()));
*(u32*)&(m_saves[i].m_gci_header.Gamecode) = 0xFFFFFFFF; m_saves[i].m_gci_header.m_gamecode = DEntry::UNINITIALIZED_GAMECODE;
m_saves[i].m_save_data.clear(); m_saves[i].m_save_data.clear();
m_saves[i].m_used_blocks.clear(); m_saves[i].m_used_blocks.clear();
m_saves[i].m_dirty = true; m_saves[i].m_dirty = true;
@ -488,7 +491,7 @@ inline s32 GCMemcardDirectory::SaveAreaRW(u32 block, bool writing)
{ {
for (u16 i = 0; i < m_saves.size(); ++i) for (u16 i = 0; i < m_saves.size(); ++i)
{ {
if (BE32(m_saves[i].m_gci_header.Gamecode) != 0xFFFFFFFF) if (m_saves[i].m_gci_header.m_gamecode != DEntry::UNINITIALIZED_GAMECODE)
{ {
if (m_saves[i].m_used_blocks.size() == 0) if (m_saves[i].m_used_blocks.size() == 0)
{ {
@ -500,7 +503,7 @@ inline s32 GCMemcardDirectory::SaveAreaRW(u32 block, bool writing)
{ {
if (!m_saves[i].LoadSaveBlocks()) if (!m_saves[i].LoadSaveBlocks())
{ {
int num_blocks = BE16(m_saves[i].m_gci_header.BlockCount); int num_blocks = m_saves[i].m_gci_header.m_block_count;
while (num_blocks) while (num_blocks)
{ {
m_saves[i].m_save_data.emplace_back(); m_saves[i].m_save_data.emplace_back();
@ -514,7 +517,7 @@ inline s32 GCMemcardDirectory::SaveAreaRW(u32 block, bool writing)
} }
m_last_block = block; m_last_block = block;
m_last_block_address = m_saves[i].m_save_data[idx].block; m_last_block_address = m_saves[i].m_save_data[idx].m_block.data();
return m_last_block; return m_last_block;
} }
} }
@ -546,12 +549,12 @@ s32 GCMemcardDirectory::DirectoryWrite(u32 dest_address, u32 length, const u8* s
bool GCMemcardDirectory::SetUsedBlocks(int save_index) bool GCMemcardDirectory::SetUsedBlocks(int save_index)
{ {
BlockAlloc* current_bat; BlockAlloc* current_bat;
if (BE16(m_bat2.UpdateCounter) > BE16(m_bat1.UpdateCounter)) if (m_bat2.m_update_counter > m_bat1.m_update_counter)
current_bat = &m_bat2; current_bat = &m_bat2;
else else
current_bat = &m_bat1; current_bat = &m_bat1;
u16 block = BE16(m_saves[save_index].m_gci_header.FirstBlock); u16 block = m_saves[save_index].m_gci_header.m_first_block;
while (block != 0xFFFF) while (block != 0xFFFF)
{ {
m_saves[save_index].m_used_blocks.push_back(block); m_saves[save_index].m_used_blocks.push_back(block);
@ -563,7 +566,7 @@ bool GCMemcardDirectory::SetUsedBlocks(int save_index)
} }
} }
u16 num_blocks = BE16(m_saves[save_index].m_gci_header.BlockCount); u16 num_blocks = m_saves[save_index].m_gci_header.m_block_count;
u16 blocks_from_bat = (u16)m_saves[save_index].m_used_blocks.size(); u16 blocks_from_bat = (u16)m_saves[save_index].m_used_blocks.size();
if (blocks_from_bat != num_blocks) if (blocks_from_bat != num_blocks)
{ {
@ -585,7 +588,7 @@ void GCMemcardDirectory::FlushToFile()
{ {
if (m_saves[i].m_dirty) if (m_saves[i].m_dirty)
{ {
if (BE32(m_saves[i].m_gci_header.Gamecode) != 0xFFFFFFFF) if (m_saves[i].m_gci_header.m_gamecode != DEntry::UNINITIALIZED_GAMECODE)
{ {
m_saves[i].m_dirty = false; m_saves[i].m_dirty = false;
if (m_saves[i].m_save_data.size() == 0) if (m_saves[i].m_save_data.size() == 0)
@ -654,7 +657,7 @@ void GCMemcardDirectory::FlushToFile()
// simultaneously // simultaneously
// this ensures that the save data for all of the current games gci files are stored in the // this ensures that the save data for all of the current games gci files are stored in the
// savestate // savestate
u32 gamecode = BE32(m_saves[i].m_gci_header.Gamecode); u32 gamecode = BE32(m_saves[i].m_gci_header.m_gamecode.data());
if (gamecode != m_game_id && gamecode != 0xFFFFFFFF && m_saves[i].m_save_data.size()) if (gamecode != m_game_id && gamecode != 0xFFFFFFFF && m_saves[i].m_save_data.size())
{ {
INFO_LOG(EXPANSIONINTERFACE, "Flushing savedata to disk for %s", INFO_LOG(EXPANSIONINTERFACE, "Flushing savedata to disk for %s",
@ -703,7 +706,7 @@ bool GCIFile::LoadSaveBlocks()
INFO_LOG(EXPANSIONINTERFACE, "Reading savedata from disk for %s", m_filename.c_str()); INFO_LOG(EXPANSIONINTERFACE, "Reading savedata from disk for %s", m_filename.c_str());
save_file.Seek(DENTRY_SIZE, SEEK_SET); save_file.Seek(DENTRY_SIZE, SEEK_SET);
u16 num_blocks = BE16(m_gci_header.BlockCount); u16 num_blocks = m_gci_header.m_block_count;
m_save_data.resize(num_blocks); m_save_data.resize(num_blocks);
if (!save_file.ReadBytes(m_save_data.data(), num_blocks * BLOCK_SIZE)) if (!save_file.ReadBytes(m_save_data.data(), num_blocks * BLOCK_SIZE))
{ {

View File

@ -199,10 +199,12 @@ void GCMemcardManager::UpdateSlotTable(int slot)
auto* icon = new QTableWidgetItem; auto* icon = new QTableWidgetItem;
icon->setData(Qt::DecorationRole, frames[0]); icon->setData(Qt::DecorationRole, frames[0]);
DEntry d; std::optional<DEntry> entry = memcard->GetDEntry(file_index);
memcard->GetDEntry(file_index, d);
const auto speed = ((d.AnimSpeed[0] & 1) << 2) + (d.AnimSpeed[1] & 1); // TODO: This is wrong, the animation speed is not static and is already correctly calculated in
// GetIconFromSaveFile(), just not returned
const u16 animation_speed = entry ? entry->m_animation_speed : 1;
const auto speed = (((animation_speed >> 8) & 1) << 2) + (animation_speed & 1);
m_slot_active_icons[slot].push_back({speed, frames}); m_slot_active_icons[slot].push_back({speed, frames});