Merge pull request #10011 from JosJuice/android-cheats-preparation

Split out code for serializing/deserializing cheat lines
This commit is contained in:
JosJuice
2021-09-08 21:36:56 +02:00
committed by GitHub
9 changed files with 189 additions and 189 deletions

View File

@ -32,16 +32,10 @@ void CheatCodeEditor::SetARCode(ActionReplay::ARCode* code)
{
m_name_edit->setText(QString::fromStdString(code->name));
QString s;
m_code_edit->clear();
for (ActionReplay::AREntry& e : code->ops)
{
s += QStringLiteral("%1 %2\n")
.arg(e.cmd_addr, 8, 16, QLatin1Char('0'))
.arg(e.value, 8, 16, QLatin1Char('0'));
}
m_code_edit->setText(s);
m_code_edit->append(QString::fromStdString(ActionReplay::SerializeLine(e)));
m_creator_label->setHidden(true);
m_creator_edit->setHidden(true);
@ -57,14 +51,10 @@ void CheatCodeEditor::SetGeckoCode(Gecko::GeckoCode* code)
m_name_edit->setText(QString::fromStdString(code->name));
m_creator_edit->setText(QString::fromStdString(code->creator));
QString code_string;
m_code_edit->clear();
for (const auto& c : code->codes)
code_string += QStringLiteral("%1 %2\n")
.arg(c.address, 8, 16, QLatin1Char('0'))
.arg(c.data, 8, 16, QLatin1Char('0'));
m_code_edit->setText(code_string);
m_code_edit->append(QString::fromStdString(c.original_line));
QString notes_string;
for (const auto& line : code->notes)
@ -135,7 +125,6 @@ bool CheatCodeEditor::AcceptAR()
if (line.isEmpty())
continue;
if (i == 0 && line[0] == u'$')
{
if (name.isEmpty())
@ -144,40 +133,17 @@ bool CheatCodeEditor::AcceptAR()
continue;
}
QStringList values = line.split(QLatin1Char{' '});
const auto parse_result = ActionReplay::DeserializeLine(line.toStdString());
bool good = true;
u32 addr = 0;
u32 value = 0;
if (values.size() == 2)
if (std::holds_alternative<ActionReplay::AREntry>(parse_result))
{
addr = values[0].toUInt(&good, 16);
if (good)
value = values[1].toUInt(&good, 16);
if (good)
entries.push_back(ActionReplay::AREntry(addr, value));
entries.push_back(std::get<ActionReplay::AREntry>(parse_result));
}
else if (std::holds_alternative<ActionReplay::EncryptedLine>(parse_result))
{
encrypted_lines.emplace_back(std::get<ActionReplay::EncryptedLine>(parse_result));
}
else
{
QStringList blocks = line.split(QLatin1Char{'-'});
if (blocks.size() == 3 && blocks[0].size() == 4 && blocks[1].size() == 4 &&
blocks[2].size() == 5)
{
encrypted_lines.emplace_back(blocks[0].toStdString() + blocks[1].toStdString() +
blocks[2].toStdString());
}
else
{
good = false;
}
}
if (!good)
{
auto result = ModalMessageBox::warning(
this, tr("Parsing Error"),
@ -260,20 +226,11 @@ bool CheatCodeEditor::AcceptGecko()
continue;
}
QStringList values = line.split(QLatin1Char{' '});
bool good = values.size() == 2;
u32 addr = 0;
u32 value = 0;
if (good)
addr = values[0].toUInt(&good, 16);
if (good)
value = values[1].toUInt(&good, 16);
if (!good)
if (std::optional<Gecko::GeckoCode::Code> c = Gecko::DeserializeLine(line.toStdString()))
{
entries.push_back(*c);
}
else
{
auto result = ModalMessageBox::warning(
this, tr("Parsing Error"),
@ -286,15 +243,6 @@ bool CheatCodeEditor::AcceptGecko()
if (result == QMessageBox::Abort)
return false;
}
else
{
Gecko::GeckoCode::Code c;
c.address = addr;
c.data = value;
c.original_line = line.toStdString();
entries.push_back(c);
}
}
if (entries.empty())

View File

@ -167,11 +167,7 @@ void GeckoCodeWidget::OnSelectionChanged()
m_code_view->clear();
for (const auto& c : code.codes)
{
m_code_view->append(QStringLiteral("%1 %2")
.arg(c.address, 8, 16, QLatin1Char('0'))
.arg(c.data, 8, 16, QLatin1Char('0')));
}
m_code_view->append(QString::fromStdString(c.original_line));
}
void GeckoCodeWidget::OnItemChanged(QListWidgetItem* item)

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()