Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding more dynamic Proto reading functions #202

Merged
merged 1 commit into from
Nov 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion xml_converter/doc/menu/display_name.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ name: Display Name
type: String
applies_to: [Category]
xml_fields: [DisplayName]
protobuf_field: display_name
protobuf_field: name
custom_functions:
read.proto:
function: proto_display_name_to_display_name_and_name
side_effects: [Name]

---
A human readable name of this category.

Expand Down
7 changes: 7 additions & 0 deletions xml_converter/doc/menu/name.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ type: String
applies_to: [Category]
xml_fields: [Name]
protobuf_field: name
custom_functions:
read.proto:
function: do_nothing
side_effects: []
write.proto:
function: do_nothing
side_effects: []
---

Notes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ void xml_attribute_to_{{attribute_name}}(
std::vector<XMLError*>* errors,
{{class_name}}* value,
bool* is_set);

std::string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, const {{class_name}}* value);

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}});
void proto_to_{{attribute_name}}({{proto_field_cpp_type}} input, {{class_name}}* value, bool* is_set);

{% if type == "Enum" %}
void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}})> setter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,13 @@ void xml_attribute_to_{{attribute_name}}(
}
{% endif %}

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}}) {
void proto_to_{{attribute_name}}({{proto_field_cpp_type}} input, {{class_name}}* value, bool* is_set) {
{{class_name}} {{attribute_name}};
{% for attribute_variable in attribute_variables: %}
{{attribute_name}}.{{attribute_variable.attribute_name}} = proto_{{attribute_name}}.{{attribute_variable.protobuf_field}}();
{{attribute_name}}.{{attribute_variable.attribute_name}} = input.{{attribute_variable.protobuf_field}}();
{% endfor %}
return {{attribute_name}};
*value = {{attribute_name}};
*is_set = true;
}

void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}}*)> setter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,18 @@ string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, co
}
}

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}}) {
switch (proto_{{attribute_name}}) {
void proto_to_{{attribute_name}}({{proto_field_cpp_type}} input, {{class_name}}* value, bool* is_set) {
switch (input) {
{% for attribute_variable in attribute_variables %}
case {{proto_field_cpp_type}}::{{attribute_variable.attribute_name}}:
return {{class_name}}::{{attribute_variable.attribute_name}};
*value = {{class_name}}::{{attribute_variable.attribute_name}};
*is_set = true;
break;
{% endfor %}
default:
return {{class_name}}::{{attribute_variables[0].attribute_name}};
*value = {{class_name}}::{{attribute_variables[0].attribute_name}};
*is_set = true;
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ string {{attribute_name}}_to_xml_attribute(const std::string& attribute_name, co
return " " + attribute_name + "=\"" + output + "\"";
}

{{class_name}} from_proto_{{attribute_name}}({{proto_field_cpp_type}} proto_{{attribute_name}}) {
void proto_to_{{attribute_name}}({{proto_field_cpp_type}} input, {{class_name}}* value, bool* is_set) {
{{class_name}} {{attribute_name}};
{% for n, attribute_variable in enumerate(attribute_variables)%}
{{attribute_name}}.{{attribute_variable.attribute_name}} = proto_{{attribute_name}}.{{attribute_variable.attribute_name}}();
{{attribute_name}}.{{attribute_variable.attribute_name}} = input.{{attribute_variable.attribute_name}}();
{% endfor %}
return {{attribute_name}};
*value = {{attribute_name}};
*is_set = true;
}

void {{attribute_name}}_to_proto({{class_name}} value, std::function<void({{proto_field_cpp_type}}*)> setter) {
Expand Down
3 changes: 1 addition & 2 deletions xml_converter/generators/cpp_templates/class_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ void {{cpp_class}}::parse_protobuf(waypoint::{{cpp_class}} proto_{{cpp_class_hea
{% else %}
if (proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}() != 0) {
{% endif %}
this->{{attribute_variable.attribute_name}} = {{attribute_variable.deserialize_proto_function}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}());
this->{{attribute_variable.attribute_flag_name}} = true;
{{attribute_variable.deserialize_proto_function}}(proto_{{cpp_class_header}}{{attribute_variable.proto_drilldown_calls}}.{{attribute_variable.protobuf_field}}(), &(this->{{attribute_variable.attribute_name}}), &(this->{{attribute_variable.attribute_flag_name}}){% for side_effect in attribute_variable.deserialize_proto_side_effects %}, &(this->{{side_effect}}){% endfor %});
}
{% endif %}
{% endfor %}
Expand Down
22 changes: 11 additions & 11 deletions xml_converter/generators/generate_cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class AttributeVariable:

# The function name and additional side effect pointers for proto deserialization.
deserialize_proto_function: str
# deserialize_proto_side_effects: List[str]
deserialize_proto_side_effects: List[str]

# The name of the field in the protobuf that this attribute corresponds to.
protobuf_field: str
Expand Down Expand Up @@ -313,7 +313,7 @@ def generate_cpp_variable_data(
serialize_proto_function="to_proto_" + component_class_name,
# serialize_proto_side_effects=[],
deserialize_proto_function="from_proto_" + component_class_name,
# deserialize_proto_side_effects=[],
deserialize_proto_side_effects=[],
)
attribute_variables.append(component_attribute_variable)
# If there aren't any components to bundle, we don't want to render the attribute
Expand All @@ -326,7 +326,7 @@ def generate_cpp_variable_data(
deserialize_xml_side_effects: List[str] = []
serialize_proto_function: str = class_name + "_to_proto"
serialize_proto_side_effects: List[str] = []
deserialize_proto_function: str = "from_proto_" + class_name
deserialize_proto_function: str = "proto_to_" + class_name
deserialize_proto_side_effects: List[str] = []
if "custom_functions" in fieldval:
# Overwrite defaults with xml/proto read/write functions
Expand All @@ -335,11 +335,11 @@ def generate_cpp_variable_data(
deserialize_xml_function, deserialize_xml_side_effects = build_custom_function_data(
fieldval["custom_functions"][custom_function]
)
elif custom_function == "read.proto":
elif custom_function == "write.xml":
serialize_xml_function, serialize_xml_side_effects = build_custom_function_data(
fieldval["custom_functions"][custom_function]
)
elif custom_function == "write.xml":
elif custom_function == "read.proto":
deserialize_proto_function, deserialize_proto_side_effects = build_custom_function_data(
fieldval["custom_functions"][custom_function]
)
Expand All @@ -353,11 +353,11 @@ def generate_cpp_variable_data(
deserialize_xml_function, deserialize_xml_side_effects = build_custom_function_data(
fieldval["custom_functions"][custom_function]
)
elif custom_function == "read.proto." + doc_type:
elif custom_function == "write.xml." + doc_type:
serialize_xml_function, serialize_xml_side_effects = build_custom_function_data(
fieldval["custom_functions"][custom_function]
)
elif custom_function == "write.xml." + doc_type:
elif custom_function == "read.proto." + doc_type:
deserialize_proto_function, deserialize_proto_side_effects = build_custom_function_data(
fieldval["custom_functions"][custom_function]
)
Expand Down Expand Up @@ -390,7 +390,7 @@ def generate_cpp_variable_data(
serialize_proto_function=serialize_proto_function,
# serialize_proto_side_effects=serialize_proto_side_effects,
deserialize_proto_function=deserialize_proto_function,
# deserialize_proto_side_effects=deserialize_proto_side_effects,
deserialize_proto_side_effects=deserialize_proto_side_effects,

uses_file_path=fieldval.get("uses_file_path", False)
)
Expand Down Expand Up @@ -480,7 +480,7 @@ def write_attribute(output_directory: str, data: Dict[str, Document]) -> None:
# serialize_xml_side_effects=[],
deserialize_xml_side_effects=[],
# serialize_proto_side_effects=[],
# deserialize_proto_side_effects=[],
deserialize_proto_side_effects=[],
)
attribute_variables.append(attribute_variable)

Expand Down Expand Up @@ -515,7 +515,7 @@ def write_attribute(output_directory: str, data: Dict[str, Document]) -> None:
# serialize_xml_side_effects=[],
deserialize_xml_side_effects=[],
# serialize_proto_side_effects=[],
# deserialize_proto_side_effects=[],
deserialize_proto_side_effects=[],
)
attribute_variables.append(attribute_variable)

Expand Down Expand Up @@ -543,7 +543,7 @@ def write_attribute(output_directory: str, data: Dict[str, Document]) -> None:
# serialize_xml_side_effects=[],
deserialize_xml_side_effects=[],
# serialize_proto_side_effects=[],
# deserialize_proto_side_effects=[],
deserialize_proto_side_effects=[],
)
attribute_variables.append(attribute_variable)

Expand Down
15 changes: 7 additions & 8 deletions xml_converter/proto/waypoint.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ message Waypoint {
}

message Category {
bool default_visibility = 1;
string display_name = 2;
bool is_separator = 3;
string name = 4;
string tip_description = 5;
repeated Category children = 6;
repeated Icon icon = 7;
repeated Trail trail = 8;
string name = 1;
repeated Category children = 2;
repeated Icon icon = 3;
repeated Trail trail = 4;
bool is_separator = 5;
bool default_visibility = 6;
string tip_description = 7;
}

message Icon {
Expand Down
10 changes: 10 additions & 0 deletions xml_converter/src/attribute/bool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ string bool_to_xml_attribute(const string& attribute_name, const bool* value) {
}
}

////////////////////////////////////////////////////////////////////////////////
// proto_to_bool
//
// Parses a bool from a proto field.
////////////////////////////////////////////////////////////////////////////////
void proto_to_bool(bool input, bool* value, bool* is_set) {
*value = input;
*is_set = true;
}

////////////////////////////////////////////////////////////////////////////////
// bool_to_proto
//
Expand Down
5 changes: 1 addition & 4 deletions xml_converter/src/attribute/bool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ void xml_attribute_to_bool(

std::string bool_to_xml_attribute(const std::string& attribute_name, const bool* value);

// Zero Cost Abstraction identity functions to make parsing and writing protobufs more uniform
inline bool const& from_proto_bool(const bool& x) {
return x;
}
void proto_to_bool(bool input, bool* value, bool* is_set);

void bool_to_proto(bool value, std::function<void(bool)> setter);
11 changes: 6 additions & 5 deletions xml_converter/src/attribute/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,22 @@ string color_to_xml_attribute(const string& attribute_name, const Color* value)
}

////////////////////////////////////////////////////////////////////////////////
// from_proto_color
// proto_to_color
//
// Converts a proto message into a Color
// Parses a Color from a proto field.
////////////////////////////////////////////////////////////////////////////////
Color from_proto_color(waypoint::RGBAColor attribute_value) {
void proto_to_color(waypoint::RGBAColor input, Color* value, bool* is_set) {
Color color;
std::stringstream stream;
uint32_t rgba = attribute_value.rgba_color();
uint32_t rgba = input.rgba_color();

color.red = convert_color_channel_int_to_float((rgba >> 24) & 0xff);
color.green = convert_color_channel_int_to_float((rgba >> 16) & 0xff);
color.blue = convert_color_channel_int_to_float((rgba >> 8) & 0xff);
color.alpha = convert_color_channel_int_to_float(rgba & 0xff);

return color;
*value = color;
*is_set = true;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion xml_converter/src/attribute/color.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ void xml_attribute_to_color(

std::string color_to_xml_attribute(const std::string& attribute_name, const Color* value);

Color from_proto_color(waypoint::RGBAColor attribute_value);
void proto_to_color(waypoint::RGBAColor input, Color* value, bool* is_set);

void color_to_proto(Color value, std::function<void(waypoint::RGBAColor*)> setter);
20 changes: 14 additions & 6 deletions xml_converter/src/attribute/cull_chirality_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,24 @@ string cull_chirality_to_xml_attribute(const std::string& attribute_name, const
}
}

CullChirality from_proto_cull_chirality(waypoint::CullChirality proto_cull_chirality) {
switch (proto_cull_chirality) {
void proto_to_cull_chirality(waypoint::CullChirality input, CullChirality* value, bool* is_set) {
switch (input) {
case waypoint::CullChirality::none:
return CullChirality::none;
*value = CullChirality::none;
*is_set = true;
break;
case waypoint::CullChirality::clockwise:
return CullChirality::clockwise;
*value = CullChirality::clockwise;
*is_set = true;
break;
case waypoint::CullChirality::counter_clockwise:
return CullChirality::counter_clockwise;
*value = CullChirality::counter_clockwise;
*is_set = true;
break;
default:
return CullChirality::none;
*value = CullChirality::none;
*is_set = true;
break;
}
}

Expand Down
3 changes: 2 additions & 1 deletion xml_converter/src/attribute/cull_chirality_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ void xml_attribute_to_cull_chirality(
std::vector<XMLError*>* errors,
CullChirality* value,
bool* is_set);

std::string cull_chirality_to_xml_attribute(const std::string& attribute_name, const CullChirality* value);

CullChirality from_proto_cull_chirality(waypoint::CullChirality proto_cull_chirality);
void proto_to_cull_chirality(waypoint::CullChirality input, CullChirality* value, bool* is_set);

void cull_chirality_to_proto(CullChirality value, std::function<void(waypoint::CullChirality)> setter);
11 changes: 6 additions & 5 deletions xml_converter/src/attribute/euler_rotation_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ string euler_rotation_to_xml_attribute(const std::string& attribute_name, const
return " " + attribute_name + "=\"" + output + "\"";
}

EulerRotation from_proto_euler_rotation(waypoint::EulerRotation proto_euler_rotation) {
void proto_to_euler_rotation(waypoint::EulerRotation input, EulerRotation* value, bool* is_set) {
EulerRotation euler_rotation;
euler_rotation.x_rotation = proto_euler_rotation.x();
euler_rotation.y_rotation = proto_euler_rotation.y();
euler_rotation.z_rotation = proto_euler_rotation.z();
return euler_rotation;
euler_rotation.x_rotation = input.x();
euler_rotation.y_rotation = input.y();
euler_rotation.z_rotation = input.z();
*value = euler_rotation;
*is_set = true;
}

void euler_rotation_to_proto(EulerRotation value, std::function<void(waypoint::EulerRotation*)> setter) {
Expand Down
3 changes: 2 additions & 1 deletion xml_converter/src/attribute/euler_rotation_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ void xml_attribute_to_euler_rotation(
std::vector<XMLError*>* errors,
EulerRotation* value,
bool* is_set);

std::string euler_rotation_to_xml_attribute(const std::string& attribute_name, const EulerRotation* value);

EulerRotation from_proto_euler_rotation(waypoint::EulerRotation proto_euler_rotation);
void proto_to_euler_rotation(waypoint::EulerRotation input, EulerRotation* value, bool* is_set);

void euler_rotation_to_proto(EulerRotation value, std::function<void(waypoint::EulerRotation*)> setter);
19 changes: 10 additions & 9 deletions xml_converter/src/attribute/festival_filter_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,17 @@ string festival_filter_to_xml_attribute(const std::string& attribute_name, const
return " " + attribute_name + "=\"" + output + "\"";
}

FestivalFilter from_proto_festival_filter(waypoint::FestivalFilter proto_festival_filter) {
void proto_to_festival_filter(waypoint::FestivalFilter input, FestivalFilter* value, bool* is_set) {
FestivalFilter festival_filter;
festival_filter.dragonbash = proto_festival_filter.dragonbash();
festival_filter.festival_of_the_four_winds = proto_festival_filter.festival_of_the_four_winds();
festival_filter.halloween = proto_festival_filter.halloween();
festival_filter.lunar_new_year = proto_festival_filter.lunar_new_year();
festival_filter.super_adventure_festival = proto_festival_filter.super_adventure_festival();
festival_filter.wintersday = proto_festival_filter.wintersday();
festival_filter.none = proto_festival_filter.none();
return festival_filter;
festival_filter.dragonbash = input.dragonbash();
festival_filter.festival_of_the_four_winds = input.festival_of_the_four_winds();
festival_filter.halloween = input.halloween();
festival_filter.lunar_new_year = input.lunar_new_year();
festival_filter.super_adventure_festival = input.super_adventure_festival();
festival_filter.wintersday = input.wintersday();
festival_filter.none = input.none();
*value = festival_filter;
*is_set = true;
}

void festival_filter_to_proto(FestivalFilter value, std::function<void(waypoint::FestivalFilter*)> setter) {
Expand Down
3 changes: 2 additions & 1 deletion xml_converter/src/attribute/festival_filter_gen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ void xml_attribute_to_festival_filter(
std::vector<XMLError*>* errors,
FestivalFilter* value,
bool* is_set);

std::string festival_filter_to_xml_attribute(const std::string& attribute_name, const FestivalFilter* value);

FestivalFilter from_proto_festival_filter(waypoint::FestivalFilter proto_festival_filter);
void proto_to_festival_filter(waypoint::FestivalFilter input, FestivalFilter* value, bool* is_set);

void festival_filter_to_proto(FestivalFilter value, std::function<void(waypoint::FestivalFilter*)> setter);
Loading