[FS] Implement SaveData sort of.

This commit is contained in:
wheremyfoodat 2023-03-12 03:15:44 +02:00
parent d24a61d5a7
commit e69e95af69
7 changed files with 75 additions and 31 deletions

View file

@ -2,6 +2,8 @@
#include <algorithm>
#include <memory>
namespace fs = std::filesystem;
CreateFileResult SaveDataArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SaveData] CreateFile not yet supported");
return CreateFileResult::Success;
@ -18,17 +20,37 @@ FileDescriptor SaveDataArchive::openFile(const FSPath& path, const FilePerms& pe
return FileError;
}
if (path.type == PathType::UTF16 /* && path.utf16_string == u"/game_header" */) {
printf("Opened file from the SaveData archive \n");
return NoFile;
if (path.type == PathType::UTF16) {
if (!isPathSafe<PathType::UTF16>(path))
Helpers::panic("Unsafe path in SaveData::OpenFile");
if (perms.raw == 0 || (perms.create() && !perms.write()))
Helpers::panic("[SaveData] Unsupported flags for OpenFile");
fs::path p = IOFile::getAppData() / "SaveData";
p += fs::path(path.utf16_string).make_preferred();
const char* permString = perms.write() ? "r+b" : "rb";
if (fs::exists(p)) { // Return file descriptor if the file exists
IOFile file(p.string().c_str(), permString);
return file.isOpen() ? file.getHandle() : FileError;
} else {
// If the file is not found, create it if the create flag is on
if (perms.create()) {
IOFile file(p.string().c_str(), "wb"); // Create file
file.close(); // Close it
file.open(p.string().c_str(), permString); // Reopen with proper perms
return file.isOpen() ? file.getHandle() : FileError;
} else {
return FileError;
}
}
}
if (path.type != PathType::Binary) {
printf("Unimplemented SaveData path type: %d\n", path.type);
return FileError;
}
return NoFile;
Helpers::panic("SaveDataArchive::OpenFile: Failed");
return FileError;
}
ArchiveBase* SaveDataArchive::openArchive(const FSPath& path) {
@ -54,16 +76,6 @@ std::optional<u32> SaveDataArchive::readFile(FileSession* file, u64 offset, u32
return std::nullopt;
}
const u64 endOffset = std::min<u64>(saveSize, offset + size); // Don't go past the end of the save file
const u32 bytesRead = endOffset - offset;
if (bytesRead != 0x22) Helpers::panic("Might want to actually implement SaveData");
static constexpr std::array<u8, 0x22> saveDataStub = { 0x00, 0x23, 0x3C, 0x77, 0x67, 0x28, 0x30, 0x33, 0x58, 0x61, 0x39, 0x61, 0x48, 0x59, 0x36, 0x55, 0x43, 0x76, 0x58, 0x61, 0x6F, 0x65, 0x48, 0x6D, 0x2B, 0x5E, 0x6F, 0x62, 0x3E, 0x6F, 0x34, 0x00, 0x77, 0x09};
for (u32 i = 0; i < bytesRead; i++) {
mem.write8(dataPointer + i, saveDataStub[i]);
}
return bytesRead;
Helpers::panic("Unimplemented SaveData::ReadFile");
return 0;
}