SettingsHandler: Migrate to new filesystem interface

Change SettingsHandler to take a buffer instead of assuming that the
setting file to read is always on the host filesystem for more
flexibility and make it possible to use the new filesystem interface.
This commit is contained in:
Léo Lam 2018-05-09 20:40:56 +02:00
parent 6e9d0ff6de
commit 09d2afa91f
4 changed files with 52 additions and 46 deletions

View File

@ -19,8 +19,6 @@
#endif #endif
#include "Common/CommonTypes.h" #include "Common/CommonTypes.h"
#include "Common/File.h"
#include "Common/FileUtil.h"
#include "Common/SettingsHandler.h" #include "Common/SettingsHandler.h"
#include "Common/Timer.h" #include "Common/Timer.h"
@ -29,30 +27,21 @@ SettingsHandler::SettingsHandler()
Reset(); Reset();
} }
bool SettingsHandler::Open(const std::string& settings_file_path) SettingsHandler::SettingsHandler(Buffer&& buffer)
{
SetBytes(std::move(buffer));
}
const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
{
return m_buffer;
}
void SettingsHandler::SetBytes(SettingsHandler::Buffer&& buffer)
{ {
Reset(); Reset();
m_buffer = std::move(buffer);
File::IOFile file{settings_file_path, "rb"};
if (!file.ReadBytes(m_buffer.data(), m_buffer.size()))
return false;
Decrypt(); Decrypt();
return true;
}
bool SettingsHandler::Save(const std::string& destination_file_path) const
{
if (!File::CreateFullPath(destination_file_path))
return false;
File::IOFile file{destination_file_path, "wb"};
return file.WriteBytes(m_buffer.data(), m_buffer.size());
}
const u8* SettingsHandler::GetData() const
{
return m_buffer.data();
} }
const std::string SettingsHandler::GetValue(const std::string& key) const std::string SettingsHandler::GetValue(const std::string& key)

View File

@ -21,14 +21,14 @@ public:
INITIAL_SEED = 0x73B5DBFA INITIAL_SEED = 0x73B5DBFA
}; };
using Buffer = std::array<u8, SETTINGS_SIZE>;
SettingsHandler(); SettingsHandler();
explicit SettingsHandler(Buffer&& buffer);
bool Open(const std::string& settings_file_path);
bool Save(const std::string& destination_file_path) const;
void AddSetting(const std::string& key, const std::string& value); void AddSetting(const std::string& key, const std::string& value);
const u8* GetData() const; const Buffer& GetBytes() const;
void SetBytes(Buffer&& buffer);
const std::string GetValue(const std::string& key); const std::string GetValue(const std::string& key);
void Decrypt(); void Decrypt();

View File

@ -25,7 +25,9 @@
#include "Core/HW/Memmap.h" #include "Core/HW/Memmap.h"
#include "Core/IOS/ES/ES.h" #include "Core/IOS/ES/ES.h"
#include "Core/IOS/ES/Formats.h" #include "Core/IOS/ES/Formats.h"
#include "Core/IOS/FS/FileSystem.h"
#include "Core/IOS/IOS.h" #include "Core/IOS/IOS.h"
#include "Core/IOS/Uids.h"
#include "Core/PowerPC/PowerPC.h" #include "Core/PowerPC/PowerPC.h"
#include "DiscIO/Enums.h" #include "DiscIO/Enums.h"
@ -225,15 +227,22 @@ bool CBoot::SetupWiiMemory()
SettingsHandler gen; SettingsHandler gen;
std::string serno; std::string serno;
const std::string settings_file_path( const std::string settings_file_path(Common::GetTitleDataPath(Titles::SYSTEM_MENU) +
Common::GetTitleDataPath(Titles::SYSTEM_MENU, Common::FROM_SESSION_ROOT) + "/" WII_SETTING); "/" WII_SETTING);
if (File::Exists(settings_file_path) && gen.Open(settings_file_path))
{
serno = gen.GetValue("SERNO");
gen.Reset();
File::Delete(settings_file_path); const auto fs = IOS::HLE::GetIOS()->GetFS();
{
SettingsHandler::Buffer data;
const auto file = fs->OpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID, settings_file_path,
IOS::HLE::FS::Mode::Read);
if (file && file->Read(data.data(), data.size()))
{
gen.SetBytes(std::move(data));
serno = gen.GetValue("SERNO");
gen.Reset();
}
} }
fs->Delete(IOS::SYSMENU_UID, IOS::SYSMENU_GID, settings_file_path);
if (serno.empty() || serno == "000000000") if (serno.empty() || serno == "000000000")
{ {
@ -258,14 +267,17 @@ bool CBoot::SetupWiiMemory()
gen.AddSetting("VIDEO", region_setting.video); gen.AddSetting("VIDEO", region_setting.video);
gen.AddSetting("GAME", region_setting.game); gen.AddSetting("GAME", region_setting.game);
if (!gen.Save(settings_file_path)) constexpr IOS::HLE::FS::Mode rw_mode = IOS::HLE::FS::Mode::ReadWrite;
const auto settings_file = fs->CreateAndOpenFile(IOS::SYSMENU_UID, IOS::SYSMENU_GID,
settings_file_path, rw_mode, rw_mode, rw_mode);
if (!settings_file || !settings_file->Write(gen.GetBytes().data(), gen.GetBytes().size()))
{ {
PanicAlertT("SetupWiiMemory: Can't create setting.txt file"); PanicAlertT("SetupWiiMemory: Can't create setting.txt file");
return false; return false;
} }
// Write the 256 byte setting.txt to memory. // Write the 256 byte setting.txt to memory.
Memory::CopyToEmu(0x3800, gen.GetData(), SettingsHandler::SETTINGS_SIZE); Memory::CopyToEmu(0x3800, gen.GetBytes().data(), gen.GetBytes().size());
INFO_LOG(BOOT, "Setup Wii Memory..."); INFO_LOG(BOOT, "Setup Wii Memory...");

View File

@ -16,7 +16,9 @@
#include "Core/CommonTitles.h" #include "Core/CommonTitles.h"
#include "Core/HW/Memmap.h" #include "Core/HW/Memmap.h"
#include "Core/IOS/FS/FileSystem.h"
#include "Core/IOS/Network/Socket.h" #include "Core/IOS/Network/Socket.h"
#include "Core/IOS/Uids.h"
namespace IOS namespace IOS
{ {
@ -82,20 +84,23 @@ IPCCommandResult NetKDRequest::IOCtl(const IOCtlRequest& request)
INFO_LOG(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_REQUEST_GENERATED_USER_ID"); INFO_LOG(IOS_WC24, "NET_KD_REQ: IOCTL_NWC24_REQUEST_GENERATED_USER_ID");
if (config.CreationStage() == NWC24::NWC24Config::NWC24_IDCS_INITIAL) if (config.CreationStage() == NWC24::NWC24Config::NWC24_IDCS_INITIAL)
{ {
const std::string settings_file_path( const std::string settings_file_path =
Common::GetTitleDataPath(Titles::SYSTEM_MENU, Common::FROM_SESSION_ROOT) + Common::GetTitleDataPath(Titles::SYSTEM_MENU) + "/" WII_SETTING;
"/" WII_SETTING);
SettingsHandler gen;
std::string area, model; std::string area, model;
bool got_settings = false;
if (File::Exists(settings_file_path) && gen.Open(settings_file_path)) const auto fs = m_ios.GetFS();
if (const auto file = fs->OpenFile(PID_KD, PID_KD, settings_file_path, FS::Mode::Read))
{ {
area = gen.GetValue("AREA"); SettingsHandler::Buffer data;
model = gen.GetValue("MODEL"); if (file->Read(data.data(), data.size()))
got_settings = true; {
const SettingsHandler gen{std::move(data)};
area = gen.GetValue("AREA");
model = gen.GetValue("MODEL");
}
} }
if (got_settings)
if (!area.empty() && !model.empty())
{ {
u8 area_code = GetAreaCode(area); u8 area_code = GetAreaCode(area);
u8 id_ctr = config.IdGen(); u8 id_ctr = config.IdGen();