From 70c6c66f2d037cc50955f9eb2d6a7704b47f0df4 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 8 Dec 2022 14:50:32 +0300 Subject: [PATCH] Gradually adding support for all features --- src/codegen.inc.php | 10 ++++++++++ tpl/macro.twig | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 1c1e495..441f567 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -86,6 +86,16 @@ function _add_twig_support(\Twig\Environment $twig) return \mtg_get_all_fields($meta, $o); } )); + $twig->addFunction(new \Twig\TwigFunction('count_optional', + function($units) + { + $opts = 0; + foreach($units as $u) + if($u->hasToken('optional')) + ++$opts; + return $opts; + } + )); $twig->addFilter(new \Twig\TwigFilter('go_type', function($type, $tokens) { diff --git a/tpl/macro.twig b/tpl/macro.twig index ce3c784..9a23fc7 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -11,6 +11,12 @@ {% endmacro %} {% macro decl_struct(meta, o) %} +{%- if o.parent and has_token(o, 'POD') -%} +{{Error("@POD structs can't have a parent: " ~ o.name)}} +{%- endif -%} +{%- if o.parent and has_token(o, 'bitfields') -%} +{{Error("@bitfields structs can't have a parent: " ~ o.name)}} +{%- endif ~%} //============================== type {{o.name}} struct { @@ -33,7 +39,7 @@ type I{{o.name}} interface { Ptr{{o.name}}() *{{o.name}} } func (*{{o.name}}) CLASS_ID() uint32 { - return %class_id% + return {{o.classid}} } func (*{{o.name}}) CLASS_NAME() string { return "{{o.name}}" @@ -71,7 +77,9 @@ func (self *{{o.name}}) Read(reader meta.Reader) error { } func (self *{{o.name}}) ReadFields(reader meta.Reader) error { self.Reset() - %read_buffer% + + {{_self.struct_read_fields(o)}} + return nil } func (self *{{o.name}}) Write(writer meta.Writer) error { @@ -121,6 +129,31 @@ fieldsMask meta.FieldsMask //@bitfields support {% endmacro %} +{% macro struct_read_fields(o) %} + +{%if has_token(o, 'bitfields') %} + use_mask, mask, err := reader.TryReadMask() + if err != nil { + return err + } + self.fieldsMask = mask +{%- endif ~%} + + _cont_size, err := reader.GetContainerSize() + if err != nil { + return err + } + + if _cont_size < {{o.fields|length - count_optional(o.fields)}} { + _cont_size = {{o.fields|length - count_optional(o.fields)}} + } + +{%- if o.parent ~%} + if err := self.{{o.parent.name}}.ReadFields(reader); err != nil { return err } +{%- endif -%} + +{% endmacro %} + {% macro decl_enum(o) %} //==============================