diff --git a/src/duckstation-qt/gamecheatsettingswidget.cpp b/src/duckstation-qt/gamecheatsettingswidget.cpp index 4d057dc47..2c4445372 100644 --- a/src/duckstation-qt/gamecheatsettingswidget.cpp +++ b/src/duckstation-qt/gamecheatsettingswidget.cpp @@ -655,7 +655,13 @@ void GameCheatSettingsWidget::editCode(const std::string_view code_name) new CheatCodeEditorDialog(this, getPathForSavingCheats(code->name), m_codes, *code, getGroupNames()); dlg->setAttribute(Qt::WA_DeleteOnClose); - connect(dlg, &QDialog::accepted, this, [this] { + connect(dlg, &QDialog::accepted, this, [this, dlg, old_name = code->name] { + if (CheatCodeEditorDialog::renameCodeInSettings(m_dialog->getSettingsInterface(), Cheats::CHEATS_CONFIG_SECTION, + old_name, dlg->getCode().name)) + { + m_dialog->saveGameSettings(); + } + reloadList(); QtHost::ReloadCheatFiles(m_dialog->getGameSerial(), false, true); }); @@ -835,6 +841,29 @@ CheatCodeEditorDialog::CheatCodeEditorDialog(QWidget* parent, std::string save_p CheatCodeEditorDialog::~CheatCodeEditorDialog() = default; +bool CheatCodeEditorDialog::renameCodeInSettings(SettingsInterface* si, const char* section, + const std::string& old_name, const std::string& new_name) +{ + if (old_name.empty() || old_name == new_name) + return false; + + bool changed = false; + if (si->RemoveFromStringList(section, Cheats::PATCH_ENABLE_CONFIG_KEY, old_name.c_str())) + { + si->AddToStringList(section, Cheats::PATCH_ENABLE_CONFIG_KEY, new_name.c_str()); + changed = true; + } + + if (u32 value; si->FindUIntValue(section, old_name.c_str(), &value)) + { + si->DeleteValue(section, old_name.c_str()); + si->SetUIntValue(section, new_name.c_str(), value); + changed = true; + } + + return changed; +} + void CheatCodeEditorDialog::onGroupSelectedIndexChanged(int index) { if (index != (m_ui.group->count() - 1)) diff --git a/src/duckstation-qt/gamecheatsettingswidget.h b/src/duckstation-qt/gamecheatsettingswidget.h index 07ad88941..585261624 100644 --- a/src/duckstation-qt/gamecheatsettingswidget.h +++ b/src/duckstation-qt/gamecheatsettingswidget.h @@ -23,6 +23,7 @@ namespace GameList { struct Entry; } +class SettingsInterface; class SettingsWindow; class QStandardItem; @@ -96,6 +97,12 @@ public: Cheats::CodeInfo code, const QStringList& group_names); ~CheatCodeEditorDialog() override; + const Cheats::CodeInfo& getCode() const { return m_code; } + + /// Moves the enable state and option value of a renamed code. Returns true if the settings were changed. + static bool renameCodeInSettings(SettingsInterface* si, const char* section, const std::string& old_name, + const std::string& new_name); + private: void onGroupSelectedIndexChanged(int index); void saveClicked(); diff --git a/src/duckstation-qt/gamepatchsettingswidget.cpp b/src/duckstation-qt/gamepatchsettingswidget.cpp index 6d1bd424a..44a43aa0b 100644 --- a/src/duckstation-qt/gamepatchsettingswidget.cpp +++ b/src/duckstation-qt/gamepatchsettingswidget.cpp @@ -121,7 +121,13 @@ void GamePatchSettingsWidget::editPatch(const std::string& name) dlg->setAttribute(Qt::WA_DeleteOnClose); dlg->setWindowTitle(tr("Patch Editor")); - connect(dlg, &QDialog::accepted, this, [this] { + connect(dlg, &QDialog::accepted, this, [this, dlg, old_name = name] { + if (CheatCodeEditorDialog::renameCodeInSettings(m_dialog->getSettingsInterface(), Cheats::PATCHES_CONFIG_SECTION, + old_name, dlg->getCode().name)) + { + m_dialog->saveGameSettings(); + } + reloadList(); QtHost::ReloadCheatFiles(m_dialog->getGameSerial(), false, true); });