MemoryCardImage: Support passing import buffers in
This commit is contained in:
parent
3aaf1d942b
commit
4dec0dee2f
|
@ -129,6 +129,13 @@ bool SaveToFile(const DataArray& data, const char* filename)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool IsValid(const DataArray& data)
|
||||||
|
{
|
||||||
|
// TODO: Check checksum?
|
||||||
|
const u8* fptr = GetFramePtr<u8>(data, 0, 0);
|
||||||
|
return fptr[0] == 'M' && fptr[1] == 'C';
|
||||||
|
}
|
||||||
|
|
||||||
void Format(DataArray* data)
|
void Format(DataArray* data)
|
||||||
{
|
{
|
||||||
// fill everything with FF
|
// fill everything with FF
|
||||||
|
@ -385,23 +392,19 @@ bool DeleteFile(DataArray* data, const FileInfo& fi)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ImportCardMCD(DataArray* data, const char* filename)
|
static bool ImportCardMCD(DataArray* data, const char* filename, std::vector<u8> file_data)
|
||||||
{
|
{
|
||||||
std::optional<std::vector<u8>> file_data = FileSystem::ReadBinaryFile(filename);
|
if (file_data.size() != DATA_SIZE)
|
||||||
if (!file_data.has_value())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (file_data->size() != DATA_SIZE)
|
|
||||||
{
|
{
|
||||||
Log_ErrorPrintf("Failed to import memory card from '%s': file is incorrect size.", filename);
|
Log_ErrorPrintf("Failed to import memory card from '%s': file is incorrect size.", filename);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::memcpy(data->data(), file_data->data(), DATA_SIZE);
|
std::memcpy(data->data(), file_data.data(), DATA_SIZE);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool ImportCardGME(DataArray* data, const char* filename)
|
static bool ImportCardGME(DataArray* data, const char* filename, std::vector<u8> file_data)
|
||||||
{
|
{
|
||||||
#pragma pack(push, 1)
|
#pragma pack(push, 1)
|
||||||
struct GMEHeader
|
struct GMEHeader
|
||||||
|
@ -417,11 +420,7 @@ static bool ImportCardGME(DataArray* data, const char* filename)
|
||||||
static_assert(sizeof(GMEHeader) == 0xF40);
|
static_assert(sizeof(GMEHeader) == 0xF40);
|
||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
std::optional<std::vector<u8>> file_data = FileSystem::ReadBinaryFile(filename);
|
if (file_data.size() < (sizeof(GMEHeader) + BLOCK_SIZE))
|
||||||
if (!file_data.has_value())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if (file_data->size() < (sizeof(GMEHeader) + BLOCK_SIZE))
|
|
||||||
{
|
{
|
||||||
Log_ErrorPrintf("Failed to import GME memory card from '%s': file is incorrect size.", filename);
|
Log_ErrorPrintf("Failed to import GME memory card from '%s': file is incorrect size.", filename);
|
||||||
return false;
|
return false;
|
||||||
|
@ -429,19 +428,19 @@ static bool ImportCardGME(DataArray* data, const char* filename)
|
||||||
|
|
||||||
// if it's too small, pad it
|
// if it's too small, pad it
|
||||||
const u32 expected_size = sizeof(GMEHeader) + DATA_SIZE;
|
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,
|
Log_WarningPrintf("GME memory card '%s' is too small (got %zu expected %u), padding with zeroes", filename,
|
||||||
file_data->size(), expected_size);
|
file_data.size(), expected_size);
|
||||||
file_data->resize(expected_size);
|
file_data.resize(expected_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
// we don't actually care about the header, just skip over it
|
// 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ImportCard(DataArray* data, const char* filename)
|
bool ImportCard(DataArray* data, const char* filename, std::vector<u8> file_data)
|
||||||
{
|
{
|
||||||
const char* extension = std::strrchr(filename, '.');
|
const char* extension = std::strrchr(filename, '.');
|
||||||
if (!extension)
|
if (!extension)
|
||||||
|
@ -453,11 +452,11 @@ bool ImportCard(DataArray* data, const char* filename)
|
||||||
if (StringUtil::Strcasecmp(extension, ".mcd") == 0 || StringUtil::Strcasecmp(extension, ".mcr") == 0 ||
|
if (StringUtil::Strcasecmp(extension, ".mcd") == 0 || StringUtil::Strcasecmp(extension, ".mcr") == 0 ||
|
||||||
StringUtil::Strcasecmp(extension, ".mc") == 0 || StringUtil::Strcasecmp(extension, ".srm") == 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)
|
else if (StringUtil::Strcasecmp(extension, ".gme") == 0)
|
||||||
{
|
{
|
||||||
return ImportCardGME(data, filename);
|
return ImportCardGME(data, filename, std::move(file_data));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -466,6 +465,15 @@ bool ImportCard(DataArray* data, const char* filename)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ImportCard(DataArray* data, const char* filename)
|
||||||
|
{
|
||||||
|
std::optional<std::vector<u8>> 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)
|
bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename)
|
||||||
{
|
{
|
||||||
std::unique_ptr<ByteStream> stream =
|
std::unique_ptr<ByteStream> stream =
|
||||||
|
|
|
@ -43,12 +43,14 @@ struct FileInfo
|
||||||
std::vector<IconFrame> icon_frames;
|
std::vector<IconFrame> icon_frames;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
bool IsValid(const DataArray& data);
|
||||||
u32 GetFreeBlockCount(const DataArray& data);
|
u32 GetFreeBlockCount(const DataArray& data);
|
||||||
std::vector<FileInfo> EnumerateFiles(const DataArray& data);
|
std::vector<FileInfo> EnumerateFiles(const DataArray& data);
|
||||||
bool ReadFile(const DataArray& data, const FileInfo& fi, std::vector<u8>* buffer);
|
bool ReadFile(const DataArray& data, const FileInfo& fi, std::vector<u8>* buffer);
|
||||||
bool WriteFile(DataArray* data, const std::string_view& filename, const std::vector<u8>& buffer);
|
bool WriteFile(DataArray* data, const std::string_view& filename, const std::vector<u8>& buffer);
|
||||||
bool DeleteFile(DataArray* data, const FileInfo& fi);
|
bool DeleteFile(DataArray* data, const FileInfo& fi);
|
||||||
bool ImportCard(DataArray* data, const char* filename);
|
bool ImportCard(DataArray* data, const char* filename);
|
||||||
|
bool ImportCard(DataArray* data, const char* filename, std::vector<u8> file_data);
|
||||||
bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename);
|
bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename);
|
||||||
bool ImportSave(DataArray* data, const char* filename);
|
bool ImportSave(DataArray* data, const char* filename);
|
||||||
} // namespace MemoryCardImage
|
} // namespace MemoryCardImage
|
||||||
|
|
Loading…
Reference in New Issue