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