From 7a403c766efe679170528fe6d994dbfabe9aebcf Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Sun, 26 Jan 2025 08:48:59 +0300 Subject: [PATCH] /apps/twnlua: report error on loading --- apps/twnlua/game.c | 46 +++++++++++++++++++++++++-------------------- apps/twnlua/state.h | 2 ++ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/apps/twnlua/game.c b/apps/twnlua/game.c index cd9b935..d06f64c 100644 --- a/apps/twnlua/game.c +++ b/apps/twnlua/game.c @@ -136,10 +136,6 @@ void game_tick(void) { lua_pop(state->L, 2); /* binding */ - // lua_register(state->L, "sprite", b_sprite); - // lua_register(state->L, "rectangle", b_rectangle); - // lua_register(state->L, "text", b_text); - bindgen_load_twn(state->L); /* now finally get to running the code */ @@ -149,31 +145,41 @@ void game_tick(void) { if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) { log_critical("%s", lua_tostring(state->L, -1)); lua_pop(state->L, 1); - } + } else + state->loaded_successfully = true; + } else { + /* got some sort of error, it should be pushed on top of the stack */ + SDL_assert(lua_isstring(state->L, -1)); + log_critical("Error loading /scripts/game.lua entry: %s", luaL_tolstring(state->L, -1, NULL)); + lua_pop(state->L, 1); } + free(game_buf); + /* from this point we have access to everything defined in lua */ } State *state = ctx.udata; - bindgen_build_context(state->L); - lua_getglobal(state->L, "ctx"); - if (!lua_isnoneornil(state->L, -1)) { - lua_getfield(state->L, -1, "udata"); - lua_setfield(state->L, -3, "udata"); - } - lua_pop(state->L, 1); - lua_setglobal(state->L, "ctx"); - - lua_getglobal(state->L, "game_tick"); - if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) { - log_critical("%s", lua_tostring(state->L, -1)); + if (state->loaded_successfully) { + bindgen_build_context(state->L); + lua_getglobal(state->L, "ctx"); + if (!lua_isnoneornil(state->L, -1)) { + lua_getfield(state->L, -1, "udata"); + lua_setfield(state->L, -3, "udata"); + } lua_pop(state->L, 1); - } + lua_setglobal(state->L, "ctx"); - lua_getglobal(state->L, "ctx"); - bindgen_upload_context(state->L); + lua_getglobal(state->L, "game_tick"); + if (lua_pcall(state->L, 0, 0, 0) != LUA_OK) { + log_critical("%s", lua_tostring(state->L, -1)); + lua_pop(state->L, 1); + } + + lua_getglobal(state->L, "ctx"); + bindgen_upload_context(state->L); + } } diff --git a/apps/twnlua/state.h b/apps/twnlua/state.h index 0eb8b3f..2ae3c14 100644 --- a/apps/twnlua/state.h +++ b/apps/twnlua/state.h @@ -3,9 +3,11 @@ #include +#include typedef struct State { lua_State *L; + bool loaded_successfully; } State;