-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathconfig.cpp
411 lines (355 loc) · 14.3 KB
/
config.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
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
#include <stdinc.h>
#include "commands.hpp"
#include "program.hpp"
#include "system.hpp"
#include <rapidxml.hpp>
#include <rapidxml_utils.hpp>
// Bob Jenkins one-at-a-time hash function
// As described in http://www.burtleburtle.net/bob/hash/doobs.html
static uint32_t one_at_a_time(const char *key)
{
uint32_t hash;
for(hash=0; *key; ++key)
{
hash += *key;
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
static int xml_stoi(const char* string)
{
try
{
return std::stoi(string, nullptr, 0);
}
catch(const std::exception& e)
{
throw ConfigError("couldn't convert string to int: {}", e.what());
}
}
static unsigned int xml_stou(const char* string)
{
try
{
return (unsigned int)(std::stoul(string, nullptr, 0));
}
catch(const std::exception& e)
{
throw ConfigError("couldn't convert string to unsigned int: {}", e.what());
}
}
static bool xml_to_bool(const char* string)
{
if(!strcmp(string, "true"))
return true;
else if(!strcmp(string, "false"))
return false;
else
throw ConfigError("boolean is not 'true' or 'false', it is '{}'", string);
}
static bool xml_to_bool(const rapidxml::xml_attribute<>* attrib, bool default_value)
{
return attrib? xml_to_bool(attrib->value()) : default_value;
}
static ArgType xml_to_argtype(const char* string)
{
if(!strcmp(string, "INT"))
return ArgType::Integer;
else if(!strcmp(string, "FLOAT"))
return ArgType::Float;
else if(!strcmp(string, "PARAM"))
return ArgType::Param;
else if(!strcmp(string, "LABEL"))
return ArgType::Label;
else if(!strcmp(string, "CONSTANT"))
return ArgType::Constant;
else if(!strcmp(string, "TEXT_LABEL"))
return ArgType::TextLabel;
else if(!strcmp(string, "TEXT_LABEL16"))
return ArgType::TextLabel16;
else if(!strcmp(string, "TEXT_LABEL32"))
return ArgType::TextLabel32;
else if(!strcmp(string, "STRING"))
return ArgType::String;
else
throw ConfigError("unexpected 'Type' attribute: {}", string);
}
static void parse_enum_node(transparent_map<std::string, shared_ptr<Enum>>& enums, const rapidxml::xml_node<>* enum_node)
{
using namespace rapidxml;
xml_attribute<>* enum_name_attrib = enum_node->first_attribute("Name");
xml_attribute<>* enum_global_attrib = enum_node->first_attribute("Global");
if(!enum_name_attrib)
throw ConfigError("missing 'Name' attribute on '<Enum>' node");
bool is_global = xml_to_bool(enum_global_attrib, false);
auto eit = enums.find(enum_name_attrib->value());
if(eit == enums.end())
{
auto enum_ptr = std::make_shared<Enum>(Enum { {}, is_global });
eit = enums.emplace(enum_name_attrib->value(), std::move(enum_ptr)).first;
}
else
{
assert(is_global == eit->second->is_global);
}
insensitive_map<std::string, int32_t>& constant_map = eit->second->values;
int32_t current_value = 0;
for(auto value_node = enum_node->first_node(); value_node; value_node = value_node->next_sibling())
{
assert(!strcmp(value_node->name(), "Constant"));
xml_attribute<>* value_name_attrib = value_node->first_attribute("Name");
xml_attribute<>* value_value_attrib = value_node->first_attribute("Value");
if(!value_name_attrib)
throw ConfigError("missing 'Name' attribute on '<Constant>' node");
if(value_value_attrib)
current_value = xml_stoi(value_value_attrib->value());
constant_map.emplace(value_name_attrib->value(), current_value);
++current_value;
}
}
static Command
parse_command_node(
const rapidxml::xml_node<>* cmd_node,
transparent_map<std::string, EntityType>& entities,
const transparent_map<std::string, shared_ptr<Enum>>& enums)
{
using namespace rapidxml;
xml_attribute<>* id_attrib = cmd_node->first_attribute("ID");
xml_attribute<>* hash_attrib = cmd_node->first_attribute("Hash");
xml_attribute<>* name_attrib = cmd_node->first_attribute("Name");
xml_attribute<>* support_attrib = cmd_node->first_attribute("Supported");
xml_attribute<>* internal_attrib = cmd_node->first_attribute("Internal");
xml_attribute<>* extension_attrib = cmd_node->first_attribute("Extension");
xml_node<>* args_node = cmd_node->first_node("Args");
if(!name_attrib || !(id_attrib || hash_attrib))
throw ConfigError("missing 'Name' or 'ID'/'Hash' attribute on '<Command>' node");
decltype(Command::args) args;
if(args_node)
{
size_t num_args = 0;
// Avoid realocations, find out the number of args beforehand.
for(auto x = args_node->first_node(); x; x = x->next_sibling())
++num_args;
args.reserve(num_args);
for(auto arg_node = args_node->first_node("Arg"); arg_node; arg_node = arg_node->next_sibling("Arg"))
{
auto type_attrib = arg_node->first_attribute("Type");
auto out_attrib = arg_node->first_attribute("Out");
auto ref_attrib = arg_node->first_attribute("Ref");
auto optional_attrib = arg_node->first_attribute("Optional");
auto allow_const_attrib = arg_node->first_attribute("AllowConst");
auto allow_gvar_attrib = arg_node->first_attribute("AllowGlobalVar");
auto allow_lvar_attrib = arg_node->first_attribute("AllowLocalVar");
auto allow_text_label_attrib = arg_node->first_attribute("AllowTextLabel");
auto allow_pointer_attrib= arg_node->first_attribute("AllowPointer");
auto preserve_case_attrib= arg_node->first_attribute("PreserveCase");
auto entity_attrib = arg_node->first_attribute("Entity");
auto enum_attrib = arg_node->first_attribute("Enum");
if(!type_attrib)
throw ConfigError("missing 'Type' attribute on '<Arg>' node");
// Cannot build with a initializer list, VS goes mad :(
Command::Arg arg;
arg.type = xml_to_argtype(type_attrib->value());
arg.optional = xml_to_bool(optional_attrib, false);
arg.is_output = xml_to_bool(out_attrib, false);
arg.is_ref = xml_to_bool(ref_attrib, false);
arg.allow_constant = xml_to_bool(allow_const_attrib, arg.is_output? false : true);
arg.allow_global_var= xml_to_bool(allow_gvar_attrib, arg.type != ArgType::Label? true : false);
arg.allow_local_var = xml_to_bool(allow_lvar_attrib, arg.type != ArgType::Label? true : false);
arg.allow_text_label = xml_to_bool(allow_text_label_attrib, false);
arg.allow_pointer = xml_to_bool(allow_pointer_attrib, false);
arg.preserve_case = xml_to_bool(preserve_case_attrib, false);
arg.entity_type = 0;
if(enum_attrib)
{
auto eit = enums.find(enum_attrib->value());
if(eit != enums.end())
{
arg.enums.emplace_back(eit->second);
arg.enums.shrink_to_fit();
}
}
if(entity_attrib)
{
auto it = entities.emplace(entity_attrib->value(), EntityType(1 + entities.size())).first;
arg.entity_type = it->second;
}
args.emplace_back(std::move(arg));
}
}
optional<uint16_t> id;
optional<uint32_t> hash;
string_view name = name_attrib->value();
if(hash_attrib)
{
hash = xml_stou(hash_attrib->value());
assert(*hash == one_at_a_time(name_attrib->value()));
}
if(id_attrib)
{
id = uint16_t(xml_stoi(id_attrib->value()) & 0x7FFF);
}
return Command {
xml_to_bool(support_attrib, true), // supported
xml_to_bool(internal_attrib, false), // internal
xml_to_bool(extension_attrib, false), // extension
std::move(id), // id
std::move(hash), // hash
std::move(args), // args
name.to_string(), // name
};
}
static void
parse_alternator_node(
insensitive_map<std::string, std::vector<const Command*>>& alternators,
const rapidxml::xml_node<>* alt_node,
const transparent_set<Command>& commands)
{
using namespace rapidxml;
xml_attribute<>* name_attrib = alt_node->first_attribute("Name");
if(!name_attrib)
throw ConfigError("missing 'Name' attribute on '<Alternator>' node");
auto eit = alternators.find(name_attrib->value());
if(eit == alternators.end())
{
eit = alternators.emplace(name_attrib->value(), std::vector<const Command*>{}).first;
}
for(auto node = alt_node->first_node(); node; node = node->next_sibling())
{
xml_attribute<>* attrib = node->first_attribute("Name");
if(!attrib)
throw ConfigError("missing 'Name' attribute on '<Alternative>' node");
auto it = commands.find(attrib->value());
if(it != commands.end())
eit->second.emplace_back(std::addressof(*it));
}
}
Commands Commands::from_xml(const std::string& config_name, const std::vector<fs::path>& xml_list)
{
using namespace rapidxml;
// Order by priority (which should be read first).
constexpr int XML_SECTION_CONSTANTS = 1;
constexpr int XML_SECTION_COMMANDS = 2;
constexpr int XML_SECTION_ALTERNATORS = 3;
struct XmlData
{
std::string buffer;
std::unique_ptr<xml_document<>> doc;
};
std::vector<XmlData> xml_vector;
std::vector<std::pair<int, xml_node<>*>> xml_sections;
transparent_set<Command> commands;
insensitive_map<std::string, std::vector<const Command*>> alternators;
transparent_map<std::string, EntityType> entities;
transparent_map<std::string, shared_ptr<Enum>> enums;
// fundamental enums
enums.emplace("MODEL", std::make_shared<Enum>(Enum { {}, false, }));
enums.emplace("DEFAULTMODEL", std::make_shared<Enum>(Enum { {}, false, }));
enums.emplace("SCRIPTSTREAM", std::make_shared<Enum>(Enum { {}, false, }));
auto xml_parse = [](const fs::path& path) -> XmlData
{
fs::path full_xml_path(path);
try
{
auto opt_buffer = read_file_utf8(full_xml_path);
if(opt_buffer == nullopt)
throw ConfigError("failed to read xml {}: {}", full_xml_path.generic_u8string(), "could not open file for reading");
auto doc = std::make_unique<xml_document<>>(); // buffer should be alive as long as doc
doc->parse<0>(&(*opt_buffer)[0]); // buffer will get modified here
return XmlData{ std::move(*opt_buffer), std::move(doc) };
}
catch(const rapidxml::parse_error& e)
{
throw ConfigError("failed to parse xml {}: {}", full_xml_path.generic_u8string(), e.what());
}
catch(const bad_optional_access& e)
{
throw ConfigError("failed to read xml {}: {}", full_xml_path.generic_u8string(), e.what());
}
};
for(auto& xml_path : xml_list)
{
fs::path path;
{
if(!xml_path.is_absolute())
{
auto begin = xml_path.begin();
if(begin != xml_path.end() && (*begin == "." || *begin == ".."))
path = xml_path;
else
path = config_path() / config_name / xml_path;
}
else
{
path = xml_path;
}
}
xml_vector.emplace_back(xml_parse(path));
if(xml_node<>* root_node = xml_vector.back().doc->first_node("GTA3Script"))
{
for(auto node = root_node->first_node(); node; node = node->next_sibling())
{
if(!strcmp(node->name(), "Commands"))
{
xml_sections.emplace_back(XML_SECTION_COMMANDS, node);
}
else if(!strcmp(node->name(), "Constants"))
{
xml_sections.emplace_back(XML_SECTION_CONSTANTS, node);
}
else if(!strcmp(node->name(), "Alternators"))
{
xml_sections.emplace_back(XML_SECTION_ALTERNATORS, node);
}
}
}
}
std::stable_sort(xml_sections.begin(), xml_sections.end(), [](const auto& a, const auto& b) {
return a.first < b.first;
});
for(auto& section_pair : xml_sections)
{
xml_node<>* node = section_pair.second;
if(section_pair.first == XML_SECTION_COMMANDS)
{
for(auto cmd_node = node->first_node(); cmd_node; cmd_node = cmd_node->next_sibling())
{
if(!strcmp(cmd_node->name(), "Command"))
{
auto command = parse_command_node(cmd_node, entities, enums);
auto insert_pair = commands.insert(std::move(command));
if(!insert_pair.second)
{
commands.erase(insert_pair.first);
insert_pair = commands.insert(std::move(command));
}
}
}
}
else if(section_pair.first == XML_SECTION_CONSTANTS)
{
for(auto const_node = node->first_node(); const_node; const_node = const_node->next_sibling())
{
if(!strcmp(const_node->name(), "Enum"))
{
parse_enum_node(enums, const_node);
}
}
}
else if(section_pair.first == XML_SECTION_ALTERNATORS)
{
for(auto alt_node = node->first_node(); alt_node; alt_node = alt_node->next_sibling())
{
if(!strcmp(alt_node->name(), "Alternator"))
{
parse_alternator_node(alternators, alt_node, commands);
}
}
}
}
return Commands { std::move(commands), std::move(alternators), std::move(entities), std::move(enums) };
}