Compare commits
8 Commits
Author | SHA1 | Date |
---|---|---|
|
da88e6b8db | |
|
1ae22cb86d | |
|
1fe32aecb3 | |
|
5cca57b440 | |
|
32bfa591d7 | |
|
02a5871f89 | |
|
d083ee09bc | |
|
6146771ce7 |
|
@ -1,3 +1,12 @@
|
|||
## 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
|
||||
|
||||
|
|
|
@ -2,9 +2,10 @@ This package is used for code generation of C# meta structs using Twig templates
|
|||
|
||||
Usage example:
|
||||
|
||||
$code = \metagen_cs\codegen(null, get_meta(),
|
||||
$output = \metagen_cs\codegen(null, get_meta(),
|
||||
[
|
||||
'namespace' => 'BitGames.Autogen'
|
||||
]);
|
||||
file_put_contents('bundle.cs', $code);
|
||||
foreach($output as $name => $text)
|
||||
file_put_contents($name, $text);
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
namespace metagen_cs;
|
||||
use Exception;
|
||||
|
||||
function codegen(?string $cache_dir, \mtgMetaInfo $meta, array $options = []) : string
|
||||
function codegen(?string $cache_dir, \mtgMetaInfo $meta, array $options = []) : array
|
||||
{
|
||||
$twig = get_twig();
|
||||
|
||||
|
@ -15,7 +15,21 @@ function codegen(?string $cache_dir, \mtgMetaInfo $meta, array $options = []) :
|
|||
if(!isset($options['namespace']))
|
||||
$options['namespace'] = 'BitGames.Autogen';
|
||||
|
||||
return $twig->render('codegen_bundle.twig', $options);
|
||||
$sliced_units = slice_units($meta->getUnits(), 50);
|
||||
|
||||
$output = array();
|
||||
|
||||
foreach($sliced_units as $slice_idx => $slice_units)
|
||||
{
|
||||
$slice_options = $options;
|
||||
$slice_options['slice_idx'] = $slice_idx;
|
||||
$slice_options['slice_units'] = $slice_units;
|
||||
$output['types_'.$slice_idx.'.cs'] = $twig->render('codegen_types_slice.twig', $slice_options);
|
||||
}
|
||||
|
||||
$output['factory.cs'] = $twig->render('codegen_factory.twig', $options);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
function get_twig(array $inc_path = []) : \Twig\Environment
|
||||
|
@ -128,6 +142,15 @@ function _add_twig_support(\Twig\Environment $twig)
|
|||
return cs_type($type);
|
||||
}
|
||||
));
|
||||
$twig->addFilter(new \Twig\TwigFilter('cs_simple_type',
|
||||
function($type)
|
||||
{
|
||||
if($type instanceof \mtgArrType)
|
||||
return cs_simple_type($type->getValue());
|
||||
else
|
||||
return cs_simple_type($type);
|
||||
}
|
||||
));
|
||||
$twig->addFilter(new \Twig\TwigFilter('cs_type_prefix',
|
||||
function($type)
|
||||
{
|
||||
|
@ -233,7 +256,7 @@ function cs_simple_type(\mtgType $type)
|
|||
case "bool":
|
||||
return "bool";
|
||||
case "blob":
|
||||
return "byte[]";
|
||||
return "ArraySegment<byte>";
|
||||
}
|
||||
throw new Exception("Unknown type '{$type}'");
|
||||
}
|
||||
|
@ -344,7 +367,7 @@ function var_reset($name, \mtgType $type, $default = null)
|
|||
$str .= " = ".trim($default, '"').";";
|
||||
}
|
||||
else
|
||||
$str .= ' = null;';
|
||||
$str .= ' = default;';
|
||||
}
|
||||
else
|
||||
throw new Exception("Unknown type '$type'");
|
||||
|
@ -396,16 +419,16 @@ function var_reset($name, \mtgType $type, $default = null)
|
|||
|
||||
if($default)
|
||||
{
|
||||
$default = is_array($default) ? $default : json_decode($default, true);
|
||||
if(is_array($default))
|
||||
$default_val = is_array($default) ? $default : json_decode($default, true);
|
||||
if(is_array($default_val))
|
||||
{
|
||||
foreach($default as $k => $v)
|
||||
foreach($default_val as $k => $v)
|
||||
{
|
||||
$kf = $type->getField($k);
|
||||
$str .= var_reset("$name." . $kf->getName(), $kf->getType(), $v);
|
||||
}
|
||||
}
|
||||
else if($default === null)
|
||||
else if(is_null_str($default))
|
||||
$str .= "$name = null; ";
|
||||
else
|
||||
throw new Exception("Bad default value for struct: " . var_export($default, true));
|
||||
|
@ -418,7 +441,7 @@ function var_reset($name, \mtgType $type, $default = null)
|
|||
|
||||
function is_null_str($default)
|
||||
{
|
||||
return is_string($default) && json_decode($default, true) === null;
|
||||
return is_string($default) && strtolower($default) === 'null';
|
||||
}
|
||||
|
||||
function var_sync($fname, \mtgType $type, $buf, array $tokens, $opts)
|
||||
|
@ -704,3 +727,41 @@ function get_field_index(\mtgMetaStruct $struct, string $field_name): int
|
|||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
function paginate($total, $step)
|
||||
{
|
||||
//pages are returned as an array where each element is in interval [N, Y)
|
||||
$pages = array();
|
||||
|
||||
$steps = (int)($total/$step);
|
||||
$rest = $total % $step;
|
||||
|
||||
for($i=1;$i<=$steps;++$i)
|
||||
$pages[] = array(($i-1)*$step, $i*$step);
|
||||
|
||||
if($rest != 0)
|
||||
$pages[] = array(($i-1)*$step, ($i-1)*$step + $rest);
|
||||
|
||||
return $pages;
|
||||
}
|
||||
|
||||
//slices array like this: [[idx0,[..]], [idx1,[..]], ...]
|
||||
function slice_units(array $units, $max)
|
||||
{
|
||||
$pages = paginate(sizeof($units), $max);
|
||||
|
||||
$sliced = array();
|
||||
|
||||
$units_keys = array_keys($units);
|
||||
|
||||
foreach($pages as $idx => $page)
|
||||
{
|
||||
$slice = array();
|
||||
for($i = $page[0];$i<$page[1];++$i)
|
||||
{
|
||||
$slice[] = $units[$units_keys[$i]];
|
||||
}
|
||||
$sliced[$idx] = $slice;
|
||||
}
|
||||
return $sliced;
|
||||
}
|
||||
|
|
|
@ -9,7 +9,9 @@ using System;
|
|||
namespace {{namespace}} {
|
||||
{% endif %}
|
||||
|
||||
{{ macro.decl_units(meta) }}
|
||||
{%- for ai in get_all_declarable_accessor_interfaces(meta) ~%}
|
||||
{{ macro.decl_accessor_interface(ai) }}
|
||||
{%- endfor ~%}
|
||||
|
||||
static public class AutogenBundle
|
||||
{
|
|
@ -0,0 +1,16 @@
|
|||
//THIS FILE IS GENERATED AUTOMATICALLY, DO NOT TOUCH IT!
|
||||
using System.Collections.Generic;
|
||||
using metagen;
|
||||
using System;
|
||||
|
||||
{%- import "macro.twig" as macro -%}
|
||||
|
||||
{% if namespace is defined ~%}
|
||||
namespace {{namespace}} {
|
||||
{% endif %}
|
||||
|
||||
{{ macro.decl_units(slice_units) }}
|
||||
|
||||
{% if namespace is defined ~%}
|
||||
} //namespace {{namespace}}
|
||||
{% endif %}
|
|
@ -1,6 +1,6 @@
|
|||
{% macro decl_units(meta) %}
|
||||
{% macro decl_units(units) %}
|
||||
|
||||
{%- for u in meta.getunits ~%}
|
||||
{%- for u in units ~%}
|
||||
{%- if u.object is instanceof('\\mtgMetaStruct') -%}
|
||||
{{ _self.decl_struct(u.object) }}
|
||||
{%- elseif u.object is instanceof('\\mtgMetaEnum') -%}
|
||||
|
@ -10,10 +10,6 @@
|
|||
{%- endif ~%}
|
||||
{%- endfor ~%}
|
||||
|
||||
{%- for ai in get_all_declarable_accessor_interfaces(meta) ~%}
|
||||
{{ _self.decl_accessor_interface(ai) }}
|
||||
{%- endfor ~%}
|
||||
|
||||
{% endmacro %}
|
||||
|
||||
{% macro decl_struct(o, extra = '') %}
|
||||
|
|
Loading…
Reference in New Issue