Archives: Implemented ExtSaveData and SharedExtSaveData

They will be stored in /extsavedata/SDMC and /extsavedata/NAND respectively.
Also redirect some APT_A functions to their APT_U equivalents.
Implemented the gamecoin.dat file in SharedExtSaveData in the PTM module.
Implemented formatting the savegame.
Retake a previous savegame if it exists instead of reporting them as not formatted every time a game is loaded.
This commit is contained in:
Subv 2014-12-29 13:04:37 -05:00
parent 3d14eb2853
commit 2c89d4d5cd
17 changed files with 268 additions and 60 deletions

View file

@ -11,6 +11,7 @@
#include "common/math_util.h"
#include "core/file_sys/archive_savedata.h"
#include "core/file_sys/archive_extsavedata.h"
#include "core/file_sys/archive_backend.h"
#include "core/file_sys/archive_sdmc.h"
#include "core/file_sys/directory_backend.h"
@ -224,25 +225,20 @@ static Archive* GetArchive(ArchiveHandle handle) {
return (itr == handle_map.end()) ? nullptr : itr->second;
}
ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code) {
ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path) {
LOG_TRACE(Service_FS, "Opening archive with id code 0x%08X", id_code);
auto itr = id_code_map.find(id_code);
if (itr == id_code_map.end()) {
if (id_code == ArchiveIdCode::SaveData) {
// When a SaveData archive is created for the first time, it is not yet formatted
// and the save file/directory structure expected by the game has not yet been initialized.
// Returning the NotFormatted error code will signal the game to provision the SaveData archive
// with the files and folders that it expects.
// The FormatSaveData service call will create the SaveData archive when it is called.
return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS,
ErrorSummary::InvalidState, ErrorLevel::Status);
}
// TODO: Verify error against hardware
return ResultCode(ErrorDescription::NotFound, ErrorModule::FS,
ErrorSummary::NotFound, ErrorLevel::Permanent);
}
ResultCode res = itr->second->backend->Open(archive_path);
if (!res.IsSuccess())
return res;
// This should never even happen in the first place with 64-bit handles,
while (handle_map.count(next_handle) != 0) {
++next_handle;
@ -395,25 +391,14 @@ ResultVal<Handle> OpenDirectoryFromArchive(ArchiveHandle archive_handle, const F
}
ResultCode FormatSaveData() {
// TODO(Subv): Actually wipe the savedata folder after creating or opening it
// Do not create the archive again if it already exists
if (id_code_map.find(ArchiveIdCode::SaveData) != id_code_map.end())
return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the correct error code
// Create the SaveData archive
std::string savedata_directory = FileUtil::GetUserPath(D_SAVEDATA_IDX);
auto savedata_archive = Common::make_unique<FileSys::Archive_SaveData>(savedata_directory,
Kernel::g_program_id);
if (savedata_archive->Initialize()) {
CreateArchive(std::move(savedata_archive), ArchiveIdCode::SaveData);
return RESULT_SUCCESS;
} else {
LOG_ERROR(Service_FS, "Can't instantiate SaveData archive with path %s",
savedata_archive->GetMountPoint().c_str());
return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the proper error code
auto archive_itr = id_code_map.find(ArchiveIdCode::SaveData);
if (archive_itr == id_code_map.end()) {
return UnimplementedFunction(ErrorModule::FS); // TODO(Subv): Find the right error
}
// Use an empty path, we do not use it when formatting the savedata
return archive_itr->second->backend->Format(FileSys::Path());
}
/// Initialize archives
@ -430,6 +415,26 @@ void ArchiveInit() {
CreateArchive(std::move(sdmc_archive), ArchiveIdCode::SDMC);
else
LOG_ERROR(Service_FS, "Can't instantiate SDMC archive with path %s", sdmc_directory.c_str());
// Create the SaveData archive
std::string savedata_directory = FileUtil::GetUserPath(D_SAVEDATA_IDX);
auto savedata_archive = Common::make_unique<FileSys::Archive_SaveData>(savedata_directory);
CreateArchive(std::move(savedata_archive), ArchiveIdCode::SaveData);
std::string extsavedata_directory = FileUtil::GetUserPath(D_EXTSAVEDATA);
auto extsavedata_archive = Common::make_unique<FileSys::Archive_ExtSaveData>(extsavedata_directory);
if (extsavedata_archive->Initialize())
CreateArchive(std::move(extsavedata_archive), ArchiveIdCode::ExtSaveData);
else
LOG_ERROR(Service_FS, "Can't instantiate ExtSaveData archive with path %s", extsavedata_directory.c_str());
std::string sharedextsavedata_directory = FileUtil::GetUserPath(D_EXTSAVEDATA);
auto sharedextsavedata_archive = Common::make_unique<FileSys::Archive_ExtSaveData>(sharedextsavedata_directory);
if (sharedextsavedata_archive->Initialize())
CreateArchive(std::move(sharedextsavedata_archive), ArchiveIdCode::SharedExtSaveData);
else
LOG_ERROR(Service_FS, "Can't instantiate SharedExtSaveData archive with path %s",
sharedextsavedata_directory.c_str());
}
/// Shutdown archives

View file

@ -29,9 +29,10 @@ typedef u64 ArchiveHandle;
/**
* Opens an archive
* @param id_code IdCode of the archive to open
* @param archive_path Path to the archive, used with Binary paths
* @return Handle to the opened archive
*/
ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code);
ResultVal<ArchiveHandle> OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path);
/**
* Closes an archive

View file

@ -107,14 +107,7 @@ static void OpenFileDirectly(Service::Interface* self) {
LOG_DEBUG(Service_FS, "archive_path=%s file_path=%s, mode=%u attributes=%d",
archive_path.DebugStr().c_str(), file_path.DebugStr().c_str(), mode.hex, attributes);
if (archive_path.GetType() != FileSys::Empty) {
LOG_ERROR(Service_FS, "archive LowPath type other than empty is currently unsupported");
cmd_buff[1] = UnimplementedFunction(ErrorModule::FS).raw;
cmd_buff[3] = 0;
return;
}
ResultVal<ArchiveHandle> archive_handle = OpenArchive(archive_id);
ResultVal<ArchiveHandle> archive_handle = OpenArchive(archive_id, archive_path);
if (archive_handle.Failed()) {
LOG_ERROR(Service_FS, "failed to get a handle for archive");
cmd_buff[1] = archive_handle.Code().raw;
@ -376,13 +369,7 @@ static void OpenArchive(Service::Interface* self) {
LOG_DEBUG(Service_FS, "archive_path=%s", archive_path.DebugStr().c_str());
if (archive_path.GetType() != FileSys::Empty) {
LOG_ERROR(Service_FS, "archive LowPath type other than empty is currently unsupported");
cmd_buff[1] = UnimplementedFunction(ErrorModule::FS).raw;
return;
}
ResultVal<ArchiveHandle> handle = OpenArchive(archive_id);
ResultVal<ArchiveHandle> handle = OpenArchive(archive_id, archive_path);
cmd_buff[1] = handle.Code().raw;
if (handle.Succeeded()) {
cmd_buff[2] = *handle & 0xFFFFFFFF;