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/LuaObjectHelpers.hpp | 51 +++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/config/lua/objects/LuaObjectHelpers.hpp (limited to 'src/config/lua/objects/LuaObjectHelpers.hpp') diff --git a/src/config/lua/objects/LuaObjectHelpers.hpp b/src/config/lua/objects/LuaObjectHelpers.hpp new file mode 100644 index 00000000..69c284ef --- /dev/null +++ b/src/config/lua/objects/LuaObjectHelpers.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include "../bindings/LuaBindingsInternal.hpp" + +#include "../../../helpers/memory/Memory.hpp" + +extern "C" { +#include +} + +namespace Config::Lua::Objects { + + template + inline int gcRef(lua_State* L) { + sc(lua_touserdata(L, 1))->~T(); + return 0; + } + + inline int readOnlyNewIndex(lua_State* L) { + return Config::Lua::Bindings::Internal::configError(L, "attempt to modify read-only hl object"); + } + + inline void registerMetatable(lua_State* L, const char* name, lua_CFunction indexFn, lua_CFunction gcFn, lua_CFunction eqFn = nullptr, lua_CFunction toStringFn = nullptr) { + luaL_newmetatable(L, name); + lua_pushcfunction(L, indexFn); + lua_setfield(L, -2, "__index"); + lua_pushcfunction(L, readOnlyNewIndex); + lua_setfield(L, -2, "__newindex"); + lua_pushcfunction(L, gcFn); + lua_setfield(L, -2, "__gc"); + + if (eqFn) { + lua_pushcfunction(L, eqFn); + lua_setfield(L, -2, "__eq"); + } + + if (toStringFn) { + lua_pushcfunction(L, toStringFn); + lua_setfield(L, -2, "__tostring"); + } + + lua_pop(L, 1); + } + + class ILuaObjectWrapper { + public: + virtual ~ILuaObjectWrapper() = default; + virtual void setup(lua_State* L) = 0; + }; + +} -- cgit v1.2.3