forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.cpp
More file actions
645 lines (599 loc) · 18.5 KB
/
Copy pathgenerator.cpp
File metadata and controls
645 lines (599 loc) · 18.5 KB
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
#include <iostream>
#include <algorithm>
#include <tinyxml.h>
#include <tinystr.h>
#include <set>
#include <map>
#include <vector>
#include <sstream>
#include <dfhack/DFIntegers.h>
using namespace std;
class indentr
{
public:
friend std::basic_ostream<char>& operator <<(std::basic_ostream<char>&, const indentr &);
indentr(int ns = 0, int size = 4)
{
numspaces = ns;
step = size;
}
void indent ()
{
numspaces += step;
if (numspaces < 0) numspaces = 0;
}
void unindent ()
{
numspaces -= step;
if (numspaces < 0) numspaces = 0;
}
int get ()
{
return numspaces;
}
void set (int ns)
{
numspaces = ns;
if (numspaces < 0) numspaces = 0;
}
private:
int step;
int numspaces;
};
std::basic_ostream<char>& operator<< (std::basic_ostream<char>& os, const indentr & idtr)
{
for(int i = 0; i < idtr.numspaces ;i++)
{
os << ' ';
}
return os;
}
class OffsetGroup
{
protected:
set <string> addresses;
set <string> hexvals;
set <string> offsets;
set <string> strings;
set <OffsetGroup *> groups;
std::string name;
OffsetGroup * parent;
public:
OffsetGroup(const std::string & _name, OffsetGroup * parent = 0);
~OffsetGroup();
virtual string toDeclaration(uint32_t indent);
virtual string toReader(uint32_t indent);
void createOffset (const std::string & key);
void createAddress (const std::string & key);
void createHexValue (const std::string & key);
void createString (const std::string & key);
OffsetGroup * createGroup ( const std::string & name );
std::string getName();
std::string getFullName();
OffsetGroup * getParent();
};
class Base : public OffsetGroup
{
public:
Base(const string & _name);
~Base();
virtual string toDeclaration(uint32_t indent);
virtual string toReader(uint32_t indent);
};
void OffsetGroup::createOffset(const string & key)
{
offsets.insert(key);
}
void OffsetGroup::createAddress(const string & key)
{
addresses.insert(key);
}
void OffsetGroup::createHexValue(const string & key)
{
hexvals.insert(key);
}
void OffsetGroup::createString(const string & key)
{
strings.insert(key);
}
OffsetGroup * OffsetGroup::createGroup(const std::string &name)
{
std::set< OffsetGroup* >::iterator iter = groups.begin();
while (iter != groups.end())
{
if((*iter)->getName() == name)
return (*iter);
iter ++;
}
OffsetGroup * ret = new OffsetGroup(name, this);
groups.insert(ret);
return ret;
}
Base::Base(const std::string& _name): OffsetGroup(_name)
{
}
Base::~Base()
{
this->OffsetGroup::~OffsetGroup();
}
OffsetGroup::OffsetGroup(const std::string & name, OffsetGroup * parent)
{
this->name = name;
this->parent = parent;
}
OffsetGroup::~OffsetGroup()
{
for( std::set< OffsetGroup* >::iterator it = groups.begin();it != groups.end();it++)
{
delete (*it);
}
groups.clear();
}
string Base::toDeclaration(uint32_t indent)
{
ostringstream os;
indentr i(indent);
os << i << "class " << name << "_offsets" << endl << i << "{" << endl;
i.indent();
os << i << "public:" << endl;
set< string >::iterator iter;
for( iter = addresses.begin(); iter != addresses.end(); iter++)
{
os << i << "Address " << *iter << ";" << endl;
}
for(iter = offsets.begin(); iter != offsets.end(); iter++)
{
os << i << "Offset " << *iter << ";" << endl;
}
for(iter = hexvals.begin(); iter != hexvals.end(); iter++)
{
os << i << "HexValue " << *iter << ";" << endl;
}
for(iter = strings.begin(); iter != strings.end(); iter++)
{
os << i << "DFString " << *iter << ";" << endl;
}
for(std::set< OffsetGroup* >::iterator iter4 = groups.begin(); iter4 != groups.end(); iter4++)
{
os << (*iter4)->toDeclaration(i.get());
}
os << endl;
os << i << "bool Load(TiXmlElement* version);" << endl;
i.unindent();
os << i << "};" << endl;
return os.str();
}
string OffsetGroup::toDeclaration(uint32_t indent)
{
ostringstream os;
indentr i(indent);
os << i << "struct grp_" << name << endl << i << "{" << endl;
i.indent();
set< string >::iterator iter;
for( iter = addresses.begin(); iter != addresses.end(); iter++)
{
os << i << "Address " << *iter << ";" << endl;
}
for(iter = offsets.begin(); iter != offsets.end(); iter++)
{
os << i << "Offset " << *iter << ";" << endl;
}
for(iter = hexvals.begin(); iter != hexvals.end(); iter++)
{
os << i << "HexValue " << *iter << ";" << endl;
}
for(iter = strings.begin(); iter != strings.end(); iter++)
{
os << i << "BaseString " << *iter << ";" << endl;
}
for(std::set< OffsetGroup* >::iterator iter4 = groups.begin(); iter4 != groups.end(); iter4++)
{
os << (*iter4)->toDeclaration(i.get());
}
i.unindent();
os << i << "} " << name << ";" << endl;
return os.str();
}
string Base::toReader(uint32_t indent)
{
return OffsetGroup::toReader(indent);
}
string OffsetGroup::toReader(uint32_t indent)
{
return "bleh\n";
}
std::string OffsetGroup::getName()
{
return name;
}
OffsetGroup * OffsetGroup::getParent()
{
return parent;
}
std::string OffsetGroup::getFullName()
{
string temp, accum;
OffsetGroup * curr = this;
while(curr)
{
temp = curr->getName() + string("/") + accum;
accum = temp;
curr = curr->getParent();
}
return accum;
}
void ParseOffsets(TiXmlElement * parent, OffsetGroup* target)
{
// we parse the groups iteratively instead of recursively
// breadcrubs acts like a makeshift stack
// first pair entry stores the current element of that level
// second pair entry the group object from OffsetGroup
typedef pair < TiXmlElement *, OffsetGroup * > groupPair;
vector< groupPair > breadcrumbs;
{
TiXmlElement* pEntry;
// we get the <Offsets>, look at the children
pEntry = parent->FirstChildElement();
if(!pEntry)
return;
breadcrumbs.push_back(groupPair(pEntry,target));
}
// work variables
OffsetGroup * currentGroup = 0;
TiXmlElement * currentElem = 0;
//cerr << "<Offsets>"<< endl;
while(1)
{
// get current work variables
currentElem = breadcrumbs.back().first;
currentGroup = breadcrumbs.back().second;
// we reached the end of the current group?
if(!currentElem)
{
// go one level up
breadcrumbs.pop_back();
// exit if no more work
if(breadcrumbs.empty())
{
break;
}
else
{
//cerr << "</group>" << endl;
continue;
}
}
if(!currentGroup)
{
groupPair & gp = breadcrumbs.back();
gp.first = gp.first->NextSiblingElement();
continue;
}
// skip non-elements
if (currentElem->Type() != TiXmlNode::ELEMENT)
{
groupPair & gp = breadcrumbs.back();
gp.first = gp.first->NextSiblingElement();
continue;
}
// we have a valid current element and current group
// get properties
string type = currentElem->Value();
std::transform(type.begin(), type.end(), type.begin(), ::tolower);
const char *cstr_name = currentElem->Attribute("name");
if(!cstr_name)
{
groupPair & gp = breadcrumbs.back();
gp.first = gp.first->NextSiblingElement();
cerr << "Entry has no name! line: " << currentElem->Row() <<endl;
continue;
}
// evaluate elements
if(type == "group")
{
// create group
OffsetGroup * og;
og = currentGroup->createGroup(cstr_name);
// advance this level to the next element
groupPair & gp = breadcrumbs.back();
gp.first = currentElem->NextSiblingElement();
// add a new level that will be processed next
breadcrumbs.push_back(groupPair(currentElem->FirstChildElement(), og));
continue;
}
else if(type == "address")
{
currentGroup->createAddress(cstr_name);
}
else if(type == "offset")
{
currentGroup->createOffset(cstr_name);
}
else if(type == "string")
{
currentGroup->createString(cstr_name);
}
else if(type == "hexvalue")
{
currentGroup->createHexValue(cstr_name);
}
// advance to next element
groupPair & gp = breadcrumbs.back();
gp.first = currentElem->NextSiblingElement();
continue;
}
//cerr << "</Offsets>"<< endl;
}
void ParseBase (TiXmlElement* entry, OffsetGroup* mem)
{
TiXmlElement* pElement;
TiXmlElement* pElement2nd;
const char *cstr_version = entry->Attribute("name");
if (!cstr_version)
{
cerr << "Base has no name at row " << entry->Row() << endl;
return;
}
// process additional entries
pElement = entry->FirstChildElement()->ToElement();
for(;pElement;pElement=pElement->NextSiblingElement())
{
// only elements get processed
const char *cstr_type = pElement->Value();
std::string type = cstr_type;
if(type == "VTable")
{
// do nothing
// parsevtables
continue;
}
else if(type == "Offsets")
{
// we don't care about the descriptions here, do nothing
ParseOffsets(pElement, mem);
continue;
}
else if (type == "Professions")
{
pElement2nd = pElement->FirstChildElement("Profession");
for(;pElement2nd;pElement2nd=pElement2nd->NextSiblingElement("Profession"))
{
const char * id = pElement2nd->Attribute("id");
const char * name = pElement2nd->Attribute("name");
// FIXME: missing some attributes here
if(id && name)
{
continue;
}
else
{
cerr << "Underspecified Profession tag at line " << pElement2nd->Row() << endl;
continue;
}
}
}
else if (type == "Jobs")
{
pElement2nd = pElement->FirstChildElement("Job");
for(;pElement2nd;pElement2nd=pElement2nd->NextSiblingElement("Job"))
{
const char * id = pElement2nd->Attribute("id");
const char * name = pElement2nd->Attribute("name");
if(id && name)
{
continue;
}
else
{
cerr << "Underspecified Job tag at line " << pElement2nd->Row() << endl;
continue;
}
}
}
else if (type == "Skills")
{
pElement2nd = pElement->FirstChildElement("Skill");
for(;pElement2nd;pElement2nd=pElement2nd->NextSiblingElement("Skill"))
{
const char * id = pElement2nd->Attribute("id");
const char * name = pElement2nd->Attribute("name");
if(id && name)
{
continue;
}
else
{
cerr << "Underspecified Skill tag at line " << pElement2nd->Row() << endl;
continue;
}
}
}
else if (type == "Traits")
{
pElement2nd = pElement->FirstChildElement("Trait");
for(;pElement2nd;pElement2nd=pElement2nd->NextSiblingElement("Trait"))
{
const char * id = pElement2nd->Attribute("id");
const char * name = pElement2nd->Attribute("name");
const char * lvl0 = pElement2nd->Attribute("level_0");
const char * lvl1 = pElement2nd->Attribute("level_1");
const char * lvl2 = pElement2nd->Attribute("level_2");
const char * lvl3 = pElement2nd->Attribute("level_3");
const char * lvl4 = pElement2nd->Attribute("level_4");
const char * lvl5 = pElement2nd->Attribute("level_5");
if(id && name && lvl0 && lvl1 && lvl2 && lvl3 && lvl4 && lvl5)
{
continue;
}
else
{
cerr << "Underspecified Trait tag at line " << pElement2nd->Row() << endl;
continue;
}
}
}
else if (type == "Labors")
{
pElement2nd = pElement->FirstChildElement("Labor");
for(;pElement2nd;pElement2nd=pElement2nd->NextSiblingElement("Labor"))
{
const char * id = pElement2nd->Attribute("id");
const char * name = pElement2nd->Attribute("name");
if(id && name)
{
continue;
}
else
{
cerr << "Underspecified Labor tag at line " << pElement2nd->Row() << endl;
continue;
}
}
}
else if (type == "Levels")
{
pElement2nd = pElement->FirstChildElement("Level");
for(;pElement2nd;pElement2nd=pElement2nd->NextSiblingElement("Level"))
{
const char * id = pElement2nd->Attribute("id");
const char * name = pElement2nd->Attribute("name");
const char * nextlvl = pElement2nd->Attribute("xpNxtLvl");
if(id && name && nextlvl)
{
continue;
}
else
{
cerr << "Underspecified Level tag at line " << pElement2nd->Row() << endl;
continue;
}
}
}
else if (type == "Moods")
{
pElement2nd = pElement->FirstChildElement("Mood");
for(;pElement2nd;pElement2nd=pElement2nd->NextSiblingElement("Mood"))
{
const char * id = pElement2nd->Attribute("id");
const char * name = pElement2nd->Attribute("name");
if(id && name)
{
continue;
}
else
{
cerr << "Underspecified Mood tag at line " << pElement2nd->Row() << endl;
continue;
}
}
}
else
{
cerr << "Unknown tag " << type << " at line " << pElement->Row() << endl;
continue;
}
} // for
} // method
// load the XML file with offsets
bool loadFile(string path_to_xml, vector <Base *> & bases)
{
TiXmlDocument doc( path_to_xml.c_str() );
//bool loadOkay = doc.LoadFile();
if (!doc.LoadFile())
{
cerr << "Can't load file: " << path_to_xml << ", " << doc.ErrorDesc() << ", line " << doc.ErrorRow() << endl;
return false;
}
TiXmlHandle hDoc(&doc);
TiXmlElement* pElem;
TiXmlHandle hRoot(0);
// block: name
{
pElem=hDoc.FirstChildElement().Element();
// should always have a valid root but handle gracefully if it does
if (!pElem)
{
cerr << "XML file has no root element" << endl;
return false;
}
string m_name=pElem->Value();
if(m_name != "DFHack")
{
cerr << "Root element isn't DFHack'" << endl;
return false;
}
// save this for later
hRoot=TiXmlHandle(pElem);
}
// transform elements
{
// trash existing list
for(uint32_t i = 0; i < bases.size(); i++)
{
delete bases[i];
}
bases.clear();
// For each base version, get offsets structure
TiXmlElement* pMemInfo=hRoot.FirstChild( "Base" ).Element();
map <string ,TiXmlElement *> map_pNamedEntries;
vector <string> v_sEntries;
for( ; pMemInfo; pMemInfo=pMemInfo->NextSiblingElement("Base"))
{
const char *name = pMemInfo->Attribute("name");
if(name)
{
string str_name = name;
Base *base = new Base(name);
ParseBase( pMemInfo , base );
bases.push_back(base);
}
}
}
return true;
}
int main ( int argc, char** argv )
{
if(argc < 2)
{
cout << "A file is required!" << ::std::endl;
return EXIT_FAILURE;
}
vector <Base *> bases;
if(loadFile(argv[1],bases))
{
for(int i = 0; i < bases.size(); i++)
{
ostringstream header;
header << "=== FILE: " << bases[i]->getName() << "_offsets.h ===" << endl;
header << "/*" << endl
<< " * This file was generated by DFHack." << endl
<< " * Any changes will be overwritten" << endl
<< " */" << endl << endl;
header << "#ifndef VERINFO_GEN_H" << endl;
header << "#define VERINFO_GEN_H" << endl << endl;
header << "#include \"DFPragma.h\"" << endl;
header << "#include \"DFExport.h\"" << endl;
header << "#include \"BaseTypes.h\"" << endl << endl;
header << bases[i]->toDeclaration(0);
header << "#endif /* VERINFO_GEN_H */" << endl;
// save file here
cout << header.str();
ostringstream code;
code << "=== FILE: " << bases[i]->getName() << "_offsets.cpp ===" << endl;
code << "/*" << endl
<< " * This file was generated by DFHack." << endl
<< " * Any changes will be overwritten" << endl
<< " */" << endl << endl;
code << "#include \"" << bases[i]->getName() << "_offsets.h\"" << endl;
code << "#include <tinyxml.h>" << endl;
code << "#include <tinystr.h>" << endl;
code << "bool "<< bases[i]->getName() <<"_offsets::Load(TiXmlElement* version)" << endl;
code << "{" << endl;
code << bases[i]->toReader(0);
code << "}" << endl;
cout << code.str();
}
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}