From f7d0b5f61f1fe344283d8b49406b807a6f640bf8 Mon Sep 17 00:00:00 2001 From: "a.chubar" Date: Thu, 19 Oct 2023 12:54:37 +0400 Subject: [PATCH] cs_accessor_interface (and similar tokens) now support pre-existing C# interfaces --- src/codegen.inc.php | 37 +++++++++++++++++++++++++++++++------ tpl/macro.twig | 2 +- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/codegen.inc.php b/src/codegen.inc.php index d82288d..33c498a 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -136,10 +136,10 @@ function _add_twig_support(\Twig\Environment $twig) return get_diff_related_units($o); } )); - $twig->addFunction(new \Twig\TwigFunction('get_all_accessor_interfaces', + $twig->addFunction(new \Twig\TwigFunction('get_all_declarable_accessor_interfaces', function($o) { - return get_all_accessor_interfaces($o); + return get_all_declarable_accessor_interfaces($o); } )); $twig->addFunction(new \Twig\TwigFunction('get_accessor_interfaces', @@ -536,9 +536,26 @@ class AccessorInterface } } +//NOTE: by default, accessor interface declarations will be generated in the autogen bundle. +//If you want your metagen struct to implement some existing C# inteface +//use quotes and prefix the fully-qualified interface name with "!", e.g. like this: +// +// ticket : string @cs_accessor_interface:"!BitGames.ServerIntegration.IRpcWithAuth" +// function get_accessor_interfaces(\mtgMetaStruct $struct) +{ + return get_accessor_interfaces_ex($struct, /* declarable_only = */ false); +} + +function get_accessor_interfaces_declarable(\mtgMetaStruct $struct) +{ + return get_accessor_interfaces_ex($struct, /* declarable_only = */ true); +} + +function get_accessor_interfaces_ex(\mtgMetaStruct $struct, $declarable_only) { $ifs = array(); + $external_mark = '"!'; foreach($struct->getFields() as $field) { $is_accessor = $field->hasToken('cs_accessor_interface'); @@ -558,6 +575,14 @@ function get_accessor_interfaces(\mtgMetaStruct $struct) elseif($is_propgetset) $ai->cs_interface_name = $field->getToken('cs_propgetset_interface'); + $is_external = strpos($ai->cs_interface_name, $external_mark) === 0; + if($is_external) + { + $ai->cs_interface_name = trim($ai->cs_interface_name, $external_mark); + if($declarable_only) + continue; + } + if(array_key_exists($ai->getUID(), $ifs)) $ai = $ifs[$ai->getUID()]; @@ -578,17 +603,17 @@ function get_accessor_interfaces(\mtgMetaStruct $struct) return $ifs; } -function get_all_accessor_interfaces(\mtgMetaInfo $info) +function get_all_declarable_accessor_interfaces(\mtgMetaInfo $info) { $all = array(); foreach($info->getUnits() as $unit) { if($unit->object instanceof \mtgMetaStruct) - $all = array_merge($all, get_accessor_interfaces($unit->object)); + $all = array_merge($all, get_accessor_interfaces_declarable($unit->object)); else if($unit->object instanceof \mtgMetaRPC) { - $all = array_merge($all, get_accessor_interfaces($unit->object->getReq())); - $all = array_merge($all, get_accessor_interfaces($unit->object->getRsp())); + $all = array_merge($all, get_accessor_interfaces_declarable($unit->object->getReq())); + $all = array_merge($all, get_accessor_interfaces_declarable($unit->object->getRsp())); } } return $all; diff --git a/tpl/macro.twig b/tpl/macro.twig index 9c9a3c3..2d63ad7 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -10,7 +10,7 @@ {%- endif ~%} {%- endfor ~%} -{%- for ai in get_all_accessor_interfaces(meta) ~%} +{%- for ai in get_all_declarable_accessor_interfaces(meta) ~%} {{ _self.decl_accessor_interface(ai) }} {%- endfor ~%}