From 2c89d4d5cd4e308b04cebb1c9bca48e12f0945da Mon Sep 17 00:00:00 2001 From: Subv Date: Mon, 29 Dec 2014 13:04:37 -0500 Subject: 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. --- src/core/file_sys/archive_backend.h | 17 +++++++++ src/core/file_sys/archive_extsavedata.cpp | 59 +++++++++++++++++++++++++++++++ src/core/file_sys/archive_extsavedata.h | 45 +++++++++++++++++++++++ src/core/file_sys/archive_romfs.cpp | 5 +++ src/core/file_sys/archive_romfs.h | 6 ++++ src/core/file_sys/archive_savedata.cpp | 25 +++++++++---- src/core/file_sys/archive_savedata.h | 19 ++++++---- src/core/file_sys/disk_archive.h | 7 +++- 8 files changed, 168 insertions(+), 15 deletions(-) create mode 100644 src/core/file_sys/archive_extsavedata.cpp create mode 100644 src/core/file_sys/archive_extsavedata.h (limited to 'src/core/file_sys') diff --git a/src/core/file_sys/archive_backend.h b/src/core/file_sys/archive_backend.h index e153917e..1612c35c 100644 --- a/src/core/file_sys/archive_backend.h +++ b/src/core/file_sys/archive_backend.h @@ -46,6 +46,9 @@ public: Path(const char* path) : type(Char), string(path) { } + Path(std::vector binary_data) : type(Binary), binary(std::move(binary_data)) { + } + Path(LowPathType type, u32 size, u32 pointer) : type(type) { switch (type) { case Binary: @@ -174,6 +177,20 @@ public: virtual ~ArchiveBackend() { } + /** + * Tries to open the archive of this type with the specified path + * @param path Path to the archive + * @return ResultCode of the operation + */ + virtual ResultCode Open(const Path& path) = 0; + + /** + * Deletes the archive contents and then re-creates the base folder + * @param path Path to the archive + * @return ResultCode of the operation, 0 on success + */ + virtual ResultCode Format(const Path& path) const = 0; + /** * Get a descriptive name for the archive (e.g. "RomFS", "SaveData", etc.) */ diff --git a/src/core/file_sys/archive_extsavedata.cpp b/src/core/file_sys/archive_extsavedata.cpp new file mode 100644 index 00000000..4759ef3a --- /dev/null +++ b/src/core/file_sys/archive_extsavedata.cpp @@ -0,0 +1,59 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include + +#include "common/common_types.h" +#include "common/file_util.h" + +#include "core/file_sys/archive_extsavedata.h" +#include "core/file_sys/disk_archive.h" +#include "core/settings.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +static std::string GetExtSaveDataPath(const std::string& mount_point, const Path& path) { + std::vector vec_data = path.AsBinary(); + const u32* data = reinterpret_cast(vec_data.data()); + u32 media_type = data[0]; + u32 save_low = data[1]; + u32 save_high = data[2]; + return Common::StringFromFormat("%s%s/%08X/%08X/", mount_point.c_str(), media_type == 0 ? "nand" : "sdmc", save_high, save_low); +} + +Archive_ExtSaveData::Archive_ExtSaveData(const std::string& mount_point) + : DiskArchive(mount_point), concrete_mount_point(mount_point) { + LOG_INFO(Service_FS, "Directory %s set as base for ExtSaveData.", this->mount_point.c_str()); +} + +bool Archive_ExtSaveData::Initialize() { + if (!FileUtil::CreateFullPath(mount_point)) { + LOG_ERROR(Service_FS, "Unable to create ExtSaveData base path."); + return false; + } + + return true; +} + +ResultCode Archive_ExtSaveData::Open(const Path& path) { + std::string fullpath = GetExtSaveDataPath(mount_point, path); + if (!FileUtil::Exists(fullpath)) { + // TODO(Subv): Check error code, this one is probably wrong + return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS, + ErrorSummary::InvalidState, ErrorLevel::Status); + } + concrete_mount_point = fullpath; + return RESULT_SUCCESS; +} + +ResultCode Archive_ExtSaveData::Format(const Path& path) const { + std::string fullpath = GetExtSaveDataPath(mount_point, path); + FileUtil::CreateFullPath(fullpath); + return RESULT_SUCCESS; +} + +} // namespace FileSys diff --git a/src/core/file_sys/archive_extsavedata.h b/src/core/file_sys/archive_extsavedata.h new file mode 100644 index 00000000..a3a14479 --- /dev/null +++ b/src/core/file_sys/archive_extsavedata.h @@ -0,0 +1,45 @@ +// Copyright 2014 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include "common/common_types.h" + +#include "core/file_sys/disk_archive.h" +#include "core/loader/loader.h" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// FileSys namespace + +namespace FileSys { + +/// File system interface to the ExtSaveData archive +class Archive_ExtSaveData final : public DiskArchive { +public: + Archive_ExtSaveData(const std::string& mount_point); + + /** + * Initialize the archive. + * @return true if it initialized successfully + */ + bool Initialize(); + + ResultCode Open(const Path& path) override; + ResultCode Format(const Path& path) const override; + std::string GetName() const override { return "ExtSaveData"; } + + const std::string& GetMountPoint() const override { + return concrete_mount_point; + } + +protected: + /** + * This holds the full directory path for this archive, it is only set after a successful call to Open, + * this is formed as ///. + * See GetExtSaveDataPath for the code that extracts this data from an archive path. + */ + std::string concrete_mount_point; +}; + +} // namespace FileSys diff --git a/src/core/file_sys/archive_romfs.cpp b/src/core/file_sys/archive_romfs.cpp index fdaf7317..2fc3831b 100644 --- a/src/core/file_sys/archive_romfs.cpp +++ b/src/core/file_sys/archive_romfs.cpp @@ -62,4 +62,9 @@ std::unique_ptr Archive_RomFS::OpenDirectory(const Path& path) return Common::make_unique(); } +ResultCode Archive_RomFS::Format(const Path& path) const { + LOG_WARNING(Service_FS, "Attempted to format ROMFS."); + return UnimplementedFunction(ErrorModule::FS); +} + } // namespace FileSys diff --git a/src/core/file_sys/archive_romfs.h b/src/core/file_sys/archive_romfs.h index 5e918f92..d4b1eb7f 100644 --- a/src/core/file_sys/archive_romfs.h +++ b/src/core/file_sys/archive_romfs.h @@ -83,6 +83,12 @@ public: */ std::unique_ptr OpenDirectory(const Path& path) const override; + ResultCode Open(const Path& path) override { + return RESULT_SUCCESS; + } + + ResultCode Format(const Path& path) const override; + private: friend class File_RomFS; diff --git a/src/core/file_sys/archive_savedata.cpp b/src/core/file_sys/archive_savedata.cpp index 97853567..280d4ff5 100644 --- a/src/core/file_sys/archive_savedata.cpp +++ b/src/core/file_sys/archive_savedata.cpp @@ -16,18 +16,29 @@ namespace FileSys { -Archive_SaveData::Archive_SaveData(const std::string& mount_point, u64 program_id) - : DiskArchive(mount_point + Common::StringFromFormat("%016X", program_id) + DIR_SEP) { +Archive_SaveData::Archive_SaveData(const std::string& mount_point) + : DiskArchive(mount_point) { LOG_INFO(Service_FS, "Directory %s set as SaveData.", this->mount_point.c_str()); } -bool Archive_SaveData::Initialize() { - if (!FileUtil::CreateFullPath(mount_point)) { - LOG_ERROR(Service_FS, "Unable to create SaveData path."); - return false; +ResultCode Archive_SaveData::Open(const Path& path) { + if (concrete_mount_point.empty()) + concrete_mount_point = Common::StringFromFormat("%s%016X", mount_point.c_str(), Kernel::g_program_id) + DIR_SEP; + if (!FileUtil::Exists(concrete_mount_point)) { + // 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. + return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS, + ErrorSummary::InvalidState, ErrorLevel::Status); } + return RESULT_SUCCESS; +} - return true; +ResultCode Archive_SaveData::Format(const Path& path) const { + FileUtil::DeleteDirRecursively(concrete_mount_point); + FileUtil::CreateFullPath(concrete_mount_point); + return RESULT_SUCCESS; } } // namespace FileSys diff --git a/src/core/file_sys/archive_savedata.h b/src/core/file_sys/archive_savedata.h index 5b0ce29e..07c7f7ef 100644 --- a/src/core/file_sys/archive_savedata.h +++ b/src/core/file_sys/archive_savedata.h @@ -17,15 +17,20 @@ namespace FileSys { /// File system interface to the SaveData archive class Archive_SaveData final : public DiskArchive { public: - Archive_SaveData(const std::string& mount_point, u64 program_id); - - /** - * Initialize the archive. - * @return true if it initialized successfully - */ - bool Initialize(); + Archive_SaveData(const std::string& mount_point); std::string GetName() const override { return "SaveData"; } + + ResultCode Open(const Path& path) override; + + ResultCode Format(const Path& path) const override; + + const std::string& GetMountPoint() const override { + return concrete_mount_point; + } + +protected: + std::string concrete_mount_point; }; } // namespace FileSys diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h index 018ebd2e..f18d96f5 100644 --- a/src/core/file_sys/disk_archive.h +++ b/src/core/file_sys/disk_archive.h @@ -25,6 +25,7 @@ public: DiskArchive(const std::string& mount_point_) : mount_point(mount_point_) {} virtual std::string GetName() const = 0; + virtual ResultCode Format(const Path& path) const { return RESULT_SUCCESS; } std::unique_ptr OpenFile(const Path& path, const Mode mode) const override; bool DeleteFile(const Path& path) const override; bool RenameFile(const Path& src_path, const Path& dest_path) const override; @@ -34,11 +35,15 @@ public: bool RenameDirectory(const Path& src_path, const Path& dest_path) const override; std::unique_ptr OpenDirectory(const Path& path) const override; + virtual ResultCode Open(const Path& path) override { + return RESULT_SUCCESS; + } + /** * Getter for the path used for this Archive * @return Mount point of that passthrough archive */ - const std::string& GetMountPoint() const { + virtual const std::string& GetMountPoint() const { return mount_point; } -- cgit v1.2.3