mirror of
https://github.com/wheremyfoodat/Panda3DS.git
synced 2025-04-21 21:05:50 +12:00
Merge branch 'master' into sd-card
This commit is contained in:
commit
42bfcaf980
68 changed files with 3543 additions and 310 deletions
|
@ -131,20 +131,20 @@ std::optional<u32> NCCHArchive::readFile(FileSession* file, u64 offset, u32 size
|
|||
}
|
||||
|
||||
auto cxi = mem.getCXI();
|
||||
IOFile& ioFile = mem.CXIFile;
|
||||
auto hb3dsx = mem.get3DSX();
|
||||
|
||||
NCCH::FSInfo fsInfo;
|
||||
// If isCXI is true then we've loaded a CXI/3DS file, else it's 3DSX
|
||||
bool isCXI = cxi != nullptr;
|
||||
|
||||
// Seek to file offset depending on if we're reading from RomFS, ExeFS, etc
|
||||
switch (type) {
|
||||
case PathType::RomFS: {
|
||||
const u64 romFSSize = cxi->romFS.size;
|
||||
const u64 romFSOffset = cxi->romFS.offset;
|
||||
const u64 romFSSize = isCXI ? cxi->romFS.size : hb3dsx->romFSSize;
|
||||
const u64 romFSOffset = isCXI ? cxi->romFS.offset : hb3dsx->romFSOffset;
|
||||
if ((offset >> 32) || (offset >= romFSSize) || (offset + size >= romFSSize)) {
|
||||
Helpers::panic("Tried to read from NCCH with too big of an offset");
|
||||
}
|
||||
|
||||
fsInfo = cxi->romFS;
|
||||
offset += 0x1000;
|
||||
break;
|
||||
}
|
||||
|
@ -154,7 +154,8 @@ std::optional<u32> NCCHArchive::readFile(FileSession* file, u64 offset, u32 size
|
|||
}
|
||||
|
||||
std::unique_ptr<u8[]> data(new u8[size]);
|
||||
auto [success, bytesRead] = cxi->readFromFile(ioFile, fsInfo, &data[0], offset, size);
|
||||
auto [success, bytesRead] =
|
||||
isCXI ? cxi->readFromFile(mem.CXIFile, cxi->romFS, &data[0], offset, size) : hb3dsx->readRomFSBytes(&data[0], offset, size);
|
||||
|
||||
if (!success) {
|
||||
Helpers::panic("Failed to read from NCCH archive");
|
||||
|
|
|
@ -69,57 +69,79 @@ std::optional<u32> SelfNCCHArchive::readFile(FileSession* file, u64 offset, u32
|
|||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto cxi = mem.getCXI();
|
||||
IOFile& ioFile = mem.CXIFile;
|
||||
bool success = false;
|
||||
std::size_t bytesRead = 0;
|
||||
std::vector<u8> data;
|
||||
|
||||
NCCH::FSInfo fsInfo;
|
||||
if (auto cxi = mem.getCXI(); cxi != nullptr) {
|
||||
IOFile& ioFile = mem.CXIFile;
|
||||
|
||||
// Seek to file offset depending on if we're reading from RomFS, ExeFS, etc
|
||||
switch (type) {
|
||||
case PathType::RomFS: {
|
||||
const u64 romFSSize = cxi->romFS.size;
|
||||
const u64 romFSOffset = cxi->romFS.offset;
|
||||
if ((offset >> 32) || (offset >= romFSSize) || (offset + size >= romFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
NCCH::FSInfo fsInfo;
|
||||
|
||||
// Seek to file offset depending on if we're reading from RomFS, ExeFS, etc
|
||||
switch (type) {
|
||||
case PathType::RomFS: {
|
||||
const u64 romFSSize = cxi->romFS.size;
|
||||
const u64 romFSOffset = cxi->romFS.offset;
|
||||
if ((offset >> 32) || (offset >= romFSSize) || (offset + size >= romFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
}
|
||||
|
||||
fsInfo = cxi->romFS;
|
||||
offset += 0x1000;
|
||||
break;
|
||||
}
|
||||
|
||||
fsInfo = cxi->romFS;
|
||||
offset += 0x1000;
|
||||
break;
|
||||
}
|
||||
case PathType::ExeFS: {
|
||||
const u64 exeFSSize = cxi->exeFS.size;
|
||||
const u64 exeFSOffset = cxi->exeFS.offset;
|
||||
if ((offset >> 32) || (offset >= exeFSSize) || (offset + size >= exeFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
}
|
||||
|
||||
case PathType::ExeFS: {
|
||||
const u64 exeFSSize = cxi->exeFS.size;
|
||||
const u64 exeFSOffset = cxi->exeFS.offset;
|
||||
if ((offset >> 32) || (offset >= exeFSSize) || (offset + size >= exeFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
fsInfo = cxi->exeFS;
|
||||
break;
|
||||
}
|
||||
|
||||
fsInfo = cxi->exeFS;
|
||||
break;
|
||||
}
|
||||
// Normally, the update RomFS should overlay the cartridge RomFS when reading from this and an update is installed.
|
||||
// So to support updates, we need to perform this overlaying. For now, read from the cartridge RomFS.
|
||||
case PathType::UpdateRomFS: {
|
||||
Helpers::warn("Reading from update RomFS but updates are currently not supported! Reading from regular RomFS instead\n");
|
||||
|
||||
// Normally, the update RomFS should overlay the cartridge RomFS when reading from this and an update is installed.
|
||||
// So to support updates, we need to perform this overlaying. For now, read from the cartridge RomFS.
|
||||
case PathType::UpdateRomFS: {
|
||||
Helpers::warn("Reading from update RomFS but updates are currently not supported! Reading from regular RomFS instead\n");
|
||||
const u64 romFSSize = cxi->romFS.size;
|
||||
const u64 romFSOffset = cxi->romFS.offset;
|
||||
if ((offset >> 32) || (offset >= romFSSize) || (offset + size >= romFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
}
|
||||
|
||||
const u64 romFSSize = cxi->romFS.size;
|
||||
const u64 romFSOffset = cxi->romFS.offset;
|
||||
if ((offset >> 32) || (offset >= romFSSize) || (offset + size >= romFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
fsInfo = cxi->romFS;
|
||||
offset += 0x1000;
|
||||
break;
|
||||
}
|
||||
|
||||
fsInfo = cxi->romFS;
|
||||
offset += 0x1000;
|
||||
break;
|
||||
default: Helpers::panic("Unimplemented file path type for SelfNCCH archive");
|
||||
}
|
||||
|
||||
default: Helpers::panic("Unimplemented file path type for SelfNCCH archive");
|
||||
data.resize(size);
|
||||
std::tie(success, bytesRead) = cxi->readFromFile(ioFile, fsInfo, &data[0], offset, size);
|
||||
}
|
||||
|
||||
std::unique_ptr<u8[]> data(new u8[size]);
|
||||
auto [success, bytesRead] = cxi->readFromFile(ioFile, fsInfo, &data[0], offset, size);
|
||||
else if (auto hb3dsx = mem.get3DSX(); hb3dsx != nullptr) {
|
||||
switch (type) {
|
||||
case PathType::RomFS: {
|
||||
const u64 romFSSize = hb3dsx->romFSSize;
|
||||
if ((offset >> 32) || (offset >= romFSSize) || (offset + size >= romFSSize)) {
|
||||
Helpers::panic("Tried to read from SelfNCCH with too big of an offset");
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default: Helpers::panic("Unimplemented file path type for 3DSX SelfNCCH archive");
|
||||
}
|
||||
|
||||
data.resize(size);
|
||||
std::tie(success, bytesRead) = hb3dsx->readRomFSBytes(&data[0], offset, size);
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
Helpers::panic("Failed to read from SelfNCCH archive");
|
||||
|
|
198
src/core/fs/archive_user_save_data.cpp
Normal file
198
src/core/fs/archive_user_save_data.cpp
Normal file
|
@ -0,0 +1,198 @@
|
|||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
#include "fs/archive_user_save_data.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
HorizonResult UserSaveDataArchive::createFile(const FSPath& path, u64 size) {
|
||||
if (path.type == PathType::UTF16) {
|
||||
if (!isPathSafe<PathType::UTF16>(path)) Helpers::panic("Unsafe path in UserSaveData::CreateFile");
|
||||
|
||||
fs::path p = IOFile::getAppData() / "SaveData";
|
||||
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("UserSaveDataArchive::OpenFile: Failed");
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
HorizonResult UserSaveDataArchive::createDirectory(const FSPath& path) {
|
||||
if (path.type == PathType::UTF16) {
|
||||
if (!isPathSafe<PathType::UTF16>(path)) Helpers::panic("Unsafe path in UserSaveData::OpenFile");
|
||||
|
||||
fs::path p = IOFile::getAppData() / "SaveData";
|
||||
p += fs::path(path.utf16_string).make_preferred();
|
||||
|
||||
if (fs::is_directory(p)) return Result::FS::AlreadyExists;
|
||||
if (fs::is_regular_file(p)) {
|
||||
Helpers::panic("File path passed to UserSaveData::CreateDirectory");
|
||||
}
|
||||
|
||||
bool success = fs::create_directory(p);
|
||||
return success ? Result::Success : Result::FS::UnexpectedFileOrDir;
|
||||
} else {
|
||||
Helpers::panic("Unimplemented UserSaveData::CreateDirectory");
|
||||
}
|
||||
}
|
||||
|
||||
HorizonResult UserSaveDataArchive::deleteFile(const FSPath& path) {
|
||||
if (path.type == PathType::UTF16) {
|
||||
if (!isPathSafe<PathType::UTF16>(path)) Helpers::panic("Unsafe path in UserSaveData::DeleteFile");
|
||||
|
||||
fs::path p = IOFile::getAppData() / "SaveData";
|
||||
p += fs::path(path.utf16_string).make_preferred();
|
||||
|
||||
if (fs::is_directory(p)) {
|
||||
Helpers::panic("UserSaveData::DeleteFile: Tried to delete directory");
|
||||
}
|
||||
|
||||
if (!fs::is_regular_file(p)) {
|
||||
return Result::FS::FileNotFoundAlt;
|
||||
}
|
||||
|
||||
std::error_code ec;
|
||||
bool success = fs::remove(p, ec);
|
||||
|
||||
// It might still be possible for fs::remove to fail, if there's eg an open handle to a file being deleted
|
||||
// In this case, print a warning, but still return success for now
|
||||
if (!success) {
|
||||
Helpers::warn("UserSaveData::DeleteFile: fs::remove failed\n");
|
||||
}
|
||||
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
Helpers::panic("UserSaveDataArchive::DeleteFile: Unknown path type");
|
||||
return Result::Success;
|
||||
}
|
||||
|
||||
FileDescriptor UserSaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) {
|
||||
if (path.type == PathType::UTF16) {
|
||||
if (!isPathSafe<PathType::UTF16>(path)) Helpers::panic("Unsafe path in UserSaveData::OpenFile");
|
||||
|
||||
if (perms.raw == 0 || (perms.create() && !perms.write())) Helpers::panic("[UserSaveData] 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Helpers::panic("UserSaveDataArchive::OpenFile: Failed");
|
||||
return FileError;
|
||||
}
|
||||
|
||||
Rust::Result<DirectorySession, HorizonResult> UserSaveDataArchive::openDirectory(const FSPath& path) {
|
||||
if (path.type == PathType::UTF16) {
|
||||
if (!isPathSafe<PathType::UTF16>(path)) Helpers::panic("Unsafe path in UserSaveData::OpenDirectory");
|
||||
|
||||
fs::path p = IOFile::getAppData() / "SaveData";
|
||||
p += fs::path(path.utf16_string).make_preferred();
|
||||
|
||||
if (fs::is_regular_file(p)) {
|
||||
printf("SaveData: OpenDirectory used with a file path");
|
||||
return Err(Result::FS::UnexpectedFileOrDir);
|
||||
}
|
||||
|
||||
if (fs::is_directory(p)) {
|
||||
return Ok(DirectorySession(this, p));
|
||||
} else {
|
||||
return Err(Result::FS::FileNotFoundAlt);
|
||||
}
|
||||
}
|
||||
|
||||
Helpers::panic("UserSaveDataArchive::OpenDirectory: Unimplemented path type");
|
||||
return Err(Result::Success);
|
||||
}
|
||||
|
||||
Rust::Result<ArchiveBase::FormatInfo, HorizonResult> UserSaveDataArchive::getFormatInfo(const FSPath& path) {
|
||||
const fs::path formatInfoPath = getFormatInfoPath();
|
||||
IOFile file(formatInfoPath, "rb");
|
||||
|
||||
// If the file failed to open somehow, we return that the archive is not formatted
|
||||
if (!file.isOpen()) {
|
||||
return Err(Result::FS::NotFormatted);
|
||||
}
|
||||
|
||||
FormatInfo ret;
|
||||
auto [success, bytesRead] = file.readBytes(&ret, sizeof(FormatInfo));
|
||||
file.close();
|
||||
|
||||
if (!success || bytesRead != sizeof(FormatInfo)) {
|
||||
Helpers::warn("UserSaveData::GetFormatInfo: Format file exists but was not properly read into the FormatInfo struct");
|
||||
return Err(Result::FS::NotFormatted);
|
||||
}
|
||||
|
||||
return Ok(ret);
|
||||
}
|
||||
|
||||
void UserSaveDataArchive::format(const FSPath& path, const ArchiveBase::FormatInfo& info) {
|
||||
const fs::path saveDataPath = IOFile::getAppData() / "SaveData";
|
||||
const fs::path formatInfoPath = getFormatInfoPath();
|
||||
|
||||
// Delete all contents by deleting the directory then recreating it
|
||||
fs::remove_all(saveDataPath);
|
||||
fs::create_directories(saveDataPath);
|
||||
|
||||
// Write format info on disk
|
||||
IOFile file(formatInfoPath, "wb");
|
||||
file.writeBytes(&info, sizeof(info));
|
||||
file.flush();
|
||||
file.close();
|
||||
}
|
||||
|
||||
Rust::Result<ArchiveBase*, HorizonResult> UserSaveDataArchive::openArchive(const FSPath& path) {
|
||||
if (path.type != PathType::Binary) {
|
||||
Helpers::panic("Unimplemented path type for UserSaveData archive: %d\n", path.type);
|
||||
return Err(Result::FS::NotFoundInvalid);
|
||||
}
|
||||
|
||||
const fs::path formatInfoPath = getFormatInfoPath();
|
||||
// Format info not found so the archive is not formatted
|
||||
if (!fs::is_regular_file(formatInfoPath)) {
|
||||
return Err(Result::FS::NotFormatted);
|
||||
}
|
||||
|
||||
return Ok((ArchiveBase*)this);
|
||||
}
|
||||
|
||||
std::optional<u32> UserSaveDataArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) {
|
||||
Helpers::panic("Unimplemented UserSaveData::ReadFile");
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue