Plugins: Enums (LogLevel, Align, GrindSide, Stance, Pop, Space, etc.)

This commit is contained in:
vtf committed 2026-10-25 22:34:48 -04:00
1 parent e134c089e6
commit b1e61caa75
6 files changed
+60 -31

No files matched your search

+20 -16
View File
@@ -230,7 +230,7 @@ struct ApiFunctions {
auto& plugin = plugin_of(L);
const auto message = text_of(L, 1);
const std::string level = luaL_optstring(L, 2, "info");
if (level != "info" && level != "warn" && level != "error") luaL_argerror(L, 2, "the level must be \"info\", \"warn\" or \"error\"");
if (!one_of(log_levels, level)) luaL_argerror(L, 2, "the level must be \"info\", \"warn\" or \"error\"");
plugin.log(level == "error" ? LogLevel::error : level == "warn" ? LogLevel::warn : LogLevel::info, message);
return 0;
}
@@ -675,21 +675,25 @@ void Api::install(Plugin& plugin, lua_State* L) {
lua_setfield(L, -2, "side");
// Named constants, read-only and built from the C++ lists so they cannot drift:
// reskate.Enums.Side.server is "server", reskate.Enums.Event.tick is "tick". Plain strings still work.
lua_createtable(L, 0, 2);
lua_createtable(L, 0, 3);
for (const auto side : {Side::client, Side::server, Side::both}) {
lua_pushstring(L, side_name(side));
lua_setfield(L, -2, side_name(side));
}
readonly();
lua_setfield(L, -2, "Side");
lua_createtable(L, 0, static_cast<int>(events.size()));
for (const auto& event : events) {
lua_pushlstring(L, event.name.data(), event.name.size());
lua_setfield(L, -2, std::string(event.name).c_str());
}
readonly();
lua_setfield(L, -2, "Event");
const auto push_enum = [&](const char* name, const auto& values, auto word) {
lua_createtable(L, 0, static_cast<int>(std::size(values)));
for (const auto& value : values) {
const std::string_view w = word(value);
lua_pushlstring(L, w.data(), w.size());
lua_setfield(L, -2, std::string(w).c_str());
}
readonly();
lua_setfield(L, -2, name);
};
const auto same = [](std::string_view w) { return w; };
lua_createtable(L, 0, 13);
push_enum("Side", std::array{Side::client, Side::server, Side::both}, [](Side side) { return std::string_view(side_name(side)); });
push_enum("Event", events, [](const Event& event) { return event.name; });
push_enum("Key", key_names, [](const auto& key) { return key.first; });
for (const auto& [name, words] : {std::pair<const char*, Words>{"LogLevel", log_levels}, {"Align", aligns}, {"GrindSide", grind_sides},
{"Stance", stances}, {"Pop", pops}, {"Space", spaces}, {"Throwdown", throwdown_modes},
{"Card", throwdown_cards}, {"EntityStatus", entity_statuses}, {"Target", send_targets}})
push_enum(name, words, same);
readonly();
lua_setfield(L, -2, "Enums");
using F = ApiFunctions;