MemoryCardImage: Implement save file importing and exporting

This commit is contained in:
Albert Liu
2021-03-04 20:50:20 -08:00
parent e939507f42
commit 50a78489f2
4 changed files with 146 additions and 5 deletions

View File

@ -466,4 +466,95 @@ bool ImportCard(DataArray* data, const char* filename)
}
}
} // namespace MemoryCardImage
bool ExportSave(DataArray* data, const FileInfo& fi, const char* filename)
{
std::unique_ptr<ByteStream> stream =
FileSystem::OpenFile(filename, BYTESTREAM_OPEN_CREATE | BYTESTREAM_OPEN_TRUNCATE | BYTESTREAM_OPEN_WRITE |
BYTESTREAM_OPEN_ATOMIC_UPDATE | BYTESTREAM_OPEN_STREAMED);
if (!stream)
{
Log_ErrorPrintf("Failed to open '%s' for writing.", filename);
return false;
}
DirectoryFrame* df_ptr = GetFramePtr<DirectoryFrame>(data, 0, fi.first_block);
std::vector<u8> header = std::vector<u8>(static_cast<size_t>(FRAME_SIZE));
std::memcpy(header.data(), df_ptr, sizeof(*df_ptr));
std::vector<u8> blocks;
if (!ReadFile(*data, fi, &blocks))
{
Log_ErrorPrintf("Failed to read save blocks from memory card data");
return false;
}
if (!stream->Write(header.data(), static_cast<u32>(header.size())) ||
!stream->Write(blocks.data(), static_cast<u32>(blocks.size())) || !stream->Commit())
{
Log_ErrorPrintf("Failed to write exported save to '%s'", filename);
stream->Discard();
return false;
}
return true;
}
bool ImportSave(DataArray* data, const char* filename)
{
FILESYSTEM_STAT_DATA sd;
if (!FileSystem::StatFile(filename, &sd))
{
Log_ErrorPrintf("Failed to stat file '%s'", filename);
return false;
}
// Make sure the size of the actual file is valid
if (sd.Size <= FRAME_SIZE || (sd.Size - FRAME_SIZE) % BLOCK_SIZE != 0u || (sd.Size - FRAME_SIZE) / BLOCK_SIZE > 15u)
{
Log_ErrorPrintf("Invalid size for save file '%s'", filename);
return false;
}
std::unique_ptr<ByteStream> stream = FileSystem::OpenFile(filename, BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
if (!stream)
{
Log_ErrorPrintf("Failed to open '%s' for reading", filename);
return false;
}
DirectoryFrame df;
if (stream->Read(&df, FRAME_SIZE) != FRAME_SIZE)
{
Log_ErrorPrintf("Failed to read directory frame from '%s'", filename);
return false;
}
// Make sure the size reported by the directory frame is valid
if (df.file_size < BLOCK_SIZE || df.file_size % BLOCK_SIZE != 0 || df.file_size / BLOCK_SIZE > 15u)
{
Log_ErrorPrintf("Invalid size (%u bytes) reported by directory frame", df.file_size);
return false;
}
// Make sure there isn't already a save with the same name
std::vector<FileInfo> fileinfos = EnumerateFiles(*data);
for (const FileInfo& fi : fileinfos)
{
if (fi.filename.compare(0, sizeof(df.filename), df.filename) == 0)
{
Log_ErrorPrintf("Save file with the same name already exists in memory card");
return false;
}
}
std::vector<u8> blocks = std::vector<u8>(static_cast<size_t>(df.file_size));
if (stream->Read(blocks.data(), df.file_size) != df.file_size)
{
Log_ErrorPrintf("Failed to read block bytes from '%s'", filename);
return false;
}
return WriteFile(data, df.filename, blocks);
}
} // namespace MemoryCardImage