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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
|
#include "ServersPage.h"
#include "ui_ServersPage.h"
#include <FileSystem.h>
#include <sstream>
#include <io/stream_reader.h>
#include <tag_string.h>
#include <tag_primitive.h>
#include <tag_list.h>
#include <tag_compound.h>
#include <minecraft/MinecraftInstance.h>
#include <QFileSystemWatcher>
#include <QMenu>
static const int COLUMN_COUNT = 2; // 3 , TBD: latency and other nice things.
struct Server
{
// Types
enum class AcceptsTextures : int
{
ASK = 0,
ALWAYS = 1,
NEVER = 2
};
// Methods
Server()
{
m_name = QObject::tr("Minecraft Server");
}
Server(const QString & name, const QString & address)
{
m_name = name;
m_address = address;
}
Server(nbt::tag_compound& server)
{
std::string addressStr(server["ip"]);
m_address = QString::fromUtf8(addressStr.c_str());
std::string nameStr(server["name"]);
m_name = QString::fromUtf8(nameStr.c_str());
if(server["icon"])
{
std::string base64str(server["icon"]);
m_icon = QByteArray::fromBase64(base64str.c_str());
}
if(server.has_key("acceptTextures", nbt::tag_type::Byte))
{
bool value = server["acceptTextures"].as<nbt::tag_byte>().get();
if(value)
{
m_acceptsTextures = AcceptsTextures::ALWAYS;
}
else
{
m_acceptsTextures = AcceptsTextures::NEVER;
}
}
}
void serialize(nbt::tag_compound& server)
{
server.insert("name", m_name.trimmed().toUtf8().toStdString());
server.insert("ip", m_address.trimmed().toUtf8().toStdString());
if(m_icon.size())
{
server.insert("icon", m_icon.toBase64().toStdString());
}
if(m_acceptsTextures != AcceptsTextures::ASK)
{
server.insert("acceptTextures", nbt::tag_byte(m_acceptsTextures == AcceptsTextures::ALWAYS));
}
}
// Data - persistent and user changeable
QString m_name;
QString m_address;
AcceptsTextures m_acceptsTextures = AcceptsTextures::ASK;
// Data - persistent and automatically updated
QByteArray m_icon;
// Data - temporary
bool m_checked = false;
bool m_up = false;
QString m_motd; // https://mctools.org/motd-creator
int m_ping = 0;
int m_currentPlayers = 0;
int m_maxPlayers = 0;
};
static std::unique_ptr <nbt::tag_compound> parseServersDat(const QString& filename)
{
try
{
QByteArray input = FS::read(filename);
std::istringstream foo(std::string(input.constData(), input.size()));
auto pair = nbt::io::read_compound(foo);
if(pair.first != "")
return nullptr;
if(pair.second == nullptr)
return nullptr;
return std::move(pair.second);
}
catch (...)
{
return nullptr;
}
}
static bool serializeServerDat(const QString& filename, nbt::tag_compound * levelInfo)
{
try
{
if(!FS::ensureFilePathExists(filename))
{
return false;
}
std::ostringstream s;
nbt::io::write_tag("", *levelInfo, s);
QByteArray val(s.str().data(), (int) s.str().size() );
FS::write(filename, val);
return true;
}
catch (...)
{
return false;
}
}
class ServersModel: public QAbstractListModel
{
Q_OBJECT
public:
enum Roles
{
ServerPtrRole = Qt::UserRole,
};
explicit ServersModel(const QString &path, QObject *parent = 0)
: QAbstractListModel(parent)
{
m_path = path;
m_watcher = new QFileSystemWatcher(this);
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &ServersModel::fileChanged);
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &ServersModel::dirChanged);
m_saveTimer.setSingleShot(true);
m_saveTimer.setInterval(5000);
connect(&m_saveTimer, &QTimer::timeout, this, &ServersModel::save_internal);
}
virtual ~ServersModel() {};
void observe()
{
if(m_observed)
{
return;
}
m_observed = true;
if(!m_loaded)
{
load();
}
updateFSObserver();
}
void unobserve()
{
if(!m_observed)
{
return;
}
m_observed = false;
updateFSObserver();
}
void lock()
{
if(m_locked)
{
return;
}
saveNow();
m_locked = true;
updateFSObserver();
}
void unlock()
{
if(!m_locked)
{
return;
}
m_locked = false;
updateFSObserver();
}
int addEmptyRow(int position)
{
if(m_locked)
{
return -1;
}
if(position < 0 || position >= rowCount())
{
position = rowCount();
}
beginInsertRows(QModelIndex(), position, position);
m_servers.insert(position, Server());
endInsertRows();
scheduleSave();
return position;
}
bool removeRow(int row)
{
if(m_locked)
{
return false;
}
if(row < 0 || row >= rowCount())
{
return false;
}
beginRemoveRows(QModelIndex(), row, row);
m_servers.removeAt(row);
endRemoveRows(); // does absolutely nothing, the selected server stays as the next line...
scheduleSave();
return true;
}
bool moveUp(int row)
{
if(m_locked)
{
return false;
}
if(row <= 0)
{
return false;
}
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
m_servers.swap(row-1, row);
endMoveRows();
scheduleSave();
return true;
}
bool moveDown(int row)
{
if(m_locked)
{
return false;
}
int count = rowCount();
if(row + 1 >= count)
{
return false;
}
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2);
m_servers.swap(row+1, row);
endMoveRows();
scheduleSave();
return true;
}
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if (section < 0 || section >= COLUMN_COUNT)
return QVariant();
if(role == Qt::DisplayRole)
{
switch(section)
{
case 0:
return tr("Name");
case 1:
return tr("Address");
case 2:
return tr("Latency");
}
}
return QAbstractListModel::headerData(section, orientation, role);
}
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
{
if (!index.isValid())
return QVariant();
int row = index.row();
int column = index.column();
if(column < 0 || column >= COLUMN_COUNT)
return QVariant();
if (row < 0 || row >= m_servers.size())
return QVariant();
switch(column)
{
case 0:
switch (role)
{
case Qt::DecorationRole:
{
auto & bytes = m_servers[row].m_icon;
if(bytes.size())
{
QPixmap px;
if(px.loadFromData(bytes))
return QIcon(px);
}
return MMC->getThemedIcon("unknown_server");
}
case Qt::DisplayRole:
return m_servers[row].m_name;
case ServerPtrRole:
return QVariant::fromValue<void *>((void *)&m_servers[row]);
default:
return QVariant();
}
case 1:
switch (role)
{
case Qt::DisplayRole:
return m_servers[row].m_address;
default:
return QVariant();
}
case 2:
switch (role)
{
case Qt::DisplayRole:
return m_servers[row].m_ping;
default:
return QVariant();
}
default:
return QVariant();
}
}
virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
return m_servers.size();
}
int columnCount(const QModelIndex & parent) const override
{
return COLUMN_COUNT;
}
Server * at(int index)
{
if(index < 0 || index >= rowCount())
{
return nullptr;
}
return &m_servers[index];
}
void setName(int row, const QString & name)
{
if(m_locked)
{
return;
}
auto server = at(row);
if(!server || server->m_name == name)
{
return;
}
server->m_name = name;
emit dataChanged(index(row, 0), index(row, COLUMN_COUNT - 1));
scheduleSave();
}
void setAddress(int row, const QString & address)
{
if(m_locked)
{
return;
}
auto server = at(row);
if(!server || server->m_address == address)
{
return;
}
server->m_address = address;
emit dataChanged(index(row, 0), index(row, COLUMN_COUNT - 1));
scheduleSave();
}
void setAcceptsTextures(int row, Server::AcceptsTextures textures)
{
if(m_locked)
{
return;
}
auto server = at(row);
if(!server || server->m_acceptsTextures == textures)
{
return;
}
server->m_acceptsTextures = textures;
emit dataChanged(index(row, 0), index(row, COLUMN_COUNT - 1));
scheduleSave();
}
void load()
{
cancelSave();
beginResetModel();
QList<Server> servers;
auto serversDat = parseServersDat(serversPath());
if(serversDat)
{
auto &serversList = serversDat->at("servers").as<nbt::tag_list>();
for(auto iter = serversList.begin(); iter != serversList.end(); iter++)
{
auto & serverTag = (*iter).as<nbt::tag_compound>();
Server s(serverTag);
servers.append(s);
}
}
m_servers.swap(servers);
m_loaded = true;
endResetModel();
}
void saveNow()
{
if(saveIsScheduled())
{
save_internal();
}
}
public slots:
void dirChanged(const QString& path)
{
qDebug() << "Changed:" << path;
load();
}
void fileChanged(const QString& path)
{
qDebug() << "Changed:" << path;
}
private slots:
void save_internal()
{
cancelSave();
QString path = serversPath();
qDebug() << "Server list about to be saved to" << path;
nbt::tag_compound out;
nbt::tag_list list;
for(auto & server: m_servers)
{
nbt::tag_compound serverNbt;
server.serialize(serverNbt);
list.push_back(std::move(serverNbt));
}
out.insert("servers", nbt::value(std::move(list)));
if(!serializeServerDat(path, &out))
{
qDebug() << "Failed to save server list:" << path << "Will try again.";
scheduleSave();
}
}
private:
void scheduleSave()
{
if(!m_loaded)
{
qDebug() << "Server list should never save if it didn't successfully load, path:" << m_path;
return;
}
if(!m_dirty)
{
m_dirty = true;
qDebug() << "Server list save is scheduled for" << m_path;
}
m_saveTimer.start();
}
void cancelSave()
{
m_dirty = false;
m_saveTimer.stop();
}
bool saveIsScheduled() const
{
return m_dirty;
}
void updateFSObserver()
{
bool observingFS = m_watcher->directories().contains(m_path);
if(m_observed && m_locked)
{
if(!observingFS)
{
qWarning() << "Will watch" << m_path;
if(!m_watcher->addPath(m_path))
{
qWarning() << "Failed to start watching" << m_path;
}
}
}
else
{
if(observingFS)
{
qWarning() << "Will stop watching" << m_path;
if(!m_watcher->removePath(m_path))
{
qWarning() << "Failed to stop watching" << m_path;
}
}
}
}
QString serversPath()
{
QFileInfo foo(FS::PathCombine(m_path, "servers.dat"));
return foo.filePath();
}
private:
bool m_loaded = false;
bool m_locked = false;
bool m_observed = false;
bool m_dirty = false;
QString m_path;
QList<Server> m_servers;
QFileSystemWatcher *m_watcher = nullptr;
QTimer m_saveTimer;
};
ServersPage::ServersPage(MinecraftInstance * inst, QWidget* parent)
: QMainWindow(parent), ui(new Ui::ServersPage)
{
ui->setupUi(this);
m_inst = inst;
m_model = new ServersModel(inst->gameRoot(), this);
ui->serversView->setIconSize(QSize(64,64));
ui->serversView->setModel(m_model);
ui->serversView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->serversView, &QTreeView::customContextMenuRequested, this, &ServersPage::ShowContextMenu);
auto head = ui->serversView->header();
if(head->count())
{
head->setSectionResizeMode(0, QHeaderView::Stretch);
for(int i = 1; i < head->count(); i++)
{
head->setSectionResizeMode(i, QHeaderView::ResizeToContents);
}
}
auto selectionModel = ui->serversView->selectionModel();
connect(selectionModel, &QItemSelectionModel::currentChanged, this, &ServersPage::currentChanged);
connect(m_inst, &MinecraftInstance::runningStatusChanged, this, &ServersPage::on_RunningState_changed);
connect(ui->nameLine, &QLineEdit::textEdited, this, &ServersPage::nameEdited);
connect(ui->addressLine, &QLineEdit::textEdited, this, &ServersPage::addressEdited);
connect(ui->resourceComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(resourceIndexChanged(int)));
connect(m_model, &QAbstractItemModel::rowsRemoved, this, &ServersPage::rowsRemoved);
m_locked = m_inst->isRunning();
if(m_locked)
{
m_model->lock();
}
updateState();
}
ServersPage::~ServersPage()
{
m_model->saveNow();
delete ui;
}
void ServersPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->serversView->mapToGlobal(pos));
delete menu;
}
QMenu * ServersPage::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();
filteredMenu->removeAction( ui->toolBar->toggleViewAction() );
return filteredMenu;
}
void ServersPage::on_RunningState_changed(bool running)
{
if(m_locked == running)
{
return;
}
m_locked = running;
if(m_locked)
{
m_model->lock();
}
else
{
m_model->unlock();
}
updateState();
}
void ServersPage::currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
int nextServer = -1;
if (!current.isValid())
{
nextServer = -1;
}
else
{
nextServer = current.row();
}
currentServer = nextServer;
updateState();
}
// WARNING: this is here because currentChanged is not accurate when removing rows. the current item needs to be fixed up after removal.
void ServersPage::rowsRemoved(const QModelIndex& parent, int first, int last)
{
if(currentServer < first)
{
// current was before the removal
return;
}
else if(currentServer >= first && currentServer <= last)
{
// current got removed...
return;
}
else
{
// current was past the removal
int count = last - first + 1;
currentServer -= count;
}
}
void ServersPage::nameEdited(const QString& name)
{
m_model->setName(currentServer, name);
}
void ServersPage::addressEdited(const QString& address)
{
m_model->setAddress(currentServer, address);
}
void ServersPage::resourceIndexChanged(int index)
{
auto acceptsTextures = Server::AcceptsTextures(index);
m_model->setAcceptsTextures(currentServer, acceptsTextures);
}
void ServersPage::updateState()
{
auto server = m_model->at(currentServer);
bool serverEditEnabled = server && !m_locked;
ui->addressLine->setEnabled(serverEditEnabled);
ui->nameLine->setEnabled(serverEditEnabled);
ui->resourceComboBox->setEnabled(serverEditEnabled);
ui->actionMove_Down->setEnabled(serverEditEnabled);
ui->actionMove_Up->setEnabled(serverEditEnabled);
ui->actionRemove->setEnabled(serverEditEnabled);
if(server)
{
ui->addressLine->setText(server->m_address);
ui->nameLine->setText(server->m_name);
ui->resourceComboBox->setCurrentIndex(int(server->m_acceptsTextures));
}
else
{
ui->addressLine->setText(QString());
ui->nameLine->setText(QString());
ui->resourceComboBox->setCurrentIndex(0);
}
ui->actionAdd->setDisabled(m_locked);
}
void ServersPage::openedImpl()
{
m_model->observe();
}
void ServersPage::closedImpl()
{
m_model->unobserve();
}
void ServersPage::on_actionAdd_triggered()
{
int position = m_model->addEmptyRow(currentServer + 1);
if(position < 0)
{
return;
}
// select the new row
ui->serversView->selectionModel()->setCurrentIndex(
m_model->index(position),
QItemSelectionModel::SelectCurrent | QItemSelectionModel::Clear | QItemSelectionModel::Rows
);
currentServer = position;
}
void ServersPage::on_actionRemove_triggered()
{
m_model->removeRow(currentServer);
}
void ServersPage::on_actionMove_Up_triggered()
{
if(m_model->moveUp(currentServer))
{
currentServer --;
}
}
void ServersPage::on_actionMove_Down_triggered()
{
if(m_model->moveDown(currentServer))
{
currentServer ++;
}
}
#include "ServersPage.moc"
|