forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.cpp
130 lines (119 loc) · 3.97 KB
/
init.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
#include "init.h"
#include "json.h"
#include "material.h"
#include "bionics.h"
#include "profession.h"
#include "skill.h"
#include "mutation.h"
#include "text_snippets.h"
#include "item_factory.h"
#include "crafting.h"
#include <string>
#include <vector>
#include <fstream>
#include <sstream> // for throwing errors
/* Currently just for loading JSON data from files in data/raw */
// TODO: make this actually load files from the named directory
std::vector<std::string> listfiles(std::string const &dirname)
{
std::vector<std::string> ret;
ret.push_back("data/json/materials.json");
ret.push_back("data/json/bionics.json");
ret.push_back("data/json/professions.json");
ret.push_back("data/json/skills.json");
ret.push_back("data/json/dreams.json");
ret.push_back("data/json/mutations.json");
ret.push_back("data/json/snippets.json");
ret.push_back("data/json/item_groups.json");
ret.push_back("data/json/recipes.json");
return ret;
}
void load_object(JsonObject &jo)
{
std::string type = jo.get_string("type");
if (type == "material") { material_type::load_material(jo);}
else if (type == "bionic") { load_bionic(jo); }
else if (type == "profession") { profession::load_profession(jo); }
else if (type == "skill") { Skill::load_skill(jo); }
else if (type == "dream") { load_dream(jo); }
else if (type == "mutation") { load_mutation(jo); }
else if (type == "snippet") { SNIPPET.load_snippet(jo); }
else if (type == "item_group") { item_controller->load_item_group(jo); }
else if (type == "recipe_category") { load_recipe_category(jo); }
else if (type == "recipe") { load_recipe(jo); }
else {
std::stringstream err;
err << jo.line_number() << ": ";
err << "unrecognized JSON object, type: \"" << type << "\"";
throw err.str();
}
}
void init_data_structures()
{
mutations_category[""].clear();
init_mutation_parts();
}
void load_json_dir(std::string const &dirname)
{
// get a list of all files in the directory
std::vector<std::string> dir = listfiles(dirname);
// iterate over each file
std::vector<std::string>::iterator it;
for (it = dir.begin(); it != dir.end(); it++) {
// open the file as a stream
std::ifstream infile(it->c_str(), std::ifstream::in | std::ifstream::binary);
// parse it
try {
JsonIn jsin(&infile);
load_all_from_json(jsin);
} catch (std::string e) {
throw *(it) + ": " + e;
}
}
}
void load_all_from_json(JsonIn &jsin)
{
char ch;
std::string type = "";
jsin.eat_whitespace();
// examine first non-whitespace char
ch = jsin.peek();
if (ch == '{') {
// find type and dispatch single object
JsonObject jo = jsin.get_object();
load_object(jo);
jo.finish();
// if there's anything else in the file, it's an error.
jsin.eat_whitespace();
if (jsin.good()) {
std::stringstream err;
err << jsin.line_number() << ": ";
err << "expected single-object file but found '";
err << jsin.peek() << "'";
throw err.str();
}
} else if (ch == '[') {
jsin.start_array();
// find type and dispatch each object until array close
while (!jsin.end_array()) {
jsin.eat_whitespace();
ch = jsin.peek();
if (ch != '{') {
std::stringstream err;
err << jsin.line_number() << ": ";
err << "expected array of objects but found '";
err << ch << "', not '{'";
throw err.str();
}
JsonObject jo = jsin.get_object();
load_object(jo);
jo.finish();
}
} else {
// not an object or an array?
std::stringstream err;
err << jsin.line_number() << ": ";
err << "expected object or array, but found '" << ch << "'";
throw err.str();
}
}