Skip to content

Commit

Permalink
Finished metadata methods. needs testing
Browse files Browse the repository at this point in the history
  • Loading branch information
erikalmeidah committed Nov 16, 2023
1 parent 5c541de commit 4dc19e0
Show file tree
Hide file tree
Showing 7 changed files with 415 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/script/lua_api/l_itemstackmeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "native_api/native_itemstackmeta.h"
#include "native_api/native_metadata.h"
#include <tuple>

/*
Expand Down Expand Up @@ -154,5 +155,16 @@ const luaL_Reg ItemStackMetaRef::methods[] = {
luamethod(MetaDataRef, equals),
luamethod(ItemStackMetaRef, set_tool_capabilities),
luamethod(ItemStackMetaRef, native_set_tool_capabilities),
luamethod(MetaDataRef, native_contains),
luamethod(MetaDataRef, native_get),
luamethod(MetaDataRef, native_get_string),
luamethod(MetaDataRef, native_set_string),
luamethod(MetaDataRef, native_get_int),
luamethod(MetaDataRef, native_set_int),
luamethod(MetaDataRef, native_get_float),
luamethod(MetaDataRef, native_set_float),
luamethod(MetaDataRef, native_to_table),
luamethod(MetaDataRef, native_from_table),
luamethod(MetaDataRef, native_equals),
{0,0}
};
1 change: 1 addition & 0 deletions src/script/lua_api/l_itemstackmeta.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ class ItemStackMetaRef : public MetaDataRef

//TO-DO fully separate l_itemstackmeta from native counterpart.
friend class NativeItemStackMetaRef;
friend class NativeMetaDataRef;
};
215 changes: 215 additions & 0 deletions src/script/lua_api/l_metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 "native_api/native_metadata.h"

// LUALIB_API
void *luaL_checkudata_is_metadataref(lua_State *L, int ud) {
Expand Down Expand Up @@ -67,6 +68,27 @@ int MetaDataRef::l_contains(lua_State *L)
return 1;
}

int MetaDataRef::l_native_contains(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);

Metadata *meta = ref->getmeta(false);
if (meta == NULL)
return 0;

int result = NativeMetaDataRef::native_contains(meta, name);

if (result == 1) {
lua_pushboolean(L, true);
return 1;
}
lua_pushboolean(L, false);
return 0;
}

// get(self, name)
int MetaDataRef::l_get(lua_State *L)
{
Expand All @@ -87,6 +109,26 @@ int MetaDataRef::l_get(lua_State *L)
return 0;
}

int MetaDataRef::l_native_get(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);

Metadata *meta = ref->getmeta(false);
if (meta == NULL)
return 0;

std::string str = NativeMetaDataRef::native_get(meta, name, str);

if (str != "") {
lua_pushlstring(L, str.c_str(), str.size());
return 1;
}
return 0;
}

// get_string(self, name)
int MetaDataRef::l_get_string(lua_State *L)
{
Expand All @@ -106,6 +148,28 @@ int MetaDataRef::l_get_string(lua_State *L)
return 1;
}

int MetaDataRef::l_native_get_string(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);

Metadata *meta = ref->getmeta(false);
if (meta == NULL) {
lua_pushlstring(L, "", 0);
return 1;
}

std::string str = NativeMetaDataRef::native_get_string(meta, name, str);

if (str != "") {
lua_pushlstring(L, str.c_str(), str.size());
}

return 1;
}

// set_string(self, name, var)
int MetaDataRef::l_set_string(lua_State *L)
{
Expand All @@ -126,6 +190,25 @@ int MetaDataRef::l_set_string(lua_State *L)
return 0;
}

int MetaDataRef::l_native_set_string(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);
size_t len = 0;
const char *s = lua_tolstring(L, 3, &len);
std::string str(s, len);

Metadata *meta = ref->getmeta(!str.empty());
if (meta == NULL || str == meta->getString(name))
return 0;

int result = NativeMetaDataRef::native_set_string(meta, name, str, ref);

return 0;
}

// get_int(self, name)
int MetaDataRef::l_get_int(lua_State *L)
{
Expand All @@ -145,6 +228,26 @@ int MetaDataRef::l_get_int(lua_State *L)
return 1;
}

int MetaDataRef::l_native_get_int(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);

Metadata *meta;
std::string result = NativeMetaDataRef::native_get_int(meta, name, ref);

if (result == "") {
lua_pushnumber(L, 0);
return 1;
}

lua_pushnumber(L, stoi(result));

return 1;
}

// set_int(self, name, var)
int MetaDataRef::l_set_int(lua_State *L)
{
Expand All @@ -164,6 +267,21 @@ int MetaDataRef::l_set_int(lua_State *L)
return 0;
}

int MetaDataRef::l_native_set_int(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);
int a = luaL_checkint(L, 3);
std::string str = itos(a);

Metadata *meta;
int result = NativeMetaDataRef::native_set_int(meta, name, ref, a);

return 0;
}

// get_float(self, name)
int MetaDataRef::l_get_float(lua_State *L)
{
Expand All @@ -183,6 +301,25 @@ int MetaDataRef::l_get_float(lua_State *L)
return 1;
}

int MetaDataRef::l_native_get_float(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);

Metadata *meta;
std::string result = NativeMetaDataRef::native_get_float(meta, name, ref);

if (result == "") {
lua_pushnumber(L, 0);
return 1;
}

lua_pushnumber(L, stof(result));
return 1;
}

// set_float(self, name, var)
int MetaDataRef::l_set_float(lua_State *L)
{
Expand All @@ -202,6 +339,20 @@ int MetaDataRef::l_set_float(lua_State *L)
return 0;
}

int MetaDataRef::l_native_set_float(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
std::string name = luaL_checkstring(L, 2);
float a = readParam<float>(L, 3);

Metadata *meta;
int result = NativeMetaDataRef::native_set_float(meta, name, ref, a);

return 0;
}

// to_table(self)
int MetaDataRef::l_to_table(lua_State *L)
{
Expand All @@ -221,6 +372,25 @@ int MetaDataRef::l_to_table(lua_State *L)
return 1;
}

int MetaDataRef::l_native_to_table(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);

Metadata *meta;
int result = NativeMetaDataRef::native_to_table(meta, ref);

if (result == -1) {
lua_pushnil(L);
return 1;
}
lua_newtable(L);

ref->handleToTable(L, meta);
return 1;
}

// from_table(self, table)
int MetaDataRef::l_from_table(lua_State *L)
{
Expand Down Expand Up @@ -250,6 +420,36 @@ int MetaDataRef::l_from_table(lua_State *L)
return 1;
}

int MetaDataRef::l_native_from_table(lua_State *L)
{
MAP_LOCK_REQUIRED;

MetaDataRef *ref = checkobject(L, 1);
int base = 2;

ref->clearMeta();

if (!lua_istable(L, base)) {
// No metadata
lua_pushboolean(L, true);
return 1;
}

// Create new metadata
Metadata *meta;
int result = NativeMetaDataRef::native_from_table(meta, ref);

if (result == -1) {
lua_pushboolean(L, false);
return 1;
}

bool was_successful = ref->handleFromTable(L, base, meta);
ref->reportMetadataChange();
lua_pushboolean(L, was_successful);
return 1;
}

void MetaDataRef::handleToTable(lua_State *L, Metadata *meta)
{
lua_newtable(L);
Expand Down Expand Up @@ -300,3 +500,18 @@ int MetaDataRef::l_equals(lua_State *L)
lua_pushboolean(L, *data1 == *data2);
return 1;
}

int MetaDataRef::l_native_equals(lua_State *L)
{
MetaDataRef *ref1 = checkobject(L, 1);
MetaDataRef *ref2 = checkobject(L, 2);
int result = NativeMetaDataRef::native_equals(ref1, ref2);

if (result == -1) {
lua_pushboolean(L, ref1->getmeta(false) == ref2->getmeta(false));
} else {
lua_pushboolean(L, *ref1->getmeta(false) == *ref2->getmeta(false));
}

return 1;
}
15 changes: 15 additions & 0 deletions src/script/lua_api/l_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,19 @@ class MetaDataRef : public ModApiBase

// equals(self, other)
static int l_equals(lua_State *L);

//Native versions
static int l_native_contains(lua_State *L);
static int l_native_get(lua_State *L);
static int l_native_get_string(lua_State *L);
static int l_native_set_string(lua_State *L);
static int l_native_get_int(lua_State *L);
static int l_native_set_int(lua_State *L);
static int l_native_get_float(lua_State *L);
static int l_native_set_float(lua_State *L);
static int l_native_to_table(lua_State *L);
static int l_native_from_table(lua_State *L);
static int l_native_equals(lua_State *L);

friend class NativeMetaDataRef;
};
2 changes: 2 additions & 0 deletions src/script/native_api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ set(common_SCRIPT_NATIVE_API_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/native_settings.cpp
${CMAKE_CURRENT_SOURCE_DIR}/native_nodetimer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/native_modchannels.cpp
${CMAKE_CURRENT_SOURCE_DIR}/native_itemstackmeta.cpp
${CMAKE_CURRENT_SOURCE_DIR}/native_metadata.cpp
PARENT_SCOPE)

set(client_SCRIPT_NATIVE_API_SRCS
Expand Down
Loading

0 comments on commit 4dc19e0

Please sign in to comment.