/
XMLimport.cpp
1993 lines (1792 loc) · 82.6 KB
/
XMLimport.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
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
* Copyright (C) 2008-2013 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* Copyright (C) 2016-2024 by Stephen Lyons - slysven@virginmedia.com *
* Copyright (C) 2016-2017 by Ian Adkins - ieadkins@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "XMLimport.h"
#include "dlgMapper.h"
#include "LuaInterface.h"
#include "TConsole.h"
#include "TMap.h"
#include "TRoomDB.h"
#include "TRoom.h"
#include "VarUnit.h"
#include "mudlet.h"
#include "pre_guard.h"
#include <QBuffer>
#include <QtMath>
#include "post_guard.h"
XMLimport::XMLimport(Host* pH)
: mpHost(pH)
, mPackageName(QString())
, mpTrigger(nullptr)
, mpTimer(nullptr)
, mpAlias(nullptr)
, mpKey(nullptr)
, mpAction(nullptr)
, mpScript(nullptr)
, mpVar(nullptr)
, gotTrigger(false)
, gotTimer(false)
, gotAlias(false)
, gotKey(false)
, gotAction(false)
, gotScript(false)
, module(0)
, mMaxRoomId(0)
, mVersionMajor(1) // 0 to 255
, mVersionMinor(0) // 0 to 999 for 3 digit decimal value
{
}
std::pair<bool, QString> XMLimport::importPackage(QFile* pfile, QString packName, int moduleFlag, QString* pVersionString)
{
mPackageName = packName;
setDevice(pfile);
module = moduleFlag;
if (!packName.isEmpty()) {
mpKey = new TKey(nullptr, mpHost);
if (module) {
mpKey->mModuleMasterFolder = true;
mpKey->mModuleMember = true;
}
mpKey->mPackageName = mPackageName;
mpKey->setIsActive(true);
mpKey->setName(mPackageName);
mpKey->setIsFolder(true);
mpTrigger = new TTrigger(nullptr, mpHost);
if (module) {
mpTrigger->mModuleMasterFolder = true;
mpTrigger->mModuleMember = true;
}
mpTrigger->mPackageName = mPackageName;
mpTrigger->setIsActive(true);
mpTrigger->setName(mPackageName);
mpTrigger->setIsFolder(true);
mpTimer = new TTimer(nullptr, mpHost);
if (module) {
mpTimer->mModuleMasterFolder = true;
mpTimer->mModuleMember = true;
}
mpTimer->mPackageName = mPackageName;
mpTimer->setIsActive(true);
mpTimer->setName(mPackageName);
mpTimer->setIsFolder(true);
mpAlias = new TAlias(nullptr, mpHost);
if (module) {
mpAlias->mModuleMasterFolder = true;
mpAlias->mModuleMember = true;
}
mpAlias->mPackageName = mPackageName;
mpAlias->setIsActive(true);
mpAlias->setName(mPackageName);
mpAlias->setScript(QString());
mpAlias->setRegexCode(QString());
mpAlias->setIsFolder(true);
mpAction = new TAction(nullptr, mpHost);
if (module) {
mpAction->mModuleMasterFolder = true;
mpAction->mModuleMember = true;
}
mpAction->mPackageName = mPackageName;
mpAction->setIsActive(true);
mpAction->setName(mPackageName);
mpAction->setIsFolder(true);
mpScript = new TScript(nullptr, mpHost);
if (module) {
mpScript->mModuleMasterFolder = true;
mpScript->mModuleMember = true;
}
mpScript->mPackageName = mPackageName;
mpScript->setIsActive(true);
mpScript->setName(mPackageName);
mpScript->setIsFolder(true);
mpHost->getTriggerUnit()->registerTrigger(mpTrigger);
mpHost->getTimerUnit()->registerTimer(mpTimer);
mpHost->getAliasUnit()->registerAlias(mpAlias);
mpHost->getActionUnit()->registerAction(mpAction);
mpHost->getKeyUnit()->registerKey(mpKey);
mpHost->getScriptUnit()->registerScript(mpScript);
}
while (!atEnd()) {
readNext();
if (isStartElement()) {
if (name() == qsl("MudletPackage")) {
QString versionString;
if (attributes().hasAttribute(qsl("version"))) {
versionString = attributes().value(qsl("version")).toString();
if (!versionString.isEmpty()) {
bool isOk = false;
const float versionNumber = versionString.toFloat(&isOk);
if (isOk) {
mVersionMajor = qFloor(versionNumber);
mVersionMinor = qRound(1000.0 * versionNumber) - (1000 * mVersionMajor);
}
if (pVersionString) {
*pVersionString = versionString;
}
}
}
if (mVersionMajor > 1
/*||(mVersionMajor==1&&mVersionMinor)*/) {
// Minor check is not currently relevant, just abort on 2.000f or more
const QString moanMsg = tr("[ ALERT ] - Sorry, the file being read:\n"
"\"%1\"\n"
"reports it has a version (%2) it must have come from a later Mudlet version,\n"
"and this one cannot read it, you need a newer Mudlet!")
.arg(pfile->fileName(), versionString);
mpHost->postMessage(moanMsg);
return {false, moanMsg};
}
readPackage();
} else if (name() == qsl("map")) {
readMap();
mpHost->mpMap->audit();
mpHost->mpMap->mpMapper->mp2dMap->init();
mpHost->mpMap->mpMapper->updateAreaComboBox();
mpHost->mpMap->mpMapper->resetAreaComboBoxToPlayerRoomArea();
mpHost->mpMap->mpMapper->show();
} else {
qDebug().nospace() << "XMLimport::importPackage(...) ERROR: "
"unrecognised element with name: "
<< name().toString() << " and content: " << text().toString();
}
}
}
if (!packName.isEmpty()) {
if (!gotTrigger) {
mpHost->getTriggerUnit()->unregisterTrigger(mpTrigger);
delete mpTrigger;
}
if (gotTimer) { // packName is NOT empty for modules...!
mpTimer->setIsActive(true);
mpTimer->enableTimer(mpTimer->getID());
} else {
mpHost->getTimerUnit()->unregisterTimer(mpTimer);
delete mpTimer;
}
if (gotAlias) {
mpAlias->setIsActive(true);
} else {
mpHost->getAliasUnit()->unregisterAlias(mpAlias);
delete mpAlias;
}
if (gotAction) {
mpHost->getActionUnit()->updateToolbar();
} else {
mpHost->getActionUnit()->unregisterAction(mpAction);
delete mpAction;
}
if (!gotKey) {
mpHost->getKeyUnit()->unregisterKey(mpKey);
delete mpKey;
}
if (!gotScript) {
mpHost->getScriptUnit()->unregisterScript(mpScript);
delete mpScript;
}
}
return {!hasError(), errorString()};
}
// returns the type of item and ID of the first (root) element
std::pair<dlgTriggerEditor::EditorViewType, int> XMLimport::importFromClipboard()
{
QString xml;
QClipboard* clipboard = QApplication::clipboard();
std::pair<dlgTriggerEditor::EditorViewType, int> result;
xml = clipboard->text(QClipboard::Clipboard);
QByteArray ba = xml.toUtf8();
QBuffer xmlBuffer(&ba);
setDevice(&xmlBuffer);
xmlBuffer.open(QIODevice::ReadOnly);
while (!atEnd()) {
readNext();
if (isStartElement()) {
if (name() == qsl("MudletPackage")) {
result = readPackage();
} else {
qDebug() << "ERROR:name=" << name().toString() << "text:" << text().toString();
}
}
}
return result;
}
void XMLimport::readVariable(TVar* pParent)
{
auto var = new TVar(pParent);
LuaInterface* lI = mpHost->getLuaInterface();
VarUnit* vu = lI->getVarUnit();
QString keyName, value;
int keyType = 0;
int valueType;
const QString what = name().toString();
while (!atEnd()) {
readNext();
if (isEndElement()) {
break;
}
if (isStartElement()) {
if (name() == qsl("name")) {
keyName = readElementText();
continue;
} else if (name() == qsl("value")) {
value = readElementText();
continue;
} else if (name() == qsl("keyType")) {
keyType = readElementText().toInt();
continue;
} else if (name() == qsl("valueType")) {
valueType = readElementText().toInt();
var->setName(keyName, keyType);
var->setValue(value, valueType);
vu->addSavedVar(var);
lI->setValue(var);
continue;
} else if (name() == qsl("VariableGroup") || name() == qsl("Variable")) {
readVariable(var);
} else {
readUnknownElement(what);
}
}
}
delete var;
}
void XMLimport::readHiddenVariables()
{
LuaInterface* lI = mpHost->getLuaInterface();
VarUnit* vu = lI->getVarUnit();
while (!atEnd()) {
readNext();
if (isEndElement()) {
break;
}
if (isStartElement()) {
if (name() == qsl("name")) {
const QString var = readElementText();
vu->addHidden(var);
continue;
}
}
}
}
void XMLimport::readVariablePackage()
{
LuaInterface* lI = mpHost->getLuaInterface();
VarUnit* vu = lI->getVarUnit();
mpVar = vu->getBase();
while (!atEnd()) {
readNext();
if (isStartElement()) {
if (name() == qsl("VariableGroup") || name() == qsl("Variable")) {
readVariable(mpVar);
} else if (name() == qsl("HiddenVariables")) {
readHiddenVariables();
} else {
readUnknownElement(qsl("VariablePackage"));
}
}
}
}
void XMLimport::readMap()
{
QMultiHash<int, int> tempAreaRoomsHash; // Keys: area id, Values: a room id in that area
while (!atEnd()) {
readNext();
if (isStartElement()) {
if (name() == qsl("areas")) {
mpHost->mpMap->mpRoomDB->clearMapDB();
mpHost->mpMap->reportStringToProgressDialog(tr("Parsing area data..."));
mpHost->mpMap->reportProgressToProgressDialog(0, 3);
readAreas();
} else if (name() == qsl("rooms")) {
mpHost->mpMap->reportStringToProgressDialog(tr("Parsing room data..."));
mpHost->mpMap->reportProgressToProgressDialog(1, 3);
readRooms(tempAreaRoomsHash);
} else if (name() == qsl("environments")) {
mpHost->mpMap->reportStringToProgressDialog(tr("Parsing environment data..."));
mpHost->mpMap->reportProgressToProgressDialog(2, 3);
readEnvColors();
}
mpHost->mpMap->reportProgressToProgressDialog(3, 3);
}
}
mpHost->mpMap->reportStringToProgressDialog(tr("Assigning rooms to their areas..."));
const int roomTotal = tempAreaRoomsHash.count();
int currentRoomCount = 0;
QListIterator<int> itAreaWithRooms(tempAreaRoomsHash.uniqueKeys());
while (itAreaWithRooms.hasNext()) {
const int areaId = itAreaWithRooms.next();
auto values = tempAreaRoomsHash.values(areaId);
QSet<int> const areaRoomsSet{values.begin(), values.end()};
if (!mpHost->mpMap->mpRoomDB->areas.contains(areaId)) {
// It is known for map files to have rooms with area Ids that are
// not in the listed areas - this cures that:
mpHost->mpMap->mpRoomDB->addArea(areaId);
}
mpHost->mpMap->mpRoomDB->setAreaRooms(areaId, areaRoomsSet);
currentRoomCount += areaRoomsSet.count();
mpHost->mpMap->reportProgressToProgressDialog(currentRoomCount, roomTotal);
}
}
void XMLimport::readEnvColors()
{
while (!atEnd()) {
readNext();
if (name() == qsl("environment")) {
readEnvColor();
}
}
}
void XMLimport::readEnvColor()
{
const int id = attributes().value(qsl("id")).toString().toInt();
const int color = attributes().value(qsl("color")).toString().toInt();
mpHost->mpMap->mEnvColors[id] = color;
}
void XMLimport::readAreas()
{
while (!atEnd()) {
readNext();
if (name() == qsl("areas")) {
break;
} else if (name() == qsl("area")) {
readArea();
}
}
}
void XMLimport::readArea()
{
if (attributes().hasAttribute(qsl("id"))) {
const int id = attributes().value(qsl("id")).toString().toInt();
const QString name = attributes().value(qsl("name")).toString();
mpHost->mpMap->mpRoomDB->addArea(id, name);
}
}
void XMLimport::readRooms(QMultiHash<int, int>& areaRoomsHash)
{
unsigned int roomCount = 0;
while (!atEnd()) {
readNext();
if (Q_LIKELY(isStartElement())) {
if (Q_LIKELY(name() == qsl("room"))) {
readRoom(areaRoomsHash, &roomCount);
} else {
readUnknownMapElement();
}
} else if (isEndElement() && name() == qsl("rooms")) {
break;
}
}
}
void XMLimport::readRoomFeatures(TRoom* pR)
{
while (!atEnd()) {
readNext();
if (Q_LIKELY(isStartElement())) {
if (name() == qsl("features")) {
continue;
} else if (Q_LIKELY(name() == qsl("feature"))) {
readRoomFeature(pR);
}
} else if (isEndElement() && name() == qsl("features")) {
break;
}
}
}
void XMLimport::readRoomFeature(TRoom* pR)
{
if (Q_LIKELY(attributes().hasAttribute(qsl("type")))) {
pR->userData.insert(qsl("feature-%1").arg(attributes().value(qsl("type"))), qsl("true"));
}
}
// This is a CPU/Time hog without the non-default (true) third argument to
// TRoomDB::addRoom(...)
void XMLimport::readRoom(QMultiHash<int, int>& areamRoomMultiHash, unsigned int* roomCount)
{
auto pT = new TRoom(mpHost->mpMap->mpRoomDB);
pT->id = attributes().value(qsl("id")).toString().toInt();
pT->area = attributes().value(qsl("area")).toString().toInt();
pT->name = attributes().value(qsl("title")).toString();
pT->environment = attributes().value(qsl("environment")).toString().toInt();
while (!atEnd()) {
readNext();
if (Q_UNLIKELY(pT->id < 1)) {
continue; // Skip further tests on exits as we'd have to throw away
// this invalid room and it would mess up the
// entranceMultiHash
} else if (Q_LIKELY(name() == qsl("exit"))) {
QString dir = attributes().value(qsl("direction")).toString();
const int e = attributes().value(qsl("target")).toString().toInt();
// If there is a "hidden" exit mark it as a locked door, otherwise
// if there is a "door" mark it as an open/closed/locked door
// depending on the value (I.R.E. MUD maps always uses "1" for "door"
// and/or "hidden" - though the latter does not always appear with
// former):
const int door = (attributes().hasAttribute(qsl("hidden")) && attributes().value(qsl("hidden")).toString().toInt() == 1)
? 3
: (attributes().hasAttribute(qsl("door")) && attributes().value(qsl("door")).toString().toInt() >= 0 && attributes().value(qsl("door")).toString().toInt() <= 3)
? attributes().value(qsl("door")).toString().toInt()
: 0;
if (dir.isEmpty()) {
if (attributes().value(qsl("special")).toString().toInt() == 1 && !attributes().value(qsl("command")).toString().isEmpty()) {
// This is how IRE XML maps mark special exits, rather than
// by just using a different string for the direction!
dir = attributes().value(qsl("command")).toString();
pT->setSpecialExit(e, dir);
pT->setDoor(dir, door);
} else {
continue;
}
} else if (dir == qsl("north")) {
pT->north = e;
pT->setDoor(qsl("n"), door);
} else if (dir == qsl("east")) {
pT->east = e;
pT->setDoor(qsl("e"), door);
} else if (dir == qsl("south")) {
pT->south = e;
pT->setDoor(qsl("s"), door);
} else if (dir == qsl("west")) {
pT->west = e;
pT->setDoor(qsl("w"), door);
} else if (dir == qsl("up")) {
pT->up = e;
pT->setDoor(qsl("up"), door);
} else if (dir == qsl("down")) {
pT->down = e;
pT->setDoor(qsl("down"), door);
} else if (dir == qsl("northeast")) {
pT->northeast = e;
pT->setDoor(qsl("ne"), door);
} else if (dir == qsl("southwest")) {
pT->southwest = e;
pT->setDoor(qsl("sw"), door);
} else if (dir == qsl("southeast")) {
pT->southeast = e;
pT->setDoor(qsl("se"), door);
} else if (dir == qsl("northwest")) {
pT->northwest = e;
pT->setDoor(qsl("nw"), door);
} else if (dir == qsl("in")) {
pT->in = e;
pT->setDoor(qsl("in"), door);
} else if (dir == qsl("out")) {
pT->out = e;
pT->setDoor(qsl("out"), door);
} else {
// TODO: Handle Special Exits
}
} else if (name() == qsl("coord")) {
if (attributes().value(qsl("x")).toString().isEmpty()) {
continue;
}
pT->x = attributes().value(qsl("x")).toString().toInt();
pT->y = attributes().value(qsl("y")).toString().toInt();
pT->z = attributes().value(qsl("z")).toString().toInt();
continue;
} else if (name() == qsl("features")) {
readRoomFeatures(pT);
} else if (Q_UNLIKELY(name().isEmpty())) {
continue;
}
if (isEndElement() && name() == qsl("room")) {
break;
}
}
if (pT->id > 0) {
if (++(*roomCount) % 100 == 0) {
mpHost->mpMap->reportStringToProgressDialog(tr("Parsing room data [count: %1]...").arg(*roomCount));
}
areamRoomMultiHash.insert(pT->area, pT->id);
// We are loading a map so can make some optimisation by setting the
// third argument as true:
mpHost->mpMap->mpRoomDB->addRoom(pT->id, pT, true);
mMaxRoomId = qMax(mMaxRoomId, pT->id); // Wasn't used but now maintains max Room Id
} else {
delete pT;
}
}
void XMLimport::readUnknownMapElement()
{
while (!atEnd()) {
readNext();
if (isEndElement()) {
break;
} else if (isStartElement()) {
readUnknownMapElement();
}
}
}
// returns the type of item and ID of the first (root) element
std::pair<dlgTriggerEditor::EditorViewType, int> XMLimport::readPackage()
{
dlgTriggerEditor::EditorViewType objectType = dlgTriggerEditor::EditorViewType::cmUnknownView;
int rootItemID = -1;
while (!atEnd()) {
readNext();
if (isEndElement()) {
break;
} else if (isStartElement()) {
if (name() == qsl("HostPackage")) {
readHostPackage();
} else if (name() == qsl("TriggerPackage")) {
objectType = dlgTriggerEditor::EditorViewType::cmTriggerView;
rootItemID = readTriggerPackage();
} else if (name() == qsl("TimerPackage")) {
objectType = dlgTriggerEditor::EditorViewType::cmTimerView;
rootItemID = readTimerPackage();
} else if (name() == qsl("AliasPackage")) {
objectType = dlgTriggerEditor::EditorViewType::cmAliasView;
rootItemID = readAliasPackage();
} else if (name() == qsl("ActionPackage")) {
objectType = dlgTriggerEditor::EditorViewType::cmActionView;
rootItemID = readActionPackage();
} else if (name() == qsl("ScriptPackage")) {
objectType = dlgTriggerEditor::EditorViewType::cmScriptView;
rootItemID = readScriptPackage();
} else if (name() == qsl("KeyPackage")) {
objectType = dlgTriggerEditor::EditorViewType::cmKeysView;
rootItemID = readKeyPackage();
} else if (name() == qsl("HelpPackage")) {
readHelpPackage();
} else if (name() == qsl("VariablePackage")) {
objectType = dlgTriggerEditor::EditorViewType::cmVarsView;
readVariablePackage();
} else {
readUnknownElement(qsl("MudletPackage"));
}
}
}
return {objectType, rootItemID};
}
void XMLimport::readHelpPackage()
{
while (!atEnd()) {
readNext();
if (isEndElement()) {
break;
} else if (isStartElement()) {
if (name() == qsl("helpURL")) {
const QString contents = readElementText();
mpHost->moduleHelp[mPackageName].insert("helpURL", contents);
}
}
}
}
// Will be on a startElement on entry, and on the matching endElement
// at exit:
void XMLimport::readUnknownElement(const QString& what)
{
if (!atEnd()) {
qDebug().nospace().noquote() << "XMLimport::readUnknownElement(\"" << what << "\") ERROR - UNKNOWN Package Element name: \"" << name().toString() << "\".";
qDebug().nospace().noquote() << " This is at byte offset: " << characterOffset() << ", which is (line:column): " << lineNumber() << ":" << columnNumber() << ".";
#if !defined(QT_STRICT_ITERATORS)
if (attributes().isEmpty()) {
qDebug().nospace().noquote() << " It has no attributes.";
} else {
// This can fail if QT_STRICT_ITERATORS is defined.
// See https://bugreports.qt.io/browse/QTBUG-45368
QVectorIterator<QXmlStreamAttribute> itAttribute(attributes());
qDebug().nospace().noquote() << " It has the following attributes:";
while (itAttribute.hasNext()) {
const auto attribute = itAttribute.next();
qDebug().nospace().noquote() << " name: \"" << attribute.name() << "\", value: \"" << attribute.value() << "\".";
}
}
#endif
// The argument to readElementText(...) is required otherwise it stops
// if a child element is encountered, the third alternative
// "IncludeChildElements" is not so helpful as it might seem as it only
// includes some of the intervening content from sub-elements. As it is
// this should advance the current position to the EndElement of the
// unexpected startElement:
qDebug().nospace().noquote() << " The (text) content is: \"" << readElementText(QXmlStreamReader::SkipChildElements) << "\"";
}
}
void XMLimport::readHostPackage()
{
while (!atEnd()) {
readNext();
if (isEndElement()) {
break;
} else if (isStartElement()) {
if (name() == qsl("Host")) {
readHost(mpHost);
} else {
readUnknownElement(qsl("HostPackage"));
}
}
}
}
void XMLimport::readHost(Host* pHost)
{
auto setBoolAttributeWithDefault = [&](const QString& attribute, bool& target, const bool defaultsTo) {
target = attributes().hasAttribute(attribute) ? attributes().value(attribute) == YES : defaultsTo;
};
auto setBoolAttribute = [&](const QString& attribute, bool& target) {
target = attributes().value(attribute) == YES;
};
setBoolAttributeWithDefault(qsl("announceIncomingText"), pHost->mAnnounceIncomingText, true);
setBoolAttributeWithDefault(qsl("advertiseScreenReader"), pHost->mAdvertiseScreenReader, false);
setBoolAttributeWithDefault(qsl("mEnableMTTS"), pHost->mEnableMTTS, true);
setBoolAttributeWithDefault(qsl("mEnableMNES"), pHost->mEnableMNES, false);
setBoolAttributeWithDefault(qsl("forceNewEnvironNegotiationOff"), pHost->mForceNewEnvironNegotiationOff, false);
setBoolAttribute(qsl("autoClearCommandLineAfterSend"), pHost->mAutoClearCommandLineAfterSend);
setBoolAttribute(qsl("printCommand"), pHost->mPrintCommand);
setBoolAttribute(qsl("mUSE_FORCE_LF_AFTER_PROMPT"), pHost->mUSE_FORCE_LF_AFTER_PROMPT);
setBoolAttribute(qsl("mUSE_UNIX_EOL"), pHost->mUSE_UNIX_EOL);
setBoolAttribute(qsl("runAllKeyMatches"), pHost->getKeyUnit()->mRunAllKeyMatches);
setBoolAttribute(qsl("mNoAntiAlias"), pHost->mNoAntiAlias);
setBoolAttribute(qsl("mEchoLuaErrors"), pHost->mEchoLuaErrors);
setBoolAttribute(qsl("mRawStreamDump"), pHost->mIsNextLogFileInHtmlFormat);
setBoolAttribute(qsl("mIsLoggingTimestamps"), pHost->mIsLoggingTimestamps);
setBoolAttribute(qsl("mAlertOnNewData"), pHost->mAlertOnNewData);
setBoolAttribute(qsl("mFORCE_NO_COMPRESSION"), pHost->mFORCE_NO_COMPRESSION);
setBoolAttribute(qsl("mFORCE_GA_OFF"), pHost->mFORCE_GA_OFF);
setBoolAttribute(qsl("mEnableGMCP"), pHost->mEnableGMCP);
setBoolAttribute(qsl("mEnableMSSP"), pHost->mEnableMSSP);
setBoolAttribute(qsl("mEnableMSDP"), pHost->mEnableMSDP);
setBoolAttribute(qsl("mEnableMSP"), pHost->mEnableMSP);
setBoolAttribute(qsl("mMapStrongHighlight"), pHost->mMapStrongHighlight);
setBoolAttribute(qsl("mEnableSpellCheck"), pHost->mEnableSpellCheck);
setBoolAttribute(qsl("mAcceptServerGUI"), pHost->mAcceptServerGUI);
setBoolAttribute(qsl("mAcceptServerMedia"), pHost->mAcceptServerMedia);
setBoolAttribute(qsl("mMapperUseAntiAlias"), pHost->mMapperUseAntiAlias);
setBoolAttribute(qsl("mEditorAutoComplete"), pHost->mEditorAutoComplete);
setBoolAttribute(qsl("mFORCE_MXP_NEGOTIATION_OFF"), pHost->mFORCE_MXP_NEGOTIATION_OFF);
setBoolAttribute(qsl("mFORCE_CHARSET_NEGOTIATION_OFF"), pHost->mFORCE_CHARSET_NEGOTIATION_OFF);
setBoolAttribute(qsl("enableTextAnalyzer"), pHost->mEnableTextAnalyzer);
setBoolAttribute(qsl("mBubbleMode"), pHost->mBubbleMode);
setBoolAttribute(qsl("mMapViewOnly"), pHost->mMapViewOnly);
setBoolAttribute(qsl("mShowRoomIDs"), pHost->mShowRoomID);
setBoolAttribute(qsl("mShowPanel"), pHost->mShowPanel);
setBoolAttribute(qsl("mHaveMapperScript"), pHost->mHaveMapperScript);
setBoolAttribute(qsl("mSslTsl"), pHost->mSslTsl);
setBoolAttribute(qsl("mSslIgnoreExpired"), pHost->mSslIgnoreExpired);
setBoolAttribute(qsl("mSslIgnoreSelfSigned"), pHost->mSslIgnoreSelfSigned);
setBoolAttribute(qsl("mSslIgnoreAll"), pHost->mSslIgnoreAll);
setBoolAttribute(qsl("mAskTlsAvailable"), pHost->mAskTlsAvailable);
setBoolAttribute(qsl("mUseProxy"), pHost->mUseProxy);
pHost->mProxyAddress = attributes().value(qsl("mProxyAddress")).toString();
if (attributes().hasAttribute(QLatin1String("mProxyPort"))) {
pHost->mProxyPort = attributes().value(qsl("mProxyPort")).toInt();
} else {
pHost->mProxyPort = 0;
}
pHost->mProxyUsername = attributes().value(qsl("mProxyUsername")).toString();
pHost->mProxyPassword = attributes().value(qsl("mProxyPassword")).toString();
pHost->set_USE_IRE_DRIVER_BUGFIX(attributes().value(qsl("USE_IRE_DRIVER_BUGFIX")) == YES);
pHost->mHighlightHistory = readDefaultTrueBool(qsl("HighlightHistory"));
pHost->mLogDir = attributes().value(qsl("logDirectory")).toString();
pHost->mFORCE_SAVE_ON_EXIT = readDefaultTrueBool(qsl("mFORCE_SAVE_ON_EXIT"));
const bool enableUserDictionary = attributes().value(qsl("mEnableUserDictionary")) == YES;
const bool useSharedDictionary = attributes().value(qsl("mUseSharedDictionary")) == YES;
pHost->setUserDictionaryOptions(enableUserDictionary, useSharedDictionary);
pHost->mMapperShowRoomBorders = readDefaultTrueBool(qsl("mMapperShowRoomBorders"));
pHost->mEditorTheme = attributes().value(QLatin1String("mEditorTheme")).toString();
pHost->mEditorThemeFile = attributes().value(QLatin1String("mEditorThemeFile")).toString();
pHost->mThemePreviewItemID = attributes().value(QLatin1String("mThemePreviewItemID")).toInt();
pHost->mThemePreviewType = attributes().value(QLatin1String("mThemePreviewType")).toString();
pHost->setHaveColorSpaceId(attributes().value(QLatin1String("mSGRCodeHasColSpaceId")).toString() == QLatin1String("yes"));
pHost->setMayRedefineColors(attributes().value(QLatin1String("mServerMayRedefineColors")).toString() == QLatin1String("yes"));
if (attributes().hasAttribute("AmbigousWidthGlyphsToBeWide")) {
const QStringView ambiguousWidthSetting(attributes().value(qsl("AmbigousWidthGlyphsToBeWide")));
if (ambiguousWidthSetting == YES) {
pHost->setWideAmbiguousEAsianGlyphs(Qt::Checked);
} else if (ambiguousWidthSetting == qsl("auto")) {
pHost->setWideAmbiguousEAsianGlyphs(Qt::PartiallyChecked);
} else {
pHost->setWideAmbiguousEAsianGlyphs(Qt::Unchecked);
}
} else {
// The encoding setting is stored as part of the profile details and NOT
// in the save file - probably because it is needed before the
// connection to the Server is initiated so it will already be in place
// which is just as well as it is needed for the automatic case...
pHost->setWideAmbiguousEAsianGlyphs(Qt::PartiallyChecked);
}
if (attributes().hasAttribute("logFileNameFormat")) {
// We previously mixed "yyyy-MM-dd{#|T}hh-MM-ss" with "yyyy-MM-dd{#|T}HH-MM-ss"
// which is slightly different {always use 24-hour clock even if AM/PM is
// present (it isn't)} and that broke some code that requires an exact
// string to work with - now always change it to "HH":
pHost->mLogFileNameFormat = attributes().value(qsl("logFileNameFormat")).toString().replace(QLatin1String("hh"), QLatin1String("HH"), Qt::CaseSensitive);
pHost->mLogFileName = attributes().value(qsl("logFileName")).toString();
}
if (attributes().hasAttribute("mEditorShowBidi")) {
pHost->setEditorShowBidi(attributes().value(qsl("mEditorShowBidi")) == YES);
} else {
pHost->setEditorShowBidi(true);
}
if (attributes().hasAttribute("caretShortcut")) {
const QStringView caretShortcut(attributes().value(qsl("caretShortcut")));
if (caretShortcut == qsl("None")) {
pHost->mCaretShortcut = Host::CaretShortcut::None;
} else if (caretShortcut == qsl("Tab")) {
pHost->mCaretShortcut = Host::CaretShortcut::Tab;
} else if (caretShortcut == qsl("CtrlTab")) {
pHost->mCaretShortcut = Host::CaretShortcut::CtrlTab;
} else if (caretShortcut == qsl("F6")) {
pHost->mCaretShortcut = Host::CaretShortcut::F6;
}
}
if (attributes().hasAttribute("blankLineBehaviour")) {
const QStringView blankLineBehaviour(attributes().value(qsl("blankLineBehaviour")));
if (blankLineBehaviour == qsl("Hide")) {
pHost->mBlankLineBehaviour = Host::BlankLineBehaviour::Hide;
} else if (blankLineBehaviour == qsl("Show")) {
pHost->mBlankLineBehaviour = Host::BlankLineBehaviour::Show;
} else if (blankLineBehaviour == qsl("ReplaceWithSpace")) {
pHost->mBlankLineBehaviour = Host::BlankLineBehaviour::ReplaceWithSpace;
}
}
if (attributes().hasAttribute(QLatin1String("mSearchEngineName"))) {
pHost->mSearchEngineName = attributes().value(QLatin1String("mSearchEngineName")).toString();
} else {
pHost->mSearchEngineName = QString("Google");
}
if (attributes().hasAttribute(QLatin1String("mTimerSupressionInterval"))) {
pHost->mTimerDebugOutputSuppressionInterval = QTime::fromString(attributes().value(QLatin1String("mTimerSupressionInterval")).toString(), QLatin1String("hh:mm:ss.zzz"));
} else {
pHost->mTimerDebugOutputSuppressionInterval = QTime();
}
if (attributes().hasAttribute(QLatin1String("mDiscordAccessFlags"))) {
pHost->mDiscordAccessFlags = static_cast<Host::DiscordOptionFlags>(attributes().value(qsl("mDiscordAccessFlags")).toString().toInt());
}
if (attributes().hasAttribute(QLatin1String("mRequiredDiscordUserName"))) {
pHost->mRequiredDiscordUserName = attributes().value(QLatin1String("mRequiredDiscordUserName")).toString();
} else {
pHost->mRequiredDiscordUserName.clear();
}
if (attributes().hasAttribute(QLatin1String("mRequiredDiscordUserDiscriminator"))) {
pHost->mRequiredDiscordUserDiscriminator = attributes().value(QLatin1String("mRequiredDiscordUserDiscriminator")).toString();
} else {
pHost->mRequiredDiscordUserDiscriminator.clear();
}
if (attributes().hasAttribute(QLatin1String("playerRoomStyle"))) {
quint8 styleCode = 0;
quint8 outerDiameterPercentage = 0;
quint8 innerDiameterPercentage = 0;
QColor outerColor;
QColor innerColor;
// Retrieve current (possibly default) settings:
pHost->getPlayerRoomStyleDetails(styleCode, outerDiameterPercentage, innerDiameterPercentage, outerColor, innerColor);
// Gather values from file:
styleCode = static_cast<quint8>(qBound(0, attributes().value(QLatin1String("playerRoomStyle")).toInt(), 255));
outerDiameterPercentage = static_cast<quint8>(qBound(0, attributes().value(QLatin1String("playerRoomOuterDiameter")).toInt(), 255));
innerDiameterPercentage = static_cast<quint8>(qBound(0, attributes().value(QLatin1String("playerRoomInnerDiameter")).toInt(), 255));
outerColor.setNamedColor(attributes().value(QLatin1String("playerRoomPrimaryColor")).toString());
innerColor.setNamedColor(attributes().value(QLatin1String("playerRoomSecondaryColor")).toString());
// Store all the settings in the Host instance:
pHost->setPlayerRoomStyleDetails(styleCode, outerDiameterPercentage, innerDiameterPercentage, outerColor, innerColor);
if (pHost->mpMap) {
// And the TMap instance:
pHost->mpMap->mPlayerRoomStyle = styleCode;
pHost->mpMap->mPlayerRoomOuterDiameterPercentage = outerDiameterPercentage;
pHost->mpMap->mPlayerRoomInnerDiameterPercentage = innerDiameterPercentage;
pHost->mpMap->mPlayerRoomOuterColor = outerColor;
pHost->mpMap->mPlayerRoomInnerColor = innerColor;
}
}
pHost->mRoomSize = attributes().value(qsl("mRoomSize")).toString().toDouble();
if (qFuzzyCompare(1.0 + pHost->mRoomSize, 1.0)) {
// The value is a float/double and the prior code using "== 0" is a BAD
// THING to do with non-integer number types!
pHost->mRoomSize = 0.5; // Same value as is in Host class initializer list
}
pHost->mLineSize = attributes().value(qsl("mLineSize")).toString().toDouble();
if (qFuzzyCompare(1.0 + pHost->mLineSize, 1.0)) {
pHost->mLineSize = 10.0; // Same value as is in Host class initializer list
}
const QStringView ignore(attributes().value(qsl("mDoubleClickIgnore")));
for (auto character : ignore) {
pHost->mDoubleClickIgnore.insert(character);
}
if (attributes().hasAttribute(QLatin1String("EditorSearchOptions"))) {
pHost->setSearchOptions(static_cast<dlgTriggerEditor::SearchOptions>(attributes().value(qsl("EditorSearchOptions")).toInt()));
}
pHost->setDebugShowAllProblemCodepoints(attributes().value(qsl("DebugShowAllProblemCodepoints")) == YES);
const bool compactInputLine = attributes().value(QLatin1String("CompactInputLine")) == YES;
pHost->setCompactInputLine(compactInputLine);
if (mudlet::self()->mpCurrentActiveHost == pHost) {
mudlet::self()->dactionInputLine->setChecked(compactInputLine);
}
if (attributes().hasAttribute(QLatin1String("CommandLineHistorySaveSize"))) {
pHost->setCommandLineHistorySaveSize(attributes().value(QLatin1String("CommandLineHistorySaveSize")).toInt());
} else {
// This is the default value, though prior to the introduction of this
// it would have effectively been zero:
pHost->setCommandLineHistorySaveSize(500);
}
if (attributes().hasAttribute(QLatin1String("NetworkPacketTimeout"))) {
// These limits are also hard coded into the QSpinBox used to adjust
// this setting in the preferences:
pHost->mTelnet.setPostingTimeout(qBound(10, attributes().value(QLatin1String("NetworkPacketTimeout")).toInt(), 500));
} else {
// The default value, also used up to Mudlet 4.12.0:
pHost->mTelnet.setPostingTimeout(300);
}
if (attributes().hasAttribute(QLatin1String("ControlCharacterHandling"))) {
switch (attributes().value(QLatin1String("ControlCharacterHandling")).toInt()) {
case 1:
pHost->setControlCharacterMode(ControlCharacterMode::Picture);
break;
case 2:
pHost->setControlCharacterMode(ControlCharacterMode::OEM);
break;
case 0:
[[fallthrough]];
default:
pHost->setControlCharacterMode(ControlCharacterMode::AsIs);
}
} else {
// The default value, also used up to Mudlet 4.14.1:
pHost->setControlCharacterMode(ControlCharacterMode::AsIs);
}
if (attributes().hasAttribute(qsl("ShowIDsInEditor"))) {
pHost->setShowIdsInEditor(attributes().value(qsl("ShowIDsInEditor")) == YES);
} else {
// The default (and for profile files from before 4.18.0):
pHost->setShowIdsInEditor(false);
}
if (attributes().hasAttribute(qsl("Large2DMapAreaExitArrows"))) {
pHost->setLargeAreaExitArrows(attributes().value(qsl("Large2DMapAreaExitArrows")) == YES);
} else {
// The default (and for map/profile files from before 4.15.0):
pHost->setLargeAreaExitArrows(false);
}
if (attributes().hasAttribute(qsl("BoldIsBright"))) {
pHost->mBoldIsBright = (attributes().value(qsl("BoldIsBright")) == YES);
} else {
// The default, backwards compatible, option is true, though false would be "better":
pHost->mBoldIsBright = true;
}
if (attributes().value(qsl("mShowInfo")) == qsl("no")) {
mpHost->mMapInfoContributors.clear();
}
QMargins borders;