From df2827cbaffbe7f9c3764f647b895a92973041d3 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 8 Dec 2022 14:23:55 +0300 Subject: [PATCH] Gradually adding support for all features --- src/codegen.inc.php | 6 ++++++ tpl/macro.twig | 45 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 2e5e440..1c1e495 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -80,6 +80,12 @@ function _add_twig_support(\Twig\Environment $twig) return $v; } )); + $twig->addFunction(new \Twig\TwigFunction('get_all_fields', + function($meta, $o) + { + return \mtg_get_all_fields($meta, $o); + } + )); $twig->addFilter(new \Twig\TwigFilter('go_type', function($type, $tokens) { diff --git a/tpl/macro.twig b/tpl/macro.twig index 125a4ab..ce3c784 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -1,7 +1,7 @@ {% macro decl_units(meta) %} {%- for u in meta.getunits ~%} {%- if u.object is instanceof('\\mtgMetaStruct') ~%} - {{ _self.decl_struct(u.object) }} + {{ _self.decl_struct(meta, u.object) }} {%- elseif u.object is instanceof('\\mtgMetaEnum') ~%} {{ _self.decl_enum(u.object) }} {%- elseif u.object is instanceof('\\mtgMetaRPC') ~%} @@ -10,7 +10,7 @@ {%- endfor ~%} {% endmacro %} -{% macro decl_struct(o) %} +{% macro decl_struct(meta, o) %} //============================== type {{o.name}} struct { @@ -20,13 +20,11 @@ type {{o.name}} struct { {{_self.decl_struct_fields(o)}} } -var _{{o.name}}_class_props map[string]string -var _{{o.name}}_class_fields []string = %fields_names% -var _{{o.name}}_fields_props meta.ClassFieldsProps -func init() { - _{{o.name}}_class_props = %class_props% - _{{o.name}}_fields_props = %fields_props% -} + +var _{{o.name}}_class_props map[string]string = {{_self.props_map(o.tokens)}} +var _{{o.name}}_class_fields []string = {{_self.struct_fields_names(meta, o)}} +var _{{o.name}}_fields_props meta.ClassFieldsProps = {{_self.struct_fields_props(meta, o)}} + func {{o.name}}_CLASS_ID() uint32 { return {{o.classid}} } @@ -88,10 +86,39 @@ func (self *{{o.name}}) WriteFields(writer meta.Writer) error { {% endmacro %} +{% macro props_map(tokens) %} +map[string]string{ +{% for k,v in tokens %} + "{{k}}" : "{{v|replace({'"' : '\\"'})}}", +{%- endfor ~%} +} +{% endmacro %} + +{% macro struct_fields_names(meta, o) %} +[]string{ +{% for f in get_all_fields(meta, o) ~%} +"{{f.name}}", +{%- endfor ~%} +} +{% endmacro %} + +{% macro struct_fields_props(meta, o) %} +map[string]map[string]string{ +{% for f in get_all_fields(meta, o) ~%} +"{{f.name|ucfirst}}" : {{_self.props_map(f.tokens)}}, +{%- endfor ~%} +} +{% endmacro %} + {% macro decl_struct_fields(o) %} {%- for f in o.fields ~%} {{f.name|ucfirst}} {{f.type|go_type(f.tokens)}} {{f.name|first != '_' ? '`json:"' ~ f.name ~ '"`'}} {%- endfor ~%} + +{%if has_token(o, 'bitfields') %} +fieldsMask meta.FieldsMask //@bitfields support +{%- endif ~%} + {% endmacro %} {% macro decl_enum(o) %}