-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #273 from wheremyfoodat/SystemSaveData
Add some SystemSaveData and FRD things
- Loading branch information
Showing
11 changed files
with
155 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
#include "archive_base.hpp" | ||
|
||
class SystemSaveDataArchive : public ArchiveBase { | ||
public: | ||
SystemSaveDataArchive(Memory& mem) : ArchiveBase(mem) {} | ||
|
||
u64 getFreeBytes() override { | ||
Helpers::panic("Unimplemented GetFreeBytes for SystemSaveData archive"); | ||
return 32_MB; | ||
} | ||
|
||
std::string name() override { return "SystemSaveData"; } | ||
|
||
//HorizonResult createDirectory(const FSPath& path) override; | ||
HorizonResult createFile(const FSPath& path, u64 size) override; | ||
|
||
HorizonResult deleteFile(const FSPath& path) override { | ||
Helpers::panic("Unimplemented DeleteFile for SystemSaveData archive"); | ||
return Result::Success; | ||
}; | ||
|
||
Rust::Result<ArchiveBase*, HorizonResult> openArchive(const FSPath& path) override; | ||
//Rust::Result<DirectorySession, HorizonResult> openDirectory(const FSPath& path) override; | ||
|
||
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override; | ||
|
||
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override { | ||
Helpers::panic("Unimplemented ReadFile for SystemSaveData archive"); | ||
return {}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <algorithm> | ||
#include "fs/archive_system_save_data.hpp" | ||
|
||
namespace fs = std::filesystem; | ||
|
||
Rust::Result<ArchiveBase*, HorizonResult> SystemSaveDataArchive::openArchive(const FSPath& path) { | ||
if (path.type != PathType::Binary) { | ||
Helpers::panic("Unimplemented path type for SystemSaveData::OpenArchive"); | ||
} | ||
|
||
return Ok((ArchiveBase*)this); | ||
} | ||
|
||
FileDescriptor SystemSaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) { | ||
// TODO: Validate this. Temporarily copied from SaveData archive | ||
|
||
if (path.type == PathType::UTF16) { | ||
if (!isPathSafe<PathType::UTF16>(path)) { | ||
Helpers::panic("Unsafe path in SystemSaveData::OpenFile"); | ||
} | ||
|
||
if (perms.raw == 0 || (perms.create() && !perms.write())) { | ||
Helpers::panic("[SystemSaveData] Unsupported flags for OpenFile"); | ||
} | ||
|
||
fs::path p = IOFile::getAppData() / ".." / "SharedFiles" / "SystemSaveData"; | ||
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; | ||
} | ||
} | ||
} | ||
|
||
Helpers::panic("SystemSaveData::OpenFile: Failed"); | ||
return FileError; | ||
} | ||
|
||
HorizonResult SystemSaveDataArchive::createFile(const FSPath& path, u64 size) { | ||
if (path.type == PathType::UTF16) { | ||
if (!isPathSafe<PathType::UTF16>(path)) { | ||
Helpers::panic("Unsafe path in SystemSaveData::CreateFile"); | ||
} | ||
|
||
fs::path p = IOFile::getAppData() / ".." / "SharedFiles" / "SystemSaveData"; | ||
p += fs::path(path.utf16_string).make_preferred(); | ||
|
||
if (fs::exists(p)) { | ||
return Result::FS::AlreadyExists; | ||
} | ||
|
||
IOFile file(p.string().c_str(), "wb"); | ||
|
||
// If the size is 0, leave the file empty and return success | ||
if (size == 0) { | ||
file.close(); | ||
return Result::Success; | ||
} | ||
|
||
// If it is not empty, seek to size - 1 and write a 0 to create a file of size "size" | ||
else if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { | ||
file.close(); | ||
return Result::Success; | ||
} | ||
|
||
file.close(); | ||
return Result::FS::FileTooLarge; | ||
} | ||
|
||
Helpers::panic("SystemSaveData::CreateFile: Failed"); | ||
return Result::Success; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters