This commit is contained in:
g.sadovnikov 2022-12-16 16:22:43 +03:00
commit eda5f475a3
3 changed files with 41 additions and 28 deletions

View File

@ -2,10 +2,22 @@ This package is used for code generation of bhl2 bindings for C# using Twig temp
Usage example:
$twig = \bhl_bind\bhl_twig();
$twig = \bhl_bind\get_twig();
$meta = \bhl_bind\prepare_meta($meta);
file_put_contents('autobind.cs',
$twig->render("codegen_register.twig",
['imports' => ['UnityEngine'],
'units' => $meta->getUnits()]
$twig->render("codegen_autobind.twig",
[
'imports' => ['UnityEngine'],
'meta' => $meta
]
)
);
file_put_contents('types.cs',
$twig->render("codegen_types.twig",
[
'meta' => $meta
]
)
);

View File

@ -4,7 +4,8 @@
"homepage": "https://git.bit5.ru/bit/metagen_bhl_bind",
"require": {
"php": ">=7.4",
"twig/twig" : "v3.4.3"
"twig/twig" : "v3.4.3",
"bit/metagen" : "^v2.0.2"
},
"autoload": {
"files": [

View File

@ -2,7 +2,7 @@
namespace bhl_bind;
use Exception;
function bhl_twig(array $inc_path = [])
function get_twig(array $inc_path = [])
{
array_unshift($inc_path, __DIR__ . "/../tpl/");
$loader = new \Twig\Loader\FilesystemLoader($inc_path);
@ -14,7 +14,7 @@ function bhl_twig(array $inc_path = [])
);
$twig->addExtension(new \Twig\Extension\DebugExtension());
bhl_add_twig_support($twig);
add_twig_support($twig);
return $twig;
}
@ -43,7 +43,7 @@ function supported_tokens()
];
}
function bhl_prepare_meta(\mtgMetaInfo $orig)
function prepare_meta(\mtgMetaInfo $orig)
{
//NOTE: making a deep clone
$meta = unserialize(serialize($orig));
@ -93,7 +93,7 @@ function bhl_prepare_meta(\mtgMetaInfo $orig)
return $meta;
}
function bhl_add_twig_support(\Twig\Environment $twig)
function add_twig_support(\Twig\Environment $twig)
{
$twig->addTest(new \Twig\TwigTest('instanceof',
function($obj, $class)
@ -104,37 +104,37 @@ function bhl_add_twig_support(\Twig\Environment $twig)
$twig->addFilter(new \Twig\TwigFilter('ns_last',
function($str)
{
return bhl_get_last_name($str);
return ns_last($str);
}
));
$twig->addFilter(new \Twig\TwigFilter('ns_prefix',
function($str)
{
return bhl_get_ns_prefix($str);
return ns_prefix($str);
}
));
$twig->addFilter(new \Twig\TwigFilter('bhl_type',
function($type)
{
return bhl_gen_type($type);
return bhl_type($type);
}
));
$twig->addFilter(new \Twig\TwigFilter('bhl_type_ref',
function($type)
{
return bhl_type_ref(is_string($type) ? $type : bhl_gen_type($type));
return bhl_type_ref(is_string($type) ? $type : bhl_type($type));
}
));
$twig->addFilter(new \Twig\TwigFilter('native_type',
function($type)
{
return bhl_gen_native_type($type);
return native_type($type);
}
));
$twig->addFilter(new \Twig\TwigFilter('norm_name',
function($name)
{
return bhl_normalize_name($name);
return norm_name($name);
}
));
$twig->addFilter(new \Twig\TwigFilter('rtrim_comma',
@ -223,18 +223,13 @@ function bhl_add_twig_support(\Twig\Environment $twig)
));
}
function bhl_get_last_name($name)
function ns_last($name)
{
$items = explode('.', $name);
return end($items);
}
function bhl_normalize_name($name)
{
return str_replace('.', '__', $name);
}
function bhl_get_ns_prefix($name)
function ns_prefix($name)
{
$items = explode('.', $name);
if(sizeof($items) == 1)
@ -247,6 +242,11 @@ function bhl_get_ns_prefix($name)
return $prefix;
}
function norm_name($name)
{
return str_replace('.', '__', $name);
}
function bhl_type_ref($type_str)
{
if(preg_match('~\[\]((\w|\.)+)~', $type_str, $ms))
@ -263,7 +263,7 @@ function bhl_type_ref($type_str)
return "types.T(\"$type_str\")";
}
function bhl_gen_type(\mtgType $type = null)
function bhl_type(\mtgType $type = null)
{
if($type === null)
return "void";
@ -282,7 +282,7 @@ function bhl_gen_type(\mtgType $type = null)
$str = "";
$str .= "func $ret_type(";
foreach($type->getArgs() as $arg)
$str .= bhl_gen_type($arg->getType()) . ",";
$str .= bhl_type($arg->getType()) . ",";
$str = rtrim($str, ',');
$str .= ")";
return $str;
@ -291,16 +291,16 @@ function bhl_gen_type(\mtgType $type = null)
{
$str = '';
foreach($type->getValues() as $sub_type)
$str .= bhl_gen_type($sub_type) . ",";
$str .= bhl_type($sub_type) . ",";
return rtrim($str, ",");
}
else if($type instanceof \mtgArrType)
return "[]" . bhl_gen_type($type->getValue());
return "[]" . bhl_type($type->getValue());
return ''.$type;
}
function bhl_gen_native_type(\mtgType $type)
function native_type(\mtgType $type)
{
//any special case
if($type == "any")
@ -311,7 +311,7 @@ function bhl_gen_native_type(\mtgType $type)
if($type->getValue()->getName() == "any")
return "bhl.ValList";
else
return "HList<".bhl_gen_native_type($type->getValue()).">";
return "HList<".native_type($type->getValue()).">";
}
//func special case