From f4aaaf6107a276521a30f952fa1ef2eb702c07a8 Mon Sep 17 00:00:00 2001 From: Fierelier Date: Wed, 25 Sep 2024 21:19:25 +0200 Subject: [PATCH] Enhance how Lua file is called --- test/lua.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/lua.c b/test/lua.c index 1e3bc5c..864949d 100644 --- a/test/lua.c +++ b/test/lua.c @@ -10,10 +10,22 @@ int main(int argc, char *argv[]) { lua_State *L = luaL_newstate(); - assert(L != NULL); - luaL_openlibs(L); - luaopen_unigi(L); // Load the wrapped module - luaL_loadfile(L, "mods/main/script/main.lua"); - lua_pcall(L, 0, 0, 0); + assert(L != NULL); + luaL_openlibs(L); + luaopen_unigi(L); // Load the wrapped module + + for (int i = 0; i < argc; i++) { + lua_pushstring(L, argv[i]); + lua_rawseti(L, -2, i + 1); + } + lua_setglobal(L, "arg"); + + if (luaL_dostring(L, + "main_path = (arg[1]:match(\"(.*[/\\\\])\") or \"./\"):sub(1,-2)\n" + "dofile(main_path .. \"/mods/main/script/main.lua\")" + ) != 0) { + fprintf(stderr, "Error running Lua code: %s\n", lua_tostring(L, -1)); + return 1; + } return 0; }