From 5976f189fcfe825e885fb9d83473d37e03263455 Mon Sep 17 00:00:00 2001 From: Asher Glick Date: Sun, 26 May 2024 22:08:28 -0500 Subject: [PATCH] Removing the requirement for protofields on each attribute --- xml_converter/doc/category/category.md | 9 +---- xml_converter/doc/menu/name.md | 9 +---- .../cpp_templates/class_template.cpp | 4 +- xml_converter/generators/generate_cpp.py | 40 ++++++++++++------- xml_converter/generators/main.py | 28 +++++++++---- xml_converter/generators/metadata.py | 2 +- xml_converter/proto/waypoint.proto | 6 --- xml_converter/src/attribute/string.hpp | 2 - xml_converter/src/category_gen.cpp | 7 ---- xml_converter/src/icon_gen.cpp | 7 ---- xml_converter/src/trail_gen.cpp | 7 ---- 11 files changed, 50 insertions(+), 71 deletions(-) diff --git a/xml_converter/doc/category/category.md b/xml_converter/doc/category/category.md index 438044d7..0790e495 100644 --- a/xml_converter/doc/category/category.md +++ b/xml_converter/doc/category/category.md @@ -4,18 +4,11 @@ type: Custom class: MarkerCategory applies_to: [Icon, Trail] xml_fields: [Type, Category] -protobuf_field: category +protobuf_field: null examples: - "mycategory" - "mycategory.subcategory" - "mycategory.subcategory.subsubcategory" -custom_functions: - read.proto: - function: do_nothing - side_effects: [] - write.proto: - function: do_nothing - side_effects: [] --- The category this object belongs to. diff --git a/xml_converter/doc/menu/name.md b/xml_converter/doc/menu/name.md index e62e87b3..84de7591 100644 --- a/xml_converter/doc/menu/name.md +++ b/xml_converter/doc/menu/name.md @@ -3,17 +3,10 @@ name: Name type: String applies_to: [Category] xml_fields: [Name] -protobuf_field: name +protobuf_field: null examples: - "mycategory" - "tribulationmode203" -custom_functions: - read.proto: - function: do_nothing - side_effects: [] - write.proto: - function: do_nothing - side_effects: [] --- Notes diff --git a/xml_converter/generators/cpp_templates/class_template.cpp b/xml_converter/generators/cpp_templates/class_template.cpp index 3204c490..cba55e91 100644 --- a/xml_converter/generators/cpp_templates/class_template.cpp +++ b/xml_converter/generators/cpp_templates/class_template.cpp @@ -103,7 +103,7 @@ vector {{cpp_class}}::as_xml(XMLWriterState* state) const { waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf(ProtoWriterState* state) const { waypoint::{{cpp_class}} proto_{{cpp_class_header}}; {% for attribute_variable in attribute_variables %} - {% if attribute_variable.is_component == false %} + {% if attribute_variable.is_component == false and attribute_variable.proto_info != None %} if (this->{{attribute_variable.attribute_flag_name}}) { {% if not attribute_variable.proto_info.is_proto_field_scalar %} std::function setter = [&proto_{{cpp_class_header}}]({{attribute_variable.proto_info.protobuf_cpp_type}}* val) { proto_{{cpp_class_header}}.{{attribute_variable.proto_info.mutable_proto_drilldown_calls}}set_allocated_{{attribute_variable.proto_info.protobuf_field}}(val); }; @@ -119,7 +119,7 @@ waypoint::{{cpp_class}} {{cpp_class}}::as_protobuf(ProtoWriterState* state) cons void {{cpp_class}}::parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_header}}, ProtoReaderState* state) { {% for attribute_variable in attribute_variables %} - {% if attribute_variable.is_component == false %} + {% if attribute_variable.is_component == false and attribute_variable.proto_info != None %} {% if not attribute_variable.proto_info.is_proto_field_scalar %} if (proto_{{cpp_class_header}}{{attribute_variable.proto_info.proto_drilldown_calls}}.has_{{attribute_variable.proto_info.protobuf_field}}()) { {% elif attribute_variable.proto_info.protobuf_cpp_type == "std::string" %} diff --git a/xml_converter/generators/generate_cpp.py b/xml_converter/generators/generate_cpp.py index 11e64da3..13932a84 100644 --- a/xml_converter/generators/generate_cpp.py +++ b/xml_converter/generators/generate_cpp.py @@ -95,7 +95,7 @@ class AttributeVariable: cpp_type: str class_name: str - proto_info: AttributeVariableProtoInfo + proto_info: Optional[AttributeVariableProtoInfo] xml_info: AttributeVariableXMLInfo attribute_flag_name: Optional[str] = "" @@ -312,11 +312,6 @@ def generate_cpp_variable_data( xml_fields.append(lowercase(x, delimiter="")) default_xml_field = fieldval.xml_fields[0] - 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) - # Compound Values are unique in that the components have xml fields in addition to the compound variable # if fieldval.variable_type in ("CompoundValue", "CompoundCustomClass"): if fieldval.variable_type == "CompoundValue" or fieldval.variable_type == "CompoundCustomClass": @@ -348,7 +343,7 @@ def generate_cpp_variable_data( serialize_proto_side_effects=[], deserialize_proto_function="from_proto_" + component_class_name, deserialize_proto_side_effects=[], - ), + ) if fieldval.protobuf_field is not None else None, xml_info=AttributeVariableXMLInfo( xml_fields=component_xml_fields, @@ -413,14 +408,14 @@ def generate_cpp_variable_data( elif fieldval.custom_functions.read_proto is not None: deserialize_proto_function = fieldval.custom_functions.read_proto - attribute_variable = AttributeVariable( - attribute_name=attribute_name, - cpp_type=cpp_type, - class_name=class_name, - - attribute_flag_name=attribute_name + "_is_set", + proto_info: Optional[AttributeVariableProtoInfo] = None + if fieldval.protobuf_field is not None: + 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) - proto_info=AttributeVariableProtoInfo( + proto_info = AttributeVariableProtoInfo( protobuf_field=protobuf_field, protobuf_cpp_type=get_proto_field_cpp_type(doc_type, fieldval.protobuf_field), is_proto_field_scalar=is_proto_field_scalar(doc_type, fieldval.protobuf_field), @@ -430,7 +425,16 @@ def generate_cpp_variable_data( serialize_proto_side_effects=convert_side_effects_to_variable_names(serialize_proto_function.side_effects), deserialize_proto_function=deserialize_proto_function.function, deserialize_proto_side_effects=convert_side_effects_to_variable_names(deserialize_proto_function.side_effects), - ), + ) + + attribute_variable = AttributeVariable( + attribute_name=attribute_name, + cpp_type=cpp_type, + class_name=class_name, + + attribute_flag_name=attribute_name + "_is_set", + + proto_info=proto_info, xml_info=AttributeVariableXMLInfo( xml_fields=xml_fields, @@ -483,6 +487,12 @@ def write_attribute(output_directory: str, data: Dict[str, Document]) -> List[st attribute_data: MetadataType = data[filepath].metadata attribute_name = attribute_name_from_markdown_data(attribute_data.name) + # Early exit if this attribute is not an attribute to generate code for + if attribute_data.variable_type not in template: + continue + if attribute_data.protobuf_field is None: + raise ValueError("We dont yet support null protobuf fields for generated attribute classes {}".format(attribute_data)) + proto_field_type: str = "" proto_field_prototype: Optional[str] = None for marker_type in attribute_data.applies_to_as_str(): diff --git a/xml_converter/generators/main.py b/xml_converter/generators/main.py index 187b2440..a3448a66 100644 --- a/xml_converter/generators/main.py +++ b/xml_converter/generators/main.py @@ -239,18 +239,27 @@ def generate_auto_docs(self, metadata: Dict[str, MetadataType], content: Dict[st ) ) - proto_field_type: str = "" - for marker_type in fieldval.applies_to_as_str(): - proto_field_type = get_proto_field_type(marker_type, fieldval.protobuf_field) - # TODO: catch discrepencies if the proto field types across - # different messages have differing types. This will be caught - # in the cpp code regardless. + proto_field_type: str + proto_field_name: str + if fieldval.protobuf_field is not None: + proto_field_name = fieldval.protobuf_field + proto_field_type = "" + for marker_type in fieldval.applies_to_as_str(): + proto_field_type = get_proto_field_type(marker_type, fieldval.protobuf_field) + # TODO: catch discrepencies if the proto field types across + # different messages have differing types. This will be caught + # in the cpp code regardless. + if proto_field_type == "": + print("Could not find proto field type from proto schema") + else: + proto_field_name = "" + proto_field_type = "None" field_rows.append(FieldRow( name=fieldval.name, xml_attribute=fieldval.xml_fields[0], alternate_xml_attributes=fieldval.xml_fields[1:], - binary_field=fieldval.protobuf_field, + binary_field=proto_field_name, binary_field_type=proto_field_type, data_type=fieldval.variable_type, usable_on_html="
".join(fieldval.applies_to_as_str()), @@ -265,7 +274,10 @@ def generate_auto_docs(self, metadata: Dict[str, MetadataType], content: Dict[st if fieldval.variable_type == "CompoundValue" or fieldval.variable_type == "CompoundCustomClass": for component_field in fieldval.components: - binary_field_name = fieldval.protobuf_field + "." + component_field.protobuf_field + if fieldval.protobuf_field is not None: + binary_field_name = fieldval.protobuf_field + "." + component_field.protobuf_field + else: + binary_field_name = "" component_field_type: str = "" for marker_type in fieldval.applies_to_as_str(): diff --git a/xml_converter/generators/metadata.py b/xml_converter/generators/metadata.py index 47da3be6..26de1152 100644 --- a/xml_converter/generators/metadata.py +++ b/xml_converter/generators/metadata.py @@ -51,7 +51,7 @@ class BaseMetadata: name: str # TODO Match this to the regex ATTRIBUTE_NAME_REGEX applies_to: List[NodeType] xml_fields: List[str] # TODO: Matche these to XML_ATTRIBUTE_REGEX - protobuf_field: str # TODO: Match this to PROTO_FIELD_REGEX + protobuf_field: Optional[str] # TODO: Match this to PROTO_FIELD_REGEX def applies_to_as_str(self) -> List[str]: return [x.value for x in self.applies_to] diff --git a/xml_converter/proto/waypoint.proto b/xml_converter/proto/waypoint.proto index 95a29c1f..cbd52424 100644 --- a/xml_converter/proto/waypoint.proto +++ b/xml_converter/proto/waypoint.proto @@ -62,9 +62,6 @@ message Icon { bool tentative__render_on_minimap = 2051; string bhdraft__schedule = 2052; float bhdraft__schedule_duration = 2053; - - // TODO: Delete this when we can parse data per marker instead of per field - bool category = 2054; } message Trail { @@ -97,9 +94,6 @@ message Trail { bool tentative__render_on_minimap = 2051; string bhdraft__schedule = 2052; float bhdraft__schedule_duration = 2053; - - // TODO: Delete this when we can parse data per marker instead of per field - bool category = 2054; } message RGBAColor { diff --git a/xml_converter/src/attribute/string.hpp b/xml_converter/src/attribute/string.hpp index db71a35d..a6cdb6a1 100644 --- a/xml_converter/src/attribute/string.hpp +++ b/xml_converter/src/attribute/string.hpp @@ -52,5 +52,3 @@ void display_name_and_name_to_proto_display_name( std::function setter, const std::string* name, const bool* is_name_set); - -#define do_nothing(...) diff --git a/xml_converter/src/category_gen.cpp b/xml_converter/src/category_gen.cpp index 251be4d3..91d69ce7 100644 --- a/xml_converter/src/category_gen.cpp +++ b/xml_converter/src/category_gen.cpp @@ -129,10 +129,6 @@ waypoint::Category Category::as_protobuf(ProtoWriterState* state) const { std::function setter = [&proto_category](std::string val) { proto_category.set_id(val); }; unique_id_to_proto(this->menu_id, state, setter); } - if (this->name_is_set) { - std::function setter = [&proto_category](std::string val) { proto_category.set_name(val); }; - do_nothing(this->name, state, setter); - } if (this->tooltip_description_is_set) { std::function setter = [&proto_category](std::string val) { proto_category.set_tip_description(val); }; string_to_proto(this->tooltip_description, state, setter); @@ -153,9 +149,6 @@ void Category::parse_protobuf(waypoint::Category proto_category, ProtoReaderStat if (proto_category.id() != "") { proto_to_unique_id(proto_category.id(), state, &(this->menu_id), &(this->menu_id_is_set)); } - if (proto_category.name() != "") { - do_nothing(proto_category.name(), state, &(this->name), &(this->name_is_set)); - } if (proto_category.tip_description() != "") { proto_to_string(proto_category.tip_description(), state, &(this->tooltip_description), &(this->tooltip_description_is_set)); } diff --git a/xml_converter/src/icon_gen.cpp b/xml_converter/src/icon_gen.cpp index f9573791..400bbb5f 100644 --- a/xml_converter/src/icon_gen.cpp +++ b/xml_converter/src/icon_gen.cpp @@ -435,10 +435,6 @@ waypoint::Icon Icon::as_protobuf(ProtoWriterState* state) const { std::function setter = [&proto_icon](float val) { proto_icon.mutable_trigger()->set_bounce_height(val); }; float_to_proto(this->bounce_height, state, setter); } - if (this->category_is_set) { - std::function setter = [&proto_icon](bool val) { proto_icon.set_category(val); }; - do_nothing(this->category, state, setter); - } if (this->color_is_set) { std::function setter = [&proto_icon](waypoint::RGBAColor* val) { proto_icon.set_allocated_rgba_color(val); }; color_to_proto(this->color, state, setter); @@ -621,9 +617,6 @@ void Icon::parse_protobuf(waypoint::Icon proto_icon, ProtoReaderState* state) { if (proto_icon.trigger().bounce_height() != 0) { proto_to_float(proto_icon.trigger().bounce_height(), state, &(this->bounce_height), &(this->bounce_height_is_set)); } - if (proto_icon.category() != 0) { - do_nothing(proto_icon.category(), state, &(this->category), &(this->category_is_set)); - } if (proto_icon.has_rgba_color()) { proto_to_color(proto_icon.rgba_color(), state, &(this->color), &(this->color_is_set)); } diff --git a/xml_converter/src/trail_gen.cpp b/xml_converter/src/trail_gen.cpp index d3d269da..50c6cd71 100644 --- a/xml_converter/src/trail_gen.cpp +++ b/xml_converter/src/trail_gen.cpp @@ -261,10 +261,6 @@ waypoint::Trail Trail::as_protobuf(ProtoWriterState* state) const { std::function setter = [&proto_trail](float val) { proto_trail.set_animation_speed(val); }; float_to_proto(this->animation_speed, state, setter); } - if (this->category_is_set) { - std::function setter = [&proto_trail](bool val) { proto_trail.set_category(val); }; - do_nothing(this->category, state, setter); - } if (this->color_is_set) { std::function setter = [&proto_trail](waypoint::RGBAColor* val) { proto_trail.set_allocated_rgba_color(val); }; color_to_proto(this->color, state, setter); @@ -370,9 +366,6 @@ void Trail::parse_protobuf(waypoint::Trail proto_trail, ProtoReaderState* state) if (proto_trail.animation_speed() != 0) { proto_to_float(proto_trail.animation_speed(), state, &(this->animation_speed), &(this->animation_speed_is_set)); } - if (proto_trail.category() != 0) { - do_nothing(proto_trail.category(), state, &(this->category), &(this->category_is_set)); - } if (proto_trail.has_rgba_color()) { proto_to_color(proto_trail.rgba_color(), state, &(this->color), &(this->color_is_set)); }