Debian Codegraph @

Source/Core/DolphinQt/Config/CheatCodeEditor.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Config/CheatCodeEditor.h"
5
6 #include <regex>
7
8 #include <QDialogButtonBox>
9 #include <QFontDatabase>
10 #include <QGridLayout>
11 #include <QLabel>
12 #include <QLineEdit>
13 #include <QStringList>
14 #include <QTextEdit>
15
16 #include "Common/StringUtil.h"
17
18 #include "Core/ARDecrypt.h"
19 #include "Core/ActionReplay.h"
20 #include "Core/GeckoCodeConfig.h"
21
22 #include "DolphinQt/QtUtils/ModalMessageBox.h"
23
24 CheatCodeEditor::CheatCodeEditor(QWidget* parent) : QDialog(parent)
25 {
26 setWindowTitle(tr("Cheat Code Editor"));
27
28 CreateWidgets();
29 ConnectWidgets();
30 }
31
32 void CheatCodeEditor::SetARCode(ActionReplay::ARCode* code)
33 {
34 m_name_edit->setText(QString::fromStdString(code->name));
35
36 m_code_edit->clear();
37
38 for (ActionReplay::AREntry& e : code->ops)
39 m_code_edit->append(QString::fromStdString(ActionReplay::SerializeLine(e)));
40
41 m_creator_label->setHidden(true);
42 m_creator_edit->setHidden(true);
43 m_notes_label->setHidden(true);
44 m_notes_edit->setHidden(true);
45
46 m_ar_code = code;
47 m_gecko_code = nullptr;
48 }
49
50 void CheatCodeEditor::SetGeckoCode(Gecko::GeckoCode* code)
51 {
52 m_name_edit->setText(QString::fromStdString(code->name));
53 m_creator_edit->setText(QString::fromStdString(code->creator));
54
55 m_code_edit->clear();
56
57 for (const auto& c : code->codes)
58 m_code_edit->append(QString::fromStdString(c.original_line));
59
60 QString notes_string;
61 for (const auto& line : code->notes)
62 notes_string += QStringLiteral("%1\n").arg(QString::fromStdString(line));
63
64 m_notes_edit->setText(notes_string);
65
66 m_creator_label->setHidden(false);
67 m_creator_edit->setHidden(false);
68 m_notes_label->setHidden(false);
69 m_notes_edit->setHidden(false);
70
71 m_gecko_code = code;
72 m_ar_code = nullptr;
73 }
74
75 void CheatCodeEditor::CreateWidgets()
76 {
77 m_name_edit = new QLineEdit;
78 m_creator_edit = new QLineEdit;
79 m_notes_edit = new QTextEdit;
80 m_code_edit = new QTextEdit;
81 m_button_box = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Save);
82
83 m_creator_label = new QLabel(tr("Creator:"));
84 m_notes_label = new QLabel(tr("Notes:"));
85
86 QGridLayout* grid_layout = new QGridLayout;
87
88 grid_layout->addWidget(new QLabel(tr("Name:")), 0, 0);
89 grid_layout->addWidget(m_name_edit, 0, 1);
90 grid_layout->addWidget(m_creator_label, 1, 0);
91 grid_layout->addWidget(m_creator_edit, 1, 1);
92 grid_layout->addWidget(m_notes_label, 2, 0);
93 grid_layout->addWidget(m_notes_edit, 2, 1);
94 grid_layout->addWidget(new QLabel(tr("Code:")), 3, 0);
95 grid_layout->addWidget(m_code_edit, 3, 1);
96 grid_layout->addWidget(m_button_box, 4, 1);
97
98 QFont monospace(QFontDatabase::systemFont(QFontDatabase::FixedFont).family());
99
100 m_code_edit->setFont(monospace);
101
102 m_code_edit->setAcceptRichText(false);
103 m_notes_edit->setAcceptRichText(false);
104
105 setLayout(grid_layout);
106 }
107
108 void CheatCodeEditor::ConnectWidgets()
109 {
110 connect(m_button_box, &QDialogButtonBox::accepted, this, &CheatCodeEditor::accept);
111 connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
112 }
113
114 bool CheatCodeEditor::AcceptAR()
115 {
116 QString name = m_name_edit->text();
117
118 std::vector<ActionReplay::AREntry> entries;
119 std::vector<std::string> encrypted_lines;
120
121 QStringList lines = m_code_edit->toPlainText().split(QLatin1Char{'\n'});
122
123 for (int i = 0; i < lines.size(); i++)
124 {
125 QString line = lines[i];
126
127 if (line.isEmpty())
128 continue;
129 if (i == 0 && line[0] == u'$')
130 {
131 if (name.isEmpty())
132 name = line.right(line.size() - 1);
133
134 continue;
135 }
136
137 const auto parse_result = ActionReplay::DeserializeLine(line.toStdString());
138
139 if (std::holds_alternative<ActionReplay::AREntry>(parse_result))
140 {
141 entries.push_back(std::get<ActionReplay::AREntry>(parse_result));
142 }
143 else if (std::holds_alternative<ActionReplay::EncryptedLine>(parse_result))
144 {
145 encrypted_lines.emplace_back(std::get<ActionReplay::EncryptedLine>(parse_result));
146 }
147 else
148 {
149 auto result = ModalMessageBox::warning(
150 this, tr("Parsing Error"),
151 tr("Unable to parse line %1 of the entered AR code as a valid "
152 "encrypted or decrypted code. Make sure you typed it correctly.\n\n"
153 "Would you like to ignore this line and continue parsing?")
154 .arg(i + 1),
155 QMessageBox::Ok | QMessageBox::Abort);
156
157 if (result == QMessageBox::Abort)
158 return false;
159 }
160 }
161
162 if (!encrypted_lines.empty())
163 {
164 if (!entries.empty())
165 {
166 auto result = ModalMessageBox::warning(
167 this, tr("Invalid Mixed Code"),
168 tr("This Action Replay code contains both encrypted and unencrypted lines; "
169 "you should check that you have entered it correctly.\n\n"
170 "Do you want to discard all unencrypted lines?"),
171 QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
172
173 // YES = Discard the unencrypted lines then decrypt the encrypted ones instead.
174 // NO = Discard the encrypted lines, keep the unencrypted ones
175 // CANCEL = Stop and let the user go back to editing
176 switch (result)
177 {
178 case QMessageBox::Yes:
179 entries.clear();
180 break;
181 case QMessageBox::No:
182 encrypted_lines.clear();
183 break;
184 case QMessageBox::Cancel:
185 return false;
186 default:
187 break;
188 }
189 }
190 ActionReplay::DecryptARCode(encrypted_lines, &entries);
191 }
192
193 if (entries.empty())
194 {
195 ModalMessageBox::critical(this, tr("Error"),
196 tr("The resulting decrypted AR code doesn't contain any lines."));
197 return false;
198 }
199
200 m_ar_code->name = name.toStdString();
201 m_ar_code->ops = std::move(entries);
202 m_ar_code->user_defined = true;
203
204 return true;
205 }
206
207 bool CheatCodeEditor::AcceptGecko()
208 {
209 QString name = m_name_edit->text();
210
211 std::vector<Gecko::GeckoCode::Code> entries;
212
213 QStringList lines = m_code_edit->toPlainText().split(QLatin1Char{'\n'});
214
215 for (int i = 0; i < lines.size(); i++)
216 {
217 QString line = lines[i];
218
219 if (line.isEmpty())
220 continue;
221
222 if (i == 0 && line[0] == u'$')
223 {
224 if (name.isEmpty())
225 name = line.right(line.size() - 1);
226
227 continue;
228 }
229
230 if (std::optional<Gecko::GeckoCode::Code> c = Gecko::DeserializeLine(line.toStdString()))
231 {
232 entries.push_back(*c);
233 }
234 else
235 {
236 auto result = ModalMessageBox::warning(
237 this, tr("Parsing Error"),
238 tr("Unable to parse line %1 of the entered Gecko code as a valid "
239 "code. Make sure you typed it correctly.\n\n"
240 "Would you like to ignore this line and continue parsing?")
241 .arg(i + 1),
242 QMessageBox::Ok | QMessageBox::Abort);
243
244 if (result == QMessageBox::Abort)
245 return false;
246 }
247 }
248
249 if (entries.empty())
250 {
251 ModalMessageBox::critical(this, tr("Error"), tr("This Gecko code doesn't contain any lines."));
252 return false;
253 }
254
255 m_gecko_code->name = name.trimmed().toStdString();
256 m_gecko_code->creator = m_creator_edit->text().trimmed().toStdString();
257 m_gecko_code->codes = std::move(entries);
258 m_gecko_code->notes = SplitString(m_notes_edit->toPlainText().toStdString(), '\n');
259 m_gecko_code->user_defined = true;
260
261 {
262 // The creator name is not expected to be present in the cheat code name. It will be extracted
263 // and moved into its dedicated "creator" field.
264 std::smatch matches;
265 if (std::regex_match(m_gecko_code->name, matches, std::regex{R"(^(.*)\[(.*)\]$)"}))
266 {
267 m_gecko_code->name = StripWhitespace(matches[1].str());
268 if (m_gecko_code->creator.empty())
269 {
270 m_gecko_code->creator = StripWhitespace(matches[2].str());
271 }
272 }
273 }
274
275 return true;
276 }
277
278 void CheatCodeEditor::accept()
279 {
280 bool success = m_gecko_code != nullptr ? AcceptGecko() : AcceptAR();
281
282 if (success)
283 QDialog::accept();
284 }