forked from mrkite/TerraFirma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
343 lines (305 loc) · 8.97 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
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
/**
* @Copyright 2015 seancode
*
* The main window for terrafirma
*/
#include <QMessageBox>
#include <QStandardPaths>
#include <QDir>
#include <QDirIterator>
#include <QFileDialog>
#include <QSettings>
#include <QDebug>
#include <QLabel>
#include "./mainwindow.h"
#include "./ui_mainwindow.h"
#include "./handle.h"
#include "./aes128.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
info = NULL;
kills = NULL;
findChests = NULL;
hilite = NULL;
QLabel *status = new QLabel();
ui->statusBar->addWidget(status, 1);
connect(ui->map, SIGNAL(error(QString)),
this, SLOT(showError(QString)));
connect(ui->map, SIGNAL(status(QString)),
status, SLOT(setText(QString)), Qt::DirectConnection);
settings = new SettingsDialog(this);
connect(settings, SIGNAL(accepted()), this, SLOT(resetPaths()));
world = QSharedPointer<World>(new World(this));
try {
world->init();
} catch (World::InitException &e) {
QMessageBox::warning(this,
e.title,
e.reason,
QMessageBox::Cancel);
}
ui->map->setTexturePath(settings->getTextures());
ui->map->setWorld(world);
connect(world.data(), SIGNAL(loadError(QString)),
this, SLOT(showError(QString)));
connect(world.data(), SIGNAL(status(QString)),
status, SLOT(setText(QString)));
connect(world.data(), SIGNAL(loaded(bool)),
this, SLOT(setNPCs(bool)));
QSettings info;
ui->actionFog_of_War->setChecked(info.value("fogOfWar", true).toBool());
ui->actionShow_Houses->setChecked(info.value("houses", false).toBool());
ui->actionShow_Wires->setChecked(info.value("wires", false).toBool());
ui->actionUse_Textures->setChecked(info.value("textures", true).toBool());
scanWorlds();
scanPlayers();
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::openWorld() {
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
QString filename = action->data().toString();
if (filename.isEmpty())
filename = QFileDialog::getOpenFileName(this, tr("Open World"),
"",
tr("Terraria Worlds (*.wld)"));
if (!filename.isEmpty()) {
if (findChests != nullptr) {
delete findChests;
findChests = nullptr;
}
ui->map->load(filename);
}
}
}
void MainWindow::save() {
QFileDialog fileDialog(this);
fileDialog.setDefaultSuffix("png");
QString filename = fileDialog.getSaveFileName(this, tr("Save PNG"),
"", "*.png");
if (!filename.isEmpty()) {
ui->map->update();
ui->map->makeCurrent();
ui->map->glFlush();
QImage img = ui->map->grabFramebuffer();
ui->map->doneCurrent();
img.save(filename, "PNG");
}
}
void MainWindow::selectPlayer() {
QAction *action = qobject_cast<QAction*>(sender());
if (action)
world->setPlayer(action->data().toString());
}
void MainWindow::findItem() {
if (findChests != NULL) {
findChests->show();
} else {
findChests = new FindChests(world->chests, this);
findChests->show();
connect(findChests, SIGNAL(jump(QPointF)),
ui->map, SLOT(jumpToLocation(QPointF)));
}
}
void MainWindow::hiliteBlock() {
if (hilite != NULL) {
hilite->show();
} else {
hilite = new HiliteDialog(world, this);
hilite->show();
connect(hilite, SIGNAL(accepted()),
ui->map, SLOT(startHilighting()));
}
}
void MainWindow::worldInfo() {
if (info != NULL) {
info->close();
delete info;
}
info = new InfoDialog(world->header, this);
info->show();
}
void MainWindow::worldKills() {
if (kills != NULL) {
kills->close();
delete kills;
}
kills = new KillDialog(world->header, world->info, this);
kills->show();
}
void MainWindow::showAbout() {
QMessageBox::about(this, tr("About %1").arg(qApp->applicationName()),
tr("<b>%1</b> v%2<br/>\n"
"© Copyright %3, %4")
.arg(qApp->applicationName())
.arg(qApp->applicationVersion())
.arg(2016)
.arg(qApp->organizationName()));
}
void MainWindow::showSettings() {
settings->show();
}
void MainWindow::resetPaths() {
scanWorlds();
scanPlayers();
ui->map->setTexturePath(settings->getTextures());
}
void MainWindow::showError(QString msg) {
QMessageBox::warning(this,
"Error",
msg,
QMessageBox::Cancel);
}
void MainWindow::setNPCs(bool loaded) {
ui->menuNPCs->clear();
if (!loaded) return;
QList<QAction *> actions;
for (auto const &npc : world->npcs) {
QAction *n = new QAction(this);
QString name;
if (npc.name.isEmpty())
name = npc.title;
else
name = tr("%1 the %2").arg(npc.name).arg(npc.title);
if (npc.homeless) {
n->setText(tr("Jump to %1's Location").arg(name));
n->setData(QPointF(npc.x, npc.y));
} else {
n->setText(tr("Jump to %1's Home").arg(name));
n->setData(QPointF(npc.homeX, npc.homeY));
}
connect(n, SIGNAL(triggered()),
this, SLOT(jumpNPC()));
actions.append(n);
}
ui->menuNPCs->addActions(actions);
ui->menuNPCs->setDisabled(actions.isEmpty());
}
void MainWindow::jumpNPC() {
QAction *action = qobject_cast<QAction*>(sender());
if (action) {
QPointF point = action->data().toPointF();
ui->map->jumpToLocation(point);
}
}
void MainWindow::scanWorlds() {
ui->menuOpen_World->clear();
bool enabled = false;
int key = 0;
for (const QString &worldDir : settings->getWorlds()) {
QDir dir(worldDir);
QDirIterator it(dir);
QList<QAction *> actions;
while (it.hasNext()) {
it.next();
if (it.fileName().endsWith(".wld")) {
QString name = worldName(it.filePath());
if (!name.isNull()) {
QAction *w = new QAction(this);
w->setText(name);
w->setData(it.filePath());
if (key < 9) {
w->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_1 + key++));
w->setShortcutContext(Qt::ApplicationShortcut);
}
connect(w, SIGNAL(triggered()),
this, SLOT(openWorld()));
actions.append(w);
}
}
}
if (!actions.isEmpty()) {
ui->menuOpen_World->addSection(worldDir);
ui->menuOpen_World->addActions(actions);
enabled = true;
}
}
ui->menuOpen_World->setDisabled(!enabled);
}
void MainWindow::scanPlayers() {
ui->menuSelect_Player->clear();
bool enabled = false;
for (const QString &playerDir : settings->getPlayers()) {
QDir dir(playerDir);
QActionGroup *group = new QActionGroup(this);
bool checked = false;
QDirIterator it(dir);
QList<QAction *> actions;
while (it.hasNext()) {
it.next();
if (it.fileName().endsWith(".plr")) {
QString name = playerName(it.filePath());
if (!name.isNull()) {
QAction *p = new QAction(this);
p->setCheckable(true);
p->setActionGroup(group);
p->setText(name);
p->setData(it.filePath());
connect(p, SIGNAL(triggered()),
this, SLOT(selectPlayer()));
if (!checked) {
p->setChecked(true);
p->trigger();
}
checked = true;
actions.append(p);
}
}
}
if (!actions.isEmpty()) {
ui->menuOpen_World->addSection(playerDir);
ui->menuSelect_Player->addActions(actions);
enabled = true;
}
}
ui->menuSelect_Player->setDisabled(!enabled);
}
QString MainWindow::worldName(QString path) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
return QString();
Handle handle(file.read(4096)); // read first 4k of world.. should be enough
file.close();
quint32 version = handle.r32();
if (version >= 135) {
QString magic = handle.read(7);
if (magic != "relogic")
return QString();
quint8 type = handle.r8();
if (type != 2)
return QString();
handle.skip(4 + 8); // revision + favorites
}
if (version >= 88) {
handle.skip(2); // # of section pointers
handle.seek(handle.r32()); // jump to first section
}
return handle.rs();
}
QString MainWindow::playerName(QString path) {
QFile file(path);
if (!file.open(QIODevice::ReadOnly))
return QString();
QString keystr = "h3y_gUyZ";
QByteArray key;
for (int i = 0; i < 8; i++) {
key.append(keystr.at(i));
key.append(static_cast<char>(0));
}
Handle handle(AES128::decrypt(file.readAll(), key, key));
file.close();
quint32 version = handle.r32();
if (version >= 135) {
QString magic = handle.read(7);
if (magic != "relogic")
return QString();
quint8 type = handle.r8();
if (type != 3)
return QString();
handle.skip(4 + 8); // revision + favorites
}
return handle.rs();
}