Skip to content

Commit

Permalink
Merge pull request #12737 from nlebeck/settingshandler-split
Browse files Browse the repository at this point in the history
Eliminate SettingsHandler's `SetBytes` and `Reset` methods
  • Loading branch information
JosJuice committed May 5, 2024
2 parents 485bdba + 36cdb4a commit 2c91367
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 57 deletions.
23 changes: 4 additions & 19 deletions Source/Core/Common/SettingsHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,21 @@

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
{
return m_buffer;
}

void SettingsHandler::SetBytes(const Buffer& buffer)
{
Reset();
m_buffer = buffer;
Decrypt();
}

std::string SettingsHandler::GetValue(std::string_view key) const
{
constexpr char delim[] = "\n";
Expand Down Expand Up @@ -84,14 +77,6 @@ void SettingsHandler::Decrypt()
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)
{
WriteLine(fmt::format("{}={}\r\n", key, value));
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/Common/SettingsHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ class SettingsHandler
void AddSetting(std::string_view key, std::string_view value);

const Buffer& GetBytes() const;
void SetBytes(const Buffer& buffer);
std::string GetValue(std::string_view key) const;

void Decrypt();
void Reset();
static std::string GenerateSerialNumber();

private:
void Decrypt();
void WriteLine(std::string_view str);
void WriteByte(u8 b);

Expand Down
17 changes: 8 additions & 9 deletions Source/Core/Core/Boot/Boot_BS2Emu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,6 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
auto entryPos = region_settings.find(SConfig::GetInstance().m_region);
RegionSetting region_setting = entryPos->second;

Common::SettingsHandler gen;
std::string serno;
std::string model = "RVL-001(" + region_setting.area + ")";
CreateSystemMenuTitleDirs();
Expand All @@ -377,9 +376,9 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
IOS::HLE::FS::Mode::Read);
if (file && file->Read(data.data(), data.size()))
{
gen.SetBytes(data);
serno = gen.GetValue("SERNO");
model = gen.GetValue("MODEL");
Common::SettingsHandler settings_reader(data);
serno = settings_reader.GetValue("SERNO");
model = settings_reader.GetValue("MODEL");

bool region_matches = false;
if (Config::Get(Config::MAIN_OVERRIDE_REGION_SETTINGS))
Expand All @@ -388,24 +387,23 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
}
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)
region_matches = true;
}

if (region_matches)
{
region_setting = RegionSetting{gen.GetValue("AREA"), gen.GetValue("VIDEO"),
gen.GetValue("GAME"), gen.GetValue("CODE")};
region_setting =
RegionSetting{settings_reader.GetValue("AREA"), settings_reader.GetValue("VIDEO"),
settings_reader.GetValue("GAME"), settings_reader.GetValue("CODE")};
}
else
{
const size_t parenthesis_pos = model.find('(');
if (parenthesis_pos != std::string::npos)
model = model.substr(0, parenthesis_pos) + '(' + region_setting.area + ')';
}

gen.Reset();
}
}
fs->Delete(IOS::SYSMENU_UID, IOS::SYSMENU_GID, settings_file_path);
Expand All @@ -423,6 +421,7 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
INFO_LOG_FMT(BOOT, "Using serial number: {}", serno);
}

Common::SettingsHandler gen;
gen.AddSetting("AREA", region_setting.area);
gen.AddSetting("MODEL", model);
gen.AddSetting("DVD", "0");
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/Core/IOS/DolphinDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ IPCReply GetRealProductCode(Core::System& system, const IOCtlVRequest& request)
if (!file.ReadBytes(data.data(), data.size()))
return IPCReply(IPC_ENOENT);

Common::SettingsHandler gen;
gen.SetBytes(data);
Common::SettingsHandler gen(data);
const std::string code = gen.GetValue("CODE");

const size_t length = std::min<size_t>(request.io_vectors[0].size, code.length());
Expand Down
24 changes: 0 additions & 24 deletions Source/UnitTests/Common/SettingsHandlerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,35 +75,11 @@ TEST(SettingsHandlerTest, DecryptMultipleSettings)
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)
{
Common::SettingsHandler handler;
handler.AddSetting("key", "val");
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)
Expand Down

0 comments on commit 2c91367

Please sign in to comment.