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.
master
Subv 2014-12-29 13:04:37 +07:00
parent 3d14eb2853
commit 2c89d4d5cd
17 changed files with 268 additions and 60 deletions

@ -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"

@ -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;

@ -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,

@ -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

@ -46,6 +46,9 @@ public:
Path(const char* path) : type(Char), string(path) {
}
Path(std::vector<u8> 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.)
*/

@ -0,0 +1,59 @@
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <sys/stat.h>
#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<u8> vec_data = path.AsBinary();
const u32* data = reinterpret_cast<const u32*>(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

@ -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 <base extsavedatapath>/<type>/<high>/<low>.
* See GetExtSaveDataPath for the code that extracts this data from an archive path.
*/
std::string concrete_mount_point;
};
} // namespace FileSys

@ -62,4 +62,9 @@ std::unique_ptr<DirectoryBackend> Archive_RomFS::OpenDirectory(const Path& path)
return Common::make_unique<Directory_RomFS>();
}
ResultCode Archive_RomFS::Format(const Path& path) const {
LOG_WARNING(Service_FS, "Attempted to format ROMFS.");
return UnimplementedFunction(ErrorModule::FS);
}
} // namespace FileSys

@ -83,6 +83,12 @@ public:
*/
std::unique_ptr<DirectoryBackend> 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;

@ -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

@ -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

@ -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<FileBackend> 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<DirectoryBackend> 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;
}

@ -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" },
};
////////////////////////////////////////////////////////////////////////////////////////////////////

@ -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

@ -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

@ -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;

@ -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<FileSys::Archive_ExtSaveData> ptm_shared_extsavedata;
static const std::vector<u8> 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<FileSys::Archive_ExtSaveData>(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<const u8*>(&default_game_coin));
gamecoin->Close();
}
}
}
} // namespace