-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathck2Parser.h
825 lines (668 loc) · 15.9 KB
/
ck2Parser.h
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
/*****************************
* Author: Max Ortner
* Date: March 10, 2019
*/
#pragma once
#ifndef CK2_NO_STDLIB
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#endif
namespace ck2
{
// Strip a given character from the beginning and ends
// of a string
std::string strip(const std::string &str, char c)
{
unsigned int first, last;
for (int i = 0; i < str.length(); i++)
if (str[i] != c)
{
first = i; break;
}
for (int i = str.length(); i > -1; i--)
{
if (str[i] != c)
{
last = i; break;
}
}
if (first > last) return std::string();
return std::string(str.begin() + first, str.begin() + last);
}
// Splits a string into a vector with a given delimeter
std::vector<std::string> split(const std::string &str, char del)
{
std::vector<std::string> r;
unsigned int last = 0;
for (int i = 0; i < str.length(); i++)
{
if (str[i] == del)
{
r.push_back(std::string(str.begin() + last, str.begin() + i));
last = i;
}
if (i == str.length() - 1)
{
int offset = 1;
if (r.size() == 0) offset = 0;
r.push_back(std::string(str.begin() + last + offset, str.end()));
break;
}
}
return r;
}
// Combiles a vector into a string with a given delimeter
std::string combine(std::vector<std::string> v, char glue)
{
std::string r;
for (int i = 0; i < v.size(); i++)
r += v.at(i) + glue;
return r;
}
//
// Pair of two objects of type F and S
//
template<typename F, typename S>
struct Pair
{
F first;
S second;
Pair()
{
}
Pair(F f, S s) :
first(f), second(s)
{
}
};
//
// A List of Pairs
//
template<typename F, typename S>
struct Dictionary
{
virtual S* at(F val)
{
for (int i = 0; i < data.size(); i++)
if (val == data.at(i).first)
return &data.at(i).second;
return nullptr;
}
virtual void push(const Pair<F, S> &val)
{
data.push_back(val);
}
virtual unsigned int size() const
{
return data.size();
}
protected:
std::vector< Pair<F, S> > data;
};
//
// File object for reading files at a given directory
//
class File
{
// Stores each line
std::vector<std::string> data;
// Key Characters to strip out of each line
std::vector<char> key_chars = {
' ', '\t'
};
public:
File(const char* directory)
{
std::fstream stream(directory, std::ios::in);
if (!stream)
throw std::runtime_error(std::string("Cannot open file ") + directory);
for (std::string line; std::getline(stream, line);)
{
for (int c = 0; c < key_chars.size(); c++)
line = strip(line, key_chars.at(c));
data.push_back(line);
}
stream.close();
}
std::vector<std::string> &getData()
{
return data;
}
};
//
// Base class of functions for parsing data
//
class Parser
{
protected:
typedef Pair<std::string, std::string> StringPair;
// Returns Pair of strings given a string of a property
StringPair getProperty(std::string line, char del = '=') const
{
StringPair r;
for (int i = 0; i < line.length(); i++)
{
if (line[i] != del) continue;
r.first = std::string(line.begin(), line.begin() + i);
r.second = std::string(line.begin() + i + 1, line.end());
break;
}
return r;
}
// Determines whether or not a given character is in a given string
bool contains(std::string line, char c) const
{
for (int i = 0; i < line.length(); i++)
if (line[i] == c)
return true;
return false;
}
// Replaces all occurences of one character with another
std::string replace(std::string line, char c, char rep) const
{
std::string r;
for (int i = 0; i < line.length(); i++)
{
if (line[i] != c)
r += line[i];
else
r += rep;
}
return r;
}
// Removes all occurrences of a character
std::string remove(std::string line, char c) const
{
std::string r;
for (int i = 0; i < line.length(); i++)
{
if (line[i] != c)
r += line[i];
}
return r;
}
};
//
// Object representing a trait
//
struct Trait
{
Trait(unsigned int i, std::string n) :
_name(n), _id(i)
{
}
std::string name() const { return _name; }
unsigned int id() const { return _id; }
private:
std::string _name;
unsigned int _id;
};
//
// Object that handles Traits csv
//
class TraitCSV : public File
{
public:
TraitCSV(std::string dir) :
File(dir.c_str())
{
if (getData().size() == 0)
throw std::runtime_error("traits.csv not found!");
}
};
TraitCSV traitCSV("traits.csv");
struct Attributes : Parser
{
Attributes(std::string line)
{
// Clean up the string from the dictionary
std::string str_attr;
str_attr = remove(line, '}');
str_attr = remove(str_attr, '{');
// Delimit the string with spaces
std::vector<std::string> ts = split(str_attr, ' ');
// Clean up each object
for (std::string &str : ts)
str = strip(str, ' ');
for (int i = 0; i < 5 && i < ts.size(); i++)
_vals[i] = std::stoi(ts.at(i));
}
// Getting the values
unsigned int diplomacy() const
{
return _dip;
}
unsigned int marshall() const
{
return _mart;
}
unsigned int stewardship() const
{
return _stew;
}
unsigned int intrigue() const
{
return _int;
}
unsigned int learning() const
{
return _learn;
}
private:
union
{
struct
{
unsigned int _vals[5];
};
struct
{
unsigned int _dip, _mart, _stew, _int, _learn;
};
};
};
struct Entity : Parser
{
unsigned int ID;
Dictionary<std::string, std::string> dictionary;
protected:
std::string getStringfromDict(std::string key)
{
if (!dictionary.at(key)) return std::string("");
return remove(*dictionary.at(key), '"');
}
float getFloatFromDict(std::string key)
{
if (!dictionary.at(key)) return -1.f;
return std::stof(*dictionary.at(key));
}
void parseData(std::vector<Pair<int, std::string>> data)
{
for (Pair<int, std::string> line : data) {
StringPair property = getProperty(line.second);
dictionary.push(property);
}
}
};
//
// Object representing a Title
//
struct Title : public Entity
{
friend class SaveFile;
std::string succession()
{
return getStringfromDict("succession");
}
std::string gender()
{
return getStringfromDict("gender");
}
unsigned int holderID()
{
return (int)getFloatFromDict("holder");
}
};
//
// Object representing a Dynasty
//
struct Dynasty : public Entity
{
friend class SaveFile;
std::string name()
{
return getStringfromDict("name");
}
std::string culture()
{
return getStringfromDict("culture");
}
std::string religion()
{
return getStringfromDict("religion");
}
};
//
// Object representing a Character
//
struct Character : public Entity
{
friend class SaveFile;
// Functions for getting data
std::string name()
{
return getStringfromDict("bn");
}
std::string birthDate()
{
return getStringfromDict("b_d");
}
std::string deathDate()
{
return getStringfromDict("d_d");
}
std::string religion()
{
return getStringfromDict("rel");
}
std::string secret_religion()
{
return getStringfromDict("secret_religion");
}
std::string government()
{
return getStringfromDict("gov");
}
std::string title(bool formatted = true)
{
std::string title = getStringfromDict("title");
if (formatted && title.length() > 0)
{
title = std::string(title.begin() + 6, title.end());
title = strip(title, '\t');
auto title_split = split(title, '_');
for (std::string &str : title_split)
str[0] = toupper(str[0]);
title = combine(title_split, ' ');
return title;
}
return title;
}
std::string nickname(bool formatted = true)
{
std::string nickname = getStringfromDict("nick");
if (formatted && nickname.length() > 0)
{
nickname = std::string(nickname.begin() + 5, nickname.end());
nickname = strip(nickname, '\t');
auto title_split = split(nickname, '_');
for (std::string &str : title_split)
str[0] = toupper(str[0]);
nickname = combine(title_split, ' ');
return nickname;
}
return nickname;
}
std::vector<Trait> traits()
{
std::vector<Trait> r;
// Clean up the string from the dictionary
std::string str_traits;
str_traits = remove(getStringfromDict("traits"), '}');
str_traits = remove(str_traits, '{');
str_traits = strip(str_traits, ' ');
// Delimit the string with spaces
std::vector<std::string> ts = split(str_traits, ' ');
// Clean up each object
for (std::string &str : ts)
str = strip(str, ' ');
// Go through each line of the csv
for (int t = 0; t < traitCSV.getData().size(); t++)
for (int i = 0; i < ts.size(); i++)
{
auto trait = getProperty(traitCSV.getData().at(t), ',');
if (trait.second == ts.at(i))
{
r.push_back(Trait(std::stoi(trait.second), trait.first));
continue;
}
}
return r;
}
Attributes attributes()
{
return Attributes(*dictionary.at("att"));
}
float health()
{
return getFloatFromDict("health");
}
float fertility()
{
return getFloatFromDict("fer");
}
float wealth()
{
return getFloatFromDict("wealth");
}
float piety()
{
return getFloatFromDict("piety");
}
float prestige()
{
return getFloatFromDict("prs");
}
Character* lover()
{
return getFromCharacterList("lover");
}
Character* father()
{
return getFromCharacterList("fat");
}
Character* mother()
{
return getFromCharacterList("mot");
}
Character* spouse()
{
return getFromCharacterList("spouse");
}
Character* host()
{
return getFromCharacterList("host");
}
Dynasty* dynasty()
{
std::string d_str = getStringfromDict("dnt");
if (d_str == "") return nullptr;
return dynastyList->at(std::stoi(d_str));
}
std::vector<Character*> children()
{
try {
return *childList->at(ID);
}
catch (...)
{
return {};
}
}
private:
Character* getFromCharacterList(std::string key)
{
try
{
if (!dictionary.at(key)) return nullptr;
int id = std::stoi(*dictionary.at(key));
return characterList->at(id);
}
catch (...)
{
return nullptr;
}
}
Dictionary<int, Character>* characterList;
Dictionary<int, std::vector<Character*>>* childList;
Dictionary<int, Dynasty>* dynastyList;
};
//
// Object that creates all data for the save file
//
class SaveFile : Parser
{
File &file;
Dictionary<int, Character> characters;
Dictionary<int, Dynasty> dynasties;
//Dictionary<int, std::vector<Title>> titles;
Dictionary<int, std::vector<Character*>> children;
typedef enum {
DYNASTY,
CHARACTERS,
PROVINCES,
TITLE,
AMOUNT
} LINES;
std::vector<int> key_lines;
void parse()
{
// Retreive critical line positions for the different categories
for (int l = 0; l < file.getData().size(); l++)
{
std::string line = file.getData().at(l);
if (line == "dynasties=" && key_lines.at(DYNASTY) == 0)
{
key_lines.at(DYNASTY) = l;
}
if (line == "character=" && key_lines.at(CHARACTERS) == 0)
{
key_lines.at(CHARACTERS) = l;
}
if (line == "provinces=" && key_lines.at(PROVINCES) == 0)
{
key_lines.at(PROVINCES) = l;
}
if (line == "title=" && key_lines.at(DYNASTY) != 0 && key_lines.at(CHARACTERS) != 0 && key_lines.at(PROVINCES) != 0 && key_lines.at(TITLE) == 0)
{
key_lines.at(TITLE) = l;
}
}
//Parse Dynasties
std::vector<Pair<int, std::string>> data;
int lastID = -1;
int level = 1;
for (int i = key_lines.at(DYNASTY) + 2; level > 0; i++)
{
std::string line(file.getData().at(i));
// Check if the dynasty is done
if (line.length() > 1 && line.at(line.length() - 1) == '=' && level == 1)
{
// If its the first person, you've got to pass the ID
if (lastID == -1)
lastID = std::stoi(std::string(line.begin(), line.begin() + line.length() - 1));
// If there is no data for some reason, dont add an object
if (data.size() == 0) continue;
Dynasty* dynasty = new Dynasty;
dynasty->ID = lastID;
dynasty->parseData(data);
dynasties.push(Pair<int, Dynasty>(dynasty->ID, *dynasty));
// Clear the data for the next character
data.clear();
// Begin ID for the next character
lastID = std::stoi(std::string(line.begin(), line.begin() + line.length() - 1));
continue;
}
// If the dynasty continues, determine whether or not to increment
// or decrement the current level
if (contains(line, '{')) level++;
if (contains(line, '}')) level--;
// Push the current line to the data
data.push_back(Pair<int, std::string>(level, line));
}
// Parse Characters
data.clear();
lastID = -1;
level = 1;
for (int i = key_lines.at(CHARACTERS) + 2; level > 0; i++)
{
std::string line(file.getData().at(i));
// Check if the character is done
if (line.length() > 1 && line.at(line.length() - 1) == '=' && level == 1)
{
// If its the first person, you've got to pass the ID
if (lastID == -1)
lastID = std::stoi(std::string(line.begin(), line.begin() + line.length() - 1));
// If there is no data for some reason, dont add an object
if (data.size() == 0) continue;
// Create the character
Character* character = new Character;
character->ID = lastID;
character->parseData(data);
// Lazy way of passing global data to the character class
character->characterList = &characters;
character->childList = &children;
character->dynastyList = &dynasties;
// Push the character
characters.push(Pair<int, Character>(lastID, *character));
// Determine if character has a mother
if (character->dictionary.at("mot"))
{
// Get the ID of the mother
int mother = std::stoi(*character->dictionary.at("mot"));
// If the mother isn't catalogued in the children list,
// make a new entry and pass this current character as one
// of their children
if (!children.at(mother))
{
children.push(
Pair<int, std::vector<Character*>>(
std::stoi(*character->dictionary.at("mot")),
{ character }
)
);
}
// Otherwise, add this character to that person's children
else
{
children.at(std::stoi(*character->dictionary.at("mot")))->push_back(character);
}
}
// Determine if character has a father
if (character->dictionary.at("fat"))
{
// Get the ID of the father
int father = std::stoi(*character->dictionary.at("fat"));
// If the father isn't catalogued in the children list,
// make a new entry and pass this current character as one
// of their children
if (!children.at(father))
{
children.push(
Pair<int, std::vector<Character*>>(
std::stoi(*character->dictionary.at("fat")),
{ character }
)
);
}
// Otherwise, add this character to that person's children
else
{
children.at(std::stoi(*character->dictionary.at("fat")))->push_back(character);
}
}
// Clear the data for the next character
data.clear();
// Begin ID for the next character
lastID = std::stoi(std::string(line.begin(), line.begin() + line.length() - 1));
continue;
}
// If the character continues, determine whether or not to increment
// or decrement the current level
if (contains(line, '{')) level++;
if (contains(line, '}')) level--;
// Push the current line to the data
data.push_back(Pair<int, std::string>(level, line));
}
}
public:
// Initialize variables and parse the data
SaveFile(File f) :
file(f), key_lines(AMOUNT)
{
parse();
}
// Get the amount of characters parsed
unsigned int sizeOfCharacters() const
{
return characters.size();
}
// Get a character from an ID
Character &getCharacter(unsigned int ID)
{
return *characters.at(ID);
}
};
}