| 1 | // Copyright 2017 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Config/GeckoCodeWidget.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <utility> |
| 8 | |
| 9 | #include <QCursor> |
| 10 | #include <QFontDatabase> |
| 11 | #include <QFormLayout> |
| 12 | #include <QHBoxLayout> |
| 13 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 14 | #include <QIcon> |
| 15 | #endif // USE_RETRO_ACHIEVEMENTS |
| 16 | #include <QLabel> |
| 17 | #include <QListWidget> |
| 18 | #include <QMenu> |
| 19 | #include <QPushButton> |
| 20 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 21 | #include <QStyle> |
| 22 | #endif // USE_RETRO_ACHIEVEMENTS |
| 23 | #include <QTextEdit> |
| 24 | #include <QVBoxLayout> |
| 25 | |
| 26 | #include "Common/FileUtil.h" |
| 27 | #include "Common/IniFile.h" |
| 28 | |
| 29 | #include "Core/AchievementManager.h" |
| 30 | #include "Core/ConfigManager.h" |
| 31 | #include "Core/GeckoCode.h" |
| 32 | #include "Core/GeckoCodeConfig.h" |
| 33 | |
| 34 | #include "DolphinQt/Config/CheatCodeEditor.h" |
| 35 | #include "DolphinQt/Config/CheatWarningWidget.h" |
| 36 | #include "DolphinQt/Config/HardcoreWarningWidget.h" |
| 37 | #include "DolphinQt/QtUtils/ModalMessageBox.h" |
| 38 | #include "DolphinQt/QtUtils/NonDefaultQPushButton.h" |
| 39 | #include "DolphinQt/QtUtils/QtUtils.h" |
| 40 | #include "DolphinQt/QtUtils/WrapInScrollArea.h" |
| 41 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 42 | #include "DolphinQt/Settings.h" |
| 43 | #endif // USE_RETRO_ACHIEVEMENTS |
| 44 | |
| 45 | GeckoCodeWidget::GeckoCodeWidget(std::string game_id, std::string gametdb_id, u16 game_revision, |
| 46 | bool restart_required) |
| 47 | : m_game_id(std::move(game_id)), m_gametdb_id(std::move(gametdb_id)), |
| 48 | m_game_revision(game_revision), m_restart_required(restart_required) |
| 49 | { |
| 50 | CreateWidgets(); |
| 51 | ConnectWidgets(); |
| 52 | |
| 53 | LoadCodes(); |
| 54 | } |
| 55 | |
| 56 | void GeckoCodeWidget::ChangeGame(std::string game_id, std::string gametdb_id, |
| 57 | const u16 game_revision) |
| 58 | { |
| 59 | m_game_id = std::move(game_id); |
| 60 | m_gametdb_id = std::move(gametdb_id); |
| 61 | m_game_revision = game_revision; |
| 62 | m_restart_required = false; |
| 63 | |
| 64 | m_gecko_codes.clear(); |
| 65 | m_code_list->clear(); |
| 66 | m_name_label->clear(); |
| 67 | m_creator_label->clear(); |
| 68 | m_code_description->clear(); |
| 69 | m_code_view->clear(); |
| 70 | |
| 71 | // If a CheatCodeEditor is open, it's now trying to add or edit a code in the previous game's code |
| 72 | // list which is no longer loaded. Letting the user save the code wouldn't make sense, so close |
| 73 | // the dialog instead. |
| 74 | m_cheat_code_editor->reject(); |
| 75 | |
| 76 | LoadCodes(); |
| 77 | } |
| 78 | |
| 79 | GeckoCodeWidget::~GeckoCodeWidget() = default; |
| 80 | |
| 81 | void GeckoCodeWidget::CreateWidgets() |
| 82 | { |
| 83 | m_warning = new CheatWarningWidget(m_game_id, m_restart_required, this); |
| 84 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 85 | m_hc_warning = new HardcoreWarningWidget(this); |
| 86 | #endif // USE_RETRO_ACHIEVEMENTS |
| 87 | m_code_list = new QtUtils::MinimumSizeHintWidget<QListWidget>; |
| 88 | m_name_label = new QLabel; |
| 89 | m_creator_label = new QLabel; |
| 90 | |
| 91 | m_code_list->setContextMenuPolicy(Qt::CustomContextMenu); |
| 92 | |
| 93 | QFont monospace(QFontDatabase::systemFont(QFontDatabase::FixedFont).family()); |
| 94 | |
| 95 | const auto line_height = QFontMetrics(font()).lineSpacing(); |
| 96 | |
| 97 | m_code_description = new QTextEdit; |
| 98 | m_code_description->setFont(monospace); |
| 99 | m_code_description->setReadOnly(true); |
| 100 | m_code_description->setFixedHeight(line_height * 5); |
| 101 | |
| 102 | m_code_view = new QTextEdit; |
| 103 | m_code_view->setFont(monospace); |
| 104 | m_code_view->setReadOnly(true); |
| 105 | m_code_view->setFixedHeight(line_height * 10); |
| 106 | |
| 107 | m_add_code = new NonDefaultQPushButton(tr("&Add New Code...")); |
| 108 | m_edit_code = new NonDefaultQPushButton(tr("&Edit Code...")); |
| 109 | m_remove_code = new NonDefaultQPushButton(tr("&Remove Code")); |
| 110 | m_download_codes = new NonDefaultQPushButton(tr("Download Codes")); |
| 111 | |
| 112 | m_cheat_code_editor = new CheatCodeEditor(this); |
| 113 | |
| 114 | m_download_codes->setToolTip(tr("Download Codes from the WiiRD Database")); |
| 115 | |
| 116 | auto* const layout = new QVBoxLayout{this}; |
| 117 | |
| 118 | layout->addWidget(m_warning); |
| 119 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 120 | layout->addWidget(m_hc_warning); |
| 121 | #endif // USE_RETRO_ACHIEVEMENTS |
| 122 | layout->addWidget(m_code_list); |
| 123 | |
| 124 | auto* info_layout = new QFormLayout; |
| 125 | |
| 126 | info_layout->addRow(tr("Name:"), m_name_label); |
| 127 | info_layout->addRow(tr("Creator:"), m_creator_label); |
| 128 | info_layout->addRow(tr("Description:"), static_cast<QWidget*>(nullptr)); |
| 129 | |
| 130 | info_layout->setFormAlignment(Qt::AlignLeft | Qt::AlignTop); |
| 131 | |
| 132 | for (QLabel* label : {m_name_label, m_creator_label}) |
| 133 | { |
| 134 | label->setTextInteractionFlags(Qt::TextSelectableByMouse); |
| 135 | label->setCursor(Qt::IBeamCursor); |
| 136 | } |
| 137 | |
| 138 | layout->addLayout(info_layout); |
| 139 | layout->addWidget(m_code_description); |
| 140 | layout->addWidget(m_code_view); |
| 141 | |
| 142 | QHBoxLayout* btn_layout = new QHBoxLayout; |
| 143 | |
| 144 | btn_layout->addWidget(m_add_code); |
| 145 | btn_layout->addWidget(m_edit_code); |
| 146 | btn_layout->addWidget(m_remove_code); |
| 147 | btn_layout->addWidget(m_download_codes); |
| 148 | |
| 149 | layout->addLayout(btn_layout); |
| 150 | } |
| 151 | |
| 152 | void GeckoCodeWidget::ConnectWidgets() |
| 153 | { |
| 154 | connect(m_code_list, &QListWidget::itemSelectionChanged, this, |
| 155 | &GeckoCodeWidget::OnSelectionChanged); |
| 156 | connect(m_code_list, &QListWidget::itemChanged, this, &GeckoCodeWidget::OnItemChanged); |
| 157 | connect(m_code_list->model(), &QAbstractItemModel::rowsMoved, this, |
| 158 | &GeckoCodeWidget::OnListReordered); |
| 159 | connect(m_code_list, &QListWidget::customContextMenuRequested, this, |
| 160 | &GeckoCodeWidget::OnContextMenuRequested); |
| 161 | |
| 162 | connect(m_add_code, &QPushButton::clicked, this, &GeckoCodeWidget::AddCode); |
| 163 | connect(m_remove_code, &QPushButton::clicked, this, &GeckoCodeWidget::RemoveCode); |
| 164 | connect(m_edit_code, &QPushButton::clicked, this, &GeckoCodeWidget::EditCode); |
| 165 | connect(m_download_codes, &QPushButton::clicked, this, &GeckoCodeWidget::DownloadCodes); |
| 166 | connect(m_warning, &CheatWarningWidget::OpenCheatEnableSettings, this, |
| 167 | &GeckoCodeWidget::OpenGeneralSettings); |
| 168 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 169 | connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this, |
| 170 | &GeckoCodeWidget::OpenAchievementSettings); |
| 171 | connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, |
| 172 | &GeckoCodeWidget::UpdateList); |
| 173 | #endif // USE_RETRO_ACHIEVEMENTS |
| 174 | } |
| 175 | |
| 176 | void GeckoCodeWidget::OnSelectionChanged() |
| 177 | { |
| 178 | const QList<QListWidgetItem*> items = m_code_list->selectedItems(); |
| 179 | const bool empty = items.empty(); |
| 180 | |
| 181 | m_edit_code->setDisabled(empty); |
| 182 | m_remove_code->setDisabled(empty); |
| 183 | |
| 184 | if (empty) |
| 185 | return; |
| 186 | |
| 187 | const QListWidgetItem* const selected = items[0]; |
| 188 | |
| 189 | const int index = selected->data(Qt::UserRole).toInt(); |
| 190 | |
| 191 | const auto& code = m_gecko_codes[index]; |
| 192 | |
| 193 | m_name_label->setText(QString::fromStdString(code.name)); |
| 194 | m_creator_label->setText(QString::fromStdString(code.creator)); |
| 195 | |
| 196 | m_code_description->clear(); |
| 197 | |
| 198 | for (const auto& line : code.notes) |
| 199 | m_code_description->append(QString::fromStdString(line)); |
| 200 | |
| 201 | m_code_view->clear(); |
| 202 | |
| 203 | for (const auto& c : code.codes) |
| 204 | m_code_view->append(QString::fromStdString(c.original_line)); |
| 205 | } |
| 206 | |
| 207 | void GeckoCodeWidget::OnItemChanged(QListWidgetItem* item) |
| 208 | { |
| 209 | const int index = item->data(Qt::UserRole).toInt(); |
| 210 | m_gecko_codes[index].enabled = (item->checkState() == Qt::Checked); |
| 211 | |
| 212 | if (!m_restart_required) |
| 213 | Gecko::SetActiveCodes(m_gecko_codes, m_game_id, m_game_revision); |
| 214 | |
| 215 | SaveCodes(); |
| 216 | } |
| 217 | |
| 218 | void GeckoCodeWidget::AddCode() |
| 219 | { |
| 220 | Gecko::GeckoCode code; |
| 221 | code.enabled = true; |
| 222 | |
| 223 | m_cheat_code_editor->SetGeckoCode(&code); |
| 224 | if (m_cheat_code_editor->exec() == QDialog::Rejected) |
| 225 | return; |
| 226 | |
| 227 | m_gecko_codes.push_back(std::move(code)); |
| 228 | SaveCodes(); |
| 229 | UpdateList(); |
| 230 | } |
| 231 | |
| 232 | void GeckoCodeWidget::EditCode() |
| 233 | { |
| 234 | const auto* item = m_code_list->currentItem(); |
| 235 | if (item == nullptr) |
| 236 | return; |
| 237 | |
| 238 | const int index = item->data(Qt::UserRole).toInt(); |
| 239 | const bool enabled = item->checkState() == Qt::Checked; |
| 240 | |
| 241 | m_cheat_code_editor->SetGeckoCode(&m_gecko_codes[index]); |
| 242 | if (m_cheat_code_editor->exec() == QDialog::Rejected) |
| 243 | return; |
| 244 | |
| 245 | SaveCodes(); |
| 246 | UpdateList(); |
| 247 | |
| 248 | if (!m_restart_required && enabled) |
| 249 | Gecko::SetActiveCodes(m_gecko_codes, m_game_id, m_game_revision); |
| 250 | } |
| 251 | |
| 252 | void GeckoCodeWidget::RemoveCode() |
| 253 | { |
| 254 | const auto* item = m_code_list->currentItem(); |
| 255 | |
| 256 | if (item == nullptr) |
| 257 | return; |
| 258 | |
| 259 | m_gecko_codes.erase(m_gecko_codes.begin() + item->data(Qt::UserRole).toInt()); |
| 260 | |
| 261 | UpdateList(); |
| 262 | SaveCodes(); |
| 263 | } |
| 264 | |
| 265 | void GeckoCodeWidget::LoadCodes() |
| 266 | { |
| 267 | if (!m_game_id.empty()) |
| 268 | { |
| 269 | Common::IniFile game_ini_local; |
| 270 | |
| 271 | // We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI |
| 272 | // will always be stored in GS/${GAMEID}.ini |
| 273 | game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini"); |
| 274 | |
| 275 | const Common::IniFile game_ini_default = |
| 276 | SConfig::LoadDefaultGameIni(m_game_id, m_game_revision); |
| 277 | m_gecko_codes = Gecko::LoadCodes(game_ini_default, game_ini_local); |
| 278 | } |
| 279 | |
| 280 | m_code_list->setEnabled(!m_game_id.empty()); |
| 281 | m_name_label->setEnabled(!m_game_id.empty()); |
| 282 | m_creator_label->setEnabled(!m_game_id.empty()); |
| 283 | m_code_description->setEnabled(!m_game_id.empty()); |
| 284 | m_code_view->setEnabled(!m_game_id.empty()); |
| 285 | |
| 286 | m_add_code->setEnabled(!m_game_id.empty()); |
| 287 | m_edit_code->setEnabled(false); |
| 288 | m_remove_code->setEnabled(false); |
| 289 | m_download_codes->setEnabled(!m_game_id.empty()); |
| 290 | |
| 291 | UpdateList(); |
| 292 | } |
| 293 | |
| 294 | void GeckoCodeWidget::SaveCodes() |
| 295 | { |
| 296 | if (m_game_id.empty()) |
| 297 | return; |
| 298 | |
| 299 | const auto ini_path = |
| 300 | std::string(File::GetUserPath(D_GAMESETTINGS_IDX)).append(m_game_id).append(".ini"); |
| 301 | |
| 302 | Common::IniFile game_ini_local; |
| 303 | game_ini_local.Load(ini_path); |
| 304 | Gecko::SaveCodes(game_ini_local, m_gecko_codes); |
| 305 | game_ini_local.Save(ini_path); |
| 306 | } |
| 307 | |
| 308 | void GeckoCodeWidget::OnContextMenuRequested() |
| 309 | { |
| 310 | QMenu menu; |
| 311 | |
| 312 | menu.addAction(tr("Sort Alphabetically"), this, &GeckoCodeWidget::SortAlphabetically); |
| 313 | menu.addAction(tr("Show Enabled Codes First"), this, &GeckoCodeWidget::SortEnabledCodesFirst); |
| 314 | menu.addAction(tr("Show Disabled Codes First"), this, &GeckoCodeWidget::SortDisabledCodesFirst); |
| 315 | |
| 316 | menu.exec(QCursor::pos()); |
| 317 | } |
| 318 | |
| 319 | void GeckoCodeWidget::SortAlphabetically() |
| 320 | { |
| 321 | m_code_list->sortItems(); |
| 322 | OnListReordered(); |
| 323 | } |
| 324 | |
| 325 | void GeckoCodeWidget::SortEnabledCodesFirst() |
| 326 | { |
| 327 | std::ranges::stable_partition(m_gecko_codes, std::identity{}, &Gecko::GeckoCode::enabled); |
| 328 | UpdateList(); |
| 329 | SaveCodes(); |
| 330 | } |
| 331 | |
| 332 | void GeckoCodeWidget::SortDisabledCodesFirst() |
| 333 | { |
| 334 | std::ranges::stable_partition(m_gecko_codes, std::logical_not{}, &Gecko::GeckoCode::enabled); |
| 335 | UpdateList(); |
| 336 | SaveCodes(); |
| 337 | } |
| 338 | |
| 339 | void GeckoCodeWidget::OnListReordered() |
| 340 | { |
| 341 | // Reorder codes based on the indices of table item |
| 342 | std::vector<Gecko::GeckoCode> codes; |
| 343 | codes.reserve(m_gecko_codes.size()); |
| 344 | |
| 345 | for (int i = 0; i < m_code_list->count(); i++) |
| 346 | { |
| 347 | const int index = m_code_list->item(i)->data(Qt::UserRole).toInt(); |
| 348 | |
| 349 | codes.push_back(std::move(m_gecko_codes[index])); |
| 350 | } |
| 351 | |
| 352 | m_gecko_codes = std::move(codes); |
| 353 | |
| 354 | UpdateList(); |
| 355 | SaveCodes(); |
| 356 | } |
| 357 | |
| 358 | void GeckoCodeWidget::UpdateList() |
| 359 | { |
| 360 | m_code_list->clear(); |
| 361 | |
| 362 | for (size_t i = 0; i < m_gecko_codes.size(); i++) |
| 363 | { |
| 364 | const auto& code = m_gecko_codes[i]; |
| 365 | |
| 366 | auto* item = new QListWidgetItem(QString::fromStdString(code.name) |
| 367 | .replace(QStringLiteral("<"), QChar::fromLatin1('<')) |
| 368 | .replace(QStringLiteral(">"), QChar::fromLatin1('>'))); |
| 369 | |
| 370 | item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | |
| 371 | Qt::ItemIsDragEnabled); |
| 372 | item->setCheckState(code.enabled ? Qt::Checked : Qt::Unchecked); |
| 373 | item->setData(Qt::UserRole, static_cast<int>(i)); |
| 374 | |
| 375 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 376 | const AchievementManager& achievement_manager = AchievementManager::GetInstance(); |
| 377 | |
| 378 | if (achievement_manager.IsHardcoreModeActive()) |
| 379 | { |
| 380 | const QIcon approved_icon = style()->standardIcon(QStyle::SP_DialogYesButton); |
| 381 | const QIcon warning_icon = style()->standardIcon(QStyle::SP_MessageBoxWarning); |
| 382 | |
| 383 | if (achievement_manager.IsApprovedGeckoCode(code, m_game_id, m_game_revision)) |
| 384 | item->setIcon(approved_icon); |
| 385 | else |
| 386 | item->setIcon(warning_icon); |
| 387 | } |
| 388 | #endif // USE_RETRO_ACHIEVEMENTS |
| 389 | |
| 390 | m_code_list->addItem(item); |
| 391 | } |
| 392 | |
| 393 | m_code_list->setDragDropMode(QAbstractItemView::InternalMove); |
| 394 | } |
| 395 | |
| 396 | void GeckoCodeWidget::DownloadCodes() |
| 397 | { |
| 398 | const auto codes_result = Gecko::DownloadCodes(m_gametdb_id); |
| 399 | |
| 400 | if (!codes_result) |
| 401 | { |
| 402 | QString message = tr("Failed to download Gecko codes. The code server may be temporarily " |
| 403 | "unavailable. Please try again later."); |
| 404 | |
| 405 | const int http_response_code = codes_result.error(); |
| 406 | if (http_response_code > 0) |
| 407 | message += tr("\n\nServer response: HTTP %1.").arg(http_response_code); |
| 408 | |
| 409 | ModalMessageBox::critical(this, tr("Download Failed"), message); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | const std::vector<Gecko::GeckoCode>& codes = *codes_result; |
| 414 | if (codes.empty()) |
| 415 | { |
| 416 | ModalMessageBox::critical(this, tr("Error"), tr("File contained no codes.")); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | size_t added_count = 0; |
| 421 | |
| 422 | for (const auto& code : codes) |
| 423 | { |
| 424 | auto it = std::ranges::find(m_gecko_codes, code); |
| 425 | |
| 426 | if (it == m_gecko_codes.end()) |
| 427 | { |
| 428 | m_gecko_codes.push_back(code); |
| 429 | added_count++; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | UpdateList(); |
| 434 | SaveCodes(); |
| 435 | |
| 436 | ModalMessageBox::information( |
| 437 | this, tr("Download complete"), |
| 438 | tr("Downloaded %1 codes. (added %2)") |
| 439 | .arg(QString::number(codes.size()), QString::number(added_count))); |
| 440 | } |