summaryrefslogtreecommitdiff
path: root/Source/Core/DolphinQt/Config/ARCodeWidget.cpp
blob: 12be8072ac227884b0f5afb895e9e86a4b3685f9 (plain)
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
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "DolphinQt/Config/ARCodeWidget.h"

#include <algorithm>
#include <functional>
#include <utility>

#include <QCursor>
#include <QHBoxLayout>
#ifdef USE_RETRO_ACHIEVEMENTS
#include <QIcon>
#endif  // USE_RETRO_ACHIEVEMENTS
#include <QListWidget>
#include <QMenu>
#include <QPushButton>
#ifdef USE_RETRO_ACHIEVEMENTS
#include <QStyle>
#endif  // USE_RETRO_ACHIEVEMENTS
#include <QVBoxLayout>

#include "Common/FileUtil.h"
#include "Common/IniFile.h"

#include "Core/AchievementManager.h"
#include "Core/ActionReplay.h"
#include "Core/ConfigManager.h"

#include "DolphinQt/Config/CheatCodeEditor.h"
#include "DolphinQt/Config/CheatWarningWidget.h"
#include "DolphinQt/Config/HardcoreWarningWidget.h"
#include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
#ifdef USE_RETRO_ACHIEVEMENTS
#include "DolphinQt/Settings.h"
#endif  // USE_RETRO_ACHIEVEMENTS

ARCodeWidget::ARCodeWidget(std::string game_id, u16 game_revision, bool restart_required)
    : m_game_id(std::move(game_id)), m_game_revision(game_revision),
      m_restart_required(restart_required)
{
  CreateWidgets();
  ConnectWidgets();

  LoadCodes();
}

ARCodeWidget::~ARCodeWidget() = default;

void ARCodeWidget::ChangeGame(std::string game_id, const u16 game_revision)
{
  m_game_id = std::move(game_id);
  m_game_revision = game_revision;
  m_restart_required = false;

  m_ar_codes.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();
}

void ARCodeWidget::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 QListWidget;
  m_code_add = new NonDefaultQPushButton(tr("&Add New Code..."));
  m_code_edit = new NonDefaultQPushButton(tr("&Edit Code..."));
  m_code_remove = new NonDefaultQPushButton(tr("&Remove Code"));
  m_code_toggle_all = new NonDefaultQPushButton(tr("Disable All"));

  m_cheat_code_editor = new CheatCodeEditor(this);

  m_code_list->setContextMenuPolicy(Qt::CustomContextMenu);

  auto* button_layout = new QHBoxLayout;

  button_layout->addWidget(m_code_add);
  button_layout->addWidget(m_code_edit);
  button_layout->addWidget(m_code_remove);
  button_layout->addWidget(m_code_toggle_all);

  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);
  layout->addLayout(button_layout);
}

void ARCodeWidget::ConnectWidgets()
{
  connect(m_warning, &CheatWarningWidget::OpenCheatEnableSettings, this,
          &ARCodeWidget::OpenGeneralSettings);
#ifdef USE_RETRO_ACHIEVEMENTS
  connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this,
          &ARCodeWidget::OpenAchievementSettings);
  connect(&Settings::Instance(), &Settings::EmulationStateChanged, this, &ARCodeWidget::UpdateList);
#endif  // USE_RETRO_ACHIEVEMENTS

  connect(m_code_list, &QListWidget::itemChanged, this, &ARCodeWidget::OnItemChanged);
  connect(m_code_list, &QListWidget::itemSelectionChanged, this, &ARCodeWidget::OnSelectionChanged);
  connect(m_code_list->model(), &QAbstractItemModel::rowsMoved, this,
          &ARCodeWidget::OnListReordered);
  connect(m_code_list, &QListWidget::customContextMenuRequested, this,
          &ARCodeWidget::OnContextMenuRequested);

  connect(m_code_add, &QPushButton::clicked, this, &ARCodeWidget::OnCodeAddClicked);
  connect(m_code_edit, &QPushButton::clicked, this, &ARCodeWidget::OnCodeEditClicked);
  connect(m_code_remove, &QPushButton::clicked, this, &ARCodeWidget::OnCodeRemoveClicked);
  connect(m_code_toggle_all, &QPushButton::clicked, this, &ARCodeWidget::OnCodeToggleAllClicked);
}

void ARCodeWidget::OnItemChanged(QListWidgetItem* item)
{
  m_ar_codes[m_code_list->row(item)].enabled = (item->checkState() == Qt::Checked);

  if (!m_restart_required)
    ActionReplay::ApplyCodes(m_ar_codes, m_game_id, m_game_revision);

  UpdateToggleButton();
  SaveCodes();
}

void ARCodeWidget::OnContextMenuRequested()
{
  QMenu menu;

  menu.addAction(tr("Sort Alphabetically"), this, &ARCodeWidget::SortAlphabetically);
  menu.addAction(tr("Show Enabled Codes First"), this, &ARCodeWidget::SortEnabledCodesFirst);
  menu.addAction(tr("Show Disabled Codes First"), this, &ARCodeWidget::SortDisabledCodesFirst);

  menu.exec(QCursor::pos());
}

void ARCodeWidget::SortAlphabetically()
{
  m_code_list->sortItems();
  OnListReordered();
}

void ARCodeWidget::SortEnabledCodesFirst()
{
  std::ranges::stable_partition(m_ar_codes, std::identity{}, &ActionReplay::ARCode::enabled);
  UpdateList();
  SaveCodes();
}

void ARCodeWidget::SortDisabledCodesFirst()
{
  std::ranges::stable_partition(m_ar_codes, std::logical_not{}, &ActionReplay::ARCode::enabled);
  UpdateList();
  SaveCodes();
}

bool ARCodeWidget::IsEveryCodeEnabled()
{
  return std::ranges::all_of(m_ar_codes, &ActionReplay::ARCode::enabled);
}

void ARCodeWidget::UpdateToggleButton()
{
  if (IsEveryCodeEnabled())
    m_code_toggle_all->setText(tr("Disable All"));
  else
    m_code_toggle_all->setText(tr("Enable All"));

  m_code_toggle_all->setDisabled(m_ar_codes.empty());
}

void ARCodeWidget::OnListReordered()
{
  // Reorder codes based on the indices of table item
  std::vector<ActionReplay::ARCode> codes;
  codes.reserve(m_ar_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_ar_codes[index]));
  }

  m_ar_codes = std::move(codes);

  SaveCodes();
}

void ARCodeWidget::OnSelectionChanged()
{
  const QList<QListWidgetItem*> items = m_code_list->selectedItems();
  const bool empty = items.empty();

  m_code_edit->setDisabled(empty);
  m_code_remove->setDisabled(empty);

  if (empty)
    return;

  const QListWidgetItem* const selected = items[0];

  const bool user_defined = m_ar_codes[m_code_list->row(selected)].user_defined;

  m_code_remove->setEnabled(user_defined);
  m_code_edit->setText(user_defined ? tr("&Edit Code...") : tr("Clone and &Edit Code..."));
}

void ARCodeWidget::UpdateList()
{
  m_code_list->clear();

  for (size_t i = 0; i < m_ar_codes.size(); i++)
  {
    const auto& ar = m_ar_codes[i];
    auto* item = new QListWidgetItem(QString::fromStdString(ar.name)
                                         .replace(QStringLiteral("&lt;"), QChar::fromLatin1('<'))
                                         .replace(QStringLiteral("&gt;"), QChar::fromLatin1('>')));

    item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable |
                   Qt::ItemIsDragEnabled);
    item->setCheckState(ar.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.IsApprovedARCode(ar, 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 ARCodeWidget::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_ar_codes = ActionReplay::LoadCodes(game_ini_default, game_ini_local);
  }

  m_code_list->setEnabled(!m_game_id.empty());
  m_code_add->setEnabled(!m_game_id.empty());
  m_code_edit->setEnabled(false);
  m_code_remove->setEnabled(false);

  UpdateList();
}

void ARCodeWidget::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);
  ActionReplay::SaveCodes(&game_ini_local, m_ar_codes);
  game_ini_local.Save(ini_path);
}

void ARCodeWidget::AddCode(ActionReplay::ARCode code)
{
  m_ar_codes.push_back(std::move(code));

  UpdateList();
  SaveCodes();
}

void ARCodeWidget::OnCodeAddClicked()
{
  ActionReplay::ARCode ar;
  ar.enabled = false;

  m_cheat_code_editor->SetARCode(&ar);
  if (m_cheat_code_editor->exec() == QDialog::Rejected)
    return;

  m_ar_codes.push_back(std::move(ar));

  UpdateList();
  SaveCodes();
}

void ARCodeWidget::OnCodeEditClicked()
{
  const auto items = m_code_list->selectedItems();
  if (items.empty())
    return;

  const auto* const selected = items[0];
  const bool enabled = selected->checkState() == Qt::Checked;

  auto& current_ar = m_ar_codes[m_code_list->row(selected)];

  if (current_ar.user_defined)
  {
    m_cheat_code_editor->SetARCode(&current_ar);
    if (m_cheat_code_editor->exec() == QDialog::Rejected)
      return;
  }
  else
  {
    ActionReplay::ARCode ar = current_ar;
    m_cheat_code_editor->SetARCode(&ar);
    if (m_cheat_code_editor->exec() == QDialog::Rejected)
      return;

    m_ar_codes.push_back(std::move(ar));
  }

  SaveCodes();
  UpdateList();

  if (!m_restart_required && enabled)
    ActionReplay::ApplyCodes(m_ar_codes, m_game_id, m_game_revision);
}

void ARCodeWidget::OnCodeRemoveClicked()
{
  auto items = m_code_list->selectedItems();

  if (items.empty())
    return;

  const auto* selected = items[0];

  m_ar_codes.erase(m_ar_codes.begin() + m_code_list->row(selected));

  SaveCodes();
  UpdateList();

  m_code_remove->setEnabled(false);
}

void ARCodeWidget::OnCodeToggleAllClicked()
{
  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 < static_cast<int>(m_ar_codes.size()); ++i)
    {
      m_ar_codes[i].enabled = new_state;
      m_code_list->item(i)->setCheckState(new_check_state);
    }
  }

  if (!m_restart_required)
    ActionReplay::ApplyCodes(m_ar_codes, m_game_id, m_game_revision);

  UpdateList();
  SaveCodes();
}