diff --git a/cmake/targets/dolphin.cmake b/cmake/targets/dolphin.cmake index f42f9040..e4f8c58f 100644 --- a/cmake/targets/dolphin.cmake +++ b/cmake/targets/dolphin.cmake @@ -39,7 +39,7 @@ set(LUA_C_FILES list(TRANSFORM LUA_C_FILES PREPEND "${LUA_SRC_DIR}/") add_library(liblua STATIC ${LUA_C_FILES}) target_include_directories(liblua PUBLIC "${LUA_SRC_DIR}") -target_compile_definitions(liblua PRIVATE LUA_USE_C89) +target_compile_definitions(liblua PUBLIC LUA_USE_C89) add_library(lua::lua ALIAS liblua) set(Lua_FOUND TRUE CACHE BOOL "Lua found" FORCE) diff --git a/src/dusk/asset/loader/script/assetscriptloader.c b/src/dusk/asset/loader/script/assetscriptloader.c index ba99edd1..8ddee48c 100644 --- a/src/dusk/asset/loader/script/assetscriptloader.c +++ b/src/dusk/asset/loader/script/assetscriptloader.c @@ -19,13 +19,13 @@ errorret_t assetScriptLoader(assetfile_t *file) { errorChain(assetFileOpen(file)); // Request loading - if(!lua_load( + if(lua_load( script->ctx->luaState, assetScriptReader, file, file->filename, NULL - ) == LUA_OK) { + ) != LUA_OK) { const char_t *strErr = lua_tostring(script->ctx->luaState, -1); lua_pop(script->ctx->luaState, 1); errorThrow("Failed to load Lua script: %s", strErr); diff --git a/src/dusk/input/input.c b/src/dusk/input/input.c index beb83e8b..e7fdb267 100644 --- a/src/dusk/input/input.c +++ b/src/dusk/input/input.c @@ -95,27 +95,27 @@ void inputUpdate(void) { if(TIME.dynamicUpdate) return; #endif - if(INPUT.eventPressed.listenerCount > 0) { - action = &INPUT.actions[0]; - do { - if(inputPressed(action->action)) { - inputevent_t inputEvent = { .action = action->action }; - eventInvoke(&INPUT.eventPressed, &inputEvent, "input_mt"); - } - action++; - } while(action < &INPUT.actions[INPUT_ACTION_COUNT]); - } + // if(INPUT.eventPressed.listenerCount > 0) { + // action = &INPUT.actions[0]; + // do { + // if(inputPressed(action->action)) { + // inputevent_t inputEvent = { .action = action->action }; + // eventInvoke(&INPUT.eventPressed, &inputEvent, "input_mt"); + // } + // action++; + // } while(action < &INPUT.actions[INPUT_ACTION_COUNT]); + // } - if(INPUT.eventReleased.listenerCount > 0) { - action = &INPUT.actions[0]; - do { - if(inputReleased(action->action)) { - inputevent_t inputEvent = { .action = action->action }; - eventInvoke(&INPUT.eventReleased, &inputEvent, "input_mt"); - } - action++; - } while(action < &INPUT.actions[INPUT_ACTION_COUNT]); - } + // if(INPUT.eventReleased.listenerCount > 0) { + // action = &INPUT.actions[0]; + // do { + // if(inputReleased(action->action)) { + // inputevent_t inputEvent = { .action = action->action }; + // eventInvoke(&INPUT.eventReleased, &inputEvent, "input_mt"); + // } + // action++; + // } while(action < &INPUT.actions[INPUT_ACTION_COUNT]); + // } } float_t inputGetCurrentValue(const inputaction_t action) { diff --git a/src/dusk/scene/scene.c b/src/dusk/scene/scene.c index 43f0cdb2..d147522e 100644 --- a/src/dusk/scene/scene.c +++ b/src/dusk/scene/scene.c @@ -37,19 +37,34 @@ static void sceneReset(void) { // Calls sceneClass:method() if it exists. Returns an error if the call fails. static errorret_t sceneCall(const char_t *method) { lua_State *L = SCRIPT_MANAGER.mainContext.luaState; + + if(SCENE.scriptRef == LUA_NOREF || SCENE.scriptRef == LUA_REFNIL) { + errorOk(); + } + lua_rawgeti(L, LUA_REGISTRYINDEX, SCENE.scriptRef); + + if(!lua_istable(L, -1)) { + lua_pop(L, 1); + errorThrow("Scene script ref %d is not a table", SCENE.scriptRef); + } + lua_getfield(L, -1, method); + if(!lua_isfunction(L, -1)) { lua_pop(L, 2); errorOk(); } + lua_pushvalue(L, -2); // self + if(lua_pcall(L, 1, 0, 0) != LUA_OK) { const char_t *err = lua_tostring(L, -1); - lua_pop(L, 2); + lua_pop(L, 2); // error + scene table errorThrow("Scene:%s failed: %s", method, err); } - lua_pop(L, 1); // pop scene class + + lua_pop(L, 1); // scene table errorOk(); } @@ -211,6 +226,7 @@ errorret_t sceneSetImmediate(const char_t *scene) { if(scene != NULL) { lua_State *L = SCRIPT_MANAGER.mainContext.luaState; + int32_t stackBase = lua_gettop(L); errorChain(scriptContextExecFile(&SCRIPT_MANAGER.mainContext, scene)); @@ -221,6 +237,7 @@ errorret_t sceneSetImmediate(const char_t *scene) { errorThrow("Scene '%s' must return a table", scene); } lua_settop(L, stackBase + 1); + SCENE.scriptRef = luaL_ref(L, LUA_REGISTRYINDEX); errorChain(sceneCall("init")); diff --git a/src/dusk/script/module/display/moduletexture.h b/src/dusk/script/module/display/moduletexture.h index 9300ff48..4764d644 100644 --- a/src/dusk/script/module/display/moduletexture.h +++ b/src/dusk/script/module/display/moduletexture.h @@ -81,7 +81,7 @@ static int moduleTextureLoad(lua_State *l) { static void moduleTexture(lua_State *L) { assertNotNull(L, "Lua state cannot be null"); - if(luaL_newmetatable(L, "texture_mt") == 1) { + if(luaL_newmetatable(L, "texture_mt")) { lua_pushcfunction(L, moduleTextureIndex); lua_setfield(L, -2, "__index"); lua_pushcfunction(L, moduleTextureToString); @@ -89,6 +89,7 @@ static void moduleTexture(lua_State *L) { lua_pushcfunction(L, moduleTextureGC); lua_setfield(L, -2, "__gc"); } + lua_pop(L, 1); lua_pushnumber(L, TEXTURE_FORMAT_RGBA); lua_setglobal(L, "TEXTURE_FORMAT_RGBA"); diff --git a/src/dusk/script/module/locale/modulelocale.h b/src/dusk/script/module/locale/modulelocale.h index 98431b03..8701b416 100644 --- a/src/dusk/script/module/locale/modulelocale.h +++ b/src/dusk/script/module/locale/modulelocale.h @@ -30,7 +30,7 @@ static int moduleLocaleGetText(lua_State *L) { luaL_error(L, "Expected plural as second argument"); return 0; } - plural = (int32_t)lua_tointeger(L, 2); + plural = (int32_t)lua_tonumber(L, 2); if(plural < 0) { luaL_error(L, "Plural cannot be negative"); return 0; diff --git a/src/dusk/script/module/module.h b/src/dusk/script/module/module.h index d4b91a39..c059eb96 100644 --- a/src/dusk/script/module/module.h +++ b/src/dusk/script/module/module.h @@ -42,5 +42,4 @@ void moduleRegister(lua_State *L) { moduleScreen(L); moduleTexture(L); moduleTileset(L); - moduleScript(L); } \ No newline at end of file