Skip to content

Commit

Permalink
Merge pull request #152 from klingbolt/poi_optimized
Browse files Browse the repository at this point in the history
[Proto Optimization] Made Icon and Trail elements of Category
  • Loading branch information
AsherGlick authored Aug 7, 2023
2 parents 53f8d5d + 00f2f28 commit 47bd8b8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 60 deletions.
1 change: 1 addition & 0 deletions xml_converter/generators/code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ def generate_cpp_variable_data(
cpp_includes.hpp_relative_includes.add("icon_gen.hpp")
cpp_includes.hpp_relative_includes.add("trail_gen.hpp")

cpp_includes.cpp_absolute_includes.add("utility")
cpp_includes.cpp_absolute_includes.add("type_traits")

for filepath, document in sorted(self.data.items()):
Expand Down
25 changes: 23 additions & 2 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ vector<string> {{cpp_class}}::as_xml() const {
return xml_node_contents;
}

waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
{% if cpp_class == "Category": %}
waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf(string full_category_name, map<string, vector<Parseable*>>* parsed_pois) const {
full_category_name += this->name;
{% else %}
waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
{% endif %}
waypoint::{{cpp_class}} proto_{{cpp_class_header}};
{% if cpp_class == "Icon": %}
waypoint::Trigger* trigger = nullptr;
Expand Down Expand Up @@ -146,8 +151,24 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
}
{% endif %}
{% if cpp_class == "Category": %}

auto pois = parsed_pois->find(full_category_name);

if (pois != parsed_pois->end()) {
for (unsigned int i = 0; i < pois->second.size(); i++) {
if (pois->second[i]->classname() == "POI") {
Icon* icon = dynamic_cast<Icon*>(pois->second[i]);
proto_{{cpp_class_header}}.add_icon()->MergeFrom(icon->as_protobuf());
}
else if (pois->second[i]->classname() == "Trail") {
Trail* trail = dynamic_cast<Trail*>(pois->second[i]);
proto_{{cpp_class_header}}.add_trail()->MergeFrom(trail->as_protobuf());
}
}
}

for (const auto& [key, val] : this->children) {
waypoint::{{cpp_class}} proto_{{cpp_class_header}}_child = val.as_protobuf();
waypoint::{{cpp_class}} proto_{{cpp_class_header}}_child = val.as_protobuf(full_category_name + ".", parsed_pois);
proto_{{cpp_class_header}}.add_children()->CopyFrom(proto_{{cpp_class_header}}_child);
}
{% endif %}
Expand Down
6 changes: 5 additions & 1 deletion xml_converter/generators/cpp_templates/class_template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ class {{cpp_class}} : public Parseable {
virtual std::vector<std::string> as_xml() const;
virtual std::string classname();
bool init_xml_attribute(rapidxml::xml_attribute<>* attribute, std::vector<XMLError*>* errors, std::string base_dir = "");
waypoint::{{cpp_class}} as_protobuf() const;
{% if cpp_class == "Category": %}
waypoint::{{cpp_class}} as_protobuf(std::string full_category_name, std::map<std::string, std::vector<Parseable*>>* parsed_pois) const;
{% else: %}
waypoint::{{cpp_class}} as_protobuf() const;
{% endif %}
void parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_header}});
{% if attributes_of_type_marker_category %}
bool validate_attributes_of_type_marker_category();
Expand Down
8 changes: 4 additions & 4 deletions xml_converter/proto/waypoint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package waypoint;

message Waypoint {
repeated Category category = 1;
repeated Icon icon = 2;
repeated Trail trail = 3;
}

message Category {
Expand All @@ -15,10 +13,11 @@ message Category {
string name = 4;
string tip_description = 5;
repeated Category children = 6;
repeated Icon icon = 7;
repeated Trail trail = 8;
}

message Icon {
Category category = 1;
TexturePath texture_path = 2;
GUID guid = 3;
int32 map_id = 4;
Expand Down Expand Up @@ -53,10 +52,10 @@ message Icon {
bool tentative__render_on_minimap = 2051;
string bhdraft__schedule = 2052;
float bhdraft__schedule_duration = 2053;
Category category = 2054;
}

message Trail {
Category category = 1;
TexturePath texture_path = 2;
GUID guid = 3;
int32 map_id = 4;
Expand Down Expand Up @@ -85,6 +84,7 @@ message Trail {
bool tentative__render_on_minimap = 2051;
string bhdraft__schedule = 2052;
float bhdraft__schedule_duration = 2053;
Category category = 2054;
}

message TexturePath {
Expand Down
22 changes: 20 additions & 2 deletions xml_converter/src/category_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iosfwd>
#include <string>
#include <type_traits>
#include <utility>

#include "attribute/bool.hpp"
#include "attribute/string.hpp"
Expand Down Expand Up @@ -94,7 +95,8 @@ vector<string> Category::as_xml() const {
return xml_node_contents;
}

waypoint::Category Category::as_protobuf() const {
waypoint::Category Category::as_protobuf(string full_category_name, map<string, vector<Parseable*>>* parsed_pois) const {
full_category_name += this->name;
waypoint::Category proto_category;
if (this->default_visibility_is_set) {
proto_category.set_default_visibility(this->default_visibility);
Expand All @@ -111,8 +113,24 @@ waypoint::Category Category::as_protobuf() const {
if (this->tooltip_description_is_set) {
proto_category.set_tip_description(this->tooltip_description);
}

auto pois = parsed_pois->find(full_category_name);

if (pois != parsed_pois->end()) {
for (unsigned int i = 0; i < pois->second.size(); i++) {
if (pois->second[i]->classname() == "POI") {
Icon* icon = dynamic_cast<Icon*>(pois->second[i]);
proto_category.add_icon()->MergeFrom(icon->as_protobuf());
}
else if (pois->second[i]->classname() == "Trail") {
Trail* trail = dynamic_cast<Trail*>(pois->second[i]);
proto_category.add_trail()->MergeFrom(trail->as_protobuf());
}
}
}

for (const auto& [key, val] : this->children) {
waypoint::Category proto_category_child = val.as_protobuf();
waypoint::Category proto_category_child = val.as_protobuf(full_category_name + ".", parsed_pois);
proto_category.add_children()->CopyFrom(proto_category_child);
}
return proto_category;
Expand Down
2 changes: 1 addition & 1 deletion xml_converter/src/category_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class Category : public Parseable {
virtual std::vector<std::string> as_xml() const;
virtual std::string classname();
bool init_xml_attribute(rapidxml::xml_attribute<>* attribute, std::vector<XMLError*>* errors, std::string base_dir = "");
waypoint::Category as_protobuf() const;
waypoint::Category as_protobuf(std::string full_category_name, std::map<std::string, std::vector<Parseable*>>* parsed_pois) const;
void parse_protobuf(waypoint::Category proto_category);
};
101 changes: 51 additions & 50 deletions xml_converter/src/xml_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,27 +104,31 @@ void remove_proto_child(waypoint::Category* proto_category, set<string> categori
}

void write_protobuf_file(string proto_directory, map<string, Category>* marker_categories, vector<Parseable*>* parsed_pois) {
// Creates a Waypoint message that contains all categories
waypoint::Waypoint all_categories;
for (const auto& category : *marker_categories) {
waypoint::Category proto_category = category.second.as_protobuf();
all_categories.add_category()->CopyFrom(proto_category);
}

waypoint::Waypoint proto_pois;
// Collects a set of map ids from Icon and Trail data
std::set<int> map_ids;
ofstream trail_data_file;
map<string, vector<Parseable*>> map_of_pois;
for (const auto& parsed_poi : *parsed_pois) {
if (parsed_poi->classname() == "POI") {
Icon* icon = dynamic_cast<Icon*>(parsed_poi);
map_ids.insert(icon->map_id);
map_of_pois[icon->category.category].push_back(icon);
}
else if (parsed_poi->classname() == "Trail") {
Trail* trail = dynamic_cast<Trail*>(parsed_poi);
map_ids.insert(trail->map_id);
map_of_pois[trail->category.category].push_back(trail);
}
}

// Creates a Waypoint message that contains all categories
waypoint::Waypoint all_categories;
for (const auto& category : *marker_categories) {
waypoint::Category proto_category = category.second.as_protobuf("", &map_of_pois);
all_categories.add_category()->CopyFrom(proto_category);
}

waypoint::Waypoint output_message;
std::set<string> categories_to_retain;
for (int map_id : map_ids) {
Expand All @@ -138,15 +142,12 @@ void write_protobuf_file(string proto_directory, map<string, Category>* marker_c
Icon* icon = dynamic_cast<Icon*>(parsed_poi);
if (icon->map_id == map_id) {
populate_categories_to_retain(icon->category.category, &categories_to_retain);
output_message.add_icon()->MergeFrom(icon->as_protobuf());
}
}
else if (parsed_poi->classname() == "Trail") {
Trail* trail = dynamic_cast<Trail*>(parsed_poi);
if (trail->map_id == map_id) {
populate_categories_to_retain(trail->category.category, &categories_to_retain);
waypoint::Trail proto_trail = trail->as_protobuf();
output_message.add_trail()->MergeFrom(proto_trail);
}
}
}
Expand Down Expand Up @@ -248,22 +249,6 @@ vector<Parseable*> parse_pois(rapidxml::xml_node<>* root_node, map<string, Categ
return markers;
}

vector<Parseable*> parse_proto_pois(waypoint::Waypoint proto_message) {
vector<Parseable*> markers;

for (int i = 0; i < proto_message.icon_size(); i++) {
Icon* icon = new Icon();
icon->parse_protobuf(proto_message.icon(i));
markers.push_back(icon);
}
for (int i = 0; i < proto_message.trail_size(); i++) {
Trail* trail = new Trail();
trail->parse_protobuf(proto_message.trail(i));
markers.push_back(trail);
}
return markers;
}

void parse_marker_categories(rapidxml::xml_node<>* node, map<string, Category>* marker_categories, vector<XMLError*>* errors, int depth = 0) {
if (get_node_name(node) == "MarkerCategory") {
string name = lowercase(find_attribute_value(node, "name"));
Expand All @@ -278,12 +263,31 @@ void parse_marker_categories(rapidxml::xml_node<>* node, map<string, Category>*
errors->push_back(new XMLNodeNameError("Unknown MarkerCategory Tag", node));
}
}
void parse_proto_marker_categories(::waypoint::Category proto_category, map<string, Category>* marker_categories) {
string name = proto_category.name();
Category* this_category = &(*marker_categories)[name];

void parse_waypoint_categories(string full_category_name, ::waypoint::Category proto_category, map<string, Category>* marker_categories, vector<Parseable*>* parsed_pois) {
full_category_name += proto_category.name();
Category* this_category = &(*marker_categories)[full_category_name];
this_category->parse_protobuf(proto_category);

for (int i = 0; i < proto_category.icon_size(); i++) {
Icon* icon = new Icon();
icon->parse_protobuf(proto_category.icon(i));
// TODO: The field category in Icon is being deprciated
// This overwrites any icon.category with its position in the heirarchy
icon->category.category = full_category_name;
parsed_pois->push_back(icon);
}
for (int i = 0; i < proto_category.trail_size(); i++) {
Trail* trail = new Trail();
trail->parse_protobuf(proto_category.trail(i));
// TODO: The field category in Trail is being deprciated
// This overwrites any trail.category with its position in the heirarchy
trail->category.category = full_category_name;
parsed_pois->push_back(trail);
}

for (int i = 0; i < proto_category.children_size(); i++) {
parse_proto_marker_categories(proto_category.children(i), &(this_category->children));
parse_waypoint_categories(full_category_name + ".", proto_category.children(i), &(this_category->children), parsed_pois);
}
}

Expand Down Expand Up @@ -335,10 +339,8 @@ void read_protobuf_file(string proto_filepath, map<string, Category>* marker_cat
infile.open(proto_filepath, ios::in | ios::binary);
proto_message.ParseFromIstream(&infile);
for (int i = 0; i < proto_message.category_size(); i++) {
parse_proto_marker_categories(proto_message.category(i), marker_categories);
parse_waypoint_categories("", proto_message.category(i), marker_categories, parsed_pois);
}
vector<Parseable*> temp_vector = parse_proto_pois(proto_message);
move(temp_vector.begin(), temp_vector.end(), back_inserter(*parsed_pois));
}

bool filename_comp(string a, string b) {
Expand Down Expand Up @@ -506,24 +508,23 @@ int main() {
cout << "The protobuf write function took " << ms << " milliseconds to run" << endl;

////////////////////////////////////////////////////////////////////////////
// This section tests that the protobuf file can be parsed back to xml
// This section can test that a protobuf file can be parsed back to xml
////////////////////////////////////////////////////////////////////////////
parsed_pois.clear();
marker_categories.clear();

begin = chrono::high_resolution_clock::now();
read_protobuf_file("./protobins/50.data", &marker_categories, &parsed_pois);
end = chrono::high_resolution_clock::now();
dur = end - begin;
ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
cout << "The protobuf read function took " << ms << " milliseconds to run" << endl;

begin = chrono::high_resolution_clock::now();
write_xml_file("./protobins/50.xml", &marker_categories, &parsed_pois);
end = chrono::high_resolution_clock::now();
dur = end - begin;
ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
cout << "The xml write function took " << ms << " milliseconds to run" << endl;
// parsed_pois.clear();
// marker_categories.clear();
// begin = chrono::high_resolution_clock::now();
// read_protobuf_file("./protobins/1.data", &marker_categories, &parsed_pois);
// end = chrono::high_resolution_clock::now();
// dur = end - begin;
// ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
// cout << "The protobuf read function took " << ms << " milliseconds to run" << endl;

// begin = chrono::high_resolution_clock::now();
// write_xml_file("./protobins/1.xml", &marker_categories, &parsed_pois);
// end = chrono::high_resolution_clock::now();
// dur = end - begin;
// ms = std::chrono::duration_cast<std::chrono::milliseconds>(dur).count();
// cout << "The xml write function took " << ms << " milliseconds to run" << endl;

return 0;
}

0 comments on commit 47bd8b8

Please sign in to comment.