-
Notifications
You must be signed in to change notification settings - Fork 1
/
datafile.cpp
2198 lines (1950 loc) · 68.1 KB
/
datafile.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
#define _CRT_SECURE_NO_WARNINGS // for sscanf
#include "datafile.h"
#include "version.h"
#include "fxhelper.h"
#include "terrain.h"
#include <climits>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#ifdef _MSC_VER
#include <windows.h>
#include <stringapiset.h>
#endif
#define HELP_GUARD 16
datafile::datafile() : m_factionId(0), m_recruitment(0), m_turn(-1), m_activefaction(m_blocks.end())
{
m_cmds.modified = false;
}
const char* UTF8BOM = "\xEF\xBB\xBF";
static bool skip_bom(std::ifstream& file)
{
// skip BOM, if any
for (int i = 0; i != 3; ++i) {
int c = file.get();
if ((char)c != UTF8BOM[i]) {
file.seekg(0);
return false;
}
}
return true;
}
// helper function that strips lines and return pointer to next line
static inline char* getNextLine(char* str)
{
char* next = str;
// search start of next line
while(*next && *next != '\n')
next++;
// overwrite cr and newline
if (next > str && next[-1] == '\r')
next[-1] = '\0';
if(*next)
*next++ = '\0'; // mark end of line
return next;
}
FXString datafile::getVersion()
{
if (m_version.empty()) {
datablock::citor block;
for (block = m_blocks.begin(); block != m_blocks.end(); block++)
{
// set turn number to that found in version block
if (block->type() == block_type::TYPE_VERSION) {
m_version = block->value("Build");
break;
}
}
}
return m_version;
}
int datafile::compareVersions(const FXString &lhs, const FXString &rhs)
{
return strcmp(lhs.text(), rhs.text());
}
int datafile::turn()
{
if (m_turn < 0) {
datablock::citor block;
for (block = m_blocks.begin(); block != m_blocks.end(); block++)
{
// set turn number to that found in version block
if (block->type() == block_type::TYPE_VERSION) {
m_turn = block->valueInt(TYPE_TURN, m_turn);
break;
}
}
}
return m_turn;
}
int datafile::getFactionIdForUnit(const datablock* unit) const
{
FXASSERT(unit->type() == block_type::TYPE_UNIT);
if (unit->valueInt(TYPE_TRAITOR, 0) != 0) {
return (int)special_faction::TRAITOR;
}
return unit->valueInt(TYPE_FACTION, (int)special_faction::ANONYMOUS);
}
FXString datafile::getFactionName(int factionId)
{
FXString name;
datablock::itor faction;
if (factionId >= 0 && getFaction(faction, factionId)) {
datablock* facPtr = &*faction;
name = facPtr->value(TYPE_FACTIONNAME);
if (facPtr->info() < 0)
{
if (name.empty()) {
name.assign("Parteigetarnt");
}
}
else
{
FXString fid = facPtr->id();
if (name.empty()) {
name.format("Partei %s (%s)", fid.text(), fid.text());
}
else {
name += " (";
name += fid;
name += ')';
}
}
}
else {
datablock block;
block.infostr(FXStringVal(factionId));
if (factionId == (int)special_faction::ANONYMOUS) {
name.assign("Parteigetarnt");
}
else {
name.assign(L"Verr\u00e4ter");
}
}
return name;
}
datablock* datafile::getMessageTarget(const datablock& msg)
{
int uid;
datablock::itor select;
uid = msg.getReference(block_type::TYPE_UNIT);
if (uid > 0 && getUnit(select, uid)) {
return &*select;
}
uid = msg.getReference(block_type::TYPE_SHIP);
if (uid > 0 && getShip(select, uid)) {
return &*select;
}
uid = msg.getReference(block_type::TYPE_BUILDING);
if (uid > 0 && getBuilding(select, uid)) {
return &*select;
}
FXString loc = msg.value("region");
if (!loc.empty()) {
int x, y, plane;
x = FXIntVal(loc.section(' ', 0));
y = FXIntVal(loc.section(' ', 1));
plane = FXIntVal(loc.section(' ', 2));
if (getRegion(select, x, y, plane)) {
return &*select;
}
}
return &*m_blocks.begin();
}
void datafile::openFile(const char* filename, std::ifstream& file, std::ios::openmode mode)
{
#ifdef _MSC_VER
WCHAR pf[MAX_PATH];
MultiByteToWideChar(CP_UTF8, 0, filename, -1, pf, MAX_PATH);
std::wstring wname(pf);
file.open(wname, mode);
#else
file.open(filename, mode);
#endif
}
// loads file, parses it and returns number of block
bool datafile::load(const FXString& filename, FXString & outError)
{
if (filename.empty()) {
return false;
}
std::ifstream file;
openFile(filename.text(), file);
if (!file.is_open())
{
outError.assign(L"Datei konnte nicht ge\u00f6ffnet werden.");
return false;
}
m_blocks.clear();
datablock* block = NULL, newblock;
datakey key;
bool utf8 = true;
skip_bom(file);
std::string line;
while (std::getline(file, line)) {
if (line.empty()) continue;
if (line.back() == '\r') {
line.pop_back();
}
const char* str = line.c_str();
// erster versuch: enthaelt die Zeile einen datakey?
if (key.parse(str, block ? block->type() : block_type::TYPE_UNKNOWN, utf8))
{
if (block)
{
int terrain;
switch (key.type())
{
case TYPE_CHARSET:
if (utf8 && key.value() != "UTF-8")
{
utf8 = false;
}
break;
case TYPE_TERRAIN:
terrain = datablock::parseTerrain(key.value());
if (terrain == data::TERRAIN_UNKNOWN)
{
block->addKey(key); // need textual representation of terrain type
terrain = datablock::parseSpecialTerrain(key.value());
}
block->terrain(terrain);
break;
default:
block->addKey(key); // appends "Value";Key - tags
}
}
}
// zweiter versuch: enthaelt die Zeile einen datablock-header?
else if (newblock.parse(str))
{
m_blocks.push_back(newblock); // appends BLOCK Info - tags
block = &m_blocks.back();
}
else if (block) {
/* fix for a bug introduced by version 1.2.7 */
const char* semi = strchr(str, ';');
if (semi) {
key.key(FXString(semi + 1), block->type());
key.value(FXString(str, semi - str));
block->addKey(key);
}
}
}
if (m_blocks.empty())
{
outError.assign(L"Die Datei konnte nicht gelesen werden.\nM\u00f6glicherweise wird das Format nicht unterst\u00fctzt.");
return false;
}
if (m_blocks.front().type() != block_type::TYPE_VERSION)
{
outError.assign("Die Datei hat das falsche Format.");
return false;
}
return true;
}
// saves file
int datafile::save(const char* filename, map_type map_filter, std::set<datablock*> const * const selection)
{
if (!filename)
return 0;
// open file for writing
std::ostringstream file;
FXFileStream filestr;
filestr.open(filename, FXStreamSave);
if (filestr.status() != FXStreamOK)
{
return -1;
}
datablock::itor block;
datablock::itor last_block = m_blocks.end();
int maxDepth = 0;
for (block = m_blocks.begin(); block != last_block; ++block) {
bool hideKeys = false;
block_type type = block->type();
if (block->info() < 0)
{
// PARTEI -1 verhindern
continue;
}
if (maxDepth) {
if (block->depth() <= maxDepth) {
// reset the skip-child behavior
maxDepth = 0;
}
else {
// do not print child-blocks now
continue;
}
}
if (type == block_type::TYPE_ISLAND)
{
// No support for Magellan-style islands
continue;
}
if (map_filter == map_type::MAP_MINIMAL) {
/* skip over anything except version or region */
if (type == block_type::TYPE_REGION) {
if (selection && !selection->empty()) {
datablock* region = &*block;
if (selection->find(region) == selection->end()) {
continue;
}
}
}
else if (type != block_type::TYPE_VERSION) {
continue;
}
}
else if (map_filter != map_type::MAP_FULL) {
/* do not include these blocks at all */
if (type == block_type::TYPE_EFFECTS
|| type == block_type::TYPE_MESSAGE
|| type == block_type::TYPE_MESSAGETYPE
|| type == block_type::TYPE_DURCHREISE
|| type == block_type::TYPE_DURCHSCHIFFUNG) {
continue;
}
/* skip these blocks with all their children */
if (type == block_type::TYPE_UNIT
|| type == block_type::TYPE_BATTLE
|| type == block_type::TYPE_SHIP) {
maxDepth = block->depth();
continue;
}
}
// Blocknamen + ID-Nummern ausgeben
file << block->string();
// VERSION auf mindestens 64 hochsetzen
if (type == block_type::TYPE_VERSION)
if (block->info() < 64)
block->infostr("64");
if (type == block_type::TYPE_REGION || type == block_type::TYPE_BATTLE ||
block->x() != 0 || block->y() != 0)
file << ' ' << block->x() << ' ' << block->y();
if (type == block_type::TYPE_COMBATSPELL || block->info())
file << ' ' << block->info();
file << std::endl;
if (type == block_type::TYPE_VERSION)
{
file << "\"UTF-8\";charset" << std::endl;
}
if (type == block_type::TYPE_REGION)
{
file << '\"' << block->terrainString().text() << "\";Terrain" << std::endl;
}
// Konfiguration-Block anpassen und Charset auf ISO-8859-1 setzen
else if (type == block_type::TYPE_VERSION)
{
block->setKey(key_type::TYPE_KONFIGURATION, getConfigurationName(map_filter));
}
else if (type == block_type::TYPE_UNIT)
{
if (isConfirmed(*block)) // TYPE_ORDERS_CONFIRMED
file << "1;ejcOrdersConfirmed" << std::endl;
}
else if (type == block_type::TYPE_COMMANDS)
{
if (att_commands* cmds = static_cast<att_commands*>(block->attachment()))
{
hideKeys = true; // hide original commands
for (att_commands::cmdlist_t::iterator it = cmds->commands.begin(); it != cmds->commands.end(); it++)
{
file << '\"';
FXString value = *it;
/*
\\ ist ein \, \" ist ein ", einzelen " sind Anfang und Ende
von Strings, \n ist ein Zeilenumbruch, (\x ist x, falls keine Sonderregel)
*/
for (int i = 0; i < value.length(); i++)
{
char c = value[i];
if (c == '\\' || c == '\"')
file << '\\';
if (c == '\n')
file << "\\n";
else
file << c;
}
file << '\"' << std::endl;
}
}
}
// Datakeys ausgeben
datakey::list_type::const_iterator tags = block->data().begin();
datakey::list_type::const_iterator iend = block->data().end();
if (hideKeys)
tags = iend; // don't save keys of this block
for (; tags != iend; tags++)
{
if (type == block_type::TYPE_FACTION) {
if (map_filter != map_type::MAP_FULL) {
if (tags->type() != TYPE_BANNER
&& tags->type() != TYPE_LOCALE
&& tags->type() != TYPE_FACTIONNAME
&& tags->type() != TYPE_EMAIL)
{
continue;
}
}
}
else if (type == block_type::TYPE_REGION)
{
if (map_filter != map_type::MAP_FULL)
{
if (tags->type() == TYPE_VISIBILITY) {
continue;
}
if (map_filter == map_type::MAP_MINIMAL)
{
int ttype = tags->type();
if (ttype != TYPE_NAME && ttype != TYPE_ISLAND && ttype != TYPE_ID)
continue;
}
}
}
else if (type == block_type::TYPE_UNIT)
{
if (tags->type() == TYPE_ORDERS_CONFIRMED)
continue; // will be set above
}
FXString value = tags->value();
// String wird mit "" umgeben...
if (tags->isInt())
file << value.text();
else
{
file << '\"';
/*
\\ ist ein \, \" ist ein ", einzelen " sind Anfang und Ende
von Strings, \n ist ein Zeilenumbruch, (\x ist x, falls keine Sonderregel)
*/
for (int i = 0; i < value.length(); i++)
{
char c = value[i];
if (c == '\\' || c == '\"')
file << '\\';
if (c == '\n')
file << "\\n";
else
file << c;
}
file << '\"';
}
// key
FXString key = tags->key();
if (!key.empty())
{
file << ';' << key.text();
}
file << std::endl;
}
// save block to file
std::string output = file.str();
filestr.save(output.c_str(), output.size());
file.str("");
}
return m_blocks.size();
}
void datafile::mergeBlock(datablock::itor& block, const datablock::itor& parent, datablock::itor& end)
{
int parent_depth = parent->depth();
block_type type = block->type();
int info = block->info();
datablock::itor insert = std::next(parent);
bool found = false;
for (datablock::itor child = insert; child != end; ++child) {
if (child->depth() == parent_depth) {
end = child;
break;
}
if (child->type() == type) {
if (insert->type() != type) {
insert = child;
}
if (child->info() == info) {
// we already have a newer report for this same block
found = true;
break;
}
}
}
if (!found) {
// we do not have this kind of block
m_blocks.insert(insert, *block);
}
}
void datafile::findOffset(datafile* new_cr, int* x_offset, int* y_offset) const
{
// first, create an index of all regions with an Id in this report:
blockhash_t regionIndex;
for (regions_map::const_iterator it = m_regions.begin(); it != m_regions.end(); ++it)
{
const datablock::itor& block = (*it).second;
int plane = block->info();
// only comparing plane 0
if (plane == 0) {
int id = block->valueInt(TYPE_ID, -1);
if (id > 0) {
regionIndex[id] = block;
}
}
}
// second, try finding a region from this report in the index:
for (auto &block : new_cr->m_blocks)
{
if (block.type() == block_type::TYPE_REGION) {
int id = block.valueInt(TYPE_ID, -1);
int plane = block.info();
if (plane == 0) {
blockhash_t::const_iterator match = regionIndex.find(id);
if (match != regionIndex.end()) {
const datablock::itor &other = (*match).second;
/**
* NB: drifting icebergs move around the map and keep their ID!
**/
const datakey *k1 = block.valueKey(TYPE_TERRAIN);
const datakey *k2 = other->valueKey(TYPE_TERRAIN);
if (k1 && k2 && k1->value() == k2->value()) {
int xo = other->x() - block.x();
int yo = other->y() - block.y();
if (xo != 0 || yo != 0) {
if (x_offset) *x_offset = xo;
if (y_offset) *y_offset = yo;
}
return;
}
}
}
}
}
}
void datafile::removeTemporary()
{
for (datablock::itor block = m_blocks.begin(); block != m_blocks.end(); ++block) {
/* remove visibility status from older of the two reports */
if (block->type() == block_type::TYPE_REGION)
{
block->removeKey(TYPE_VISIBILITY);
}
}
}
void datafile::merge(datafile * old_cr, int x_offset, int y_offset)
{
// dann: Datei an den aktuellen CR anfuegen (nur Karteninformationen)
datablock::itor old_end = old_cr->m_blocks.end();
bool copy_children = false;
for (datablock::itor old_r = old_cr->m_blocks.begin(); old_r != old_end;)
{
// handle only regions
if (old_r->type() == block_type::TYPE_REGION)
{
int x = old_r->x();
int y = old_r->y();
int plane = old_r->info();
if (plane == 0) {
x += x_offset;
y += y_offset;
}
datablock::itor new_r;
if (getRegion(new_r, x, y, old_r->info())) // add some info to old cr (island names)
{
bool is_seen = !new_r->hasKey(TYPE_VISIBILITY);
copy_children = false;
if (const datakey* islandkey = old_r->valueKey(TYPE_ISLAND))
{
if (!new_r->valueKey(TYPE_ISLAND))
{
if (islandkey && !islandkey->isInt()) // add only Vorlage-style islands (easier)
new_r->addKey(*islandkey);
}
}
if (!is_seen) {
// old region contained good data that we may want to keep
for (const datakey& key : old_r->data())
{
if (key.type() == TYPE_NAME || key.type() == TYPE_TERRAIN) {
continue;
}
if (!new_r->hasKey(key.type())) {
new_r->addKey(key);
}
}
// copy child blocks if we don't have them
int depth = old_r->depth();
datablock::itor new_end = m_blocks.end();
for (old_r++; old_r != old_end && old_r->type() != block_type::TYPE_REGION; ++old_r)
{
if (old_r->depth() == depth + 1) {
block_type type = old_r->type();
if (!datafile::isEphemeral(type)) {
mergeBlock(old_r, new_r, new_end);
}
}
}
if (old_r == old_end) {
break;
}
if (old_r->type() != block_type::TYPE_REGION)
{
++old_r;
}
}
else {
++old_r;
}
continue;
}
else // append region to this cr
{
if (x_offset || y_offset) {
if (old_r->info() == 0) {
old_r->move(x_offset, y_offset);
}
}
copy_children = true;
old_r->attachment(nullptr);
m_blocks.push_back(*old_r);
}
}
else if (copy_children) {
/* part of a new region that is appended to the report, copy detail blocks */
switch (old_r->type()) {
case block_type::TYPE_BUILDING:
case block_type::TYPE_PRICES:
case block_type::TYPE_BORDER:
case block_type::TYPE_RESOURCE:
m_blocks.push_back(*old_r);
break;
default:
break;
}
}
++old_r;
}
createHashTables();
if (old_cr->getActiveFactionId() != getActiveFactionId()) {
if (m_password.empty()) {
m_password = old_cr->m_password;
}
}
}
const char* datafile::getConfigurationName(map_type type)
{
if (type == map_type::MAP_NORMAL) {
return "CSMapFX:Export";
}
if (type == map_type::MAP_MINIMAL) {
return "CSMapFX:Map";
}
return "CSMapFX";
}
// ====================================
// === datafile command loading routine
FXString decodeLine(const std::string& line, bool& checkUTF8, bool& utf8)
{
// check if line is REGION or EINHEIT command
if (checkUTF8) {
if (!isUTF8(line.c_str(), line.length())) {
checkUTF8 = false;
utf8 = false;
}
}
if (!utf8) {
return iso2utf(line.c_str(), line.length());
}
return FXString(line.c_str());
}
static FXString regionCommand(const datablock ®ion)
{
FXString result = "REGION " + FXStringVal(region.x()) + "," + FXStringVal(region.y());
if (region.info()) {
result += "," + FXStringVal(region.info());
}
result += " ; " + region.value(TYPE_NAME);
result += " (" + region.terrainString() + ")";
return result;
}
// loads command file and attaches the commands to the units
int datafile::loadCmds(const FXString& filename)
{
bool utf8 = true, checkUTF8 = true; // file might not be UTF8
if (filename.empty())
return 0;
if (m_factionId == 0)
throw std::runtime_error("Kein Report geladen, kann Befehlsdatei nicht einlesen.");
// load plain text file
std::ifstream file;
openFile(filename.text(), file);
if (!file.is_open()) {
throw std::runtime_error(FXString(L"Datei konnte nicht ge\u00f6ffnet werden.").text());
}
if (skip_bom(file)) {
checkUTF8 = false;
utf8 = true;
}
std::string line;
while (std::getline(file, line))
{
if (line.empty()) continue;
if (line.back() == '\r') {
line.pop_back();
}
const char* ptr = line.c_str();
// skip indentation
while (*ptr && isspace(*ptr))
ptr++;
// when not empty, break
if (*ptr && *ptr != ';') {
// found first non-empty line
FXString header(ptr);
if (header.section(' ', 0) != "ERESSEA" && header.section(' ', 0) != "PARTEI") {
throw std::runtime_error(FXString(L"Keine g\u00fcltige Befehlsdatei.").text());
}
// parse header line:
// ERESSEA ioen "PASSWORT"
FXString id36 = header.section(' ', 1);
char* endptr = nullptr;
int factionId = strtol(id36.text(), &endptr, 36);
if (endptr && *endptr) // id36 string has to be consumed by strtol
throw std::runtime_error((L"Keine g\u00fcltige Parteinummer: " + id36).text());
if (factionId != m_factionId)
throw std::runtime_error((L"Die Befehle sind f\u00fcr eine andere Partei: " + id36).text());
FXString password = header.section(' ', 2);
FXint pos = password.find('"', 0);
while (pos >= 0) {
password.erase(pos);
pos = password.find('"', pos);
}
if (!password.empty()) {
m_password = password;
}
break;
}
}
// consider command file as correct, read in commands
m_cmds.prefix_lines.clear();
m_cmds.region_lines.clear();
int headerindent = 0, indent = 0;
// process lines
FXString cmd, str;
while(std::getline(file, line)) {
if (line.empty()) continue;
if (line.back() == '\r') {
line.pop_back();
}
// strip the line
if (!line.empty()) {
str = decodeLine(line, checkUTF8, utf8);
indent = str.find_first_not_of("\t ");
if (!str.trim().empty()) {}
cmd = str.before(' ');
cmd.upper();
if (cmd == "REGION" || cmd == "EINHEIT") {
break;
}
// add line to prefix
m_cmds.prefix_lines.push_back(str);
}
}
// check for end of file:
if (!file.good())
throw std::runtime_error("Keine Befehle gefunden.");
att_commands region_list;
att_commands* cmds_list = NULL;
std::vector<int> unit_order;
datablock::itor block, region = m_blocks.end();
int unitId = 0;
while (file.good())
{
if (cmd == "REGION" || cmd == "EINHEIT")
{
FXString param = str.section(' ', 1);
headerindent = indent;
if (cmd == "EINHEIT")
{
unitId = strtol(param.text(), nullptr, 36);
if (!getUnit(block, unitId))
{
throw std::runtime_error(("Einheit nicht gefunden: " + str).text());
}
if (region != m_blocks.end()) {
Coordinates coor(region->x(), region->y(), region->info());
if (!hasChild(region, block)) {
if (!getParent(region, block)) {
throw std::runtime_error(("Einheit in falscher Region: " + str).text());
}
coor = Coordinates(region->x(), region->y(), region->info());
region_list.header = regionCommand(*region);
}
m_cmds.region_lines[coor] = region_list;
region_list.clear();
// add to order list for units of this region
unit_order.push_back(unitId);
region = m_blocks.end();
}
setConfirmed(block, false); // TODO: cumbersome, but reading orders without a `; bestaetigt` comment must do this.
cmds_list = nullptr;
datablock::itor cmdb;
if (getCommands(cmdb, block)) {
if (att_commands* cmds = static_cast<att_commands*>(cmdb->attachment())) {
cmds_list = cmds;
}
}
if (!cmds_list) {
throw std::runtime_error(("Einheit hat keinen Befehlsblock: " + str).text());
}
}
else {
unitId = 0;
if (cmd == "REGION")
{
FXString xstr = param.section(',', 0);
FXString ystr = param.section(',', 1);
FXString zstr = param.section(',', 2);
int x = strtol(xstr.text(), nullptr, 10);
int y = strtol(ystr.text(), nullptr, 10);
int z = strtol(zstr.text(), nullptr, 10);
if (!getRegion(region, x, y, z))
{
throw std::runtime_error(("Region nicht gefunden: " + param).text());
}
unit_order.clear();
cmds_list = ®ion_list;
}
}
if (cmds_list) {
cmds_list->prefix_lines.clear();
cmds_list->commands.clear();
cmds_list->postfix_lines.clear();
cmds_list->header = str;
}
}
else if (cmds_list)
{
if (!str.empty()) {
if (str.left(1) == ";") {
cmd = str.section(' ', 1);
if (unitId != 0 && flatten(cmd.lower()) == "bestaetigt") {
// don't add "; bestaetigt" comment, just set confirmed flag
setConfirmed(block, true);
}
else if (indent > headerindent) {
cmds_list->addCommand(str);
}
else if (cmds_list->commands.empty()) {
cmds_list->prefix_lines.push_back(str);
}
else {
// add to postfix if it follows some commands
cmds_list->postfix_lines.push_back(str);
}
}
else {
cmds_list->addCommand(str);
}
}
}
// strip the line
if (!std::getline(file, line)) {
break;
}
if (!line.empty()) {
if (line.back() == '\r') {
line.pop_back();
}
}
// check if line is REGION oder EINHEIT command
str = decodeLine(line, checkUTF8, utf8);
indent = str.find_first_not_of("\t ");
cmd = str.trim().before(' ');
cmd.upper();
if (flatten(cmd) == "naechster") break;
}
cmdfilename(filename);
modifiedCmds(false);
return true;
}
void datafile::writeCmds(std::ostream &out, const att_commands *cmds)
{
out << cmds->header.text() << "\n";
// output prefix lines
for (const FXString &itor : cmds->prefix_lines)
out << itor.text() << "\n";
// output changed commands
for (const FXString &itor : cmds->commands)
out << " " << itor.text() << "\n";
// output postfix lines
out << "\n";
for (const FXString &itor : cmds->postfix_lines)
out << itor.text() << "\n";
}
// saves command file
int datafile::saveCmds(const FXString& filename, const FXString& password, bool stripped)
{
if (filename.empty())
return -1;
if (m_factionId == 0)
return -1;
// Datei zum Schreiben \u00f6ffnen
std::ofstream out(filename.text());
if (!out.is_open())
return -1;
// Alles ok soweit...
if (!stripped)
modifiedCmds(false); // command export doesn't change modified flag
// get recruitment costs
if (!recruitment())
m_recruitment = 100;