Merge pull request #5089 from lioncash/gcmemcard

GCMemcard: Minor cleanup
This commit is contained in:
JosJuice
2017-03-15 08:59:32 +01:00
committed by GitHub
5 changed files with 207 additions and 204 deletions
+9 -9
View File
@@ -70,22 +70,22 @@ enum
class MemoryCardBase
{
public:
explicit MemoryCardBase(int _card_index = 0, int sizeMb = MemCard2043Mb)
: card_index(_card_index), nintendo_card_id(sizeMb)
explicit MemoryCardBase(int card_index = 0, int size_mbits = MemCard2043Mb)
: m_card_index(card_index), m_nintendo_card_id(size_mbits)
{
}
virtual ~MemoryCardBase() {}
virtual s32 Read(u32 address, s32 length, u8* destaddress) = 0;
virtual s32 Write(u32 destaddress, s32 length, const u8* srcaddress) = 0;
virtual s32 Read(u32 src_address, s32 length, u8* dest_address) = 0;
virtual s32 Write(u32 dest_address, s32 length, const u8* src_address) = 0;
virtual void ClearBlock(u32 address) = 0;
virtual void ClearAll() = 0;
virtual void DoState(PointerWrap& p) = 0;
u32 GetCardId() const { return nintendo_card_id; }
bool IsAddressInBounds(u32 address) const { return address <= (memory_card_size - 1); }
u32 GetCardId() const { return m_nintendo_card_id; }
bool IsAddressInBounds(u32 address) const { return address <= (m_memory_card_size - 1); }
protected:
int card_index;
u16 nintendo_card_id;
u32 memory_card_size;
int m_card_index;
u16 m_nintendo_card_id;
u32 m_memory_card_size;
};
struct GCMBlock
File diff suppressed because it is too large Load Diff
@@ -17,33 +17,33 @@
// Uncomment this to write the system data of the memorycard from directory to disc
//#define _WRITE_MC_HEADER 1
void MigrateFromMemcardFile(const std::string& strDirectoryName, int card_index);
void MigrateFromMemcardFile(const std::string& directory_name, int card_index);
class GCMemcardDirectory : public MemoryCardBase, NonCopyable
{
public:
GCMemcardDirectory(const std::string& directory, int slot, u16 sizeMb, bool shift_jis,
DiscIO::Region card_region, int gameId);
GCMemcardDirectory(const std::string& directory, int slot, u16 size_mbits, bool shift_jis,
DiscIO::Region card_region, int game_id);
~GCMemcardDirectory();
void FlushToFile();
void FlushThread();
s32 Read(u32 address, s32 length, u8* destaddress) override;
s32 Write(u32 destaddress, s32 length, const u8* srcaddress) override;
s32 Read(u32 src_address, s32 length, u8* dest_address) override;
s32 Write(u32 dest_address, s32 length, const u8* src_address) override;
void ClearBlock(u32 address) override;
void ClearAll() override {}
void DoState(PointerWrap& p) override;
private:
int LoadGCI(const std::string& fileName, DiscIO::Region card_region, bool currentGameOnly);
int LoadGCI(const std::string& file_name, DiscIO::Region card_region, bool current_game_only);
inline s32 SaveAreaRW(u32 block, bool writing = false);
// s32 DirectoryRead(u32 offset, u32 length, u8* destaddress);
s32 DirectoryWrite(u32 destaddress, u32 length, const u8* srcaddress);
// s32 DirectoryRead(u32 offset, u32 length, u8* dest_address);
s32 DirectoryWrite(u32 dest_address, u32 length, const u8* src_address);
inline void SyncSaves();
bool SetUsedBlocks(int saveIndex);
bool SetUsedBlocks(int save_index);
u32 m_GameId;
s32 m_LastBlock;
u8* m_LastBlockAddress;
u32 m_game_id;
s32 m_last_block;
u8* m_last_block_address;
Header m_hdr;
Directory m_dir1, m_dir2;
@@ -51,7 +51,7 @@ private:
std::vector<GCIFile> m_saves;
std::vector<std::string> m_loaded_saves;
std::string m_SaveDirectory;
std::string m_save_directory;
const std::chrono::seconds flush_interval = std::chrono::seconds(1);
Common::Event m_flush_trigger;
std::mutex m_write_mutex;
+38 -36
View File
@@ -22,38 +22,39 @@
#define SIZE_TO_Mb (1024 * 8 * 16)
#define MC_HDR_SIZE 0xA000
MemoryCard::MemoryCard(const std::string& filename, int _card_index, u16 sizeMb)
: MemoryCardBase(_card_index, sizeMb), m_filename(filename)
MemoryCard::MemoryCard(const std::string& filename, int card_index, u16 size_mbits)
: MemoryCardBase(card_index, size_mbits), m_filename(filename)
{
File::IOFile pFile(m_filename, "rb");
if (pFile)
File::IOFile file(m_filename, "rb");
if (file)
{
// Measure size of the existing memcard file.
memory_card_size = (u32)pFile.GetSize();
nintendo_card_id = memory_card_size / SIZE_TO_Mb;
m_memcard_data = std::make_unique<u8[]>(memory_card_size);
memset(&m_memcard_data[0], 0xFF, memory_card_size);
m_memory_card_size = (u32)file.GetSize();
m_nintendo_card_id = m_memory_card_size / SIZE_TO_Mb;
m_memcard_data = std::make_unique<u8[]>(m_memory_card_size);
memset(&m_memcard_data[0], 0xFF, m_memory_card_size);
INFO_LOG(EXPANSIONINTERFACE, "Reading memory card %s", m_filename.c_str());
pFile.ReadBytes(&m_memcard_data[0], memory_card_size);
file.ReadBytes(&m_memcard_data[0], m_memory_card_size);
}
else
{
// Create a new 128Mb memcard
nintendo_card_id = sizeMb;
memory_card_size = sizeMb * SIZE_TO_Mb;
m_nintendo_card_id = size_mbits;
m_memory_card_size = size_mbits * SIZE_TO_Mb;
m_memcard_data = std::make_unique<u8[]>(memory_card_size);
m_memcard_data = std::make_unique<u8[]>(m_memory_card_size);
// Fills in MC_HDR_SIZE bytes
GCMemcard::Format(&m_memcard_data[0], m_filename.find(".JAP.raw") != std::string::npos, sizeMb);
memset(&m_memcard_data[MC_HDR_SIZE], 0xFF, memory_card_size - MC_HDR_SIZE);
GCMemcard::Format(&m_memcard_data[0], m_filename.find(".JAP.raw") != std::string::npos,
size_mbits);
memset(&m_memcard_data[MC_HDR_SIZE], 0xFF, m_memory_card_size - MC_HDR_SIZE);
INFO_LOG(EXPANSIONINTERFACE, "No memory card found. A new one was created instead.");
}
// Class members (including inherited ones) have now been initialized, so
// it's safe to startup the flush thread (which reads them).
m_flush_buffer = std::make_unique<u8[]>(memory_card_size);
m_flush_buffer = std::make_unique<u8[]>(m_memory_card_size);
m_flush_thread = std::thread(&MemoryCard::FlushThread, this);
}
@@ -74,7 +75,8 @@ void MemoryCard::FlushThread()
return;
}
Common::SetCurrentThreadName(StringFromFormat("Memcard %d flushing thread", card_index).c_str());
Common::SetCurrentThreadName(
StringFromFormat("Memcard %d flushing thread", m_card_index).c_str());
const auto flush_interval = std::chrono::seconds(15);
@@ -94,9 +96,9 @@ void MemoryCard::FlushThread()
// Opening the file is purposefully done each iteration to ensure the
// file doesn't disappear out from under us after the first check.
File::IOFile pFile(m_filename, "r+b");
File::IOFile file(m_filename, "r+b");
if (!pFile)
if (!file)
{
std::string dir;
SplitPath(m_filename, &dir, nullptr, nullptr);
@@ -104,11 +106,11 @@ void MemoryCard::FlushThread()
{
File::CreateFullPath(dir);
}
pFile.Open(m_filename, "wb");
file.Open(m_filename, "wb");
}
// Note - pFile may have changed above, after ctor
if (!pFile)
// Note - file may have changed above, after ctor
if (!file)
{
PanicAlertT(
"Could not write memory card file %s.\n\n"
@@ -124,14 +126,14 @@ void MemoryCard::FlushThread()
{
std::unique_lock<std::mutex> l(m_flush_mutex);
memcpy(&m_flush_buffer[0], &m_memcard_data[0], memory_card_size);
memcpy(&m_flush_buffer[0], &m_memcard_data[0], m_memory_card_size);
}
pFile.WriteBytes(&m_flush_buffer[0], memory_card_size);
file.WriteBytes(&m_flush_buffer[0], m_memory_card_size);
if (!do_exit)
{
Core::DisplayMessage(StringFromFormat("Wrote memory card %c contents to %s",
card_index ? 'B' : 'A', m_filename.c_str())
m_card_index ? 'B' : 'A', m_filename.c_str())
.c_str(),
4000);
}
@@ -147,29 +149,29 @@ void MemoryCard::MakeDirty()
m_dirty.Set();
}
s32 MemoryCard::Read(u32 srcaddress, s32 length, u8* destaddress)
s32 MemoryCard::Read(u32 src_address, s32 length, u8* dest_address)
{
if (!IsAddressInBounds(srcaddress))
if (!IsAddressInBounds(src_address))
{
PanicAlertT("MemoryCard: Read called with invalid source address (0x%x)", srcaddress);
PanicAlertT("MemoryCard: Read called with invalid source address (0x%x)", src_address);
return -1;
}
memcpy(destaddress, &m_memcard_data[srcaddress], length);
memcpy(dest_address, &m_memcard_data[src_address], length);
return length;
}
s32 MemoryCard::Write(u32 destaddress, s32 length, const u8* srcaddress)
s32 MemoryCard::Write(u32 dest_address, s32 length, const u8* src_address)
{
if (!IsAddressInBounds(destaddress))
if (!IsAddressInBounds(dest_address))
{
PanicAlertT("MemoryCard: Write called with invalid destination address (0x%x)", destaddress);
PanicAlertT("MemoryCard: Write called with invalid destination address (0x%x)", dest_address);
return -1;
}
{
std::unique_lock<std::mutex> l(m_flush_mutex);
memcpy(&m_memcard_data[destaddress], srcaddress, length);
memcpy(&m_memcard_data[dest_address], src_address, length);
}
MakeDirty();
return length;
@@ -194,14 +196,14 @@ void MemoryCard::ClearAll()
{
{
std::unique_lock<std::mutex> l(m_flush_mutex);
memset(&m_memcard_data[0], 0xFF, memory_card_size);
memset(&m_memcard_data[0], 0xFF, m_memory_card_size);
}
MakeDirty();
}
void MemoryCard::DoState(PointerWrap& p)
{
p.Do(card_index);
p.Do(memory_card_size);
p.DoArray(&m_memcard_data[0], memory_card_size);
p.Do(m_card_index);
p.Do(m_memory_card_size);
p.DoArray(&m_memcard_data[0], m_memory_card_size);
}
+3 -3
View File
@@ -17,13 +17,13 @@ class PointerWrap;
class MemoryCard : public MemoryCardBase
{
public:
MemoryCard(const std::string& filename, int _card_index, u16 sizeMb = MemCard2043Mb);
MemoryCard(const std::string& filename, int card_index, u16 size_mbits = MemCard2043Mb);
~MemoryCard();
void FlushThread();
void MakeDirty();
s32 Read(u32 address, s32 length, u8* destaddress) override;
s32 Write(u32 destaddress, s32 length, const u8* srcaddress) override;
s32 Read(u32 src_address, s32 length, u8* dest_address) override;
s32 Write(u32 dest_address, s32 length, const u8* src_address) override;
void ClearBlock(u32 address) override;
void ClearAll() override;
void DoState(PointerWrap& p) override;