diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 33c498a..dc57829 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -82,6 +82,26 @@ function _add_twig_support(\Twig\Environment $twig) return $v; } )); + $twig->addFunction(new \Twig\TwigFunction('field_index', + function(\mtgMetaStruct $s, string $f): int + { + $idx = get_field_index($s, $f); + if($idx == -1) + { + throw new Exception("field `$f` not found in `{$s->getName()}`"); + } + + return $idx; + } + )); + $twig->addFunction(new \Twig\TwigFunction('find_struct', + function(string $name) use ($twig): \mtgMetaStruct + { + $meta = $twig->getGlobals()['meta']; + $unit = $meta->findUnit($name); + return $unit->object; + } + )); $twig->addFilter(new \Twig\TwigFilter('cs_type', function($type) { @@ -619,3 +639,16 @@ function get_all_declarable_accessor_interfaces(\mtgMetaInfo $info) return $all; } +function get_field_index(\mtgMetaStruct $struct, string $field_name): int +{ + $idx = -1; + foreach($struct->getFields() as $field) + { + $idx++; + if($field->getName() == $field_name) + { + return $idx; + } + } + return -1; +} diff --git a/tpl/macro.twig b/tpl/macro.twig index e44a9a9..64d8d3c 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -395,7 +395,7 @@ public class {{o.name}} : IRpc {% set field_idx = field_idx + 1 %} {%- if not has_token(f, 'nodiff') -%} - {{_self.field_diff(f, field_idx)}} + {{_self.field_diff(f, field_idx, o)}} {%- endif -%} {% endfor %} @@ -561,7 +561,7 @@ public class {{o.name}} : IRpc {% endmacro %} -{%- macro field_diff(f, field_idx) -%} +{%- macro field_diff(f, field_idx, o) -%} {% if f.type is instanceof('\\mtgMetaStruct') %} {%- if not has_token(f.type, 'POD') -%} @@ -594,7 +594,7 @@ public class {{o.name}} : IRpc if(diff != null) MetaIO.SetFieldDirty(ref diff.fields_mask, {{field_idx}}); if(removed != null && removed.{{f.name}}.Count > 0) - MetaIO.SetFieldDirty(ref removed.fields_mask, {{field_idx}}); + MetaIO.SetFieldDirty(ref removed.fields_mask, {{ field_index(find_struct(o.name ~ 'RemovedIds'), f.name) }}); } {% elseif f.type is instanceof('\\mtgBuiltinType') %} if(diff != null)