From 5ba33f846129f3e6c0505b19f94634b24505e828 Mon Sep 17 00:00:00 2001 From: Vaxry <43317083+vaxerski@users.noreply.github.com> Date: Sun, 26 Apr 2026 15:16:36 +0100 Subject: config/lua: init lua config manager, use lua if available (#13817) Adds a new lua config manager, which will automatically be used if there is a lua config present. --- src/config/lua/objects/LuaTimer.cpp | 109 ++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/config/lua/objects/LuaTimer.cpp (limited to 'src/config/lua/objects/LuaTimer.cpp') diff --git a/src/config/lua/objects/LuaTimer.cpp b/src/config/lua/objects/LuaTimer.cpp new file mode 100644 index 00000000..46f9a519 --- /dev/null +++ b/src/config/lua/objects/LuaTimer.cpp @@ -0,0 +1,109 @@ +#include "LuaTimer.hpp" + +#include +#include + +using namespace Config::Lua; + +static constexpr const char* MT = "HL.Timer"; + +namespace { + struct STimerRef { + WP timer; + int timeoutMs = 0; + }; +} + +static int timerEq(lua_State* L) { + const auto* lhs = sc(luaL_checkudata(L, 1, MT)); + const auto* rhs = sc(luaL_checkudata(L, 2, MT)); + + lua_pushboolean(L, lhs->timer.lock() == rhs->timer.lock()); + return 1; +} + +static int timerToString(lua_State* L) { + const auto* ref = sc(luaL_checkudata(L, 1, MT)); + const auto timer = ref->timer.lock(); + + if (!timer) + lua_pushstring(L, "HL.Timer(expired)"); + else + lua_pushfstring(L, "HL.Timer(%p)", timer.get()); + + return 1; +} + +static int timerSetEnabled(lua_State* L) { + auto* ref = sc(luaL_checkudata(L, 1, MT)); + luaL_checktype(L, 2, LUA_TBOOLEAN); + + const auto timer = ref->timer.lock(); + if (!timer) + return 0; + + if (lua_toboolean(L, 2)) + timer->updateTimeout(std::chrono::milliseconds(std::max(ref->timeoutMs, 1))); + else + timer->updateTimeout(std::nullopt); + + return 0; +} + +static int timerSetTimeout(lua_State* L) { + auto* ref = sc(luaL_checkudata(L, 1, MT)); + luaL_checktype(L, 2, LUA_TNUMBER); + + const auto timer = ref->timer.lock(); + if (!timer) + return 0; + + uint64_t x = lua_tointeger(L, 2); + + if (x < 1) + return Config::Lua::Bindings::Internal::configError(L, "hl.timer:set_timeout: timeout must be greater or equal to 1ms"); + + timer->updateTimeout(std::chrono::milliseconds(x)); + ref->timeoutMs = x; + + return 0; +} + +static int timerIsEnabled(lua_State* L) { + auto* ref = sc(luaL_checkudata(L, 1, MT)); + + const auto timer = ref->timer.lock(); + if (!timer) { + lua_pushnil(L); + return 1; + } + + lua_pushboolean(L, timer->armed()); + return 1; +} + +static int timerIndex(lua_State* L) { + luaL_checkudata(L, 1, MT); + const std::string_view key = luaL_checkstring(L, 2); + + if (key == "set_enabled") + lua_pushcfunction(L, timerSetEnabled); + else if (key == "is_enabled") + lua_pushcfunction(L, timerIsEnabled); + else if (key == "set_timeout") + lua_pushcfunction(L, timerSetTimeout); + else + lua_pushnil(L); + + return 1; +} + +void Objects::CLuaTimer::setup(lua_State* L) { + registerMetatable(L, MT, timerIndex, gcRef, timerEq, timerToString); +} + +void Objects::CLuaTimer::push(lua_State* L, const SP& timer, int timeoutMs) { + new (lua_newuserdata(L, sizeof(STimerRef))) STimerRef{WP(timer), timeoutMs}; + luaL_getmetatable(L, MT); + lua_setmetatable(L, -2); +} -- cgit v1.2.3