From eb091af73a600cfbd963276f94bed43a1c1d6ebf Mon Sep 17 00:00:00 2001 From: Asher Glick Date: Sat, 30 Sep 2023 14:44:29 -0500 Subject: [PATCH] Making the ability to set values in nested protos cleaner The previous way handled allocation itself, however the protobuf library seems to be able to handle that themselves without issue. This allows for a simple single line set instead of an additional "if the intermediate layer does not exist then create it" block in every nested variable block. --- xml_converter/generators/code_generator.py | 31 +++++--- .../cpp_templates/class_template.cpp | 55 +++---------- xml_converter/src/icon_gen.cpp | 79 ++++--------------- 3 files changed, 48 insertions(+), 117 deletions(-) diff --git a/xml_converter/generators/code_generator.py b/xml_converter/generators/code_generator.py index 87a69075..0c814949 100644 --- a/xml_converter/generators/code_generator.py +++ b/xml_converter/generators/code_generator.py @@ -159,6 +159,11 @@ class AttributeVariable: # 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 = "" + + # The CPP code to inject into the variable setter to drill down to the + # variable we are looking for. eg ".mutable_trigger()" or "mutable_one()->mutable_two()->" + mutable_proto_drilldown_calls: str = "" + uses_file_path: bool = False is_component: bool = False @@ -333,8 +338,6 @@ def generate_cpp_variable_data( xml_fields: List[str] = [] side_effects: List[str] = [] write_to_xml: bool = True - protobuf_field: str = "" - proto_drilldown_calls: str = "" default_xml_field: str = "" args: List[str] = XML_ATTRIBUTE_PARSER_DEFAULT_ARGUMENTS.copy() @@ -365,7 +368,10 @@ def generate_cpp_variable_data( xml_fields.append(lowercase(x, delimiter="")) default_xml_field = fieldval['xml_fields'][0] - proto_drilldown_calls, protobuf_field = split_field_into_drilldown(fieldval["protobuf_field"]) + proto_drilldown_calls: str + mutable_proto_drilldown_calls: str + protobuf_field: str + proto_drilldown_calls, mutable_proto_drilldown_calls, protobuf_field = split_field_into_drilldown(fieldval["protobuf_field"]) if fieldval.get("uses_file_path", False): args.append("base_dir") @@ -416,6 +422,7 @@ def generate_cpp_variable_data( default_xml_field=default_xml_field, protobuf_field=protobuf_field, proto_drilldown_calls=proto_drilldown_calls, + mutable_proto_drilldown_calls=mutable_proto_drilldown_calls, args=args, write_to_xml=write_to_xml, attribute_flag_name=attribute_name + "_is_set", @@ -447,7 +454,6 @@ def write_attribute(self, output_directory: str) -> None: attribute_variable: AttributeVariable metadata: Dict[str, SchemaType] = {} xml_fields: List[str] = [] - proto_drilldown_calls: str = "" template: Dict[str, Template] = { "MultiflagValue": env.get_template("multiflagvalue.cpp"), "CompoundValue": env.get_template("compoundvalue.cpp"), @@ -464,7 +470,10 @@ def write_attribute(self, output_directory: str) -> None: metadata[filepath] = self.data[filepath].metadata attribute_name = attribute_name_from_markdown_data(metadata[filepath]['name']) - proto_drilldown_calls, protobuf_field = split_field_into_drilldown(metadata[filepath]["protobuf_field"]) + proto_drilldown_calls: str + mutable_proto_drilldown_calls: str + protobuf_field: str + proto_drilldown_calls, mutable_proto_drilldown_calls, protobuf_field = split_field_into_drilldown(metadata[filepath]["protobuf_field"]) if metadata[filepath]['type'] == "MultiflagValue": for flag in metadata[filepath]['flags']: @@ -480,6 +489,7 @@ def write_attribute(self, output_directory: str) -> None: xml_fields=xml_fields, protobuf_field=protobuf_field, proto_drilldown_calls=proto_drilldown_calls, + mutable_proto_drilldown_calls=mutable_proto_drilldown_calls ) attribute_variables.append(attribute_variable) @@ -503,6 +513,7 @@ def write_attribute(self, output_directory: str) -> None: xml_fields=xml_fields, protobuf_field=component["protobuf_field"], proto_drilldown_calls=proto_drilldown_calls, + mutable_proto_drilldown_calls=mutable_proto_drilldown_calls ) attribute_variables.append(attribute_variable) @@ -519,6 +530,7 @@ def write_attribute(self, output_directory: str) -> None: xml_fields=xml_fields, protobuf_field=protobuf_field, proto_drilldown_calls=proto_drilldown_calls, + mutable_proto_drilldown_calls=mutable_proto_drilldown_calls ) attribute_variables.append(attribute_variable) @@ -746,14 +758,15 @@ def generate_auto_docs(self, metadata: Dict[str, SchemaType], content: Dict[str, # Splits the field string into a cpp drilldown function call stack and the # final proto field name. # EG: -# field: "trigger.range" -# returns: (".trigger()", "range") +# field: "trigger.subclass.fieldname" +# returns: (".trigger().subclass()", "mutable_trigger()->mutable_subclass()->", "fieldname") ################################################################################ -def split_field_into_drilldown(field: str) -> Tuple[str, str]: +def split_field_into_drilldown(field: str) -> Tuple[str, str, str]: components = field.split(".") proto_drilldown_calls = "".join([".{}()".format(x) for x in components[:-1]]) + mutable_proto_drilldown_calls = "".join(["mutable_{}()->".format(x) for x in components[:-1]]) protobuf_field = components[-1] - return proto_drilldown_calls, protobuf_field + return proto_drilldown_calls, mutable_proto_drilldown_calls, protobuf_field ############################################################################ diff --git a/xml_converter/generators/cpp_templates/class_template.cpp b/xml_converter/generators/cpp_templates/class_template.cpp index 752a0f9a..47433b42 100644 --- a/xml_converter/generators/cpp_templates/class_template.cpp +++ b/xml_converter/generators/cpp_templates/class_template.cpp @@ -95,56 +95,23 @@ vector {{cpp_class}}::as_xml() const { waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf() const { waypoint::{{cpp_class}} proto_{{cpp_class_header}}; - {% if cpp_class == "Icon": %} - waypoint::Trigger* trigger = nullptr; - {% endif %} {% for attribute_variable in attribute_variables %} {% if attribute_variable.is_component == false %} - {% 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) { - trigger = new waypoint::Trigger(); - } - trigger->set_allocated_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}})); - } - {% elif (attribute_variable.attribute_type == "Enum")%} - if (this->{{attribute_variable.attribute_flag_name}}) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}})); - } - {% else: %} - if (this->{{attribute_variable.attribute_flag_name}}) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_{{attribute_variable.protobuf_field}}(this->{{attribute_variable.attribute_name}}); - } - {% endif %} + {% if (attribute_variable.attribute_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"])%} + if (this->{{attribute_variable.attribute_flag_name}}) { + proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}})); + } + {% elif (attribute_variable.attribute_type == "Enum")%} + if (this->{{attribute_variable.attribute_flag_name}}) { + proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}})); + } {% else: %} - {% if (attribute_variable.attribute_type == "Enum")%} - if (this->{{attribute_variable.attribute_flag_name}}) { - proto_{{cpp_class_header}}.set_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}})); - } - {% elif (attribute_variable.attribute_type in ["MultiflagValue", "CompoundValue", "Custom", "CompoundCustomClass"])%} - if (this->{{attribute_variable.attribute_flag_name}}) { - proto_{{cpp_class_header}}.set_allocated_{{attribute_variable.protobuf_field}}(to_proto_{{attribute_variable.class_name}}(this->{{attribute_variable.attribute_name}})); - } - {% else: %} - if (this->{{attribute_variable.attribute_flag_name}}) { - proto_{{cpp_class_header}}.set_{{attribute_variable.protobuf_field}}(this->{{attribute_variable.attribute_name}}); - } - {% endif %} + if (this->{{attribute_variable.attribute_flag_name}}) { + proto_{{cpp_class_header}}.{{attribute_variable.mutable_proto_drilldown_calls}}set_{{attribute_variable.protobuf_field}}(this->{{attribute_variable.attribute_name}}); + } {% endif %} {% endif %} {% endfor %} - {% if cpp_class == "Icon": %} - if (trigger != nullptr) { - proto_{{cpp_class_header}}.set_allocated_trigger(trigger); - } - {% endif %} return proto_{{cpp_class_header}}; } diff --git a/xml_converter/src/icon_gen.cpp b/xml_converter/src/icon_gen.cpp index 2eed3865..5cce5459 100644 --- a/xml_converter/src/icon_gen.cpp +++ b/xml_converter/src/icon_gen.cpp @@ -480,7 +480,6 @@ vector Icon::as_xml() const { waypoint::Icon Icon::as_protobuf() const { waypoint::Icon proto_icon; - waypoint::Trigger* trigger = nullptr; if (this->achievement_bitmask_is_set) { proto_icon.set_achievement_bit(this->achievement_bitmask); } @@ -488,28 +487,16 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_achievement_id(this->achievement_id); } if (this->auto_trigger_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_auto_trigger(this->auto_trigger); + proto_icon.mutable_trigger()->set_auto_trigger(this->auto_trigger); } if (this->bounce_delay_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_bounce_delay(this->bounce_delay); + proto_icon.mutable_trigger()->set_bounce_delay(this->bounce_delay); } if (this->bounce_duration_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_bounce_duration(this->bounce_duration); + proto_icon.mutable_trigger()->set_bounce_duration(this->bounce_duration); } if (this->bounce_height_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_bounce_height(this->bounce_height); + proto_icon.mutable_trigger()->set_bounce_height(this->bounce_height); } if (this->can_fade_is_set) { proto_icon.set_can_fade(this->can_fade); @@ -521,16 +508,10 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_allocated_rgba_color(to_proto_color(this->color)); } if (this->copy_clipboard_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_action_copy_clipboard(this->copy_clipboard); + proto_icon.mutable_trigger()->set_action_copy_clipboard(this->copy_clipboard); } if (this->copy_message_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_action_copy_message(this->copy_message); + proto_icon.mutable_trigger()->set_action_copy_message(this->copy_message); } if (this->cull_chirality_is_set) { proto_icon.set_cull_chirality(to_proto_cull_chirality(this->cull_chirality)); @@ -551,19 +532,13 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_allocated_guid(to_proto_unique_id(this->guid)); } if (this->has_countdown_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_has_countdown(this->has_countdown); + proto_icon.mutable_trigger()->set_has_countdown(this->has_countdown); } if (this->height_offset_is_set) { proto_icon.set_height_offset(this->height_offset); } if (this->hide_category_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_allocated_action_hide_category(to_proto_marker_category(this->hide_category)); + proto_icon.mutable_trigger()->set_allocated_action_hide_category(to_proto_marker_category(this->hide_category)); } if (this->icon_is_set) { proto_icon.set_allocated_texture_path(to_proto_image(this->icon)); @@ -572,16 +547,10 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_tentative__scale(this->icon_size); } if (this->info_message_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_action_info_message(this->info_message); + proto_icon.mutable_trigger()->set_action_info_message(this->info_message); } if (this->invert_visibility_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_invert_display(this->invert_visibility); + proto_icon.mutable_trigger()->set_invert_display(this->invert_visibility); } if (this->map_display_size_is_set) { proto_icon.set_map_display_size(this->map_display_size); @@ -617,16 +586,10 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_tentative__render_on_minimap(this->render_on_minimap); } if (this->reset_behavior_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_reset_behavior(to_proto_reset_behavior(this->reset_behavior)); + proto_icon.mutable_trigger()->set_reset_behavior(to_proto_reset_behavior(this->reset_behavior)); } if (this->reset_length_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_reset_length(this->reset_length); + proto_icon.mutable_trigger()->set_reset_length(this->reset_length); } if (this->scale_on_map_with_zoom_is_set) { proto_icon.set_scale_on_map_with_zoom(this->scale_on_map_with_zoom); @@ -638,10 +601,7 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_bhdraft__schedule_duration(this->schedule_duration); } if (this->show_category_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_allocated_action_show_category(to_proto_marker_category(this->show_category)); + proto_icon.mutable_trigger()->set_allocated_action_show_category(to_proto_marker_category(this->show_category)); } if (this->specialization_filter_is_set) { proto_icon.set_allocated_specialization_filter(to_proto_specialization_filter(this->specialization_filter)); @@ -650,10 +610,7 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_allocated_species_filter(to_proto_species_filter(this->species_filter)); } if (this->toggle_category_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_allocated_action_toggle_category(to_proto_marker_category(this->toggle_category)); + proto_icon.mutable_trigger()->set_allocated_action_toggle_category(to_proto_marker_category(this->toggle_category)); } if (this->tooltip_description_is_set) { proto_icon.set_tip_description(this->tooltip_description); @@ -662,13 +619,7 @@ waypoint::Icon Icon::as_protobuf() const { proto_icon.set_tip_name(this->tooltip_name); } if (this->trigger_range_is_set) { - if (trigger == nullptr) { - trigger = new waypoint::Trigger(); - } - trigger->set_range(this->trigger_range); - } - if (trigger != nullptr) { - proto_icon.set_allocated_trigger(trigger); + proto_icon.mutable_trigger()->set_range(this->trigger_range); } return proto_icon; }