From b037d7a0b9212933d3823280407f44709afef58d Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Tue, 14 Jan 2025 01:35:54 +0300 Subject: [PATCH] /apps/twnlua: ctx uploading --- apps/twnlua/bindgen.py | 29 ++++++++++++++++++++++++----- apps/twnlua/game.c | 4 ++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/apps/twnlua/bindgen.py b/apps/twnlua/bindgen.py index 6197329..af61aad 100755 --- a/apps/twnlua/bindgen.py +++ b/apps/twnlua/bindgen.py @@ -43,7 +43,22 @@ def to_table(typedesc, variable, indent = 0): else: raise BaseException("Unhandled return field type '%s'" % (field["type"])) binding += ' ' * indent + "lua_setfield(L, -2, \"%s\");\n" % field["name"] - # binding += ' ' * indent + "lua_pop(L, 1);\n" + return binding + + +def from_table(typedesc, variable, indent = 0): + binding = "" + for field in typedesc["fields"]: + binding += ' ' * indent + "lua_getfield(L, -1, \"%s\");\n" % field["name"] + if field["type"] == "float": + binding += ' ' * indent + "%s = (float)lua_tonumber(L, -1);\n" % (variable + ".%s" % field["name"]) + elif field["type"] == "bool": + binding += ' ' * indent + "%s = lua_toboolean(L, -1);\n" % (variable + ".%s" % field["name"]) + elif field["type"] in api["types"]: + binding += from_table(api["types"][field["type"]], variable + ".%s" % field["name"], indent + 4) + else: + raise BaseException("Unhandled return field type '%s'" % (field["type"])) + binding += ' ' * indent + "lua_pop(L, 1);\n" return binding @@ -94,8 +109,6 @@ for procedure, procedure_desc in api["procedures"].items(): else: raise BaseException("Unhandled parameter type '%s'" % (parameter["type"])) - # binding += " lua_pop(L, %i);\n" % (1 + len(procedure_desc["params"]) if "params" in procedure_desc else 0) - if "return" in procedure_desc: if procedure_desc["return"] == "bool": binding += " lua_pushboolean(L, (int)(%s(%s)));\n" % (procedure, ", ".join(param["name"] for param in procedure_desc["params"])) @@ -176,9 +189,9 @@ print(loader) unloader = "extern void bindgen_unload_%s(lua_State *L);\n" % api["name"] -unloader += "void bindgen_unload_%s(lua_State *L) {\n(void)L;\n" % api["name"] +unloader += "void bindgen_unload_%s(lua_State *L) {\n (void)L;\n" % api["name"] unloader += '\n'.join(deinitializers) -unloader += "}\n" +unloader += "\n}\n" print(unloader) @@ -188,5 +201,11 @@ if api["name"] == "twn": contexter = "extern void bindgen_build_context(lua_State *L);\n" contexter += "void bindgen_build_context(lua_State *L) {\n" contexter += to_table(api["types"]["Context"], "ctx", 4) + contexter += "}\n\n" + + contexter += "extern void bindgen_upload_context(lua_State *L);\n" + contexter += "void bindgen_upload_context(lua_State *L) {\n" + contexter += from_table(api["types"]["Context"], "ctx", 4) contexter += "}" + print(contexter) diff --git a/apps/twnlua/game.c b/apps/twnlua/game.c index 5574741..cd9b935 100644 --- a/apps/twnlua/game.c +++ b/apps/twnlua/game.c @@ -14,6 +14,7 @@ void bindgen_load_twn(lua_State *L); void bindgen_unload_twn(lua_State *L); void bindgen_build_context(lua_State *L); +void bindgen_upload_context(lua_State *L); /* require will go through physicsfs exclusively so that scripts can be in the data dir */ @@ -170,6 +171,9 @@ void game_tick(void) { log_critical("%s", lua_tostring(state->L, -1)); lua_pop(state->L, 1); } + + lua_getglobal(state->L, "ctx"); + bindgen_upload_context(state->L); }