add egg creation to qtgui debug menu
[openc2e.git] / tools / praydumper.cpp
blob683a8cfeabfceaee9006c7d71efcbd0c2c444a86
1 #include "pray.h"
2 #include <iostream>
3 #include <fstream>
4 #include <unistd.h>
5 #include <boost/filesystem/convenience.hpp>
7 using std::cout;
8 using std::cerr;
9 using std::endl;
10 using std::ifstream;
11 using std::ofstream;
12 using std::vector;
14 #define NO_TAGBLOCKS 11
15 const char *tagblocks[NO_TAGBLOCKS] = {
16 "AGNT", // C3 agent
17 "DSAG", // DS agent
18 "MACH", // SM agent
19 "HAND", // SM agent
20 "LIVE", // SM agent
21 "MONK", // SM agent
22 "EXPC", // C3 creature info
23 "DSEX", // DS creature info
24 "SFAM", // C3 starter family
25 "EGGS", // eggs
26 "DFAM" // DS starter family
29 int main(int argc, char **argv) {
30 bool outputfiles = true, notags = false, usageerror = false;
31 int ch;
32 while ((ch = getopt(argc, argv, "on")) != -1) {
33 switch (ch) {
34 case 'o': outputfiles = false; break;
35 case 'n': notags = true; break;
36 default: usageerror = true; break;
39 argc -= optind;
40 argv += optind;
41 if ((argc != 1) || usageerror) {
42 cerr << "syntax: praydumper -o -n filename\n-o don't output non-tag blocks as files\n-n do not interpret tag blocks" << endl;
43 return 1;
46 fs::path inputfile = fs::path(argv[0], fs::native);
47 if (!fs::exists(inputfile)) {
48 cerr << "input file doesn't exist!" << endl;
49 return 1;
52 cout << "(- praydumper-generated PRAY file from '" << argv[0] << "' -)" << endl;
53 cout << endl << "\"en-GB\"" << endl;
54 prayFile file(inputfile);
56 for (vector<prayBlock *>::iterator x = file.blocks.begin(); x != file.blocks.end(); x++) {
57 // TODO: s/"/\\"/ in the data (use find/replace methods of string)
59 (*x)->load();
61 bool handled = false;
62 for (unsigned int i = 0; i < NO_TAGBLOCKS; i++) {
63 if ((*x)->type == tagblocks[i]) {
64 handled = true;
65 cout << endl << "group " << (*x)->type << " \"" << (*x)->name << "\"" << endl;
67 (*x)->parseTags();
69 for (std::map<std::string, int>::iterator y = (*x)->integerValues.begin(); y != (*x)->integerValues.end(); y++) {
70 cout << "\"" << y->first << "\" " << y->second << endl;
73 for (std::map<std::string, std::string>::iterator y = (*x)->stringValues.begin(); y != (*x)->stringValues.end(); y++) {
74 std::string name = y->first;
75 if ((name.substr(0, 7) == "Script ") || (name.substr(0, 13) == "Remove script")) {
76 name = (*x)->name + " - " + name + ".cos";
77 ofstream output(name.c_str());
78 output.write(y->second.c_str(), y->second.size());
79 cout << "\"" << y->first << "\" @ \"" << name << "\"" << endl;
80 } else {
81 cout << "\"" << y->first << "\" \"" << y->second << "\"" << endl;
87 if (!handled) {
88 cout << endl << "inline " << (*x)->type << " \"" << (*x)->name << "\" \"" << (*x)->name << "\"" << endl;
90 if (outputfiles) {
91 ofstream output((*x)->name.c_str());
92 output.write((char *)(*x)->getBuffer(), (*x)->getSize());