From cd8a7fe47298be12620cab62be8cf0816fabb095 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Tue, 15 Nov 2022 10:45:12 -0500 Subject: Add MetaDataRef:get_keys (#12841) --- src/script/lua_api/l_metadata.cpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/script/lua_api/l_metadata.cpp') diff --git a/src/script/lua_api/l_metadata.cpp b/src/script/lua_api/l_metadata.cpp index 8388bc089..68b79331b 100644 --- a/src/script/lua_api/l_metadata.cpp +++ b/src/script/lua_api/l_metadata.cpp @@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serverenvironment.h" #include "map.h" #include "server.h" +#include "util/basic_macros.h" MetaDataRef *MetaDataRef::checkAnyMetadata(lua_State *L, int narg) { @@ -196,6 +197,31 @@ int MetaDataRef::l_set_float(lua_State *L) return 0; } +// get_keys(self) +int MetaDataRef::l_get_keys(lua_State *L) +{ + MAP_LOCK_REQUIRED; + + MetaDataRef *ref = checkAnyMetadata(L, 1); + + IMetadata *meta = ref->getmeta(false); + if (meta == NULL) { + lua_newtable(L); + return 1; + } + + std::vector keys_; + const std::vector &keys = meta->getKeys(&keys_); + + int i = 0; + lua_createtable(L, keys.size(), 0); + for (const std::string &key : keys) { + lua_pushlstring(L, key.c_str(), key.size()); + lua_rawseti(L, -2, ++i); + } + return 1; +} + // to_table(self) int MetaDataRef::l_to_table(lua_State *L) { -- cgit v1.2.3