diff options
Diffstat (limited to 'src/script/lua_api/l_metadata.cpp')
-rw-r--r-- | src/script/lua_api/l_metadata.cpp | 26 |
1 files changed, 26 insertions, 0 deletions
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<std::string> keys_; + const std::vector<std::string> &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) { |