Merge pull request #12737 from nlebeck/settingshandler-split

Eliminate SettingsHandler's `SetBytes` and `Reset` methods
This commit is contained in:
JosJuice 2024-05-05 22:35:23 +02:00 committed by GitHub
commit 2c91367429
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 14 additions and 57 deletions

View File

@ -17,14 +17,14 @@
namespace Common namespace Common
{ {
SettingsHandler::SettingsHandler() SettingsHandler::SettingsHandler() : m_buffer{}, m_position{0}, m_key{INITIAL_SEED}, decoded{""}
{ {
Reset();
} }
SettingsHandler::SettingsHandler(const Buffer& buffer) SettingsHandler::SettingsHandler(const Buffer& buffer) : SettingsHandler()
{ {
SetBytes(buffer); m_buffer = buffer;
Decrypt();
} }
const SettingsHandler::Buffer& SettingsHandler::GetBytes() const const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
@ -32,13 +32,6 @@ const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
return m_buffer; return m_buffer;
} }
void SettingsHandler::SetBytes(const Buffer& buffer)
{
Reset();
m_buffer = buffer;
Decrypt();
}
std::string SettingsHandler::GetValue(std::string_view key) const std::string SettingsHandler::GetValue(std::string_view key) const
{ {
constexpr char delim[] = "\n"; constexpr char delim[] = "\n";
@ -84,14 +77,6 @@ void SettingsHandler::Decrypt()
std::erase(decoded, '\x0d'); std::erase(decoded, '\x0d');
} }
void SettingsHandler::Reset()
{
decoded = "";
m_position = 0;
m_key = INITIAL_SEED;
m_buffer = {};
}
void SettingsHandler::AddSetting(std::string_view key, std::string_view value) void SettingsHandler::AddSetting(std::string_view key, std::string_view value)
{ {
WriteLine(fmt::format("{}={}\r\n", key, value)); WriteLine(fmt::format("{}={}\r\n", key, value));

View File

@ -30,14 +30,12 @@ public:
void AddSetting(std::string_view key, std::string_view value); void AddSetting(std::string_view key, std::string_view value);
const Buffer& GetBytes() const; const Buffer& GetBytes() const;
void SetBytes(const Buffer& buffer);
std::string GetValue(std::string_view key) const; std::string GetValue(std::string_view key) const;
void Decrypt();
void Reset();
static std::string GenerateSerialNumber(); static std::string GenerateSerialNumber();
private: private:
void Decrypt();
void WriteLine(std::string_view str); void WriteLine(std::string_view str);
void WriteByte(u8 b); void WriteByte(u8 b);

View File

@ -363,7 +363,6 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
auto entryPos = region_settings.find(SConfig::GetInstance().m_region); auto entryPos = region_settings.find(SConfig::GetInstance().m_region);
RegionSetting region_setting = entryPos->second; RegionSetting region_setting = entryPos->second;
Common::SettingsHandler gen;
std::string serno; std::string serno;
std::string model = "RVL-001(" + region_setting.area + ")"; std::string model = "RVL-001(" + region_setting.area + ")";
CreateSystemMenuTitleDirs(); CreateSystemMenuTitleDirs();
@ -377,9 +376,9 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
IOS::HLE::FS::Mode::Read); IOS::HLE::FS::Mode::Read);
if (file && file->Read(data.data(), data.size())) if (file && file->Read(data.data(), data.size()))
{ {
gen.SetBytes(data); Common::SettingsHandler settings_reader(data);
serno = gen.GetValue("SERNO"); serno = settings_reader.GetValue("SERNO");
model = gen.GetValue("MODEL"); model = settings_reader.GetValue("MODEL");
bool region_matches = false; bool region_matches = false;
if (Config::Get(Config::MAIN_OVERRIDE_REGION_SETTINGS)) if (Config::Get(Config::MAIN_OVERRIDE_REGION_SETTINGS))
@ -388,15 +387,16 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
} }
else else
{ {
const std::string code = gen.GetValue("CODE"); const std::string code = settings_reader.GetValue("CODE");
if (code.size() >= 2 && CodeRegion(code[1]) == SConfig::GetInstance().m_region) if (code.size() >= 2 && CodeRegion(code[1]) == SConfig::GetInstance().m_region)
region_matches = true; region_matches = true;
} }
if (region_matches) if (region_matches)
{ {
region_setting = RegionSetting{gen.GetValue("AREA"), gen.GetValue("VIDEO"), region_setting =
gen.GetValue("GAME"), gen.GetValue("CODE")}; RegionSetting{settings_reader.GetValue("AREA"), settings_reader.GetValue("VIDEO"),
settings_reader.GetValue("GAME"), settings_reader.GetValue("CODE")};
} }
else else
{ {
@ -404,8 +404,6 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
if (parenthesis_pos != std::string::npos) if (parenthesis_pos != std::string::npos)
model = model.substr(0, parenthesis_pos) + '(' + region_setting.area + ')'; model = model.substr(0, parenthesis_pos) + '(' + region_setting.area + ')';
} }
gen.Reset();
} }
} }
fs->Delete(IOS::SYSMENU_UID, IOS::SYSMENU_GID, settings_file_path); fs->Delete(IOS::SYSMENU_UID, IOS::SYSMENU_GID, settings_file_path);
@ -423,6 +421,7 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
INFO_LOG_FMT(BOOT, "Using serial number: {}", serno); INFO_LOG_FMT(BOOT, "Using serial number: {}", serno);
} }
Common::SettingsHandler gen;
gen.AddSetting("AREA", region_setting.area); gen.AddSetting("AREA", region_setting.area);
gen.AddSetting("MODEL", model); gen.AddSetting("MODEL", model);
gen.AddSetting("DVD", "0"); gen.AddSetting("DVD", "0");

View File

@ -138,8 +138,7 @@ IPCReply GetRealProductCode(Core::System& system, const IOCtlVRequest& request)
if (!file.ReadBytes(data.data(), data.size())) if (!file.ReadBytes(data.data(), data.size()))
return IPCReply(IPC_ENOENT); return IPCReply(IPC_ENOENT);
Common::SettingsHandler gen; Common::SettingsHandler gen(data);
gen.SetBytes(data);
const std::string code = gen.GetValue("CODE"); const std::string code = gen.GetValue("CODE");
const size_t length = std::min<size_t>(request.io_vectors[0].size, code.length()); const size_t length = std::min<size_t>(request.io_vectors[0].size, code.length());

View File

@ -75,35 +75,11 @@ TEST(SettingsHandlerTest, DecryptMultipleSettings)
EXPECT_EQ(handler.GetValue("foo"), "bar"); EXPECT_EQ(handler.GetValue("foo"), "bar");
} }
TEST(SettingsHandlerTest, SetBytesOverwritesExistingBuffer)
{
Common::SettingsHandler handler(BUFFER_A);
ASSERT_EQ(handler.GetValue("key"), "val");
ASSERT_EQ(handler.GetValue("foo"), "");
handler.SetBytes(BUFFER_B);
EXPECT_EQ(handler.GetValue("foo"), "bar");
EXPECT_EQ(handler.GetValue("key"), "");
}
TEST(SettingsHandlerTest, GetValueOnSameInstance) TEST(SettingsHandlerTest, GetValueOnSameInstance)
{ {
Common::SettingsHandler handler; Common::SettingsHandler handler;
handler.AddSetting("key", "val"); handler.AddSetting("key", "val");
EXPECT_EQ(handler.GetValue("key"), ""); EXPECT_EQ(handler.GetValue("key"), "");
Common::SettingsHandler::Buffer buffer = handler.GetBytes();
handler.SetBytes(buffer);
EXPECT_EQ(handler.GetValue("key"), "val");
}
TEST(SettingsHandlerTest, GetValueAfterReset)
{
Common::SettingsHandler handler(BUFFER_A);
ASSERT_EQ(handler.GetValue("key"), "val");
handler.Reset();
EXPECT_EQ(handler.GetValue("key"), "");
} }
TEST(SettingsHandlerTest, EncryptAddsLFOnNullChar) TEST(SettingsHandlerTest, EncryptAddsLFOnNullChar)