From e0a488a42b74f0609b399cce855213477b3a810e Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 14 Dec 2022 16:39:10 +0300 Subject: [PATCH 1/4] Experimenting with composer deps --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 5871cd1..bae2bab 100644 --- a/composer.json +++ b/composer.json @@ -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": [ From 2ab3c3c6555b07021228a92c34b4ba30db438e10 Mon Sep 17 00:00:00 2001 From: ps Date: Thu, 15 Dec 2022 00:43:54 +0300 Subject: [PATCH 2/4] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'README.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3dc937e..c7632ac 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,10 @@ This package is used for code generation of bhl2 bindings for C# using Twig temp Usage example: $twig = \bhl_bind\bhl_twig(); + $meta = \bhl_bind\bhl_prepare_meta($meta); file_put_contents('autobind.cs', - $twig->render("codegen_register.twig", + $twig->render("codegen_autobind.twig", ['imports' => ['UnityEngine'], - 'units' => $meta->getUnits()] + 'meta' => $meta] ) ); \ No newline at end of file From 8f7ca8119a04e77082110f1c9a02b8c135f4a41c Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 15 Dec 2022 12:18:12 +0300 Subject: [PATCH 3/4] More consitent naming --- src/bind.inc.php | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/src/bind.inc.php b/src/bind.inc.php index 0bb8224..e282013 100644 --- a/src/bind.inc.php +++ b/src/bind.inc.php @@ -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 From 76d3002e682dddc2a4033688932037428b5a56bf Mon Sep 17 00:00:00 2001 From: ps Date: Thu, 15 Dec 2022 20:54:54 +0300 Subject: [PATCH 4/4] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB(?= =?UTF-8?q?=D0=B0)=20=D0=BD=D0=B0=20'README.md'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c7632ac..290e66c 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,22 @@ This package is used for code generation of bhl2 bindings for C# using Twig temp Usage example: - $twig = \bhl_bind\bhl_twig(); - $meta = \bhl_bind\bhl_prepare_meta($meta); + $twig = \bhl_bind\get_twig(); + $meta = \bhl_bind\prepare_meta($meta); + file_put_contents('autobind.cs', $twig->render("codegen_autobind.twig", - ['imports' => ['UnityEngine'], - 'meta' => $meta] + [ + 'imports' => ['UnityEngine'], + 'meta' => $meta + ] + ) + ); + + file_put_contents('types.cs', + $twig->render("codegen_types.twig", + [ + 'meta' => $meta + ] ) ); \ No newline at end of file