From 4dec0dee2feff7cdf50d4d83758b05e0d6130940 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 21 Mar 2021 00:10:32 +1000 Subject: [PATCH] MemoryCardImage: Support passing import buffers in --- src/core/memory_card_image.cpp | 48 ++++++++++++++++++++-------------- src/core/memory_card_image.h | 2 ++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/core/memory_card_image.cpp b/src/core/memory_card_image.cpp index 2e38d99c5..562b9286b 100644 --- a/src/core/memory_card_image.cpp +++ b/src/core/memory_card_image.cpp @@ -129,6 +129,13 @@ bool SaveToFile(const DataArray& data, const char* filename) return true; } +bool IsValid(const DataArray& data) +{ + // TODO: Check checksum? + const u8* fptr = GetFramePtr(data, 0, 0); + return fptr[0] == 'M' && fptr[1] == 'C'; +} + void Format(DataArray* data) { // fill everything with FF @@ -385,23 +392,19 @@ bool DeleteFile(DataArray* data, const FileInfo& fi) return true; } -static bool ImportCardMCD(DataArray* data, const char* filename) +static bool ImportCardMCD(DataArray* data, const char* filename, std::vector file_data) { - std::optional> file_data = FileSystem::ReadBinaryFile(filename); - if (!file_data.has_value()) - return false; - - if (file_data->size() != DATA_SIZE) + if (file_data.size() != DATA_SIZE) { Log_ErrorPrintf("Failed to import memory card from '%s': file is incorrect size.", filename); return false; } - std::memcpy(data->data(), file_data->data(), DATA_SIZE); + std::memcpy(data->data(), file_data.data(), DATA_SIZE); return true; } -static bool ImportCardGME(DataArray* data, const char* filename) +static bool ImportCardGME(DataArray* data, const char* filename, std::vector file_data) { #pragma pack(push, 1) struct GMEHeader @@ -417,11 +420,7 @@ static bool ImportCardGME(DataArray* data, const char* filename) static_assert(sizeof(GMEHeader) == 0xF40); #pragma pack(pop) - std::optional> file_data = FileSystem::ReadBinaryFile(filename); - if (!file_data.has_value()) - return false; - - if (file_data->size() < (sizeof(GMEHeader) + BLOCK_SIZE)) + if (file_data.size() < (sizeof(GMEHeader) + BLOCK_SIZE)) { Log_ErrorPrintf("Failed to import GME memory card from '%s': file is incorrect size.", filename); return false; @@ -429,19 +428,19 @@ static bool ImportCardGME(DataArray* data, const char* filename) // if it's too small, pad it const u32 expected_size = sizeof(GMEHeader) + DATA_SIZE; - if (file_data->size() < expected_size) + if (file_data.size() < expected_size) { Log_WarningPrintf("GME memory card '%s' is too small (got %zu expected %u), padding with zeroes", filename, - file_data->size(), expected_size); - file_data->resize(expected_size); + file_data.size(), expected_size); + file_data.resize(expected_size); } // we don't actually care about the header, just skip over it - std::memcpy(data->data(), file_data->data() + sizeof(GMEHeader), DATA_SIZE); + std::memcpy(data->data(), file_data.data() + sizeof(GMEHeader), DATA_SIZE); return true; } -bool ImportCard(DataArray* data, const char* filename) +bool ImportCard(DataArray* data, const char* filename, std::vector file_data) { const char* extension = std::strrchr(filename, '.'); if (!extension) @@ -453,11 +452,11 @@ bool ImportCard(DataArray* data, const char* filename) if (StringUtil::Strcasecmp(extension, ".mcd") == 0 || StringUtil::Strcasecmp(extension, ".mcr") == 0 || StringUtil::Strcasecmp(extension, ".mc") == 0 || StringUtil::Strcasecmp(extension, ".srm") == 0) { - return ImportCardMCD(data, filename); + return ImportCardMCD(data, filename, std::move(file_data)); } else if (StringUtil::Strcasecmp(extension, ".gme") == 0) { - return ImportCardGME(data, filename); + return ImportCardGME(data, filename, std::move(file_data)); } else { @@ -466,6 +465,15 @@ bool ImportCard(DataArray* data, const char* filename) } } +bool ImportCard(DataArray* data, const char* filename) +{ + std::optional> file_data = FileSystem::ReadBinaryFile(filename); + if (!file_data.has_value()) + return false; + + return ImportCard(data, filename, std::move(file_data.value())); +} + bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename) { std::unique_ptr stream = diff --git a/src/core/memory_card_image.h b/src/core/memory_card_image.h index 9ade380cd..d047b26a2 100644 --- a/src/core/memory_card_image.h +++ b/src/core/memory_card_image.h @@ -43,12 +43,14 @@ struct FileInfo std::vector icon_frames; }; +bool IsValid(const DataArray& data); u32 GetFreeBlockCount(const DataArray& data); std::vector EnumerateFiles(const DataArray& data); bool ReadFile(const DataArray& data, const FileInfo& fi, std::vector* buffer); bool WriteFile(DataArray* data, const std::string_view& filename, const std::vector& buffer); bool DeleteFile(DataArray* data, const FileInfo& fi); bool ImportCard(DataArray* data, const char* filename); +bool ImportCard(DataArray* data, const char* filename, std::vector file_data); bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename); bool ImportSave(DataArray* data, const char* filename); } // namespace MemoryCardImage