1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Add MetaDataRef:get_keys (#12841)

This commit is contained in:
Jude Melton-Houghton 2022-11-15 10:45:12 -05:00 committed by GitHub
parent 1a045da0dd
commit cd8a7fe472
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 125 additions and 0 deletions

View file

@ -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)
{