From a91a1ec515166e29c838237a2c87ad41b0db8596 Mon Sep 17 00:00:00 2001 From: "a.chubar" Date: Fri, 18 Apr 2025 17:44:40 +0300 Subject: [PATCH] Using a dedicated wrapper for i18n strings to streamline the generated code --- CHANGELOG.md | 3 +++ src/codegen.inc.php | 20 ++++++++++++++++---- tpl/macro.twig | 44 +++++++++++++++++++------------------------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3c1b34..228efef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## v7.0.0 +- Using a dedicated wrapper for i18n strings to streamline the generated code + ## v6.1.0 - Removed redundant accessor interface declarations in generated slices diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 7996a9e..68db43e 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -137,9 +137,9 @@ function _add_twig_support(\Twig\Environment $twig) } )); $twig->addFilter(new \Twig\TwigFilter('cs_type', - function($type) + function($type, $fld = null) { - return cs_type($type); + return cs_type($type, $fld); } )); $twig->addFilter(new \Twig\TwigFilter('cs_simple_type', @@ -213,10 +213,14 @@ function _add_twig_support(\Twig\Environment $twig) )); } -function cs_type(\mtgType $type) +function cs_type(\mtgType $type, $fld = null) { if($type instanceof \mtgBuiltinType || $type instanceof \mtgUserType) + { + if($fld != null && $fld->hasToken("flt_i18n")) + return "MetaI18NString"; return cs_simple_type($type); + } else if($type instanceof \mtgArrType) return "List<" . cs_simple_type($type->getValue()) . ">"; else @@ -451,7 +455,10 @@ function var_sync($fname, \mtgType $type, $buf, array $tokens, $opts) $str = ''; if($type instanceof \mtgBuiltinType) { - $str .= "MetaIO.Sync({$buf}, ref {$fname}, \"{$key_name}\", {$opts});\n"; + if($type->isString() && array_key_exists('flt_i18n', $tokens)) + $str .= "$fname.SyncSingular({$buf}, \"{$key_name}\", {$opts});\n"; //TODO: plurals support + else + $str .= "MetaIO.Sync({$buf}, ref {$fname}, \"{$key_name}\", {$opts});\n"; } else if($type instanceof \mtgMetaStruct) { @@ -605,6 +612,11 @@ class AccessorInterfaceField return $this->field->getName(); } + function hasToken($token) + { + return $this->field->hasToken($token); + } + function getCamelFieldName() { $name_parts = explode("_", $this->field->getName()); diff --git a/tpl/macro.twig b/tpl/macro.twig index 60c094f..7510c86 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -156,20 +156,19 @@ public FieldsMask fields_mask; {%- macro decl_struct_field(o, f) -%} {{_self.attributes(f)}} -{% if has_token(f, 'flt_i18n') -%} -{{f.type}} _{{f.name}} = ""; -List<{{f.type}}> __{{f.name}} = new List<{{f.type}}>(); -{%- endif ~%} -public {{f.type|cs_type|obscure_type(f)}} {{f.name}} {% if not has_token(o, 'POD') -%} {{_self.decl_init_value(f)}} {%- endif -%} {% if not has_token(f, 'flt_i18n') -%};{%- endif -%} +public {{f.type|cs_type(f)|obscure_type(f)}} {{f.name}} {% if not has_token(o, 'POD') -%} {{_self.decl_init_value(f)}} {%- endif -%}; {%- endmacro -%} {% macro decl_init_value(f) %} {%- if f.type is instanceof('\\mtgBuiltinType') -%} {%- if f.type.isstring -%} -{% if has_token(f, 'flt_i18n') -%} -{get{return plural_{{f.name}}(double.NaN);}set{_{{f.name}}=value;}} -{{f.type}} plural_{{f.name}}(double force_n){return MetaI18N.I18NPick(_{{f.name}},__{{f.name}},"#",force_n);} -{%- else -%} + {% if has_token(f, 'flt_i18n') -%} + {%- if has_token(f, 'default') -%} += new({{token(f, 'default')}}) + {%- else -%} += new() + {%- endif -%} + {%- else -%} = "" {%- endif ~%} {%- endif -%} @@ -177,7 +176,7 @@ public {{f.type|cs_type|obscure_type(f)}} {{f.name}} {% if not has_token(o, 'POD {%- if has_token(f, 'default') and token(f, 'default') == 'null' -%} /*null*/ {%- else -%} -= new {{f.type|cs_type}}() += new {{f.type|cs_type(f)}}() {%- endif -%} {%- endif -%} {% endmacro %} @@ -187,12 +186,11 @@ public {{f.type|cs_type|obscure_type(f)}} {{f.name}} {% if not has_token(o, 'POD base.Reset(); {%- endif -%} {%- for f in o.fields ~%} -{% set fname = f.name %} {% if has_token(f, 'flt_i18n') -%} -{% set fname = '_' ~ f.name %} -if(_{{fname}} == null) _{{fname}} = new(); else _{{fname}}.Clear(); +{{f.name}}?.Reset(); +{%- else ~%} +{{var_reset(f.name, f.type, token_or(f, 'default', null))}} {%- endif ~%} -{{var_reset(fname, f.type, token_or(f, 'default', null))}} {%- endfor -%} {%- if has_token(o, 'bitfields') ~%} ResetFieldMask(); @@ -291,11 +289,7 @@ bitctx.SyncMaskHeader(); base.SyncFields(ctx); {%- endif -%} {%- for f in o.fields ~%} -{% set fname = f.name %} -{% if has_token(f, 'flt_i18n') -%} -{% set fname = '_' ~ f.name %} -{%- endif -%} -{{var_sync(fname, f.type, 'ctx', f.tokens, get_sync_opts(o, 'bitctx'))}} +{{var_sync(f.name, f.type, 'ctx', f.tokens, get_sync_opts(o, 'bitctx'))}} {%- endfor -%} {%- endmacro -%} @@ -750,12 +744,12 @@ public interface {{ai.interfacename}} { {%- for aif in ai.fields ~%} {% if aif.is_accessor -%} - {{aif.field.type|cs_type}} Get{{aif.camelfieldname}}(); - void Set{{aif.camelfieldname}}({{aif.field.type|cs_type}} v); + {{aif.field.type|cs_type(aif)}} Get{{aif.camelfieldname}}(); + void Set{{aif.camelfieldname}}({{aif.field.type|cs_type(aif)}} v); {% endif -%} {%- if aif.is_propget -%} - {{aif.field.type|cs_type}} {{aif.camelfieldname}} { + {{aif.field.type|cs_type(aif)}} {{aif.camelfieldname}} { {%- if aif.is_propget -%} get; {%- endif %} @@ -772,18 +766,18 @@ public interface {{ai.interfacename}} {%- for ai in get_accessor_interfaces(o) ~%} {%- for aif in ai.fields ~%} {% if aif.is_accessor %} - public {{aif.field.type|cs_type}} Get{{aif.camelfieldname}}() + public {{aif.field.type|cs_type(aif)}} Get{{aif.camelfieldname}}() { return {{aif.field.name}}; } - public void Set{{aif.camelfieldname}}({{aif.field.type|cs_type}} v) + public void Set{{aif.camelfieldname}}({{aif.field.type|cs_type(aif)}} v) { this.{{aif.field.name}} = v; } {% endif %} {% if aif.is_propget or aif.is_propset %} - public {{aif.field.type|cs_type}} {{aif.camelfieldname}} { + public {{aif.field.type|cs_type(aif)}} {{aif.camelfieldname}} { {%- if aif.is_propget -%} get => this.{{aif.field.name}}; {%- endif -%}