From 2c89d4d5cd4e308b04cebb1c9bca48e12f0945da Mon Sep 17 00:00:00 2001 From: Subv Date: Mon, 29 Dec 2014 13:04:37 -0500 Subject: [PATCH] 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/common/common_paths.h | 1 + src/common/file_util.cpp | 2 + src/common/file_util.h | 1 + src/core/CMakeLists.txt | 2 + 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 ++- src/core/hle/service/apt_a.cpp | 13 ++++- src/core/hle/service/fs/archive.cpp | 59 ++++++++++++----------- src/core/hle/service/fs/archive.h | 3 +- src/core/hle/service/fs/fs_user.cpp | 17 +------ src/core/hle/service/ptm_u.cpp | 47 ++++++++++++++++++ 17 files changed, 268 insertions(+), 60 deletions(-) create mode 100644 src/core/file_sys/archive_extsavedata.cpp create mode 100644 src/core/file_sys/archive_extsavedata.h diff --git a/src/common/common_paths.h b/src/common/common_paths.h index 9d62a8368..d3f0702bc 100644 --- a/src/common/common_paths.h +++ b/src/common/common_paths.h @@ -40,6 +40,7 @@ #define MAPS_DIR "maps" #define CACHE_DIR "cache" #define SDMC_DIR "sdmc" +#define EXTSAVEDATA_DIR "extsavedata" #define SAVEDATA_DIR "savedata" #define SYSDATA_DIR "sysdata" #define SYSSAVEDATA_DIR "syssavedata" diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index bba830c70..c44ad4ca1 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -676,6 +676,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new paths[D_MAPS_IDX] = paths[D_USER_IDX] + MAPS_DIR DIR_SEP; paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP; + paths[D_EXTSAVEDATA] = paths[D_USER_IDX] + EXTSAVEDATA_DIR DIR_SEP; paths[D_SAVEDATA_IDX] = paths[D_USER_IDX] + SAVEDATA_DIR DIR_SEP; paths[D_SYSDATA_IDX] = paths[D_USER_IDX] + SYSDATA_DIR DIR_SEP; paths[D_SYSSAVEDATA_IDX] = paths[D_USER_IDX] + SYSSAVEDATA_DIR DIR_SEP; @@ -720,6 +721,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new paths[D_MAPS_IDX] = paths[D_USER_IDX] + MAPS_DIR DIR_SEP; paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP; + paths[D_EXTSAVEDATA] = paths[D_USER_IDX] + EXTSAVEDATA_DIR DIR_SEP; paths[D_SAVEDATA_IDX] = paths[D_USER_IDX] + SAVEDATA_DIR DIR_SEP; paths[D_SYSSAVEDATA_IDX] = paths[D_USER_IDX] + SYSSAVEDATA_DIR DIR_SEP; paths[D_SHADERCACHE_IDX] = paths[D_USER_IDX] + SHADERCACHE_DIR DIR_SEP; diff --git a/src/common/file_util.h b/src/common/file_util.h index 293c30941..ec2415473 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -27,6 +27,7 @@ enum { D_STATESAVES_IDX, D_SCREENSHOTS_IDX, D_SDMC_IDX, + D_EXTSAVEDATA, D_SAVEDATA_IDX, D_SYSDATA_IDX, D_SYSSAVEDATA_IDX, diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index fdd97c184..89ea70d23 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -17,6 +17,7 @@ set(SRCS arm/skyeye_common/vfp/vfpdouble.cpp arm/skyeye_common/vfp/vfpinstr.cpp arm/skyeye_common/vfp/vfpsingle.cpp + file_sys/archive_extsavedata.cpp file_sys/archive_romfs.cpp file_sys/archive_savedata.cpp file_sys/archive_sdmc.cpp @@ -104,6 +105,7 @@ set(HEADERS arm/skyeye_common/vfp/vfp_helper.h arm/arm_interface.h file_sys/archive_backend.h + file_sys/archive_extsavedata.h file_sys/archive_romfs.h file_sys/archive_savedata.h file_sys/archive_sdmc.h diff --git a/src/core/file_sys/archive_backend.h b/src/core/file_sys/archive_backend.h index e153917ea..1612c35c2 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 000000000..4759ef3ae --- /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 000000000..a3a144799 --- /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 fdaf73179..2fc3831b7 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 5e918f92d..d4b1eb7f2 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 97853567c..280d4ff5d 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 5b0ce29e6..07c7f7eff 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 018ebd2ed..f18d96f5a 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; } diff --git a/src/core/hle/service/apt_a.cpp b/src/core/hle/service/apt_a.cpp index dcf5ec4fe..a22f5f15d 100644 --- a/src/core/hle/service/apt_a.cpp +++ b/src/core/hle/service/apt_a.cpp @@ -6,14 +6,21 @@ #include "core/hle/hle.h" #include "core/hle/service/apt_a.h" +namespace APT_U { + extern void Initialize(Service::Interface* self); + extern void GetLockHandle(Service::Interface* self); + extern void ReceiveParameter(Service::Interface* self); + extern void GlanceParameter(Service::Interface* self); +} + //////////////////////////////////////////////////////////////////////////////////////////////////// // Namespace APT_A namespace APT_A { const Interface::FunctionInfo FunctionTable[] = { - {0x00010040, nullptr, "GetLockHandle?"}, - {0x00020080, nullptr, "Initialize?"}, + {0x00010040, APT_U::GetLockHandle, "GetLockHandle?"}, + {0x00020080, APT_U::Initialize, "Initialize?"}, {0x00030040, nullptr, "Enable?"}, {0x00040040, nullptr, "Finalize?"}, {0x00050040, nullptr, "GetAppletManInfo?"}, @@ -22,6 +29,8 @@ const Interface::FunctionInfo FunctionTable[] = { {0x00430040, nullptr, "NotifyToWait?"}, {0x004B00C2, nullptr, "AppletUtility?"}, {0x00550040, nullptr, "WriteInputToNsState?"}, + {0x000D0080, APT_U::ReceiveParameter,"ReceiveParameter" }, + {0x000E0080, APT_U::GlanceParameter,"GlanceParameter" }, }; //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp index 487bf3aa7..f19ca3a9f 100644 --- a/src/core/hle/service/fs/archive.cpp +++ b/src/core/hle/service/fs/archive.cpp @@ -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 OpenArchive(ArchiveIdCode id_code) { +ResultVal 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 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(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(savedata_directory); + CreateArchive(std::move(savedata_archive), ArchiveIdCode::SaveData); + + std::string extsavedata_directory = FileUtil::GetUserPath(D_EXTSAVEDATA); + auto extsavedata_archive = Common::make_unique(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(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 diff --git a/src/core/hle/service/fs/archive.h b/src/core/hle/service/fs/archive.h index b39bc41b6..c23b8cc46 100644 --- a/src/core/hle/service/fs/archive.h +++ b/src/core/hle/service/fs/archive.h @@ -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 OpenArchive(ArchiveIdCode id_code); +ResultVal OpenArchive(ArchiveIdCode id_code, FileSys::Path& archive_path); /** * Closes an archive diff --git a/src/core/hle/service/fs/fs_user.cpp b/src/core/hle/service/fs/fs_user.cpp index b1a465274..7eb32146d 100644 --- a/src/core/hle/service/fs/fs_user.cpp +++ b/src/core/hle/service/fs/fs_user.cpp @@ -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 archive_handle = OpenArchive(archive_id); + ResultVal 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 handle = OpenArchive(archive_id); + ResultVal handle = OpenArchive(archive_id, archive_path); cmd_buff[1] = handle.Code().raw; if (handle.Succeeded()) { cmd_buff[2] = *handle & 0xFFFFFFFF; diff --git a/src/core/hle/service/ptm_u.cpp b/src/core/hle/service/ptm_u.cpp index d1498f05c..9cc700c46 100644 --- a/src/core/hle/service/ptm_u.cpp +++ b/src/core/hle/service/ptm_u.cpp @@ -3,6 +3,8 @@ // Refer to the license.txt file included. #include "common/log.h" +#include "common/make_unique.h" +#include "core/file_sys/archive_extsavedata.h" #include "core/hle/hle.h" #include "core/hle/service/ptm_u.h" @@ -11,6 +13,24 @@ namespace PTM_U { +/** + * Represents the gamecoin file structure in the SharedExtData archive + * More information in 3dbrew (http://www.3dbrew.org/wiki/Extdata#Shared_Extdata_0xf000000b_gamecoin.dat) + */ +struct GameCoin { + u32 magic; ///< Magic number: 0x4F00 + u16 total_coins; ///< Total Play Coins + u16 total_coins_on_date; ///< Total Play Coins obtained on the date stored below. + u32 step_count; ///< Total step count at the time a new Play Coin was obtained. + u32 last_step_count; ///< Step count for the day the last Play Coin was obtained + u16 year; + u8 month; + u8 day; +}; +static const GameCoin default_game_coin = { 0x4F00, 42, 0, 0, 0, 2014, 12, 29 }; +static std::unique_ptr ptm_shared_extsavedata; +static const std::vector ptm_shared_extdata_id = {0, 0, 0, 0, 0x0B, 0, 0, 0xF0, 0, 0, 0, 0}; + /// Charge levels used by PTM functions enum class ChargeLevels : u32 { CriticalBattery = 1, @@ -120,6 +140,33 @@ const Interface::FunctionInfo FunctionTable[] = { Interface::Interface() { Register(FunctionTable, ARRAY_SIZE(FunctionTable)); + // Create the SharedExtSaveData archive 0xF000000B and the gamecoin.dat file + // TODO(Subv): In the future we should use the FS service to query this archive + std::string extsavedata_directory = FileUtil::GetUserPath(D_EXTSAVEDATA); + ptm_shared_extsavedata = Common::make_unique(extsavedata_directory); + if (!ptm_shared_extsavedata->Initialize()) { + LOG_CRITICAL(Service_PTM, "Could not initialize ExtSaveData archive for the PTM:U service"); + return; + } + FileSys::Path archive_path(ptm_shared_extdata_id); + ResultCode result = ptm_shared_extsavedata->Open(archive_path); + // If the archive didn't exist, create the files inside + if (result.description == ErrorDescription::FS_NotFormatted) { + // Format the archive to clear the directories + ptm_shared_extsavedata->Format(archive_path); + // Open it again to get a valid archive now that the folder exists + ptm_shared_extsavedata->Open(archive_path); + FileSys::Path gamecoin_path("gamecoin.dat"); + FileSys::Mode open_mode = {}; + open_mode.write_flag = 1; + open_mode.create_flag = 1; + // Open the file and write the default gamecoin information + auto gamecoin = ptm_shared_extsavedata->OpenFile(gamecoin_path, open_mode); + if (gamecoin != nullptr) { + gamecoin->Write(0, sizeof(GameCoin), 1, reinterpret_cast(&default_game_coin)); + gamecoin->Close(); + } + } } } // namespace