From 8a852d2cab145e74e1b5d791aa6a25d82bdf4683 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Sun, 11 Jun 2023 01:05:44 -0700 Subject: [PATCH] Implement `SaveDataArchive::createFile` Based on `ExtSaveDataArchive::createFile`. Gets some games farther that try to create save-data upon boot. --- src/core/fs/archive_save_data.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index 758c1312..5e020fbb 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -5,7 +5,29 @@ namespace fs = std::filesystem; FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) { - Helpers::panic("[SaveData] CreateFile not yet supported"); + if (size == 0) + Helpers::panic("SaveData file does not support size == 0"); + + if (path.type == PathType::UTF16) { + if (!isPathSafe(path)) + Helpers::panic("Unsafe path in SaveData::CreateFile"); + + fs::path p = IOFile::getAppData() / "SaveData"; + p += fs::path(path.utf16_string).make_preferred(); + + if (fs::exists(p)) + return FSResult::AlreadyExists; + + // Create a file of size "size" by creating an empty one, seeking to size - 1 and just writing a 0 there + IOFile file(p.string().c_str(), "wb"); + if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { + return FSResult::Success; + } + + return FSResult::FileTooLarge; + } + + Helpers::panic("SaveDataArchive::OpenFile: Failed"); return FSResult::Success; }