diff options
| author | 2016-11-28 23:47:41 +0200 | |
|---|---|---|
| committer | 2016-11-29 23:50:00 +0200 | |
| commit | 5af117e00cf35131cbc02af9f244340d1ddc7568 (patch) | |
| tree | e9ecc8cfb0ad931b336191455ba6d30b1bbfbe51 /src | |
| parent | Merge pull request #2196 from Subv/system_mode (diff) | |
| download | yuzu-5af117e00cf35131cbc02af9f244340d1ddc7568.tar.gz yuzu-5af117e00cf35131cbc02af9f244340d1ddc7568.tar.xz yuzu-5af117e00cf35131cbc02af9f244340d1ddc7568.zip | |
FileSys: abstract SD save data archive source
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/file_sys/archive_savedata.cpp | 79 | ||||
| -rw-r--r-- | src/core/file_sys/archive_savedata.h | 8 | ||||
| -rw-r--r-- | src/core/file_sys/archive_source_sd_savedata.cpp | 93 | ||||
| -rw-r--r-- | src/core/file_sys/archive_source_sd_savedata.h | 30 | ||||
| -rw-r--r-- | src/core/hle/service/fs/archive.cpp | 3 |
6 files changed, 136 insertions, 79 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 299f1f261..3f762ed52 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -22,6 +22,7 @@ set(SRCS | |||
| 22 | file_sys/archive_savedata.cpp | 22 | file_sys/archive_savedata.cpp |
| 23 | file_sys/archive_sdmc.cpp | 23 | file_sys/archive_sdmc.cpp |
| 24 | file_sys/archive_sdmcwriteonly.cpp | 24 | file_sys/archive_sdmcwriteonly.cpp |
| 25 | file_sys/archive_source_sd_savedata.cpp | ||
| 25 | file_sys/archive_systemsavedata.cpp | 26 | file_sys/archive_systemsavedata.cpp |
| 26 | file_sys/disk_archive.cpp | 27 | file_sys/disk_archive.cpp |
| 27 | file_sys/ivfc_archive.cpp | 28 | file_sys/ivfc_archive.cpp |
| @@ -167,6 +168,7 @@ set(HEADERS | |||
| 167 | file_sys/archive_savedata.h | 168 | file_sys/archive_savedata.h |
| 168 | file_sys/archive_sdmc.h | 169 | file_sys/archive_sdmc.h |
| 169 | file_sys/archive_sdmcwriteonly.h | 170 | file_sys/archive_sdmcwriteonly.h |
| 171 | file_sys/archive_source_sd_savedata.h | ||
| 170 | file_sys/archive_systemsavedata.h | 172 | file_sys/archive_systemsavedata.h |
| 171 | file_sys/directory_backend.h | 173 | file_sys/directory_backend.h |
| 172 | file_sys/disk_archive.h | 174 | file_sys/disk_archive.h |
diff --git a/src/core/file_sys/archive_savedata.cpp b/src/core/file_sys/archive_savedata.cpp index ecb44a215..61f7654f7 100644 --- a/src/core/file_sys/archive_savedata.cpp +++ b/src/core/file_sys/archive_savedata.cpp | |||
| @@ -2,96 +2,29 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <algorithm> | ||
| 6 | #include <memory> | ||
| 7 | #include "common/common_types.h" | ||
| 8 | #include "common/file_util.h" | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "common/string_util.h" | ||
| 11 | #include "core/file_sys/archive_savedata.h" | 5 | #include "core/file_sys/archive_savedata.h" |
| 12 | #include "core/file_sys/savedata_archive.h" | ||
| 13 | #include "core/hle/kernel/process.h" | 6 | #include "core/hle/kernel/process.h" |
| 14 | #include "core/hle/service/fs/archive.h" | ||
| 15 | 7 | ||
| 16 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 8 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 17 | // FileSys namespace | 9 | // FileSys namespace |
| 18 | 10 | ||
| 19 | namespace FileSys { | 11 | namespace FileSys { |
| 20 | 12 | ||
| 21 | static std::string GetSaveDataContainerPath(const std::string& sdmc_directory) { | 13 | ArchiveFactory_SaveData::ArchiveFactory_SaveData( |
| 22 | return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/", sdmc_directory.c_str(), | 14 | std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata) |
| 23 | SYSTEM_ID.c_str(), SDCARD_ID.c_str()); | 15 | : sd_savedata_source(sd_savedata) {} |
| 24 | } | ||
| 25 | |||
| 26 | static std::string GetSaveDataPath(const std::string& mount_location, u64 program_id) { | ||
| 27 | u32 high = (u32)(program_id >> 32); | ||
| 28 | u32 low = (u32)(program_id & 0xFFFFFFFF); | ||
| 29 | return Common::StringFromFormat("%s%08x/%08x/data/00000001/", mount_location.c_str(), high, | ||
| 30 | low); | ||
| 31 | } | ||
| 32 | |||
| 33 | static std::string GetSaveDataMetadataPath(const std::string& mount_location, u64 program_id) { | ||
| 34 | u32 high = (u32)(program_id >> 32); | ||
| 35 | u32 low = (u32)(program_id & 0xFFFFFFFF); | ||
| 36 | return Common::StringFromFormat("%s%08x/%08x/data/00000001.metadata", mount_location.c_str(), | ||
| 37 | high, low); | ||
| 38 | } | ||
| 39 | |||
| 40 | ArchiveFactory_SaveData::ArchiveFactory_SaveData(const std::string& sdmc_directory) | ||
| 41 | : mount_point(GetSaveDataContainerPath(sdmc_directory)) { | ||
| 42 | LOG_INFO(Service_FS, "Directory %s set as SaveData.", this->mount_point.c_str()); | ||
| 43 | } | ||
| 44 | 16 | ||
| 45 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const Path& path) { | 17 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveFactory_SaveData::Open(const Path& path) { |
| 46 | std::string concrete_mount_point = | 18 | return sd_savedata_source->Open(Kernel::g_current_process->codeset->program_id); |
| 47 | GetSaveDataPath(mount_point, Kernel::g_current_process->codeset->program_id); | ||
| 48 | if (!FileUtil::Exists(concrete_mount_point)) { | ||
| 49 | // When a SaveData archive is created for the first time, it is not yet formatted and the | ||
| 50 | // save file/directory structure expected by the game has not yet been initialized. | ||
| 51 | // Returning the NotFormatted error code will signal the game to provision the SaveData | ||
| 52 | // archive with the files and folders that it expects. | ||
| 53 | return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS, | ||
| 54 | ErrorSummary::InvalidState, ErrorLevel::Status); | ||
| 55 | } | ||
| 56 | |||
| 57 | auto archive = std::make_unique<SaveDataArchive>(std::move(concrete_mount_point)); | ||
| 58 | return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); | ||
| 59 | } | 19 | } |
| 60 | 20 | ||
| 61 | ResultCode ArchiveFactory_SaveData::Format(const Path& path, | 21 | ResultCode ArchiveFactory_SaveData::Format(const Path& path, |
| 62 | const FileSys::ArchiveFormatInfo& format_info) { | 22 | const FileSys::ArchiveFormatInfo& format_info) { |
| 63 | std::string concrete_mount_point = | 23 | return sd_savedata_source->Format(Kernel::g_current_process->codeset->program_id, format_info); |
| 64 | GetSaveDataPath(mount_point, Kernel::g_current_process->codeset->program_id); | ||
| 65 | FileUtil::DeleteDirRecursively(concrete_mount_point); | ||
| 66 | FileUtil::CreateFullPath(concrete_mount_point); | ||
| 67 | |||
| 68 | // Write the format metadata | ||
| 69 | std::string metadata_path = | ||
| 70 | GetSaveDataMetadataPath(mount_point, Kernel::g_current_process->codeset->program_id); | ||
| 71 | FileUtil::IOFile file(metadata_path, "wb"); | ||
| 72 | |||
| 73 | if (file.IsOpen()) { | ||
| 74 | file.WriteBytes(&format_info, sizeof(format_info)); | ||
| 75 | return RESULT_SUCCESS; | ||
| 76 | } | ||
| 77 | return RESULT_SUCCESS; | ||
| 78 | } | 24 | } |
| 79 | 25 | ||
| 80 | ResultVal<ArchiveFormatInfo> ArchiveFactory_SaveData::GetFormatInfo(const Path& path) const { | 26 | ResultVal<ArchiveFormatInfo> ArchiveFactory_SaveData::GetFormatInfo(const Path& path) const { |
| 81 | std::string metadata_path = | 27 | return sd_savedata_source->GetFormatInfo(Kernel::g_current_process->codeset->program_id); |
| 82 | GetSaveDataMetadataPath(mount_point, Kernel::g_current_process->codeset->program_id); | ||
| 83 | FileUtil::IOFile file(metadata_path, "rb"); | ||
| 84 | |||
| 85 | if (!file.IsOpen()) { | ||
| 86 | LOG_ERROR(Service_FS, "Could not open metadata information for archive"); | ||
| 87 | // TODO(Subv): Verify error code | ||
| 88 | return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS, | ||
| 89 | ErrorSummary::InvalidState, ErrorLevel::Status); | ||
| 90 | } | ||
| 91 | |||
| 92 | ArchiveFormatInfo info = {}; | ||
| 93 | file.ReadBytes(&info, sizeof(info)); | ||
| 94 | return MakeResult<ArchiveFormatInfo>(info); | ||
| 95 | } | 28 | } |
| 96 | 29 | ||
| 97 | } // namespace FileSys | 30 | } // namespace FileSys |
diff --git a/src/core/file_sys/archive_savedata.h b/src/core/file_sys/archive_savedata.h index 6a372865a..41aa6f189 100644 --- a/src/core/file_sys/archive_savedata.h +++ b/src/core/file_sys/archive_savedata.h | |||
| @@ -4,10 +4,7 @@ | |||
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <memory> | 7 | #include "core/file_sys/archive_source_sd_savedata.h" |
| 8 | #include <string> | ||
| 9 | #include "core/file_sys/archive_backend.h" | ||
| 10 | #include "core/hle/result.h" | ||
| 11 | 8 | ||
| 12 | //////////////////////////////////////////////////////////////////////////////////////////////////// | 9 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
| 13 | // FileSys namespace | 10 | // FileSys namespace |
| @@ -17,7 +14,7 @@ namespace FileSys { | |||
| 17 | /// File system interface to the SaveData archive | 14 | /// File system interface to the SaveData archive |
| 18 | class ArchiveFactory_SaveData final : public ArchiveFactory { | 15 | class ArchiveFactory_SaveData final : public ArchiveFactory { |
| 19 | public: | 16 | public: |
| 20 | ArchiveFactory_SaveData(const std::string& mount_point); | 17 | explicit ArchiveFactory_SaveData(std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source); |
| 21 | 18 | ||
| 22 | std::string GetName() const override { | 19 | std::string GetName() const override { |
| 23 | return "SaveData"; | 20 | return "SaveData"; |
| @@ -30,6 +27,7 @@ public: | |||
| 30 | 27 | ||
| 31 | private: | 28 | private: |
| 32 | std::string mount_point; | 29 | std::string mount_point; |
| 30 | std::shared_ptr<ArchiveSource_SDSaveData> sd_savedata_source; | ||
| 33 | }; | 31 | }; |
| 34 | 32 | ||
| 35 | } // namespace FileSys | 33 | } // namespace FileSys |
diff --git a/src/core/file_sys/archive_source_sd_savedata.cpp b/src/core/file_sys/archive_source_sd_savedata.cpp new file mode 100644 index 000000000..2d8a950a3 --- /dev/null +++ b/src/core/file_sys/archive_source_sd_savedata.cpp | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/file_util.h" | ||
| 6 | #include "common/logging/log.h" | ||
| 7 | #include "common/string_util.h" | ||
| 8 | #include "core/file_sys/archive_source_sd_savedata.h" | ||
| 9 | #include "core/file_sys/savedata_archive.h" | ||
| 10 | #include "core/hle/service/fs/archive.h" | ||
| 11 | |||
| 12 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 13 | // FileSys namespace | ||
| 14 | |||
| 15 | namespace FileSys { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | std::string GetSaveDataContainerPath(const std::string& sdmc_directory) { | ||
| 20 | return Common::StringFromFormat("%sNintendo 3DS/%s/%s/title/", sdmc_directory.c_str(), | ||
| 21 | SYSTEM_ID.c_str(), SDCARD_ID.c_str()); | ||
| 22 | } | ||
| 23 | |||
| 24 | std::string GetSaveDataPath(const std::string& mount_location, u64 program_id) { | ||
| 25 | u32 high = static_cast<u32>(program_id >> 32); | ||
| 26 | u32 low = static_cast<u32>(program_id & 0xFFFFFFFF); | ||
| 27 | return Common::StringFromFormat("%s%08x/%08x/data/00000001/", mount_location.c_str(), high, | ||
| 28 | low); | ||
| 29 | } | ||
| 30 | |||
| 31 | std::string GetSaveDataMetadataPath(const std::string& mount_location, u64 program_id) { | ||
| 32 | u32 high = static_cast<u32>(program_id >> 32); | ||
| 33 | u32 low = static_cast<u32>(program_id & 0xFFFFFFFF); | ||
| 34 | return Common::StringFromFormat("%s%08x/%08x/data/00000001.metadata", mount_location.c_str(), | ||
| 35 | high, low); | ||
| 36 | } | ||
| 37 | |||
| 38 | } // namespace | ||
| 39 | |||
| 40 | ArchiveSource_SDSaveData::ArchiveSource_SDSaveData(const std::string& sdmc_directory) | ||
| 41 | : mount_point(GetSaveDataContainerPath(sdmc_directory)) { | ||
| 42 | LOG_INFO(Service_FS, "Directory %s set as SaveData.", mount_point.c_str()); | ||
| 43 | } | ||
| 44 | |||
| 45 | ResultVal<std::unique_ptr<ArchiveBackend>> ArchiveSource_SDSaveData::Open(u64 program_id) { | ||
| 46 | std::string concrete_mount_point = GetSaveDataPath(mount_point, program_id); | ||
| 47 | if (!FileUtil::Exists(concrete_mount_point)) { | ||
| 48 | // When a SaveData archive is created for the first time, it is not yet formatted and the | ||
| 49 | // save file/directory structure expected by the game has not yet been initialized. | ||
| 50 | // Returning the NotFormatted error code will signal the game to provision the SaveData | ||
| 51 | // archive with the files and folders that it expects. | ||
| 52 | return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS, | ||
| 53 | ErrorSummary::InvalidState, ErrorLevel::Status); | ||
| 54 | } | ||
| 55 | |||
| 56 | auto archive = std::make_unique<SaveDataArchive>(std::move(concrete_mount_point)); | ||
| 57 | return MakeResult<std::unique_ptr<ArchiveBackend>>(std::move(archive)); | ||
| 58 | } | ||
| 59 | |||
| 60 | ResultCode ArchiveSource_SDSaveData::Format(u64 program_id, | ||
| 61 | const FileSys::ArchiveFormatInfo& format_info) { | ||
| 62 | std::string concrete_mount_point = GetSaveDataPath(mount_point, program_id); | ||
| 63 | FileUtil::DeleteDirRecursively(concrete_mount_point); | ||
| 64 | FileUtil::CreateFullPath(concrete_mount_point); | ||
| 65 | |||
| 66 | // Write the format metadata | ||
| 67 | std::string metadata_path = GetSaveDataMetadataPath(mount_point, program_id); | ||
| 68 | FileUtil::IOFile file(metadata_path, "wb"); | ||
| 69 | |||
| 70 | if (file.IsOpen()) { | ||
| 71 | file.WriteBytes(&format_info, sizeof(format_info)); | ||
| 72 | return RESULT_SUCCESS; | ||
| 73 | } | ||
| 74 | return RESULT_SUCCESS; | ||
| 75 | } | ||
| 76 | |||
| 77 | ResultVal<ArchiveFormatInfo> ArchiveSource_SDSaveData::GetFormatInfo(u64 program_id) const { | ||
| 78 | std::string metadata_path = GetSaveDataMetadataPath(mount_point, program_id); | ||
| 79 | FileUtil::IOFile file(metadata_path, "rb"); | ||
| 80 | |||
| 81 | if (!file.IsOpen()) { | ||
| 82 | LOG_ERROR(Service_FS, "Could not open metadata information for archive"); | ||
| 83 | // TODO(Subv): Verify error code | ||
| 84 | return ResultCode(ErrorDescription::FS_NotFormatted, ErrorModule::FS, | ||
| 85 | ErrorSummary::InvalidState, ErrorLevel::Status); | ||
| 86 | } | ||
| 87 | |||
| 88 | ArchiveFormatInfo info = {}; | ||
| 89 | file.ReadBytes(&info, sizeof(info)); | ||
| 90 | return MakeResult<ArchiveFormatInfo>(info); | ||
| 91 | } | ||
| 92 | |||
| 93 | } // namespace FileSys | ||
diff --git a/src/core/file_sys/archive_source_sd_savedata.h b/src/core/file_sys/archive_source_sd_savedata.h new file mode 100644 index 000000000..b33126c31 --- /dev/null +++ b/src/core/file_sys/archive_source_sd_savedata.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | // Copyright 2016 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <memory> | ||
| 8 | #include <string> | ||
| 9 | #include "core/file_sys/archive_backend.h" | ||
| 10 | #include "core/hle/result.h" | ||
| 11 | |||
| 12 | //////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 13 | // FileSys namespace | ||
| 14 | |||
| 15 | namespace FileSys { | ||
| 16 | |||
| 17 | /// A common source of SD save data archive | ||
| 18 | class ArchiveSource_SDSaveData { | ||
| 19 | public: | ||
| 20 | explicit ArchiveSource_SDSaveData(const std::string& mount_point); | ||
| 21 | |||
| 22 | ResultVal<std::unique_ptr<ArchiveBackend>> Open(u64 program_id); | ||
| 23 | ResultCode Format(u64 program_id, const FileSys::ArchiveFormatInfo& format_info); | ||
| 24 | ResultVal<ArchiveFormatInfo> GetFormatInfo(u64 program_id) const; | ||
| 25 | |||
| 26 | private: | ||
| 27 | std::string mount_point; | ||
| 28 | }; | ||
| 29 | |||
| 30 | } // namespace FileSys | ||
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index 4c29784e8..a067e9262 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp | |||
| @@ -535,7 +535,8 @@ void RegisterArchiveTypes() { | |||
| 535 | sdmc_directory.c_str()); | 535 | sdmc_directory.c_str()); |
| 536 | 536 | ||
| 537 | // Create the SaveData archive | 537 | // Create the SaveData archive |
| 538 | auto savedata_factory = std::make_unique<FileSys::ArchiveFactory_SaveData>(sdmc_directory); | 538 | auto sd_savedata_source = std::make_shared<FileSys::ArchiveSource_SDSaveData>(sdmc_directory); |
| 539 | auto savedata_factory = std::make_unique<FileSys::ArchiveFactory_SaveData>(sd_savedata_source); | ||
| 539 | RegisterArchiveType(std::move(savedata_factory), ArchiveIdCode::SaveData); | 540 | RegisterArchiveType(std::move(savedata_factory), ArchiveIdCode::SaveData); |
| 540 | 541 | ||
| 541 | auto extsavedata_factory = | 542 | auto extsavedata_factory = |