forked from mrkite/minutor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitionmanager.cpp
574 lines (537 loc) · 18.8 KB
/
definitionmanager.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
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/** Copyright (c) 2013, Sean Kasun */
#include <QtWidgets/QHBoxLayout>
#include <QtWidgets/QTabWidget>
#include <QtWidgets/QTableWidget>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QPushButton>
#include <QStandardPaths>
#include <QtWidgets/QFileDialog>
#include <algorithm>
#include "./definitionmanager.h"
#include "./biomeidentifier.h"
#include "./blockidentifier.h"
#include "./dimensionidentifier.h"
#include "./entityidentifier.h"
#include "./flatteningconverter.h"
#include "./mapview.h"
#include "./json.h"
#include "./zipreader.h"
#include "./definitionupdater.h"
DefinitionManager::DefinitionManager(QWidget *parent) :
QWidget(parent),
biomeManager(BiomeIdentifier::Instance()),
blockManager(BlockIdentifier::Instance()),
dimensionManager(DimensionIdentifier::Instance()),
entityManager(EntityIdentifier::Instance()),
flatteningConverter(FlatteningConverter::Instance()),
isUpdating(false)
{
setWindowFlags(Qt::Window);
setWindowTitle(tr("Definitions"));
QVBoxLayout *layout = new QVBoxLayout;
QStringList labels;
labels << tr("Name") << tr("Version") << tr("Type");
table = new QTableWidget(0, 3);
table->setHorizontalHeaderLabels(labels);
table->horizontalHeader()->setSectionResizeMode(
QHeaderView::ResizeToContents);
table->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
table->horizontalHeader()->setHighlightSections(false);
table->verticalHeader()->hide();
table->setShowGrid(false);
table->setSelectionBehavior(QAbstractItemView::SelectRows);
layout->addWidget(table, 1);
QWidget *buttonBar = new QWidget;
QHBoxLayout *buttons = new QHBoxLayout;
QPushButton *add = new QPushButton(tr("Add Pack..."));
connect(add, SIGNAL(clicked()),
this, SLOT(addPack()));
buttons->addWidget(add);
QPushButton *remove = new QPushButton(tr("Remove Pack"));
connect(remove, SIGNAL(clicked()),
this, SLOT(removePack()));
connect(this, SIGNAL(packSelected(bool)),
remove, SLOT(setEnabled(bool)));
buttons->addWidget(remove);
QPushButton *save = new QPushButton(tr("Export Pack..."));
connect(save, SIGNAL(clicked()),
this, SLOT(exportPack()));
connect(this, SIGNAL(packSelected(bool)),
save, SLOT(setEnabled(bool)));
buttons->addWidget(save);
buttonBar->setLayout(buttons);
layout->addWidget(buttonBar, 0);
emit packSelected(false);
setLayout(layout);
// check & repair definition files
this->checkAndRepair();
// we load the definitions in backwards order for priority
QSettings settings;
sorted = settings.value("packs").toList();
for (int i = sorted.length() - 1; i >= 0; i--)
loadDefinition(sorted[i].toString());
// hook up table selection signal
connect(table,
SIGNAL(currentItemChanged(QTableWidgetItem*, QTableWidgetItem*)),
this, SLOT(selectedPack(QTableWidgetItem*, QTableWidgetItem*)));
// fill out table
refresh();
}
DefinitionManager::~DefinitionManager() {}
void DefinitionManager::checkAndRepair()
{
// create definition data folder on disk
QString destdir = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
QDir dir;
dir.mkpath(destdir);
// get known definition packs from application default settings storage
QSettings settings;
QList<QVariant> known_packs = settings.value("packs").toList();
// in Minutor up to 2.2.0 we used hash without seed, which is incompatible to Qt5.12
// force clean old hashed files generated without an extra seed
// this assumes, that these hashes will never occur otherwise
const QStringList old_hashed_list { "1050220429", "1241760321", "1443276275", "1798448990", "2422344665" };
for ( const auto& old_hashed_file : old_hashed_list ) {
QString old_path = destdir + "/" + old_hashed_file + ".json";
QFile::remove(old_path);
known_packs.removeOne(old_path);
}
// repair when definitions is on disk, but missing in settings
QDirIterator on_disk(destdir, QDir::Files | QDir::Readable);
while (on_disk.hasNext()) {
on_disk.next();
if (!known_packs.contains(on_disk.filePath())) {
known_packs.append(on_disk.filePath());
}
}
// repair when definition is in settings, but file is missing
for (const auto& def: known_packs) {
if (!QFile::exists(def.toString()))
known_packs.removeOne(def.toString());
}
// copy over built-in definitions if necessary
QDirIterator build_in(":/definitions", QDir::Files | QDir::Readable);
while (build_in.hasNext()) {
build_in.next();
installJson(build_in.filePath(), false, false);
}
// all changed definitions are now in sorted -> copy over
known_packs.append(sorted);
// store repaired list of definitions
settings.setValue("packs", known_packs);
}
void DefinitionManager::refresh() {
table->clearContents();
table->setRowCount(0);
QStringList types;
types << tr("block") << tr("biome") << tr("dimension")
<< tr("entity") << tr("pack") << tr("converter");
for (int i = 0; i < sorted.length(); i++) {
Definition &def = definitions[sorted[i].toString()];
int row = table->rowCount();
table->insertRow(row);
QTableWidgetItem *name = new QTableWidgetItem(def.name);
name->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
name->setData(Qt::UserRole, def.path);
table->setItem(row, 0, name);
QTableWidgetItem *ver = new QTableWidgetItem(def.version);
ver->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
ver->setData(Qt::UserRole, def.path);
table->setItem(row, 1, ver);
QTableWidgetItem *type = new QTableWidgetItem(types[def.type]);
type->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
type->setData(Qt::UserRole, def.path);
table->setItem(row, 2, type);
}
}
void DefinitionManager::selectedPack(QTableWidgetItem *item,
QTableWidgetItem *) {
emit packSelected(item != NULL);
if (item != NULL)
selected = item->data(Qt::UserRole).toString();
else
selected = QString();
}
void DefinitionManager::toggledPack(bool onoff) {
if (definitions.contains(selected)) {
Definition &def = definitions[selected];
def.enabled = onoff;
switch (def.type) {
case Definition::Block:
if (onoff)
blockManager.enableDefinitions(def.id);
else
blockManager.disableDefinitions(def.id);
break;
case Definition::Biome:
if (onoff)
biomeManager.enableDefinitions(def.id);
else
biomeManager.disableDefinitions(def.id);
break;
case Definition::Dimension:
if (onoff)
dimensionManager.enableDefinitions(def.id);
else
dimensionManager.disableDefinitions(def.id);
break;
case Definition::Entity:
if (onoff)
entityManager.enableDefinitions(def.id);
else
entityManager.disableDefinitions(def.id);
break;
case Definition::Pack:
if (onoff) {
blockManager.enableDefinitions(def.blockid);
biomeManager.enableDefinitions(def.biomeid);
dimensionManager.enableDefinitions(def.dimensionid);
entityManager.enableDefinitions(def.entityid);
} else {
blockManager.disableDefinitions(def.blockid);
biomeManager.disableDefinitions(def.biomeid);
dimensionManager.disableDefinitions(def.dimensionid);
entityManager.disableDefinitions(def.entityid);
}
break;
}
}
emit packsChanged();
refresh();
}
void DefinitionManager::addPack() {
QString packName = QFileDialog::getOpenFileName(this, tr("Open Pack"),
QString(), tr("Definitions (*.zip *.json)"));
if (!packName.isEmpty()) {
if (packName.endsWith(".json", Qt::CaseInsensitive)) // raw json
installJson(packName);
else
installZip(packName);
emit packsChanged();
QSettings settings;
settings.setValue("packs", sorted);
refresh();
}
}
void DefinitionManager::installJson(QString path, bool overwrite,
bool install) {
QString destdir = QStandardPaths::writableLocation(
QStandardPaths::DataLocation);
std::unique_ptr<JSONData> def;
QFile f(path);
f.open(QIODevice::ReadOnly);
try {
def = JSON::parse(f.readAll());
f.close();
} catch (JSONParseException e) {
f.close();
QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
e.reason,
QMessageBox::Cancel);
return;
}
QString key = def->at("name")->asString() + def->at("type")->asString();
QString exeversion = def->at("version")->asString();
QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".json";
// check if build in version is newer than version on disk
if (QFile::exists(dest)) {
QFile f(dest);
f.open(QIODevice::ReadOnly);
try {
def = JSON::parse(f.readAll());
f.close();
} catch (JSONParseException e) {
f.close();
return;
}
QString fileversion = def->at("version")->asString();
if (exeversion.compare(fileversion, Qt::CaseInsensitive) > 0) {
// force overwriting outdated local copy
QFile::remove(dest);
QFile::copy(path, dest);
QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner);
}
}
// import new definition (if file is not present)
if (!QFile::exists(dest) || overwrite) {
if (QFile::exists(dest) && install) {
removeDefinition(dest);
QFile::remove(dest);
}
if (!QFile::copy(path, dest)) {
QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
tr("Copy error"),
QMessageBox::Cancel);
return;
}
// fix permissions since we might be copying a readonly resource.
QFile::setPermissions(dest, QFile::ReadOwner|QFile::WriteOwner);
sorted.prepend(dest);
if (install)
loadDefinition(dest);
}
}
void DefinitionManager::installZip(QString path, bool overwrite,
bool install) {
QString destdir = QStandardPaths::writableLocation(
QStandardPaths::DataLocation);
ZipReader zip(path);
if (!zip.open()) {
QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
tr("Corrupt zip"),
QMessageBox::Cancel);
return;
}
// fetch the pack info
std::unique_ptr<JSONData> info;
try {
info = JSON::parse(zip.get("pack_info.json"));
} catch (JSONParseException e) {
QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
tr("pack_info.json : %1").arg(e.reason),
QMessageBox::Cancel);
zip.close();
return;
}
// let's verify all the jsons in the pack
for (int i = 0; i < info->at("data")->length(); i++) {
std::unique_ptr<JSONData> def;
try {
def = JSON::parse(zip.get(info->at("data")->at(i)->asString()));
} catch (JSONParseException e) {
QMessageBox::warning(this, tr("Couldn't install %1").arg(path),
tr("%1: %2")
.arg(info->at("data")->at(i)->asString(),
e.reason), QMessageBox::Cancel);
zip.close();
return;
}
}
QString key = info->at("name")->asString() + info->at("type")->asString();
QString dest = destdir + "/" + QString("%1").arg(qHash(key,42)) + ".zip";
if (!QFile::exists(dest) || overwrite) {
if (QFile::exists(dest) && install)
removeDefinition(dest);
if (!QFile::copy(path, dest)) {
QMessageBox::warning(this,
tr("Couldn't install %1").arg(path),
tr("Copy error"),
QMessageBox::Cancel);
return;
}
sorted.prepend(dest);
if (install)
loadDefinition(dest);
}
}
void DefinitionManager::removePack() {
// find selected pack
if (definitions.contains(selected)) {
int ret = QMessageBox::question(this, tr("Delete Pack"),
tr("Are you sure you want to delete %1?")
.arg(definitions[selected].name),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::No);
if (ret == QMessageBox::Yes)
removeDefinition(selected);
}
}
void DefinitionManager::exportPack() {
// find selected pack
if (definitions.contains(selected)) {
QString fname = definitions[selected].name;
switch (definitions[selected].type) {
case Definition::Block: fname+="_blocks"; break;
case Definition::Biome: fname+="_biomes"; break;
case Definition::Dimension: fname+="_dims"; break;
default: break;
}
if (selected.endsWith(".zip"))
fname += ".zip";
else
fname += ".json";
QString dest = QFileDialog::getSaveFileName(this, tr("Save Pack As"),
fname, tr("Definitions (*.zip *.json)"));
if (!dest.isEmpty()) {
if (!QFile::copy(selected, dest)) {
QMessageBox::warning(this,
tr("Couldn't write to %1").arg(dest),
tr("Copy error"),
QMessageBox::Cancel);
}
}
}
}
QSize DefinitionManager::minimumSizeHint() const {
return QSize(300, 300);
}
QSize DefinitionManager::sizeHint() const {
return QSize(400, 300);
}
void DefinitionManager::loadDefinition(QString path) {
// determine if we're loading a single json or a pack
if (path.endsWith(".json", Qt::CaseInsensitive)) {
std::unique_ptr<JSONData> def;
QFile f(path);
if (!f.open(QIODevice::ReadOnly)) return;
try {
def = JSON::parse(f.readAll());
f.close();
} catch (JSONParseException e) {
f.close();
return;
}
Definition d;
d.name = def->at("name")->asString();
d.version = def->at("version")->asString();
d.path = path;
d.update = def->at("update")->asString();
QString type = def->at("type")->asString();
QString key = d.name + type;
d.enabled = true; // should look this up
if (type == "block") {
d.id = flatteningConverter.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")));
d.type = Definition::Converter;
} else if (type == "flatblock") {
d.id = blockManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")));
d.type = Definition::Block;
} else if (type == "biome") {
d.id = biomeManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")));
d.type = Definition::Biome;
} else if (type == "dimension") {
d.id = dimensionManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")));
d.type = Definition::Dimension;
} else if (type == "entity") {
d.id = entityManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")));
d.type = Definition::Entity;
} else {
return; // unknown type
}
definitions.insert(path, d);
} else {
ZipReader zip(path);
if (!zip.open())
return;
std::unique_ptr<JSONData> info;
try {
info = JSON::parse(zip.get("pack_info.json"));
} catch (JSONParseException e) {
zip.close();
return;
}
Definition d;
d.name = info->at("name")->asString();
d.version = info->at("version")->asString();
d.update = info->at("update")->asString();
d.path = path;
d.enabled = true;
d.id = 0;
d.type = Definition::Pack;
d.blockid = -1;
d.biomeid = -1;
d.dimensionid = -1;
d.entityid = -1;
QString key = d.name+"pack";
for (int i = 0; i < info->at("data")->length(); i++) {
std::unique_ptr<JSONData> def;
try {
def = JSON::parse(zip.get(info->at("data")->at(i)->asString()));
} catch (JSONParseException e) {
continue;
}
QString type = def->at("type")->asString();
if (type == "block") {
// d.blockid = flatteningConverter->addDefinitions(
// dynamic_cast<JSONArray*>(def->at("data")), d.blockid);
} else if (type == "flatblock") {
d.blockid = blockManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")), d.blockid);
} else if (type == "biome") {
d.biomeid = biomeManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")), d.biomeid);
} else if (type == "dimension") {
d.dimensionid = dimensionManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")), d.dimensionid);
} else if (type == "entity") {
d.entityid = entityManager.addDefinitions(
dynamic_cast<JSONArray*>(def->at("data")), d.entityid);
}
}
definitions.insert(path, d);
zip.close();
}
}
void DefinitionManager::removeDefinition(QString path) {
// find the definition and remove it from disk
Definition &def = definitions[path];
if (def.path == path) {
switch (def.type) {
case Definition::Block:
blockManager.disableDefinitions(def.id);
break;
case Definition::Biome:
biomeManager.disableDefinitions(def.id);
break;
case Definition::Dimension:
dimensionManager.disableDefinitions(def.id);
break;
case Definition::Entity:
entityManager.disableDefinitions(def.id);
break;
case Definition::Pack:
blockManager.disableDefinitions(def.blockid);
biomeManager.disableDefinitions(def.biomeid);
dimensionManager.disableDefinitions(def.dimensionid);
entityManager.disableDefinitions(def.entityid);
break;
}
definitions.remove(path);
QFile::remove(path);
sorted.removeOne(path);
QSettings settings;
settings.setValue("packs", sorted);
emit packsChanged();
refresh();
}
}
void DefinitionManager::checkForUpdates() {
// show update dialog
if (!isUpdating)
autoUpdate();
if (!isUpdating) { // nothing needs updating
// hide update dialog
// show completion
}
}
void DefinitionManager::autoUpdate() {
for (int i = 0; i < sorted.length(); i++) {
QString name = sorted[i].toString();
Definition &def = definitions[name];
if (!def.update.isEmpty()) {
isUpdating = true;
auto updater = new DefinitionUpdater(name, def.update, def.version);
connect(updater, SIGNAL(updated (DefinitionUpdater *, QString, QString)),
this, SLOT (updatePack(DefinitionUpdater *, QString, QString)));
updateQueue.append(updater);
updater->update();
}
}
QSettings settings;
settings.setValue("packupdate", QDateTime::currentDateTime()); // store current time
settings.remove("packupdates"); // remove now unused entries from registry
}
void DefinitionManager::updatePack(DefinitionUpdater *updater,
QString filename,
QString version) {
updateQueue.removeOne(updater);
delete updater;
if (updateQueue.isEmpty())
emit updateFinished();
}