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 <vector>
#include <fmt/format.h>
#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<int>(type));
}
void LoadPatchSection(const std::string& section, std::vector<Patch>& patches, IniFile& globalIni,
IniFile& localIni)
void LoadPatchSection(const std::string& section, std::vector<Patch>* 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<Patch>& 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<Patch>& 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<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)
{
std::vector<std::string> 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))

View File

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

View File

@ -7,8 +7,6 @@
#include <QListWidget>
#include <QPushButton>
#include <fmt/format.h>
#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<std::string> lines;
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));
}
}
}
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()