diff --git a/.gitea/workflows/build.yml b/.gitea/workflows/build.yml index 0794482..ace56fe 100644 --- a/.gitea/workflows/build.yml +++ b/.gitea/workflows/build.yml @@ -17,7 +17,7 @@ jobs: - name: Install dependencies run: | apt-get update - apt-get install -y build-essential cmake python3 python3-pip python3-polib python3-pil libsdl2-dev libgl1-mesa-dev libzip-dev python3-dotenv python3-pyqt5 + apt-get install -y build-essential cmake python3 python3-pip python3-polib python3-pil libsdl2-dev libgl1-mesa-dev libzip-dev python3-dotenv python3-pyqt5 python3-opengl - name: Configure CMake run: cmake -S . -B build -DDUSK_TARGET_SYSTEM=linux - name: Build @@ -39,7 +39,7 @@ jobs: - name: Install dependencies run: | apt-get update - apt-get install -y build-essential cmake python3 python3-pip python3-polib python3-pil libsdl2-dev libgl1-mesa-dev libzip-dev python3-dotenv python3-pyqt5 + apt-get install -y build-essential cmake python3 python3-pip python3-polib python3-pil libsdl2-dev libgl1-mesa-dev libzip-dev python3-dotenv python3-pyqt5 python3-opengl - name: Configure CMake run: cmake -S . -B build -DDUSK_TARGET_SYSTEM=psp - name: Build diff --git a/src/script/scriptmanager.c b/src/script/scriptmanager.c index faad38c..3db2623 100644 --- a/src/script/scriptmanager.c +++ b/src/script/scriptmanager.c @@ -7,6 +7,7 @@ #include "scriptmanager.h" #include "util/memory.h" +#include "assert/assert.h" #include "asset/asset.h" @@ -28,31 +29,13 @@ errorret_t scriptManagerInit() { luaL_openlibs(SCRIPT_MANAGER.luaState); lua_register(SCRIPT_MANAGER.luaState, "luaCallable", luaCallable); - // Script test. - assetscript_t script; - assetLoad("script/test.dsf", &script); - - if(lua_load(SCRIPT_MANAGER.luaState, assetScriptReader, &script, "script/test.dsf", NULL) != LUA_OK) { - const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1); - lua_pop(SCRIPT_MANAGER.luaState, 1); - errorThrow("Failed to load Lua script: %s", strErr); - } - - if(lua_pcall(SCRIPT_MANAGER.luaState, 0, LUA_MULTRET, 0) != LUA_OK) { - const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1); - lua_pop(SCRIPT_MANAGER.luaState, 1); - errorThrow("Failed to execute Lua script: %s", strErr); - } - - assetScriptDispose(&script); - + errorChain(scriptManagerExecFile("script/test.dsf")); errorOk(); } errorret_t scriptManagerExecString(const char_t *script) { - if(SCRIPT_MANAGER.luaState == NULL) { - errorThrow("Lua state is not initialized"); - } + assertNotNull(script, "Script cannot be NULL"); + assertNotNull(SCRIPT_MANAGER.luaState, "Lua state is not initialized"); if(luaL_dostring(SCRIPT_MANAGER.luaState, script) != LUA_OK) { const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1); @@ -64,16 +47,27 @@ errorret_t scriptManagerExecString(const char_t *script) { } errorret_t scriptManagerExecFile(const char_t *filename) { - if(SCRIPT_MANAGER.luaState == NULL) { - errorThrow("Lua state is not initialized"); - } + assertNotNull(filename, "Filename cannot be NULL"); + assertNotNull(SCRIPT_MANAGER.luaState, "Lua state is not initialized"); - if(luaL_dofile(SCRIPT_MANAGER.luaState, filename) != LUA_OK) { + assetscript_t script; + errorChain(assetLoad(filename, &script)); + + if(lua_load( + SCRIPT_MANAGER.luaState, assetScriptReader, &script, filename, NULL + ) != LUA_OK) { const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1); lua_pop(SCRIPT_MANAGER.luaState, 1); - errorThrow("Failed to execute Lua file: %s", strErr); + errorThrow("Failed to load Lua script: %s", strErr); } + if(lua_pcall(SCRIPT_MANAGER.luaState, 0, LUA_MULTRET, 0) != LUA_OK) { + const char_t *strErr = lua_tostring(SCRIPT_MANAGER.luaState, -1); + lua_pop(SCRIPT_MANAGER.luaState, 1); + errorThrow("Failed to execute Lua script: %s", strErr); + } + + errorChain(assetScriptDispose(&script)); errorOk(); } diff --git a/src/script/scriptmanager.h b/src/script/scriptmanager.h index 72661db..c0749b6 100644 --- a/src/script/scriptmanager.h +++ b/src/script/scriptmanager.h @@ -30,7 +30,15 @@ errorret_t scriptManagerInit(); * @param script The script to execute. * @return The error return value. */ -errorret_t scriptManagerExec(const char_t *script); +errorret_t scriptManagerExecString(const char_t *script); + +/** + * Execute a Lua script from a file. + * + * @param filename The filename of the script to execute. + * @return The error return value. + */ +errorret_t scriptManagerExecFile(const char_t *filename); /** * Dispose of the script manager.