-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainwindow.cpp
executable file
·274 lines (246 loc) · 8.49 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "class/cheatcodes.cpp"
#include "core/cheatcodes-converters.cpp"
#include "core/cheatcodes.cpp"
#include "core/convertbmps.cpp"
#include "core/romfuncs.cpp"
#include "functions.h"
#include "ui_mainwindow.h"
#include "variables.h"
char romname[25];
MainWindow::MainWindow(QWidget* parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("GBAATM-Rebirth - 1.0.0 Alpha 3");
settings = new QSettings("Mte90", "GBAATM");
connect(ui->input_btn, &QPushButton::pressed, this, &MainWindow::openRom);
connect(ui->output_btn, &QPushButton::pressed, this, &MainWindow::saveRom);
connect(ui->opencht, &QPushButton::pressed, this, &MainWindow::openCheat);
connect(ui->deadbeef, &QPushButton::pressed, this, &MainWindow::deadbeef);
connect(ui->patchgame, &QPushButton::pressed, this, &MainWindow::patchGame);
connect(ui->loadbg, &QPushButton::pressed, this, &MainWindow::loadBg);
connect(ui->loadfont, &QPushButton::pressed, this, &MainWindow::loadFont);
connect(ui->loadselection, &QPushButton::pressed, this, &MainWindow::loadSelectionBar);
if (settings->value("files/input").toString() != "") {
ui->input_path->setText(settings->value("files/input").toString());
this->readRom();
}
if (settings->value("files/output").toString() != "") {
ui->output_path->setText(settings->value("files/output").toString());
}
if (settings->value("rom/menutitle").toString() != "") {
ui->menu_text->setText(settings->value("rom/menutitle").toString());
}
}
void
MainWindow::openRom()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open GBA rom"), settings->value("files/input").toString(),
tr("Image Files (*.gba *.rom *.bin)"));
if (!fileName.isEmpty()) {
ui->input_path->setText(fileName);
settings->setValue("files/input", fileName);
}
this->readRom();
}
void
MainWindow::loadBg()
{
QFileInfo info(settings->value("files/input").toString());
QString fileName = QFileDialog::getOpenFileName(this, tr("Open background image"), info.dir().path(), tr("Image Files (*.bmp)"));
if (!fileName.isEmpty()) {
int goodbmp;
goodbmp = bmp2short(fileName.toLocal8Bit().data(), menubgshort, 1);
if (goodbmp == 1)
customizetrainer.background = 1;
}
}
void
MainWindow::loadFont()
{
QFileInfo info(settings->value("files/input").toString());
QString fileName = QFileDialog::getOpenFileName(this, tr("Open font image"), info.dir().path(), tr("Image Files (*.bmp)"));
if (!fileName.isEmpty()) {
int goodbmp;
goodbmp = bmp2short(fileName.toLocal8Bit().data(), menufontshort, 3);
if (goodbmp == 1)
customizetrainer.font = 1;
}
}
void
MainWindow::loadSelectionBar()
{
QFileInfo info(settings->value("files/input").toString());
QString fileName = QFileDialog::getOpenFileName(this, tr("Open background image"), info.dir().path(), tr("Image Files (*.bmp)"));
if (!fileName.isEmpty()) {
int goodbmp;
goodbmp = bmp2short(fileName.toLocal8Bit().data(), menuselectshort, 2);
if (goodbmp == 1)
customizetrainer.selectionbar = 1;
}
}
void
MainWindow::saveRom()
{
QString fileName = QFileDialog::getSaveFileName(this, tr("Save GBA rom"), settings->value("files/output").toString(),
tr("Image Files (*.gba *.rom *.bin)"));
if (!fileName.isEmpty()) {
ui->output_path->setText(fileName);
settings->setValue("files/output", fileName);
}
}
void
MainWindow::openCheat()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open cheat file"), settings->value("files/cheat").toString(),
tr("CHT Files (*.cht);;Text Files (*.txt);;Assembly Code File (*.asm)"));
if (!fileName.isEmpty()) {
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
this->appendLog(tr("Cheat has some issues on reading"));
return;
}
QString content = QString::fromUtf8(file.readAll());
settings->setValue("files/cheat", fileName);
this->appendLog(tr("Cheat loaded"));
ui->cheats->setPlainText(content);
}
}
void
MainWindow::readRom()
{
QString fileName = ui->input_path->text();
if (QFileInfo::exists(fileName)) {
getromname(fileName.toLocal8Bit().data(), romname);
ui->romname->setText(romname);
QFile f(fileName);
if (f.open(QFile::ReadOnly)) {
QString md5 = QCryptographicHash::hash(f.readAll(),QCryptographicHash::Md5).toHex();
ui->checksum->setText(QString("MD5 Checksum: %1").arg(md5));
}
this->appendLog(tr("Valid rom found"));
} else {
ui->romname->setText(tr("Input file not found"));
}
}
void
MainWindow::appendLog(QString text)
{
ui->log->appendPlainText(text);
}
void
MainWindow::removeIfExists(QString path)
{
if (QFileInfo::exists(path)) {
QFile file(path);
file.remove();
}
}
bool
MainWindow::isOutputDefined()
{
QString fileName = ui->output_path->text();
if (fileName.isEmpty()) {
return false;
}
return true;
}
bool
MainWindow::deadbeef()
{
ui->log->setPlainText("");
this->appendLog(tr("DEADBEEF process in progress"));
if (this->isOutputDefined()) {
this->removeIfExists(ui->output_path->text());
char* output = ui->output_path->text().toLocal8Bit().data();
char* input = ui->input_path->text().toLocal8Bit().data();
QString status = deadbeefrom(input, output);
this->appendLog(QString(output));
if (status.compare("-1") == 0) {
this->appendLog(tr("Something went wrong"));
return false;
}
this->appendLog(status);
this->appendLog(tr("DEADBEEF function added successfully"));
this->appendLog(tr("Run the game in an emulator and look at "
"0x3000000-0x300007FFF for DEADBEEFs"));
this->appendLog(tr("0x3007F00-0x3007FFF is a good place to start"));
this->appendLog(tr("Then enter the RAM to use address where at least 2 "
"DEADBEEFs are in a row"));
} else {
this->appendLog(tr("Output not defined"));
}
return true;
}
bool
MainWindow::prepareKeys()
{
if (ui->trainer_enable_keys->text().length() > 0) {
traineractions.enablekey = ConvertKeys(ui->trainer_enable_keys->text().toLocal8Bit().data());
if (traineractions.enablekey == 0x3ff) {
traineractions.enablekey = 0xfe;
ui->trainer_enable_keys->setText("L+R+A");
}
sprintf(traineractions.enablekeystr, "%s", ui->trainer_enable_keys->text().toLocal8Bit().data());
} else {
this->appendLog(tr("Enable keys is empty"));
return false;
}
if (ui->trainer_disable_keys->text().length() > 0) {
traineractions.disablekey = ConvertKeys(ui->trainer_disable_keys->text().toLocal8Bit().data());
if (traineractions.disablekey == 0x3ff) {
traineractions.disablekey = 0xfd;
ui->trainer_disable_keys->setText("L+R+B");
}
sprintf(traineractions.disablekeystr, "%s", ui->trainer_disable_keys->text().toLocal8Bit().data());
} else {
this->appendLog(tr("Disable keys is empty"));
return false;
}
return true;
}
void
MainWindow::patchGame()
{
ui->log->setPlainText("");
this->appendLog(tr("Game patching in progress"));
if (this->isOutputDefined()) {
this->appendLog(tr("Trainer enabled"));
if (!this->prepareKeys()) {
return;
}
Cheatcodes cheats;
if (ui->cheats->toPlainText().length() > 0) {
int cheat_type;
if (ui->mode->currentText() == "Codebreaker/GS V3") { // cb/gssp
cheat_type = 1;
} else if (ui->mode->currentText() == "RAW (VBA)") { // raw
cheat_type = 2;
} else { // gs
cheat_type = 3;
}
cheats.init(ui->cheats->toPlainText().toLocal8Bit().data(), ui->ram_block->currentText().toLocal8Bit().data(), cheat_type);
this->appendLog(tr("Cheats added"));
if (ui->menu_text->text().length() == 0) {
ui->menu_text->setText(ui->romname->text());
}
settings->setValue("rom/menutitle", ui->menu_text->text().toUpper());
cheats.titleGeneration(ui->menu_text->text());
} else {
this->appendLog(tr("Cheatcodes not imported"));
return;
}
this->removeIfExists(ui->output_path->text());
QString output = patchrom(ui->input_path->text().toLocal8Bit().data(), ui->output_path->text().toLocal8Bit().data(), cheats,
traineractions, ui->execute_every->text().toInt(), ui->vblank->isChecked(), customizetrainer);
this->appendLog(output);
} else {
this->appendLog(tr("Output not defined"));
}
}
MainWindow::~MainWindow()
{
delete ui;
}