ptm: access FS via backend directly

This commit is contained in:
Weiyi Wang 2018-09-28 17:21:45 -04:00
parent b03332d09b
commit 2757eff122

View file

@ -3,10 +3,12 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <cinttypes> #include <cinttypes>
#include "common/common_paths.h"
#include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/file_sys/archive_extsavedata.h"
#include "core/file_sys/errors.h" #include "core/file_sys/errors.h"
#include "core/file_sys/file_backend.h" #include "core/file_sys/file_backend.h"
#include "core/hle/service/fs/archive.h"
#include "core/hle/service/ptm/ptm.h" #include "core/hle/service/ptm/ptm.h"
#include "core/hle/service/ptm/ptm_gets.h" #include "core/hle/service/ptm/ptm_gets.h"
#include "core/hle/service/ptm/ptm_play.h" #include "core/hle/service/ptm/ptm_play.h"
@ -136,42 +138,44 @@ void Module::Interface::CheckNew3DS(Kernel::HLERequestContext& ctx) {
} }
static void WriteGameCoinData(GameCoin gamecoin_data) { static void WriteGameCoinData(GameCoin gamecoin_data) {
std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
FileSys::ArchiveFactory_ExtSaveData extdata_archive_factory(nand_directory, true);
FileSys::Path archive_path(ptm_shared_extdata_id); FileSys::Path archive_path(ptm_shared_extdata_id);
auto archive_result = auto archive_result = extdata_archive_factory.Open(archive_path);
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path); std::unique_ptr<FileSys::ArchiveBackend> archive;
FileSys::Path gamecoin_path("/gamecoin.dat"); FileSys::Path gamecoin_path("/gamecoin.dat");
// If the archive didn't exist, create the files inside // If the archive didn't exist, create the files inside
if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) { if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) {
// Format the archive to create the directories // Format the archive to create the directories
Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, extdata_archive_factory.Format(archive_path, FileSys::ArchiveFormatInfo());
FileSys::ArchiveFormatInfo(), archive_path);
// Open it again to get a valid archive now that the folder exists // Open it again to get a valid archive now that the folder exists
archive_result = archive = extdata_archive_factory.Open(archive_path).Unwrap();
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
// Create the game coin file // Create the game coin file
Service::FS::CreateFileInArchive(*archive_result, gamecoin_path, sizeof(GameCoin)); archive->CreateFile(gamecoin_path, sizeof(GameCoin));
} else { } else {
ASSERT_MSG(archive_result.Succeeded(), "Could not open the PTM SharedExtSaveData archive!"); ASSERT_MSG(archive_result.Succeeded(), "Could not open the PTM SharedExtSaveData archive!");
archive = std::move(archive_result).Unwrap();
} }
FileSys::Mode open_mode = {}; FileSys::Mode open_mode = {};
open_mode.write_flag.Assign(1); open_mode.write_flag.Assign(1);
// Open the file and write the default gamecoin information // Open the file and write the default gamecoin information
auto gamecoin_result = auto gamecoin_result = archive->OpenFile(gamecoin_path, open_mode);
Service::FS::OpenFileFromArchive(*archive_result, gamecoin_path, open_mode);
if (gamecoin_result.Succeeded()) { if (gamecoin_result.Succeeded()) {
auto gamecoin = std::move(gamecoin_result).Unwrap(); auto gamecoin = std::move(gamecoin_result).Unwrap();
gamecoin->backend->Write(0, sizeof(GameCoin), true, gamecoin->Write(0, sizeof(GameCoin), true, reinterpret_cast<const u8*>(&gamecoin_data));
reinterpret_cast<const u8*>(&gamecoin_data)); gamecoin->Close();
gamecoin->backend->Close();
} }
} }
static GameCoin ReadGameCoinData() { static GameCoin ReadGameCoinData() {
std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
FileSys::ArchiveFactory_ExtSaveData extdata_archive_factory(nand_directory, true);
FileSys::Path archive_path(ptm_shared_extdata_id); FileSys::Path archive_path(ptm_shared_extdata_id);
auto archive_result = auto archive_result = extdata_archive_factory.Open(archive_path);
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
if (!archive_result.Succeeded()) { if (!archive_result.Succeeded()) {
LOG_ERROR(Service_PTM, "Could not open the PTM SharedExtSaveData archive!"); LOG_ERROR(Service_PTM, "Could not open the PTM SharedExtSaveData archive!");
return default_game_coin; return default_game_coin;
@ -181,8 +185,7 @@ static GameCoin ReadGameCoinData() {
FileSys::Mode open_mode = {}; FileSys::Mode open_mode = {};
open_mode.read_flag.Assign(1); open_mode.read_flag.Assign(1);
auto gamecoin_result = auto gamecoin_result = (*archive_result)->OpenFile(gamecoin_path, open_mode);
Service::FS::OpenFileFromArchive(*archive_result, gamecoin_path, open_mode);
if (!gamecoin_result.Succeeded()) { if (!gamecoin_result.Succeeded()) {
LOG_ERROR(Service_PTM, "Could not open the game coin data file!"); LOG_ERROR(Service_PTM, "Could not open the game coin data file!");
return default_game_coin; return default_game_coin;
@ -190,17 +193,18 @@ static GameCoin ReadGameCoinData() {
u16 result; u16 result;
auto gamecoin = std::move(gamecoin_result).Unwrap(); auto gamecoin = std::move(gamecoin_result).Unwrap();
GameCoin gamecoin_data; GameCoin gamecoin_data;
gamecoin->backend->Read(0, sizeof(GameCoin), reinterpret_cast<u8*>(&gamecoin_data)); gamecoin->Read(0, sizeof(GameCoin), reinterpret_cast<u8*>(&gamecoin_data));
gamecoin->backend->Close(); gamecoin->Close();
return gamecoin_data; return gamecoin_data;
} }
Module::Module() { Module::Module() {
// Open the SharedExtSaveData archive 0xF000000B and create the gamecoin.dat file if it doesn't // Open the SharedExtSaveData archive 0xF000000B and create the gamecoin.dat file if it doesn't
// exist // exist
std::string nand_directory = FileUtil::GetUserPath(FileUtil::UserPath::NANDDir);
FileSys::ArchiveFactory_ExtSaveData extdata_archive_factory(nand_directory, true);
FileSys::Path archive_path(ptm_shared_extdata_id); FileSys::Path archive_path(ptm_shared_extdata_id);
auto archive_result = auto archive_result = extdata_archive_factory.Open(archive_path);
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
// If the archive didn't exist, write the default game coin file // If the archive didn't exist, write the default game coin file
if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) { if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) {
WriteGameCoinData(default_game_coin); WriteGameCoinData(default_game_coin);