-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.cpp
More file actions
200 lines (159 loc) · 6.17 KB
/
Copy pathconfig.cpp
File metadata and controls
200 lines (159 loc) · 6.17 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
#include "config.hpp"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <array>
#include <unordered_map>
#include <cctype>
#include "util.hpp"
static const std::string &getTypeString(Json::ValueType type) {
static const std::array<std::string, 8> strings = {
"null",
"int",
"uint",
"real",
"string",
"boolean",
"array",
"object"
};
static const std::string unknown = "unknown";
if (static_cast<std::size_t>(type) >= strings.size())
return unknown;
return strings[type];
}
static void checkMembers(
const std::string &objectPath,
const Json::Value &object,
const std::unordered_map<std::string, Json::ValueType> &recognized
) {
std::string errors;
for (auto &name : object.getMemberNames()) {
Json::ValueType type;
try {
type = recognized.at(name);
} catch (std::out_of_range &exc) {
errors += "Unrecognized member '" + name + "' found in the " + objectPath + " object.";
continue;
}
if (object[name].type() != type &&
type != Json::ValueType::nullValue) {
if (!errors.empty())
errors += '\n';
errors +=
"Member " + objectPath + "." + name + " must have type '" + getTypeString(type) +
"', but has type '" + getTypeString(object[name].type()) + "'.";
}
}
if (!errors.empty())
throw ConfigError(errors);
}
static void checkSpriteInfo(const std::string &parentPath, const Json::Value &info) {
if (info.isString())
return;
throw ConfigError(
"Member " + parentPath + ".sprite must be either a valid RGB value "
"or a path to an image file."
);
}
static SpriteInfo *parseSpriteInfo(const Json::Value &value) {
if (value.isInt())
return new SpriteInfoSquareRGB(value.asInt());
auto s = value.asString();
if (s.front() == '#') {
try {
return new SpriteInfoSquareRGB(std::stoi(s.substr(1), nullptr, 16));
} catch (...) {/* ignore */}
}
return new SpriteInfoImagePath(s);
}
void Config::parseGame() {
checkMembers("root", root, {
{"spriteWidth", Json::ValueType::intValue},
{"spriteHeight", Json::ValueType::intValue},
{"columnNumber", Json::ValueType::intValue},
{"rowNumber", Json::ValueType::intValue},
{"moveDelay", Json::ValueType::intValue},
{"unitsPerLeague", Json::ValueType::intValue},
{"leagues", Json::ValueType::objectValue},
});
for (auto &league : root["leagues"].getMemberNames()) {
const std::string leaguePath = "leagues." + league;
checkMembers(leaguePath, root["leagues"][league], {
{"directory", Json::ValueType::stringValue},
{"defaultSprite", Json::ValueType::nullValue},
{"startKind", Json::ValueType::stringValue},
{"unitKinds", Json::ValueType::objectValue}
});
Json::Value defaultSprite;
if (root["leagues"][league].isMember("defaultSprite")) {
checkSpriteInfo(leaguePath, root["leagues"][league]["defaultSprite"]);
defaultSprite = root["leagues"][league]["defaultSprite"];
} else
defaultSprite = GetRandom(0x1000000);
LeagueInfo info = {
.directory = root["leagues"][league].get("directory", league).asString(),
.startKind = root["leagues"][league].get("startKind", "start").asString()
};
for (auto &kind : root["leagues"][league]["unitKinds"].getMemberNames()) {
const std::string unitKindPath = leaguePath + ".unitKinds." + kind;
checkMembers(unitKindPath, root["leagues"][league]["unitKinds"][kind], {
{"exec", Json::ValueType::stringValue},
{"sprite", Json::ValueType::nullValue}
});
if (root["leagues"][league]["unitKinds"][kind].isMember("sprite"))
checkSpriteInfo(unitKindPath, root["leagues"][league]["unitKinds"][kind]["sprite"]);
info.unitKinds[kind] = {
.exec = root["leagues"][league]["unitKinds"][kind].get("exec", kind + ".dasm").asString(),
.sprite = std::shared_ptr<SpriteInfo>(
parseSpriteInfo(
root["leagues"][league]["unitKinds"][kind].get("sprite", defaultSprite)
)
)
};
}
leagueInfo[league] = info;
}
}
void Config::parseTest() {
abort();
/*for (auto &name : root["tests"].getMemberNames()) {
}*/
}
Config::Config(const char *path) {
auto fs = FileOpenIn(path);
Json::Reader reader;
if (!reader.parse(fs, root))
throw ConfigError(reader);
if (!root.isObject())
throw ConfigError("The root must an object.");
if (root.isMember("tests")) {
checkMembers("root", root, {{"tests", Json::ValueType::objectValue}});
parseTest();
} else
parseGame();
}
ConfigError::ConfigError(const std::string &reason) {
this->reason = reason;
}
ConfigError::ConfigError(const Json::Reader &reader) {
reason = reader.getFormattedErrorMessages();
}
std::ostream &operator <<(std::ostream &os, const SpriteInfo *info) {
switch (info->getType()) {
case SpriteInfo::Type::SquareRGB: {
auto rgb = static_cast<const SpriteInfoSquareRGB *>(info);
auto flags = std::cout.flags();
auto fill = std::cout.fill('0');
os << '#' << std::uppercase << std::setfill('0') <<
std::hex << std::setw(2) << (int)rgb->red <<
std::hex << std::setw(2) << (int)rgb->green <<
std::hex << std::setw(2) << (int)rgb->blue;
std::cout.flags(flags);
std::cout.fill(fill);
return os;
}
case SpriteInfo::Type::ImagePath:
return os << *static_cast<const SpriteInfoImagePath *>(info);
}
}