From b525473d7957a2f0b7417243de34fbda876bc95f Mon Sep 17 00:00:00 2001 From: Mm2PL Date: Thu, 3 Oct 2024 16:24:54 +0200 Subject: [PATCH] Make createEnumTable to replace direct Lua API usage Co-authored-by: Nerixyz --- src/controllers/plugins/LuaUtilities.hpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/controllers/plugins/LuaUtilities.hpp b/src/controllers/plugins/LuaUtilities.hpp index 780189eca..0612138ae 100644 --- a/src/controllers/plugins/LuaUtilities.hpp +++ b/src/controllers/plugins/LuaUtilities.hpp @@ -308,6 +308,7 @@ bool pop(lua_State *L, T *out, StackIdx idx = -1) * Values in this table may change. * * @returns stack index of newly created table + * @deprecated */ template StackIdx pushEnumTable(lua_State *L) @@ -326,6 +327,29 @@ StackIdx pushEnumTable(lua_State *L) return out; } +/** + * @brief Creates a table mapping enum names to unique values. + * + * Values in this table may change. + * + * @returns Sol reference to the table + */ +template + requires std::is_enum_v +sol::table createEnumTable(sol::state_view &lua) +{ + constexpr auto values = magic_enum::enum_values(); + auto out = lua.create_table(0, values.size()); + for (const T v : values) + { + std::string_view name = magic_enum::enum_name(v); + std::string str(name); + + out.raw_set(str, v); + } + return out; +} + // Represents a Lua function on the stack template class CallbackFunction