| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/ARCodeWidget.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <QCursor> |
| 10 | #include <QHBoxLayout> |
| 11 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 12 | #include <QIcon> |
| 13 | #endif // USE_RETRO_ACHIEVEMENTS |
| 14 | #include <QListWidget> |
| 15 | #include <QMenu> |
| 16 | #include <QPushButton> |
| 17 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 18 | #include <QStyle> |
| 19 | #endif // USE_RETRO_ACHIEVEMENTS |
| 20 | #include <QVBoxLayout> |
| 21 | |
| 22 | #include "Common/FileUtil.h" |
| 23 | #include "Common/IniFile.h" |
| 24 | |
| 25 | #include "Core/AchievementManager.h" |
| 26 | #include "Core/ActionReplay.h" |
| 27 | #include "Core/ConfigManager.h" |
| 28 | |
| 29 | #include "DolphinQt/Config/CheatCodeEditor.h" |
| 30 | #include "DolphinQt/Config/CheatWarningWidget.h" |
| 31 | #include "DolphinQt/Config/HardcoreWarningWidget.h" |
| 32 | #include "DolphinQt/QtUtils/NonDefaultQPushButton.h" |
| 33 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 34 | #include "DolphinQt/Settings.h" |
| 35 | #endif // USE_RETRO_ACHIEVEMENTS |
| 36 | |
| 37 | ARCodeWidget::ARCodeWidget(std::string game_id, u16 game_revision, bool restart_required) |
| 38 | : m_game_id(std::move(game_id)), m_game_revision(game_revision), |
| 39 | m_restart_required(restart_required) |
| 40 | { |
| 41 | CreateWidgets(); |
| 42 | ConnectWidgets(); |
| 43 | |
| 44 | LoadCodes(); |
| 45 | } |
| 46 | |
| 47 | ARCodeWidget::~ARCodeWidget() = default; |
| 48 | |
| 49 | void ARCodeWidget::ChangeGame(std::string game_id, const u16 game_revision) |
| 50 | { |
| 51 | m_game_id = std::move(game_id); |
| 52 | m_game_revision = game_revision; |
| 53 | m_restart_required = false; |
| 54 | |
| 55 | m_ar_codes.clear(); |
| 56 | |
| 57 | // If a CheatCodeEditor is open, it's now trying to add or edit a code in the previous game's code |
| 58 | // list which is no longer loaded. Letting the user save the code wouldn't make sense, so close |
| 59 | // the dialog instead. |
| 60 | m_cheat_code_editor->reject(); |
| 61 | |
| 62 | LoadCodes(); |
| 63 | } |
| 64 | |
| 65 | void ARCodeWidget::CreateWidgets() |
| 66 | { |
| 67 | m_warning = new CheatWarningWidget(m_game_id, m_restart_required, this); |
| 68 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 69 | m_hc_warning = new HardcoreWarningWidget(this); |
| 70 | #endif // USE_RETRO_ACHIEVEMENTS |
| 71 | m_code_list = new QListWidget; |
| 72 | m_code_add = new NonDefaultQPushButton(tr("&Add New Code...")); |
| 73 | m_code_edit = new NonDefaultQPushButton(tr("&Edit Code...")); |
| 74 | m_code_remove = new NonDefaultQPushButton(tr("&Remove Code")); |
| 75 | |
| 76 | m_cheat_code_editor = new CheatCodeEditor(this); |
| 77 | |
| 78 | m_code_list->setContextMenuPolicy(Qt::CustomContextMenu); |
| 79 | |
| 80 | auto* button_layout = new QHBoxLayout; |
| 81 | |
| 82 | button_layout->addWidget(m_code_add); |
| 83 | button_layout->addWidget(m_code_edit); |
| 84 | button_layout->addWidget(m_code_remove); |
| 85 | |
| 86 | auto* const layout = new QVBoxLayout{this}; |
| 87 | |
| 88 | layout->addWidget(m_warning); |
| 89 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 90 | layout->addWidget(m_hc_warning); |
| 91 | #endif // USE_RETRO_ACHIEVEMENTS |
| 92 | layout->addWidget(m_code_list); |
| 93 | layout->addLayout(button_layout); |
| 94 | } |
| 95 | |
| 96 | void ARCodeWidget::ConnectWidgets() |
| 97 | { |
| 98 | connect(m_warning, &CheatWarningWidget::OpenCheatEnableSettings, this, |
| 99 | &ARCodeWidget::OpenGeneralSettings); |
| 100 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 101 | connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this, |
| 102 | &ARCodeWidget::OpenAchievementSettings); |
| 103 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, &ARCodeWidget::UpdateList); |
| 104 | #endif // USE_RETRO_ACHIEVEMENTS |
| 105 | |
| 106 | connect(m_code_list, &QListWidget::itemChanged, this, &ARCodeWidget::OnItemChanged); |
| 107 | connect(m_code_list, &QListWidget::itemSelectionChanged, this, &ARCodeWidget::OnSelectionChanged); |
| 108 | connect(m_code_list->model(), &QAbstractItemModel::rowsMoved, this, |
| 109 | &ARCodeWidget::OnListReordered); |
| 110 | connect(m_code_list, &QListWidget::customContextMenuRequested, this, |
| 111 | &ARCodeWidget::OnContextMenuRequested); |
| 112 | |
| 113 | connect(m_code_add, &QPushButton::clicked, this, &ARCodeWidget::OnCodeAddClicked); |
| 114 | connect(m_code_edit, &QPushButton::clicked, this, &ARCodeWidget::OnCodeEditClicked); |
| 115 | connect(m_code_remove, &QPushButton::clicked, this, &ARCodeWidget::OnCodeRemoveClicked); |
| 116 | } |
| 117 | |
| 118 | void ARCodeWidget::OnItemChanged(QListWidgetItem* item) |
| 119 | { |
| 120 | m_ar_codes[m_code_list->row(item)].enabled = (item->checkState() == Qt::Checked); |
| 121 | |
| 122 | if (!m_restart_required) |
| 123 | ActionReplay::ApplyCodes(m_ar_codes, m_game_id, m_game_revision); |
| 124 | |
| 125 | UpdateList(); |
| 126 | SaveCodes(); |
| 127 | } |
| 128 | |
| 129 | void ARCodeWidget::OnContextMenuRequested() |
| 130 | { |
| 131 | QMenu menu; |
| 132 | |
| 133 | menu.addAction(tr("Sort Alphabetically"), this, &ARCodeWidget::SortAlphabetically); |
| 134 | menu.addAction(tr("Show Enabled Codes First"), this, &ARCodeWidget::SortEnabledCodesFirst); |
| 135 | menu.addAction(tr("Show Disabled Codes First"), this, &ARCodeWidget::SortDisabledCodesFirst); |
| 136 | |
| 137 | menu.exec(QCursor::pos()); |
| 138 | } |
| 139 | |
| 140 | void ARCodeWidget::SortAlphabetically() |
| 141 | { |
| 142 | m_code_list->sortItems(); |
| 143 | OnListReordered(); |
| 144 | } |
| 145 | |
| 146 | void ARCodeWidget::SortEnabledCodesFirst() |
| 147 | { |
| 148 | std::ranges::stable_partition(m_ar_codes, std::identity{}, &ActionReplay::ARCode::enabled); |
| 149 | UpdateList(); |
| 150 | SaveCodes(); |
| 151 | } |
| 152 | |
| 153 | void ARCodeWidget::SortDisabledCodesFirst() |
| 154 | { |
| 155 | std::ranges::stable_partition(m_ar_codes, std::logical_not{}, &ActionReplay::ARCode::enabled); |
| 156 | UpdateList(); |
| 157 | SaveCodes(); |
| 158 | } |
| 159 | |
| 160 | void ARCodeWidget::OnListReordered() |
| 161 | { |
| 162 | // Reorder codes based on the indices of table item |
| 163 | std::vector<ActionReplay::ARCode> codes; |
| 164 | codes.reserve(m_ar_codes.size()); |
| 165 | |
| 166 | for (int i = 0; i < m_code_list->count(); i++) |
| 167 | { |
| 168 | const int index = m_code_list->item(i)->data(Qt::UserRole).toInt(); |
| 169 | |
| 170 | codes.push_back(std::move(m_ar_codes[index])); |
| 171 | } |
| 172 | |
| 173 | m_ar_codes = std::move(codes); |
| 174 | |
| 175 | SaveCodes(); |
| 176 | } |
| 177 | |
| 178 | void ARCodeWidget::OnSelectionChanged() |
| 179 | { |
| 180 | const QList<QListWidgetItem*> items = m_code_list->selectedItems(); |
| 181 | const bool empty = items.empty(); |
| 182 | |
| 183 | m_code_edit->setDisabled(empty); |
| 184 | m_code_remove->setDisabled(empty); |
| 185 | |
| 186 | if (empty) |
| 187 | return; |
| 188 | |
| 189 | const QListWidgetItem* const selected = items[0]; |
| 190 | |
| 191 | const bool user_defined = m_ar_codes[m_code_list->row(selected)].user_defined; |
| 192 | |
| 193 | m_code_remove->setEnabled(user_defined); |
| 194 | m_code_edit->setText(user_defined ? tr("&Edit Code...") : tr("Clone and &Edit Code...")); |
| 195 | } |
| 196 | |
| 197 | void ARCodeWidget::UpdateList() |
| 198 | { |
| 199 | m_code_list->clear(); |
| 200 | |
| 201 | for (size_t i = 0; i < m_ar_codes.size(); i++) |
| 202 | { |
| 203 | const auto& ar = m_ar_codes[i]; |
| 204 | auto* item = new QListWidgetItem(QString::fromStdString(ar.name) |
| 205 | .replace(QStringLiteral("<"), QChar::fromLatin1('<')) |
| 206 | .replace(QStringLiteral(">"), QChar::fromLatin1('>'))); |
| 207 | |
| 208 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | |
| 209 | Qt::ItemIsDragEnabled); |
| 210 | item->setCheckState(ar.enabled ? Qt::Checked : Qt::Unchecked); |
| 211 | item->setData(Qt::UserRole, static_cast<int>(i)); |
| 212 | |
| 213 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 214 | const AchievementManager& achievement_manager = AchievementManager::GetInstance(); |
| 215 | |
| 216 | if (achievement_manager.IsHardcoreModeActive()) |
| 217 | { |
| 218 | const QIcon approved_icon = style()->standardIcon(QStyle::SP_DialogYesButton); |
| 219 | const QIcon warning_icon = style()->standardIcon(QStyle::SP_MessageBoxWarning); |
| 220 | |
| 221 | if (achievement_manager.IsApprovedARCode(ar, m_game_id, m_game_revision)) |
| 222 | item->setIcon(approved_icon); |
| 223 | else |
| 224 | item->setIcon(warning_icon); |
| 225 | } |
| 226 | #endif // USE_RETRO_ACHIEVEMENTS |
| 227 | |
| 228 | m_code_list->addItem(item); |
| 229 | } |
| 230 | |
| 231 | m_code_list->setDragDropMode(QAbstractItemView::InternalMove); |
| 232 | } |
| 233 | |
| 234 | void ARCodeWidget::LoadCodes() |
| 235 | { |
| 236 | if (!m_game_id.empty()) |
| 237 | { |
| 238 | Common::IniFile game_ini_local; |
| 239 | |
| 240 | // We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI |
| 241 | // will always be stored in GS/${GAMEID}.ini |
| 242 | game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"); |
| 243 | |
| 244 | const Common::IniFile game_ini_default = |
| 245 | SConfig::LoadDefaultGameIni(m_game_id, m_game_revision); |
| 246 | m_ar_codes = ActionReplay::LoadCodes(game_ini_default, game_ini_local); |
| 247 | } |
| 248 | |
| 249 | m_code_list->setEnabled(!m_game_id.empty()); |
| 250 | m_code_add->setEnabled(!m_game_id.empty()); |
| 251 | m_code_edit->setEnabled(false); |
| 252 | m_code_remove->setEnabled(false); |
| 253 | |
| 254 | UpdateList(); |
| 255 | } |
| 256 | |
| 257 | void ARCodeWidget::SaveCodes() |
| 258 | { |
| 259 | if (m_game_id.empty()) |
| 260 | return; |
| 261 | |
| 262 | const auto ini_path = |
| 263 | std::string(File::GetUserPath(D_GAMESETTINGS_IDX)).append(m_game_id).append(".ini"); |
| 264 | |
| 265 | Common::IniFile game_ini_local; |
| 266 | game_ini_local.Load(ini_path); |
| 267 | ActionReplay::SaveCodes(&game_ini_local, m_ar_codes); |
| 268 | game_ini_local.Save(ini_path); |
| 269 | } |
| 270 | |
| 271 | void ARCodeWidget::AddCode(ActionReplay::ARCode code) |
| 272 | { |
| 273 | m_ar_codes.push_back(std::move(code)); |
| 274 | |
| 275 | UpdateList(); |
| 276 | SaveCodes(); |
| 277 | } |
| 278 | |
| 279 | void ARCodeWidget::OnCodeAddClicked() |
| 280 | { |
| 281 | ActionReplay::ARCode ar; |
| 282 | ar.enabled = true; |
| 283 | |
| 284 | m_cheat_code_editor->SetARCode(&ar); |
| 285 | if (m_cheat_code_editor->exec() == QDialog::Rejected) |
| 286 | return; |
| 287 | |
| 288 | m_ar_codes.push_back(std::move(ar)); |
| 289 | |
| 290 | UpdateList(); |
| 291 | SaveCodes(); |
| 292 | } |
| 293 | |
| 294 | void ARCodeWidget::OnCodeEditClicked() |
| 295 | { |
| 296 | const auto items = m_code_list->selectedItems(); |
| 297 | if (items.empty()) |
| 298 | return; |
| 299 | |
| 300 | const auto* const selected = items[0]; |
| 301 | const bool enabled = selected->checkState() == Qt::Checked; |
| 302 | |
| 303 | auto& current_ar = m_ar_codes[m_code_list->row(selected)]; |
| 304 | |
| 305 | if (current_ar.user_defined) |
| 306 | { |
| 307 | m_cheat_code_editor->SetARCode(¤t_ar); |
| 308 | if (m_cheat_code_editor->exec() == QDialog::Rejected) |
| 309 | return; |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | ActionReplay::ARCode ar = current_ar; |
| 314 | m_cheat_code_editor->SetARCode(&ar); |
| 315 | if (m_cheat_code_editor->exec() == QDialog::Rejected) |
| 316 | return; |
| 317 | |
| 318 | m_ar_codes.push_back(std::move(ar)); |
| 319 | } |
| 320 | |
| 321 | SaveCodes(); |
| 322 | UpdateList(); |
| 323 | |
| 324 | if (!m_restart_required && enabled) |
| 325 | ActionReplay::ApplyCodes(m_ar_codes, m_game_id, m_game_revision); |
| 326 | } |
| 327 | |
| 328 | void ARCodeWidget::OnCodeRemoveClicked() |
| 329 | { |
| 330 | auto items = m_code_list->selectedItems(); |
| 331 | |
| 332 | if (items.empty()) |
| 333 | return; |
| 334 | |
| 335 | const auto* selected = items[0]; |
| 336 | |
| 337 | m_ar_codes.erase(m_ar_codes.begin() + m_code_list->row(selected)); |
| 338 | |
| 339 | SaveCodes(); |
| 340 | UpdateList(); |
| 341 | |
| 342 | m_code_remove->setEnabled(false); |
| 343 | } |