-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgui_addon_registry_parser.cpp
More file actions
287 lines (243 loc) · 9.54 KB
/
gui_addon_registry_parser.cpp
File metadata and controls
287 lines (243 loc) · 9.54 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
#include "lxgui/gui_addon_registry.hpp"
#include "lxgui/gui_event_emitter.hpp"
#include "lxgui/gui_frame.hpp"
#include "lxgui/gui_layout_node.hpp"
#include "lxgui/gui_out.hpp"
#include "lxgui/gui_parser_common.hpp"
#include "lxgui/gui_root.hpp"
#include "lxgui/gui_virtual_root.hpp"
#include "lxgui/utils_file_system.hpp"
#include "lxgui/utils_string.hpp"
#include <lxgui/extern_sol2_state.hpp>
#if defined(LXGUI_ENABLE_XML_PARSER)
# include <lxgui/extern_pugixml.hpp>
#endif
#if defined(LXGUI_ENABLE_YAML_PARSER)
# include <lxgui/extern_ryml.hpp>
#endif
#include <fstream>
namespace lxgui::gui {
class file_line_mappings {
public:
explicit file_line_mappings(const std::string& file_name) : file_name_(file_name) {
std::ifstream stream(file_name);
if (!stream.is_open())
return;
std::string line;
std::size_t prev_pos = 0u;
while (std::getline(stream, line)) {
file_content_ += '\n' + line;
line_offsets_.push_back(prev_pos);
prev_pos += line.size() + 1u;
}
line_offsets_.push_back(prev_pos);
is_open_ = true;
}
bool is_open() const {
return is_open_;
}
const std::string& get_content() const {
return file_content_;
}
std::pair<std::size_t, std::size_t> get_line_info(std::size_t offset) const {
auto iter = std::lower_bound(line_offsets_.begin(), line_offsets_.end(), offset);
if (iter == line_offsets_.end())
return std::make_pair(0, 0);
std::size_t line_nbr = iter - line_offsets_.begin();
std::size_t char_offset = offset - *iter + 1u;
return std::make_pair(line_nbr, char_offset);
}
std::string get_location(std::size_t offset) const {
auto location = get_line_info(offset);
if (location.first == 0)
return file_name_ + ":?";
else
return file_name_ + ":" + utils::to_string(location.first);
}
private:
bool is_open_ = false;
std::string file_name_;
std::string file_content_;
std::vector<std::size_t> line_offsets_;
};
std::string normalize_node_name(const std::string& name, bool capital_first) {
std::string normalized;
bool next_capitalize = capital_first;
for (auto c : name) {
if (next_capitalize)
c = std::toupper(c);
next_capitalize = c == '_';
if (next_capitalize)
continue;
normalized.push_back(c);
}
return normalized;
}
#if defined(LXGUI_ENABLE_XML_PARSER)
void set_node(const file_line_mappings& file, layout_node& node, const pugi::xml_node& xml_node) {
auto location = file.get_location(xml_node.offset_debug());
node.set_location(location);
node.set_value_location(location);
node.set_name(normalize_node_name(xml_node.name(), true));
for (const auto& attr : xml_node.attributes()) {
std::string name = normalize_node_name(attr.name(), false);
if (const auto* node_attr = node.try_get_attribute(name)) {
gui::out << gui::warning << location << ": attribute '" << name
<< "' duplicated; only first value will be used." << std::endl;
node_attr->mark_as_not_accessed();
continue;
}
auto& attrib = node.add_attribute();
attrib.set_location(location);
attrib.set_value_location(location);
attrib.set_name(std::move(name));
attrib.set_value(attr.value());
}
std::string value;
for (const auto& elem_node : xml_node.children()) {
if (elem_node.type() == pugi::node_pcdata || elem_node.type() == pugi::node_cdata) {
value += elem_node.value();
} else {
auto& child = node.add_child();
set_node(file, child, elem_node);
}
}
node.set_value(value);
}
#endif
#if defined(LXGUI_ENABLE_YAML_PARSER)
std::string to_string(const c4::csubstr& c_string) {
return std::string(c_string.data(), c_string.size());
}
void set_node(
const file_line_mappings& file,
const ryml::Tree& tree,
layout_node& node,
const ryml::ConstNodeRef& yaml_node) {
std::string location;
if (yaml_node.has_key())
location = file.get_location(yaml_node.key().data() - tree.arena().data());
else if (yaml_node.has_val())
location = file.get_location(yaml_node.val().data() - tree.arena().data());
node.set_location(location);
node.set_value_location(location);
if (yaml_node.has_key())
node.set_name(normalize_node_name(to_string(yaml_node.key()), true));
for (auto elem_node : yaml_node.children()) {
switch (elem_node.type()) {
case ryml::KEYVAL: {
std::string name = normalize_node_name(to_string(elem_node.key()), false);
std::string attr_location =
file.get_location(elem_node.key().data() - tree.arena().data());
if (const auto* node_attr = node.try_get_attribute(name)) {
gui::out << gui::warning << attr_location << ": attribute '" << name
<< "' duplicated; only first value will be used." << std::endl;
gui::out << gui::warning << std::string(attr_location.size(), ' ')
<< " first occurence at: '" << std::endl;
gui::out << gui::warning << std::string(attr_location.size(), ' ') << " "
<< node_attr->get_location() << std::endl;
node_attr->mark_as_not_accessed();
continue;
}
auto& attrib = node.add_attribute();
attrib.set_location(std::move(attr_location));
attrib.set_value_location(
file.get_location(elem_node.val().data() - tree.arena().data()));
attrib.set_name(std::move(name));
attrib.set_value(to_string(elem_node.val()));
break;
}
case ryml::KEYMAP: [[fallthrough]];
case ryml::MAP: [[fallthrough]];
case ryml::KEYSEQ: {
auto& child = node.add_child();
set_node(file, tree, child, elem_node);
break;
}
default: {
gui::out << gui::warning << location << ": unsupported YAML node type: '"
<< elem_node.type_str() << "'." << std::endl;
break;
}
}
}
}
#endif
void addon_registry::parse_layout_file_(const std::string& file_name, const addon& add_on) {
file_line_mappings file(file_name);
if (!file.is_open()) {
gui::out << gui::error << file_name << ": could not open file for parsing." << std::endl;
return;
}
layout_node root;
bool parsed = false;
const std::string extension = utils::get_file_extension(file_name);
#if defined(LXGUI_ENABLE_XML_PARSER)
if (extension == ".xml") {
const unsigned int options = pugi::parse_ws_pcdata_single;
pugi::xml_document doc;
pugi::xml_parse_result result =
doc.load_buffer(file.get_content().c_str(), file.get_content().size(), options);
if (!result) {
gui::out << gui::error << file.get_location(result.offset) << ": "
<< result.description() << std::endl;
return;
}
set_node(file, root, doc.first_child());
parsed = true;
}
#endif
#if defined(LXGUI_ENABLE_YAML_PARSER)
if (extension == ".yml" || extension == ".yaml") {
ryml::Tree tree;
ryml::parse_in_arena(ryml::to_csubstr(file.get_content()), &tree);
set_node(file, tree, root, tree.rootref().first_child());
parsed = true;
}
#endif
if (!parsed) {
gui::out << gui::error << file_name
<< ": no parser registered for extension '" + extension + "'." << std::endl;
return;
}
for (const auto& node : root.get_children()) {
if (node.get_name() == "Script") {
std::string script_file =
add_on.directory + "/" + node.get_attribute_value<std::string>("file");
auto result = lua_.do_file(script_file);
if (!result.valid()) {
std::string err = result.get<sol::error>().what();
gui::out << gui::error << err << std::endl;
event_emitter_.fire_event("LUA_ERROR", {err});
}
} else if (node.get_name() == "Include") {
parse_layout_file_(
add_on.directory + "/" + node.get_attribute_value<std::string>("file"), add_on);
} else {
try {
auto attr = frame_core_attributes{parse_core_attributes(
root_.get_registry(), virtual_root_.get_registry(), node, nullptr)};
utils::observer_ptr<frame> obj;
auto parent = attr.parent; // copy here to prevent use-after-move
if (parent) {
obj = parent->create_child(std::move(attr));
} else {
if (attr.is_virtual) {
obj = virtual_root_.create_root_frame(std::move(attr));
} else {
obj = root_.create_root_frame(std::move(attr));
}
}
if (!obj)
continue;
obj->set_addon(&add_on);
obj->parse_layout(node);
obj->notify_loaded();
} catch (const utils::exception& e) {
gui::out << gui::error << e.get_description() << std::endl;
}
}
}
warn_for_not_accessed_node(root);
}
} // namespace lxgui::gui