Skip to content

Commit

Permalink
Creating Simpler Nested Proto Parse Templates
Browse files Browse the repository at this point in the history
The previous logic had duplicated code chunks. The new logic is much more flexible and unifies the template branches to remove duplicate chunks.
  • Loading branch information
AsherGlick committed Sep 30, 2023
1 parent bbd1c60 commit 031d6a4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 90 deletions.
29 changes: 18 additions & 11 deletions xml_converter/generators/code_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,10 @@ class AttributeVariable:
xml_bundled_components: List[str] = field(default_factory=list)
attribute_flag_name: Optional[str] = ""
write_to_xml: bool = True
is_trigger: bool = False

# The CPP code to inject into the variable getter to drill down to the
# variable we are looking for. eg ".trigger()" or ".one().two()"
proto_drilldown_calls: str = ""
uses_file_path: bool = False
is_component: bool = False

Expand Down Expand Up @@ -331,7 +334,7 @@ def generate_cpp_variable_data(
side_effects: List[str] = []
write_to_xml: bool = True
protobuf_field: str = ""
is_trigger: bool = False
proto_drilldown_calls: str = ""
default_xml_field: str = ""

args: List[str] = XML_ATTRIBUTE_PARSER_DEFAULT_ARGUMENTS.copy()
Expand Down Expand Up @@ -362,11 +365,13 @@ def generate_cpp_variable_data(
xml_fields.append(lowercase(x, delimiter=""))
default_xml_field = fieldval['xml_fields'][0]

# TODO: this should handle more then just `trigger.` variants
if fieldval["protobuf_field"].startswith("trigger"):
is_trigger = True
proto_drilldown_calls = ".trigger()"
# TODO this would not properly catch trigger.something.ourvariable
protobuf_field = fieldval["protobuf_field"].split('.')[1]
else:
is_trigger = False
proto_drilldown_calls = ""
protobuf_field = fieldval["protobuf_field"]

if fieldval.get("uses_file_path", False):
Expand Down Expand Up @@ -417,7 +422,7 @@ def generate_cpp_variable_data(
xml_fields=xml_fields,
default_xml_field=default_xml_field,
protobuf_field=protobuf_field,
is_trigger=is_trigger,
proto_drilldown_calls=proto_drilldown_calls,
args=args,
write_to_xml=write_to_xml,
attribute_flag_name=attribute_name + "_is_set",
Expand Down Expand Up @@ -449,7 +454,7 @@ def write_attribute(self, output_directory: str) -> None:
attribute_variable: AttributeVariable
metadata: Dict[str, SchemaType] = {}
xml_fields: List[str] = []
is_trigger: bool = False
proto_drilldown_calls: str = ""
template: Dict[str, Template] = {
"MultiflagValue": env.get_template("multiflagvalue.cpp"),
"CompoundValue": env.get_template("compoundvalue.cpp"),
Expand All @@ -466,11 +471,13 @@ def write_attribute(self, output_directory: str) -> None:
metadata[filepath] = self.data[filepath].metadata
attribute_name = attribute_name_from_markdown_data(metadata[filepath]['name'])

# TODO this should handle more then just `trigger.` variants
if metadata[filepath]["protobuf_field"].startswith("trigger"):
is_trigger = True
proto_drilldown_calls = ".trigger()"
# TODO this would not properly catch trigger.something.ourvariable
protobuf_field = metadata[filepath]["protobuf_field"].split('.')[1]
else:
is_trigger = False
proto_drilldown_calls = ""
protobuf_field = metadata[filepath]["protobuf_field"]

if metadata[filepath]['type'] == "MultiflagValue":
Expand All @@ -486,7 +493,7 @@ def write_attribute(self, output_directory: str) -> None:
class_name=attribute_name,
xml_fields=xml_fields,
protobuf_field=protobuf_field,
is_trigger=is_trigger,
proto_drilldown_calls=proto_drilldown_calls,
)
attribute_variables.append(attribute_variable)

Expand All @@ -509,7 +516,7 @@ def write_attribute(self, output_directory: str) -> None:
class_name=attribute_name,
xml_fields=xml_fields,
protobuf_field=component["protobuf_field"],
is_trigger=is_trigger,
proto_drilldown_calls=proto_drilldown_calls,
)
attribute_variables.append(attribute_variable)

Expand All @@ -525,7 +532,7 @@ def write_attribute(self, output_directory: str) -> None:
class_name=attribute_name,
xml_fields=xml_fields,
protobuf_field=protobuf_field,
is_trigger=is_trigger,
proto_drilldown_calls=proto_drilldown_calls,
)
attribute_variables.append(attribute_variable)

Expand Down
69 changes: 21 additions & 48 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
{% endif %}
{% for attribute_variable in attribute_variables %}
{% if attribute_variable.is_component == false %}
{% if (attribute_variable.is_trigger == true)%}
{% if (attribute_variable.proto_drilldown_calls != "")%}{# TODO: This is a hack to preserve functionality when removing is_trigger #}
{% if (attribute_variable.attribute_type == "Custom")%}
if (this->{{attribute_variable.attribute_flag_name}}) {
if (trigger == nullptr) {
Expand Down Expand Up @@ -149,55 +149,28 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const {
}

void {{cpp_class}}::parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_header}}) {
{% if cpp_class == "Icon": %}
waypoint::Trigger trigger = proto_{{cpp_class_header}}.trigger();
{% endif %}
{% for attribute_variable in attribute_variables %}
{% if attribute_variable.is_component == false %}
{% if (attribute_variable.is_trigger == true) %}
{% if (attribute_variable.attribute_type == "Custom") %}
if (trigger.has_{{attribute_variable.protobuf_field}}()) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(trigger.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif attribute_variable.class_name == "string" %}
if (trigger.{{attribute_variable.protobuf_field}}() != "") {
this->{{attribute_variable.attribute_name}} = trigger.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif (attribute_variable.attribute_type == "Enum") %}
if (trigger.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(trigger.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% else: %}
if (trigger.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = trigger.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% endif %}
{% else: %}
{% if (attribute_variable.attribute_type == "Enum") %}
if (proto_{{cpp_class_header}}.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(proto_{{cpp_class_header}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif (attribute_variable.attribute_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"])%}
if (proto_{{cpp_class_header}}.has_{{attribute_variable.protobuf_field}}()) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(proto_{{cpp_class_header}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif (attribute_variable.class_name == "string") %}
if (proto_{{cpp_class_header}}.{{attribute_variable.protobuf_field}}() != "") {
this->{{attribute_variable.attribute_name}} = proto_{{cpp_class_header}}.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% else: %}
if (proto_{{cpp_class_header}}.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = proto_{{cpp_class_header}}.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% endif %}
{% if (attribute_variable.attribute_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"]) %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.has_{{attribute_variable.protobuf_field}}()) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif (attribute_variable.class_name == "string") %}{# TODO: why is this .class_name when the others are .attribute_name #}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != "") {
this->{{attribute_variable.attribute_name}} = proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% elif (attribute_variable.attribute_type == "Enum") %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = from_proto_{{attribute_variable.class_name}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% else %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
this->{{attribute_variable.attribute_name}} = proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}();
this->{{attribute_variable.attribute_flag_name}} = true;
}
{% endif %}
{% endif %}
{% endfor %}
Expand Down
61 changes: 30 additions & 31 deletions xml_converter/src/icon_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,6 @@ waypoint::Icon Icon::as_protobuf() const {
}

void Icon::parse_protobuf(waypoint::Icon proto_icon) {
waypoint::Trigger trigger = proto_icon.trigger();
if (proto_icon.achievement_bit() != 0) {
this->achievement_bitmask = proto_icon.achievement_bit();
this->achievement_bitmask_is_set = true;
Expand All @@ -683,20 +682,20 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->achievement_id = proto_icon.achievement_id();
this->achievement_id_is_set = true;
}
if (trigger.auto_trigger() != 0) {
this->auto_trigger = trigger.auto_trigger();
if (proto_icon.trigger().auto_trigger() != 0) {
this->auto_trigger = proto_icon.trigger().auto_trigger();
this->auto_trigger_is_set = true;
}
if (trigger.bounce_delay() != 0) {
this->bounce_delay = trigger.bounce_delay();
if (proto_icon.trigger().bounce_delay() != 0) {
this->bounce_delay = proto_icon.trigger().bounce_delay();
this->bounce_delay_is_set = true;
}
if (trigger.bounce_duration() != 0) {
this->bounce_duration = trigger.bounce_duration();
if (proto_icon.trigger().bounce_duration() != 0) {
this->bounce_duration = proto_icon.trigger().bounce_duration();
this->bounce_duration_is_set = true;
}
if (trigger.bounce_height() != 0) {
this->bounce_height = trigger.bounce_height();
if (proto_icon.trigger().bounce_height() != 0) {
this->bounce_height = proto_icon.trigger().bounce_height();
this->bounce_height_is_set = true;
}
if (proto_icon.can_fade() != 0) {
Expand All @@ -711,12 +710,12 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->color = from_proto_color(proto_icon.rgba_color());
this->color_is_set = true;
}
if (trigger.action_copy_clipboard() != "") {
this->copy_clipboard = trigger.action_copy_clipboard();
if (proto_icon.trigger().action_copy_clipboard() != "") {
this->copy_clipboard = proto_icon.trigger().action_copy_clipboard();
this->copy_clipboard_is_set = true;
}
if (trigger.action_copy_message() != "") {
this->copy_message = trigger.action_copy_message();
if (proto_icon.trigger().action_copy_message() != "") {
this->copy_message = proto_icon.trigger().action_copy_message();
this->copy_message_is_set = true;
}
if (proto_icon.cull_chirality() != 0) {
Expand All @@ -743,16 +742,16 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->guid = from_proto_unique_id(proto_icon.guid());
this->guid_is_set = true;
}
if (trigger.has_countdown() != 0) {
this->has_countdown = trigger.has_countdown();
if (proto_icon.trigger().has_countdown() != 0) {
this->has_countdown = proto_icon.trigger().has_countdown();
this->has_countdown_is_set = true;
}
if (proto_icon.height_offset() != 0) {
this->height_offset = proto_icon.height_offset();
this->height_offset_is_set = true;
}
if (trigger.has_action_hide_category()) {
this->hide_category = from_proto_marker_category(trigger.action_hide_category());
if (proto_icon.trigger().has_action_hide_category()) {
this->hide_category = from_proto_marker_category(proto_icon.trigger().action_hide_category());
this->hide_category_is_set = true;
}
if (proto_icon.has_texture_path()) {
Expand All @@ -763,12 +762,12 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->icon_size = proto_icon.tentative__scale();
this->icon_size_is_set = true;
}
if (trigger.action_info_message() != "") {
this->info_message = trigger.action_info_message();
if (proto_icon.trigger().action_info_message() != "") {
this->info_message = proto_icon.trigger().action_info_message();
this->info_message_is_set = true;
}
if (trigger.invert_display() != 0) {
this->invert_visibility = trigger.invert_display();
if (proto_icon.trigger().invert_display() != 0) {
this->invert_visibility = proto_icon.trigger().invert_display();
this->invert_visibility_is_set = true;
}
if (proto_icon.map_display_size() != 0) {
Expand Down Expand Up @@ -815,12 +814,12 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->render_on_minimap = proto_icon.tentative__render_on_minimap();
this->render_on_minimap_is_set = true;
}
if (trigger.reset_behavior() != 0) {
this->reset_behavior = from_proto_reset_behavior(trigger.reset_behavior());
if (proto_icon.trigger().reset_behavior() != 0) {
this->reset_behavior = from_proto_reset_behavior(proto_icon.trigger().reset_behavior());
this->reset_behavior_is_set = true;
}
if (trigger.reset_length() != 0) {
this->reset_length = trigger.reset_length();
if (proto_icon.trigger().reset_length() != 0) {
this->reset_length = proto_icon.trigger().reset_length();
this->reset_length_is_set = true;
}
if (proto_icon.scale_on_map_with_zoom() != 0) {
Expand All @@ -835,8 +834,8 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->schedule_duration = proto_icon.bhdraft__schedule_duration();
this->schedule_duration_is_set = true;
}
if (trigger.has_action_show_category()) {
this->show_category = from_proto_marker_category(trigger.action_show_category());
if (proto_icon.trigger().has_action_show_category()) {
this->show_category = from_proto_marker_category(proto_icon.trigger().action_show_category());
this->show_category_is_set = true;
}
if (proto_icon.has_specialization_filter()) {
Expand All @@ -847,8 +846,8 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->species_filter = from_proto_species_filter(proto_icon.species_filter());
this->species_filter_is_set = true;
}
if (trigger.has_action_toggle_category()) {
this->toggle_category = from_proto_marker_category(trigger.action_toggle_category());
if (proto_icon.trigger().has_action_toggle_category()) {
this->toggle_category = from_proto_marker_category(proto_icon.trigger().action_toggle_category());
this->toggle_category_is_set = true;
}
if (proto_icon.tip_description() != "") {
Expand All @@ -859,8 +858,8 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon) {
this->tooltip_name = proto_icon.tip_name();
this->tooltip_name_is_set = true;
}
if (trigger.range() != 0) {
this->trigger_range = trigger.range();
if (proto_icon.trigger().range() != 0) {
this->trigger_range = proto_icon.trigger().range();
this->trigger_range_is_set = true;
}
}

0 comments on commit 031d6a4

Please sign in to comment.