Compare commits

..

No commits in common. "master" and "v5.0.0" have entirely different histories.

4 changed files with 50 additions and 59 deletions

View File

@ -1,18 +1,5 @@
## v7.1.0
- Using a shared helper method to streamline the generated code
## 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
## v5.0.0
- Splitting one huge codegenerated file into multiple ones
## v4.10.0
- Added @flt_i18n token to support translations
## v4.9.2
- Added @cs_obsolete token, that mark fields with [Obsolete]. @cs_obsolete:"Comment" will add summary with "Comment" to field

View File

@ -2,10 +2,9 @@ This package is used for code generation of C# meta structs using Twig templates
Usage example:
$output = \metagen_cs\codegen(null, get_meta(),
$code = \metagen_cs\codegen(null, get_meta(),
[
'namespace' => 'BitGames.Autogen'
]);
foreach($output as $name => $text)
file_put_contents($name, $text);
file_put_contents('bundle.cs', $code);

View File

@ -137,9 +137,9 @@ function _add_twig_support(\Twig\Environment $twig)
}
));
$twig->addFilter(new \Twig\TwigFilter('cs_type',
function($type, $fld = null)
function($type)
{
return cs_type($type, $fld);
return cs_type($type);
}
));
$twig->addFilter(new \Twig\TwigFilter('cs_simple_type',
@ -213,14 +213,10 @@ function _add_twig_support(\Twig\Environment $twig)
));
}
function cs_type(\mtgType $type, $fld = null)
function cs_type(\mtgType $type)
{
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
@ -260,7 +256,7 @@ function cs_simple_type(\mtgType $type)
case "bool":
return "bool";
case "blob":
return "ArraySegment<byte>";
return "byte[]";
}
throw new Exception("Unknown type '{$type}'");
}
@ -371,7 +367,7 @@ function var_reset($name, \mtgType $type, $default = null)
$str .= " = ".trim($default, '"').";";
}
else
$str .= ' = default;';
$str .= ' = null;';
}
else
throw new Exception("Unknown type '$type'");
@ -423,16 +419,16 @@ function var_reset($name, \mtgType $type, $default = null)
if($default)
{
$default_val = is_array($default) ? $default : json_decode($default, true);
if(is_array($default_val))
$default = is_array($default) ? $default : json_decode($default, true);
if(is_array($default))
{
foreach($default_val as $k => $v)
foreach($default as $k => $v)
{
$kf = $type->getField($k);
$str .= var_reset("$name." . $kf->getName(), $kf->getType(), $v);
}
}
else if(is_null_str($default))
else if($default === null)
$str .= "$name = null; ";
else
throw new Exception("Bad default value for struct: " . var_export($default, true));
@ -445,7 +441,7 @@ function var_reset($name, \mtgType $type, $default = null)
function is_null_str($default)
{
return is_string($default) && strtolower($default) === 'null';
return is_string($default) && json_decode($default, true) === null;
}
function var_sync($fname, \mtgType $type, $buf, array $tokens, $opts)
@ -455,10 +451,7 @@ function var_sync($fname, \mtgType $type, $buf, array $tokens, $opts)
$str = '';
if($type instanceof \mtgBuiltinType)
{
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";
$str .= "MetaIO.Sync({$buf}, ref {$fname}, \"{$key_name}\", {$opts});\n";
}
else if($type instanceof \mtgMetaStruct)
{
@ -612,11 +605,6 @@ class AccessorInterfaceField
return $this->field->getName();
}
function hasToken($token)
{
return $this->field->hasToken($token);
}
function getCamelFieldName()
{
$name_parts = explode("_", $this->field->getName());

View File

@ -10,6 +10,10 @@
{%- endif ~%}
{%- endfor ~%}
{%- for ai in get_all_declarable_accessor_interfaces(meta) ~%}
{{ _self.decl_accessor_interface(ai) }}
{%- endfor ~%}
{% endmacro %}
{% macro decl_struct(o, extra = '') %}
@ -156,19 +160,20 @@ public FieldsMask fields_mask;
{%- macro decl_struct_field(o, f) -%}
{{_self.attributes(f)}}
public {{f.type|cs_type(f)|obscure_type(f)}} {{f.name}} {% if not has_token(o, 'POD') -%} {{_self.decl_init_value(f)}} {%- endif -%};
{% 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 -%}
{%- endmacro -%}
{% macro decl_init_value(f) %}
{%- if f.type is instanceof('\\mtgBuiltinType') -%}
{%- if f.type.isstring -%}
{% if has_token(f, 'flt_i18n') -%}
{%- if has_token(f, 'default') -%}
= new({{token(f, 'default')}})
{%- else -%}
= new()
{%- endif -%}
{%- else -%}
{% 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 -%}
= ""
{%- endif ~%}
{%- endif -%}
@ -176,7 +181,7 @@ public {{f.type|cs_type(f)|obscure_type(f)}} {{f.name}} {% if not has_token(o, '
{%- if has_token(f, 'default') and token(f, 'default') == 'null' -%}
/*null*/
{%- else -%}
= new {{f.type|cs_type(f)}}()
= new {{f.type|cs_type}}()
{%- endif -%}
{%- endif -%}
{% endmacro %}
@ -186,11 +191,12 @@ public {{f.type|cs_type(f)|obscure_type(f)}} {{f.name}} {% if not has_token(o, '
base.Reset();
{%- endif -%}
{%- for f in o.fields ~%}
{% set fname = f.name %}
{% if has_token(f, 'flt_i18n') -%}
{{f.name}}?.Reset();
{%- else ~%}
{{var_reset(f.name, f.type, token_or(f, 'default', null))}}
{% set fname = '_' ~ f.name %}
if(_{{fname}} == null) _{{fname}} = new(); else _{{fname}}.Clear();
{%- endif ~%}
{{var_reset(fname, f.type, token_or(f, 'default', null))}}
{%- endfor -%}
{%- if has_token(o, 'bitfields') ~%}
ResetFieldMask();
@ -289,7 +295,11 @@ bitctx.SyncMaskHeader();
base.SyncFields(ctx);
{%- endif -%}
{%- for f in o.fields ~%}
{{var_sync(f.name, f.type, 'ctx', f.tokens, get_sync_opts(o, 'bitctx'))}}
{% 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'))}}
{%- endfor -%}
{%- endmacro -%}
@ -319,7 +329,14 @@ base.SyncFields(ctx);
int fields_amount = {{fields_count(o)}};
var primary_id_mask = FieldsMask.MakeClean(fields_amount);
SetPrimaryFieldsChanged(ref primary_id_mask);
return FieldsMask.CheckContentsChanged(fields_amount, ref primary_id_mask, ref fields_mask);
for(int i = 0; i < fields_amount; i++)
{
bool is_primary_id = primary_id_mask.IsDirty(i);
if(!is_primary_id && fields_mask.IsDirty(i))
return true;
}
return false;
}
public void SetDirtyMask()
@ -737,12 +754,12 @@ public interface {{ai.interfacename}}
{
{%- for aif in ai.fields ~%}
{% if aif.is_accessor -%}
{{aif.field.type|cs_type(aif)}} Get{{aif.camelfieldname}}();
void Set{{aif.camelfieldname}}({{aif.field.type|cs_type(aif)}} v);
{{aif.field.type|cs_type}} Get{{aif.camelfieldname}}();
void Set{{aif.camelfieldname}}({{aif.field.type|cs_type}} v);
{% endif -%}
{%- if aif.is_propget -%}
{{aif.field.type|cs_type(aif)}} {{aif.camelfieldname}} {
{{aif.field.type|cs_type}} {{aif.camelfieldname}} {
{%- if aif.is_propget -%}
get;
{%- endif %}
@ -759,18 +776,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(aif)}} Get{{aif.camelfieldname}}()
public {{aif.field.type|cs_type}} Get{{aif.camelfieldname}}()
{
return {{aif.field.name}};
}
public void Set{{aif.camelfieldname}}({{aif.field.type|cs_type(aif)}} v)
public void Set{{aif.camelfieldname}}({{aif.field.type|cs_type}} v)
{
this.{{aif.field.name}} = v;
}
{% endif %}
{% if aif.is_propget or aif.is_propset %}
public {{aif.field.type|cs_type(aif)}} {{aif.camelfieldname}} {
public {{aif.field.type|cs_type}} {{aif.camelfieldname}} {
{%- if aif.is_propget -%}
get => this.{{aif.field.name}};
{%- endif -%}