[FS] Cleanup, stub SDMC, start implementing SaveData

This commit is contained in:
wheremyfoodat 2022-10-12 17:29:36 +03:00
parent 8cf55162d0
commit b6a1da21a9
13 changed files with 135 additions and 15 deletions

View file

@ -15,7 +15,7 @@ bool SelfNCCHArchive::openFile(const FSPath& path) {
return true;
}
ArchiveBase* SelfNCCHArchive::openArchive(FSPath& path) {
ArchiveBase* SelfNCCHArchive::openArchive(const FSPath& path) {
if (path.type != PathType::Empty) {
printf("Invalid path type for SelfNCCH archive: %d\n", path.type);
return nullptr;

View file

@ -0,0 +1,17 @@
#include "fs/archive_save_data.hpp"
#include <memory>
bool SaveDataArchive::openFile(const FSPath& path) {
Helpers::panic("SaveDataArchive::OpenFile");
return false;
}
ArchiveBase* SaveDataArchive::openArchive(const FSPath& path) {
Helpers::panic("SaveDataArchive::OpenArchive");
return nullptr;
}
std::optional<u32> SaveDataArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) {
Helpers::panic("SaveDataArchive::ReadFile");
return std::nullopt;
}

View file

@ -0,0 +1,17 @@
#include "fs/archive_sdmc.hpp"
#include <memory>
bool SDMCArchive::openFile(const FSPath& path) {
printf("SDMCArchive::OpenFile: Failed");
return false;
}
ArchiveBase* SDMCArchive::openArchive(const FSPath& path) {
printf("SDMCArchive::OpenArchive: Failed");
return nullptr;
}
std::optional<u32> SDMCArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) {
printf("SDMCArchive::ReadFile: Failed");
return std::nullopt;
}

View file

@ -150,6 +150,7 @@ void Kernel::sleepThread(s64 ns) {
if (ns < 0) {
Helpers::panic("Sleeping a thread for a negative amount of ns");
} else if (ns == 0) { // Used when we want to force a thread switch
// TODO: Does sleep(0) always switch to another thread, or can it return to the same one if it's the highest prio one?
threads[currentThreadIndex].status = ThreadStatus::Ready;
switchToNextThread();
} else { // If we're sleeping for > 0 ns

View file

@ -6,6 +6,8 @@
using namespace ELFIO;
std::optional<u32> Memory::loadELF(std::ifstream& file) {
loadedCXI = std::nullopt; // ELF files don't have a CXI, so set this to null
elfio reader;
if (!file.good() || !reader.load(file)) {
printf("Woops failed to load ELF\n");

View file

@ -21,13 +21,15 @@ void FSService::reset() {}
ArchiveBase* FSService::getArchiveFromID(u32 id) {
switch (id) {
case ArchiveID::SelfNCCH: return &selfNcch;
case ArchiveID::SaveData: return &saveData;
case ArchiveID::SDMC: return &sdmc;
default:
Helpers::panic("Unknown archive. ID: %d\n", id);
return nullptr;
}
}
std::optional<Handle> FSService::openFile(ArchiveBase* archive, const FSPath& path) {
std::optional<Handle> FSService::openFileHandle(ArchiveBase* archive, const FSPath& path) {
bool opened = archive->openFile(path);
if (opened) {
auto handle = kernel.makeObject(KernelObjectType::File);
@ -40,6 +42,27 @@ std::optional<Handle> FSService::openFile(ArchiveBase* archive, const FSPath& pa
}
}
std::optional<Handle> FSService::openArchiveHandle(u32 archiveID, const FSPath& path) {
ArchiveBase* archive = getArchiveFromID(archiveID);
if (archive == nullptr) [[unlikely]] {
Helpers::panic("OpenArchive: Tried to open unknown archive %d.", archiveID);
return std::nullopt;
}
bool opened = archive->openArchive(path);
if (opened) {
auto handle = kernel.makeObject(KernelObjectType::Archive);
auto& archiveObject = kernel.getObjects()[handle];
archiveObject.data = new ArchiveSession(archive, path);
return handle;
}
else {
return std::nullopt;
}
}
void FSService::handleSyncRequest(u32 messagePointer) {
const u32 command = mem.read32(messagePointer);
switch (command) {
@ -56,8 +79,22 @@ void FSService::initialize(u32 messagePointer) {
}
void FSService::openArchive(u32 messagePointer) {
log("FS::OpenArchive (failure)\n");
mem.write32(messagePointer + 4, Result::Failure);
const u32 archiveID = mem.read32(messagePointer + 4);
const u32 archivePathType = mem.read32(messagePointer + 8);
const u32 archivePathSize = mem.read32(messagePointer + 12);
const u32 archivePathPointer = mem.read32(messagePointer + 20);
FSPath archivePath{ .type = archivePathType, .size = archivePathSize, .pointer = archivePathPointer };
log("FS::OpenArchive (archive ID = %d, archive path type = %d)\n", archiveID, archivePathType);
std::optional<Handle> handle = openArchiveHandle(archiveID, archivePath);
if (handle.has_value()) {
mem.write32(messagePointer + 4, Result::Success);
mem.write64(messagePointer + 8, handle.value());
} else {
log("FS::OpenArchive: Failed to open archive with id = %d\n", archiveID);
mem.write32(messagePointer + 4, Result::Failure);
}
}
void FSService::openFileDirectly(u32 messagePointer) {
@ -86,7 +123,7 @@ void FSService::openFileDirectly(u32 messagePointer) {
Helpers::panic("OpenFileDirectly: Failed to open archive with given path");
}
std::optional<Handle> handle = openFile(archive, filePath);
std::optional<Handle> handle = openFileHandle(archive, filePath);
if (!handle.has_value()) {
Helpers::panic("OpenFileDirectly: Failed to open file with given path");
} else {