From fb96ecb7da563d8b66ce4083db239da402e3aebc Mon Sep 17 00:00:00 2001 From: JosJuice Date: Mon, 21 Jun 2021 19:56:27 +0200 Subject: [PATCH] Move patch saving code to PatchEngine --- Source/Core/Core/PatchEngine.cpp | 53 ++++++++++++++++--- Source/Core/Core/PatchEngine.h | 5 +- .../Core/DolphinQt/Config/PatchesWidget.cpp | 44 ++------------- 3 files changed, 54 insertions(+), 48 deletions(-) diff --git a/Source/Core/Core/PatchEngine.cpp b/Source/Core/Core/PatchEngine.cpp index 0a815b37b1..afd20df61c 100644 --- a/Source/Core/Core/PatchEngine.cpp +++ b/Source/Core/Core/PatchEngine.cpp @@ -14,6 +14,8 @@ #include #include +#include + #include "Common/Assert.h" #include "Common/IniFile.h" #include "Common/StringUtil.h" @@ -43,8 +45,8 @@ const char* PatchTypeAsString(PatchType type) return s_patch_type_strings.at(static_cast(type)); } -void LoadPatchSection(const std::string& section, std::vector& patches, IniFile& globalIni, - IniFile& localIni) +void LoadPatchSection(const std::string& section, std::vector* patches, + const IniFile& globalIni, const IniFile& localIni) { const IniFile* inis[2] = {&globalIni, &localIni}; @@ -64,7 +66,7 @@ void LoadPatchSection(const std::string& section, std::vector& patches, I // Take care of the previous code if (!currentPatch.name.empty()) { - patches.push_back(currentPatch); + patches->push_back(currentPatch); } currentPatch.entries.clear(); @@ -110,19 +112,56 @@ void LoadPatchSection(const std::string& section, std::vector& patches, I if (!currentPatch.name.empty() && !currentPatch.entries.empty()) { - patches.push_back(currentPatch); + patches->push_back(currentPatch); } - ReadEnabledAndDisabled(*ini, section, &patches); + ReadEnabledAndDisabled(*ini, section, patches); if (ini == &globalIni) { - for (Patch& patch : patches) + for (Patch& patch : *patches) patch.default_enabled = patch.enabled; } } } +void SavePatchSection(IniFile* local_ini, const std::vector& patches) +{ + std::vector lines; + std::vector lines_enabled; + std::vector lines_disabled; + + for (const auto& patch : patches) + { + if (patch.enabled != patch.default_enabled) + (patch.enabled ? lines_enabled : lines_disabled).emplace_back('$' + patch.name); + + if (!patch.user_defined) + continue; + + lines.emplace_back('$' + patch.name); + + for (const auto& entry : patch.entries) + { + if (!entry.conditional) + { + lines.emplace_back(fmt::format("0x{:08X}:{}:0x{:08X}", entry.address, + PatchEngine::PatchTypeAsString(entry.type), entry.value)); + } + else + { + lines.emplace_back(fmt::format("0x{:08X}:{}:0x{:08X}:0x{:08X}", entry.address, + PatchEngine::PatchTypeAsString(entry.type), entry.value, + entry.comparand)); + } + } + } + + local_ini->SetLines("OnFrame_Enabled", lines_enabled); + local_ini->SetLines("OnFrame_Disabled", lines_disabled); + local_ini->SetLines("OnFrame", lines); +} + static void LoadSpeedhacks(const std::string& section, IniFile& ini) { std::vector keys; @@ -161,7 +200,7 @@ void LoadPatches() IniFile globalIni = SConfig::GetInstance().LoadDefaultGameIni(); IniFile localIni = SConfig::GetInstance().LoadLocalGameIni(); - LoadPatchSection("OnFrame", s_on_frame, globalIni, localIni); + LoadPatchSection("OnFrame", &s_on_frame, globalIni, localIni); // Check if I'm syncing Codes if (Config::Get(Config::SESSION_CODE_SYNC_OVERRIDE)) diff --git a/Source/Core/Core/PatchEngine.h b/Source/Core/Core/PatchEngine.h index 2c6c60d21d..f3c4d3d6ac 100644 --- a/Source/Core/Core/PatchEngine.h +++ b/Source/Core/Core/PatchEngine.h @@ -42,8 +42,9 @@ struct Patch const char* PatchTypeAsString(PatchType type); int GetSpeedhackCycles(const u32 addr); -void LoadPatchSection(const std::string& section, std::vector& patches, IniFile& globalIni, - IniFile& localIni); +void LoadPatchSection(const std::string& section, std::vector* patches, + const IniFile& globalIni, const IniFile& localIni); +void SavePatchSection(IniFile* local_ini, const std::vector& patches); void LoadPatches(); bool ApplyFramePatches(); void Shutdown(); diff --git a/Source/Core/DolphinQt/Config/PatchesWidget.cpp b/Source/Core/DolphinQt/Config/PatchesWidget.cpp index fd16714d94..2c08d932c0 100644 --- a/Source/Core/DolphinQt/Config/PatchesWidget.cpp +++ b/Source/Core/DolphinQt/Config/PatchesWidget.cpp @@ -7,8 +7,6 @@ #include #include -#include - #include "Common/FileUtil.h" #include "Common/IniFile.h" #include "Common/StringUtil.h" @@ -28,7 +26,7 @@ PatchesWidget::PatchesWidget(const UICommon::GameFile& game) IniFile game_ini_default = SConfig::GetInstance().LoadDefaultGameIni(m_game_id, m_game_revision); - PatchEngine::LoadPatchSection("OnFrame", m_patches, game_ini_default, game_ini_local); + PatchEngine::LoadPatchSection("OnFrame", &m_patches, game_ini_default, game_ini_local); CreateWidgets(); ConnectWidgets(); @@ -128,44 +126,12 @@ void PatchesWidget::OnRemove() void PatchesWidget::SavePatches() { - std::vector lines; - std::vector lines_enabled; - std::vector lines_disabled; - - for (const auto& patch : m_patches) - { - if (patch.enabled != patch.default_enabled) - (patch.enabled ? lines_enabled : lines_disabled).emplace_back('$' + patch.name); - - if (!patch.user_defined) - continue; - - lines.emplace_back('$' + patch.name); - - for (const auto& entry : patch.entries) - { - if (!entry.conditional) - { - lines.emplace_back(fmt::format("0x{:08X}:{}:0x{:08X}", entry.address, - PatchEngine::PatchTypeAsString(entry.type), entry.value)); - } - else - { - lines.emplace_back(fmt::format("0x{:08X}:{}:0x{:08X}:0x{:08X}", entry.address, - PatchEngine::PatchTypeAsString(entry.type), entry.value, - entry.comparand)); - } - } - } + const std::string ini_path = File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"; IniFile game_ini_local; - game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"); - - game_ini_local.SetLines("OnFrame_Enabled", lines_enabled); - game_ini_local.SetLines("OnFrame_Disabled", lines_disabled); - game_ini_local.SetLines("OnFrame", lines); - - game_ini_local.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"); + game_ini_local.Load(ini_path); + PatchEngine::SavePatchSection(&game_ini_local, m_patches); + game_ini_local.Save(ini_path); } void PatchesWidget::Update()