From 4793f65fcc9eed51fdb34978772e54b38239fed0 Mon Sep 17 00:00:00 2001 From: Madpwnhammer Date: Thu, 12 Oct 2023 11:05:44 +0300 Subject: [PATCH] added ecs lite bindings --- src/bind.inc.php | 4 +- tpl/macro.twig | 176 ++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 178 insertions(+), 2 deletions(-) diff --git a/src/bind.inc.php b/src/bind.inc.php index 706e9dc..dd85057 100644 --- a/src/bind.inc.php +++ b/src/bind.inc.php @@ -27,6 +27,7 @@ function supported_tokens() 'bhl_bind', 'bhl_no_new', 'bhl_ecs_component', + 'bhl_ecslite_component', 'bhl_ecs_component_ref', 'bhl_coroutine', 'bhl_static', @@ -49,7 +50,7 @@ function prepare_meta(\mtgMetaInfo $orig) $arr_proxies = array(); foreach($meta->getUnits() as $u) { - if($u->object instanceof \mtgMetaStruct && !$u->object->hasToken('bhl_ecs_component')) + if($u->object instanceof \mtgMetaStruct && !$u->object->hasToken('bhl_ecs_component') && !$u->object->hasToken('bhl_ecslite_component')) { $need_to_replace = false; $fields = $u->object->getFields(); @@ -59,6 +60,7 @@ function prepare_meta(\mtgMetaInfo $orig) if($fld->getType() instanceof \mtgArrType) { $proxy_name = "List_{$fld->getType()->getValue()->getName()}"; + var_dump($proxy_name); if(!isset($arr_proxies[$proxy_name])) { $proxy = new \mtgMetaStruct($proxy_name); diff --git a/tpl/macro.twig b/tpl/macro.twig index 5e57632..0a8a51d 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -933,6 +933,178 @@ Script_{{o.name|norm_name}}.Method_{{m.name}}.ReturnValue(frm, stack } {%- endmacro -%} +{%- macro reg_ecslite_component(o) ~%} + { + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_Ensure", Types.Void, + #if !BHL_FRONT + delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + EcsPackedEntityWithWorld e = default; + stack.PopRelease().Decode(ref e); + + if (e.Unpack(out EcsWorld world, out int id) == false) + return null; + + ref var cmp = ref world.Ensure(id); + + return null; + } + #else + null + #endif + , + new FuncArgSymbol("__self", types.T("ecs.Entity")) + ); + types.ns.Define(fn); + } + + { + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_Del", Types.Void, + #if !BHL_FRONT + delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + EcsPackedEntityWithWorld e = default; + stack.PopRelease().Decode(ref e); + + if (e.Unpack(out EcsWorld world, out int id) == false) + return null; + + world.Del(id); + + return null; + } + #else + null + #endif + , + new FuncArgSymbol("__self", types.T("ecs.Entity")) + ); + types.ns.Define(fn); + } + + { + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_Exists", Types.Bool, + #if !BHL_FRONT + delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + EcsPackedEntityWithWorld e = default; + stack.PopRelease().Decode(ref e); + + if (e.Unpack(out EcsWorld world, out int id) == false) + return null; + + var dv = bhl.Val.New(frm.vm); + dv.SetBool(world.Has(id)); + stack.Push(dv); + + return null; + } + #else + null + #endif + , + new FuncArgSymbol("__self", types.T("ecs.Entity")) + ); + types.ns.Define(fn); + } + + {% for f in o.getfields %} + {{ _self.ecslite_component_field(o, f) }} + {% endfor %} + + {% for m in o.getfuncs %} + {{ _self.ecslite_component_func(o, m) }} + {% endfor %} + +{%- endmacro -%} + +{%- macro ecslite_component_field(o, f) ~%} + { + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_{{f.name}}", {{f.type|bhl_type_ref}}, + {%- if token_or(f, 'bhl_set', 1) != 0 ~%} + 1, + {%- endif ~%} + delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + #if !BHL_FRONT + Val dv = null; + + {%- if token_or(f, 'bhl_set', 1) != 0 ~%} + if(args_info.CountArgs() > 1) + dv = stack.Pop(); + {%- endif ~%} + + EcsPackedEntityWithWorld e = default; + stack.PopRelease().Decode(ref e); + + if (e.Unpack(out EcsWorld world, out int id) == false) + return null; + + ref var cmp = ref world.Ensure(id); + var v = cmp.{{f.name}}; + + {%- if token_or(f, 'bhl_set', 1) != 0 ~%} + if(dv != null) { + {{ _self.val2native(f.type, 'dv', 'v') }}; + cmp.{{f.name}} = v; + dv.Release(); + } + {%- endif ~%} + + dv = Val.New(frm.vm); + { + {{ _self.native2val(f.type, 'v', 'dv') }}; + } + stack.Push(dv); + #endif + return null; + }, + new FuncArgSymbol("e", types.T("ecs.Entity")) + {%- if token_or(f, 'bhl_set', 1) != 0 ~%} + , new FuncArgSymbol("v", {{f.type|bhl_type_ref}}) + {%- endif ~%} + ); + types.ns.Define(fn); + } +{%- endmacro -%} + +{%- macro ecslite_component_func(o, m) ~%} + { + var fn = new FuncSymbolNative(new Origin(), "{{o.name}}_{{m.name}}", + {{m.returntype|bhl_type_ref}}, + {{ count_default_args(m) }}, + #if !BHL_FRONT + delegate(VM.Frame frm, ValStack stack, FuncArgsInfo args_info, ref BHS status) { + EcsPackedEntityWithWorld e = default; + + {{ _self.read_args2natives(m, '__') }} + + stack.PopRelease().Decode(ref e); + + if (e.Unpack(out EcsWorld world, out int id) == false) + return null; + + ref var cmp = ref world.Ensure(id); + + {%~ if m.returntype %} + var return_val = + {%- endif ~%} + + cmp.{{m.name}}( + {%- for arg in m.args ~%} __{{arg.name}}{% if not loop.last %},{% endif %} {%- endfor ~%} + ); + + {{ _self.return_val(m, 'return_val') }} + + return null; + } + #else + null + #endif + , new FuncArgSymbol("__self", types.T("ecs.Entity")) + {%- if m.args %},{% endif ~%} + {{ _self.func_decl_args(m) }} + ); + types.ns.Define(fn); + } +{%- endmacro -%} + {%- macro return_val(f, return_var) -%} {%- if f.returntype -%} {%- if f.returntype is instanceof('\\mtgMultiType') -%} @@ -1055,6 +1227,8 @@ public partial class Script_{{u.object.name}} { {%- if has_token(u.object, 'bhl_ecs_component') -%} {{ _self.reg_ecs_component(u.object) }} + {%- elseif has_token(u.object, 'bhl_ecslite_component') -%} + {{ _self.reg_ecslite_component(u.object) }} {%- elseif has_token(u.object, 'bhl_native_arr_proxy') -%} {{ _self.reg_native_arr_proxy(u.object) }} {%- else -%} @@ -1080,7 +1254,7 @@ public partial class Script_{{u.object.name}} { //assign global static types {%- for u in units -%} {%- if u.object is instanceof('\\mtgMetaStruct') or u.object is instanceof('\\mtgMetaInterface') ~%} - {%- if not has_token(u.object, 'bhl_ecs_component') ~%} + {%- if not has_token(u.object, 'bhl_ecs_component') and not has_token(u.object, 'bhl_ecslite_component') ~%} { var tmp = types.T("{{u.object.name}}").Get(); if(tmp == null)