Qt/cheats: don't accept imported cheats unless valid

This commit is contained in:
Megamouse 2025-12-27 15:49:10 +01:00
parent 77aa5d4bbf
commit 739c178aac
3 changed files with 22 additions and 7 deletions

View File

@ -34,7 +34,7 @@ bool cheat_info::from_str(std::string_view cheat_line)
s64 val64 = 0;
if (cheat_vec.size() != 5 || !try_to_int64(&val64, cheat_vec[2], 0, cheat_type_max - 1))
{
log_cheat.fatal("Failed to parse cheat line");
log_cheat.error("Failed to parse cheat line: '%s'", cheat_line);
return false;
}

View File

@ -129,16 +129,27 @@ void cheat_engine::save() const
cheat_file.write(out.c_str(), out.size());
}
void cheat_engine::import_cheats_from_str(std::string_view str_cheats)
bool cheat_engine::import_cheats_from_str(std::string_view str_cheats)
{
const auto cheats_vec = fmt::split_sv(str_cheats, {"^^^"});
std::vector<cheat_info> valid_cheats;
for (const auto& cheat_line : cheats_vec)
{
cheat_info new_cheat;
if (new_cheat.from_str(cheat_line))
cheats[new_cheat.game][new_cheat.offset] = new_cheat;
if (!new_cheat.from_str(cheat_line))
return false;
valid_cheats.push_back(std::move(new_cheat));
}
for (const cheat_info& new_cheat : valid_cheats)
{
cheats[new_cheat.game][new_cheat.offset] = new_cheat;
}
return true;
}
std::string cheat_engine::export_cheats_to_str() const
@ -677,7 +688,7 @@ cheat_manager_dialog::cheat_manager_dialog(QWidget* parent)
{
const int row = sel->row();
if (rows.count(row))
if (rows.contains(row))
continue;
g_cheat.erase(tbl_cheats->item(row, cheat_table_columns::title)->text().toStdString(), tbl_cheats->item(row, cheat_table_columns::offset)->data(Qt::UserRole).toUInt());
@ -690,7 +701,11 @@ cheat_manager_dialog::cheat_manager_dialog(QWidget* parent)
connect(import_cheats, &QAction::triggered, [this]()
{
QClipboard* clipboard = QGuiApplication::clipboard();
g_cheat.import_cheats_from_str(clipboard->text().toStdString());
if (!g_cheat.import_cheats_from_str(clipboard->text().toStdString()))
{
QMessageBox::warning(this, tr("Failure"), tr("Failed to import cheats."));
return;
}
update_cheat_list();
});

View File

@ -25,7 +25,7 @@ public:
cheat_info* get(const std::string& game, const u32 offset);
bool erase(const std::string& game, const u32 offset);
void import_cheats_from_str(std::string_view str_cheats);
bool import_cheats_from_str(std::string_view str_cheats);
std::string export_cheats_to_str() const;
void save() const;