Move patch saving code to PatchEngine

This commit is contained in:
JosJuice 2021-06-21 19:56:27 +02:00
parent 4816195366
commit fb96ecb7da
3 changed files with 54 additions and 48 deletions

View File

@ -14,6 +14,8 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <fmt/format.h>
#include "Common/Assert.h" #include "Common/Assert.h"
#include "Common/IniFile.h" #include "Common/IniFile.h"
#include "Common/StringUtil.h" #include "Common/StringUtil.h"
@ -43,8 +45,8 @@ const char* PatchTypeAsString(PatchType type)
return s_patch_type_strings.at(static_cast<int>(type)); return s_patch_type_strings.at(static_cast<int>(type));
} }
void LoadPatchSection(const std::string& section, std::vector<Patch>& patches, IniFile& globalIni, void LoadPatchSection(const std::string& section, std::vector<Patch>* patches,
IniFile& localIni) const IniFile& globalIni, const IniFile& localIni)
{ {
const IniFile* inis[2] = {&globalIni, &localIni}; const IniFile* inis[2] = {&globalIni, &localIni};
@ -64,7 +66,7 @@ void LoadPatchSection(const std::string& section, std::vector<Patch>& patches, I
// Take care of the previous code // Take care of the previous code
if (!currentPatch.name.empty()) if (!currentPatch.name.empty())
{ {
patches.push_back(currentPatch); patches->push_back(currentPatch);
} }
currentPatch.entries.clear(); currentPatch.entries.clear();
@ -110,19 +112,56 @@ void LoadPatchSection(const std::string& section, std::vector<Patch>& patches, I
if (!currentPatch.name.empty() && !currentPatch.entries.empty()) 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) if (ini == &globalIni)
{ {
for (Patch& patch : patches) for (Patch& patch : *patches)
patch.default_enabled = patch.enabled; patch.default_enabled = patch.enabled;
} }
} }
} }
void SavePatchSection(IniFile* local_ini, const std::vector<Patch>& patches)
{
std::vector<std::string> lines;
std::vector<std::string> lines_enabled;
std::vector<std::string> 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) static void LoadSpeedhacks(const std::string& section, IniFile& ini)
{ {
std::vector<std::string> keys; std::vector<std::string> keys;
@ -161,7 +200,7 @@ void LoadPatches()
IniFile globalIni = SConfig::GetInstance().LoadDefaultGameIni(); IniFile globalIni = SConfig::GetInstance().LoadDefaultGameIni();
IniFile localIni = SConfig::GetInstance().LoadLocalGameIni(); 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 // Check if I'm syncing Codes
if (Config::Get(Config::SESSION_CODE_SYNC_OVERRIDE)) if (Config::Get(Config::SESSION_CODE_SYNC_OVERRIDE))

View File

@ -42,8 +42,9 @@ struct Patch
const char* PatchTypeAsString(PatchType type); const char* PatchTypeAsString(PatchType type);
int GetSpeedhackCycles(const u32 addr); int GetSpeedhackCycles(const u32 addr);
void LoadPatchSection(const std::string& section, std::vector<Patch>& patches, IniFile& globalIni, void LoadPatchSection(const std::string& section, std::vector<Patch>* patches,
IniFile& localIni); const IniFile& globalIni, const IniFile& localIni);
void SavePatchSection(IniFile* local_ini, const std::vector<Patch>& patches);
void LoadPatches(); void LoadPatches();
bool ApplyFramePatches(); bool ApplyFramePatches();
void Shutdown(); void Shutdown();

View File

@ -7,8 +7,6 @@
#include <QListWidget> #include <QListWidget>
#include <QPushButton> #include <QPushButton>
#include <fmt/format.h>
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/IniFile.h" #include "Common/IniFile.h"
#include "Common/StringUtil.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); 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(); CreateWidgets();
ConnectWidgets(); ConnectWidgets();
@ -128,44 +126,12 @@ void PatchesWidget::OnRemove()
void PatchesWidget::SavePatches() void PatchesWidget::SavePatches()
{ {
std::vector<std::string> lines; const std::string ini_path = File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini";
std::vector<std::string> lines_enabled;
std::vector<std::string> 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));
}
}
}
IniFile game_ini_local; IniFile game_ini_local;
game_ini_local.Load(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.SetLines("OnFrame_Enabled", lines_enabled); game_ini_local.Save(ini_path);
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");
} }
void PatchesWidget::Update() void PatchesWidget::Update()