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.
#include <cinttypes>
#include "common/common_paths.h"
#include "common/file_util.h"
#include "common/logging/log.h"
#include "core/file_sys/archive_extsavedata.h"
#include "core/file_sys/errors.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_gets.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) {
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);
auto archive_result =
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
auto archive_result = extdata_archive_factory.Open(archive_path);
std::unique_ptr<FileSys::ArchiveBackend> archive;
FileSys::Path gamecoin_path("/gamecoin.dat");
// If the archive didn't exist, create the files inside
if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) {
// Format the archive to create the directories
Service::FS::FormatArchive(Service::FS::ArchiveIdCode::SharedExtSaveData,
FileSys::ArchiveFormatInfo(), archive_path);
extdata_archive_factory.Format(archive_path, FileSys::ArchiveFormatInfo());
// Open it again to get a valid archive now that the folder exists
archive_result =
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
archive = extdata_archive_factory.Open(archive_path).Unwrap();
// Create the game coin file
Service::FS::CreateFileInArchive(*archive_result, gamecoin_path, sizeof(GameCoin));
archive->CreateFile(gamecoin_path, sizeof(GameCoin));
} else {
ASSERT_MSG(archive_result.Succeeded(), "Could not open the PTM SharedExtSaveData archive!");
archive = std::move(archive_result).Unwrap();
}
FileSys::Mode open_mode = {};
open_mode.write_flag.Assign(1);
// Open the file and write the default gamecoin information
auto gamecoin_result =
Service::FS::OpenFileFromArchive(*archive_result, gamecoin_path, open_mode);
auto gamecoin_result = archive->OpenFile(gamecoin_path, open_mode);
if (gamecoin_result.Succeeded()) {
auto gamecoin = std::move(gamecoin_result).Unwrap();
gamecoin->backend->Write(0, sizeof(GameCoin), true,
reinterpret_cast<const u8*>(&gamecoin_data));
gamecoin->backend->Close();
gamecoin->Write(0, sizeof(GameCoin), true, reinterpret_cast<const u8*>(&gamecoin_data));
gamecoin->Close();
}
}
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);
auto archive_result =
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
auto archive_result = extdata_archive_factory.Open(archive_path);
if (!archive_result.Succeeded()) {
LOG_ERROR(Service_PTM, "Could not open the PTM SharedExtSaveData archive!");
return default_game_coin;
@ -181,8 +185,7 @@ static GameCoin ReadGameCoinData() {
FileSys::Mode open_mode = {};
open_mode.read_flag.Assign(1);
auto gamecoin_result =
Service::FS::OpenFileFromArchive(*archive_result, gamecoin_path, open_mode);
auto gamecoin_result = (*archive_result)->OpenFile(gamecoin_path, open_mode);
if (!gamecoin_result.Succeeded()) {
LOG_ERROR(Service_PTM, "Could not open the game coin data file!");
return default_game_coin;
@ -190,17 +193,18 @@ static GameCoin ReadGameCoinData() {
u16 result;
auto gamecoin = std::move(gamecoin_result).Unwrap();
GameCoin gamecoin_data;
gamecoin->backend->Read(0, sizeof(GameCoin), reinterpret_cast<u8*>(&gamecoin_data));
gamecoin->backend->Close();
gamecoin->Read(0, sizeof(GameCoin), reinterpret_cast<u8*>(&gamecoin_data));
gamecoin->Close();
return gamecoin_data;
}
Module::Module() {
// Open the SharedExtSaveData archive 0xF000000B and create the gamecoin.dat file if it doesn't
// 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);
auto archive_result =
Service::FS::OpenArchive(Service::FS::ArchiveIdCode::SharedExtSaveData, archive_path);
auto archive_result = extdata_archive_factory.Open(archive_path);
// If the archive didn't exist, write the default game coin file
if (archive_result.Code() == FileSys::ERR_NOT_FORMATTED) {
WriteGameCoinData(default_game_coin);