Implement vector and node conversion in Lua (#12609)

Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
Jude Melton-Houghton 2022-10-18 18:01:44 -04:00 committed by GitHub
parent 23e9f5db43
commit b38ffdec27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 191 additions and 167 deletions

@ -10,3 +10,4 @@ dofile(commonpath .. "chatcommands.lua")
dofile(clientpath .. "chatcommands.lua") dofile(clientpath .. "chatcommands.lua")
dofile(clientpath .. "death_formspec.lua") dofile(clientpath .. "death_formspec.lua")
dofile(clientpath .. "misc.lua") dofile(clientpath .. "misc.lua")
assert(loadfile(commonpath .. "item_s.lua"))({}) -- Just for push/read node functions

@ -223,3 +223,17 @@ function builtin_shared.cache_content_ids()
end end
end end
end end
if core.set_read_node and core.set_push_node then
local function read_node(node)
return name2content[node.name], node.param1, node.param2
end
core.set_read_node(read_node)
core.set_read_node = nil
local function push_node(content, param1, param2)
return {name = content2name[content], param1 = param1, param2 = param2}
end
core.set_push_node(push_node)
core.set_push_node = nil
end

@ -1,5 +1,4 @@
_G.core = {} _G.core = {}
_G.vector = {metatable = {}}
dofile("builtin/common/vector.lua") dofile("builtin/common/vector.lua")
dofile("builtin/common/misc_helpers.lua") dofile("builtin/common/misc_helpers.lua")

@ -1,5 +1,4 @@
_G.core = {} _G.core = {}
_G.vector = {metatable = {}}
_G.setfenv = require 'busted.compatibility'.setfenv _G.setfenv = require 'busted.compatibility'.setfenv

@ -1,4 +1,4 @@
_G.vector = {metatable = {}} _G.vector = {}
dofile("builtin/common/vector.lua") dofile("builtin/common/vector.lua")
describe("vector", function() describe("vector", function()

@ -6,8 +6,10 @@ Note: The vector.*-functions must be able to accept old vectors that had no meta
-- localize functions -- localize functions
local setmetatable = setmetatable local setmetatable = setmetatable
-- vector.metatable is set by C++. vector = {}
local metatable = vector.metatable
local metatable = {}
vector.metatable = metatable
local xyz = {"x", "y", "z"} local xyz = {"x", "y", "z"}
@ -366,3 +368,22 @@ function vector.dir_to_rotation(forward, up)
end end
return rot return rot
end end
if rawget(_G, "core") and core.set_read_vector and core.set_push_vector then
local function read_vector(v)
return v.x, v.y, v.z
end
core.set_read_vector(read_vector)
core.set_read_vector = nil
if rawget(_G, "jit") then
-- This is necessary to prevent trace aborts.
local function push_vector(x, y, z)
return (fast_new(x, y, z))
end
core.set_push_vector(push_vector)
else
core.set_push_vector(fast_new)
end
core.set_push_vector = nil
end

@ -1,5 +1,4 @@
_G.core = {get_once = function(_) end} _G.core = {get_once = function(_) end}
_G.vector = {metatable = {}}
_G.unpack = table.unpack _G.unpack = table.unpack
_G.serverlistmgr = {} _G.serverlistmgr = {}

@ -196,6 +196,7 @@ void Client::loadMods()
// Load builtin // Load builtin
scanModIntoMemory(BUILTIN_MOD_NAME, getBuiltinLuaPath()); scanModIntoMemory(BUILTIN_MOD_NAME, getBuiltinLuaPath());
m_script->loadModFromMemory(BUILTIN_MOD_NAME); m_script->loadModFromMemory(BUILTIN_MOD_NAME);
m_script->checkSetByBuiltin();
ModConfiguration modconf; ModConfiguration modconf;
{ {

@ -226,6 +226,7 @@ bool GUIEngine::loadMainMenuScript()
std::string script = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "init.lua"; std::string script = porting::path_share + DIR_DELIM "builtin" + DIR_DELIM "init.lua";
try { try {
m_script->loadScript(script); m_script->loadScript(script);
m_script->checkSetByBuiltin();
// Menu script loaded // Menu script loaded
return true; return true;
} catch (const ModError &e) { } catch (const ModError &e) {

@ -1175,43 +1175,27 @@ NodeBox read_nodebox(lua_State *L, int index)
} }
/******************************************************************************/ /******************************************************************************/
MapNode readnode(lua_State *L, int index, const NodeDefManager *ndef) MapNode readnode(lua_State *L, int index)
{ {
lua_getfield(L, index, "name"); lua_pushvalue(L, index);
if (!lua_isstring(L, -1)) lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_NODE);
throw LuaError("Node name is not set or is not a string!"); lua_insert(L, -2);
std::string name = lua_tostring(L, -1); lua_call(L, 1, 3);
lua_pop(L, 1); content_t content = lua_tointeger(L, -3);
u8 param1 = lua_tointeger(L, -2);
u8 param1 = 0; u8 param2 = lua_tointeger(L, -1);
lua_getfield(L, index, "param1"); lua_pop(L, 3);
if (!lua_isnil(L, -1)) return MapNode(content, param1, param2);
param1 = lua_tonumber(L, -1);
lua_pop(L, 1);
u8 param2 = 0;
lua_getfield(L, index, "param2");
if (!lua_isnil(L, -1))
param2 = lua_tonumber(L, -1);
lua_pop(L, 1);
content_t id = CONTENT_IGNORE;
if (!ndef->getId(name, id))
throw LuaError("\"" + name + "\" is not a registered node!");
return {id, param1, param2};
} }
/******************************************************************************/ /******************************************************************************/
void pushnode(lua_State *L, const MapNode &n, const NodeDefManager *ndef) void pushnode(lua_State *L, const MapNode &n)
{ {
lua_createtable(L, 0, 3); lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_NODE);
lua_pushstring(L, ndef->get(n).name.c_str()); lua_pushinteger(L, n.getContent());
lua_setfield(L, -2, "name");
lua_pushinteger(L, n.getParam1()); lua_pushinteger(L, n.getParam1());
lua_setfield(L, -2, "param1");
lua_pushinteger(L, n.getParam2()); lua_pushinteger(L, n.getParam2());
lua_setfield(L, -2, "param2"); lua_call(L, 3, 1);
} }
/******************************************************************************/ /******************************************************************************/

@ -129,10 +129,8 @@ void read_inventory_list (lua_State *L, int tableindex,
Inventory *inv, const char *name, Inventory *inv, const char *name,
IGameDef *gdef, int forcesize=-1); IGameDef *gdef, int forcesize=-1);
MapNode readnode (lua_State *L, int index, MapNode readnode (lua_State *L, int index);
const NodeDefManager *ndef); void pushnode (lua_State *L, const MapNode &n);
void pushnode (lua_State *L, const MapNode &n,
const NodeDefManager *ndef);
void read_groups (lua_State *L, int index, void read_groups (lua_State *L, int index,

@ -48,17 +48,19 @@ extern "C" {
} \ } \
} while (0) } while (0)
#define CHECK_POS_COORD(name) CHECK_TYPE(-1, "vector coordinate " name, LUA_TNUMBER) #define CHECK_POS_COORD(index, name) CHECK_TYPE(index, "vector coordinate " name, LUA_TNUMBER)
#define CHECK_POS_TAB(index) CHECK_TYPE(index, "vector", LUA_TTABLE) #define CHECK_POS_TAB(index) CHECK_TYPE(index, "vector", LUA_TTABLE)
/** /**
* A helper which sets the vector metatable for the table on top of the stack * A helper which calls CUSTOM_RIDX_READ_VECTOR with the argument at the given index
*/ */
static void set_vector_metatable(lua_State *L) static void read_v3_aux(lua_State *L, int index)
{ {
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE); lua_pushvalue(L, index);
lua_setmetatable(L, -2); lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
lua_insert(L, -2);
lua_call(L, 1, 3);
} }
// Retrieve an integer vector where all components are optional // Retrieve an integer vector where all components are optional
@ -79,14 +81,11 @@ static bool getv3intfield(lua_State *L, int index,
void push_v3f(lua_State *L, v3f p) void push_v3f(lua_State *L, v3f p)
{ {
lua_createtable(L, 0, 3); lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
lua_pushnumber(L, p.X); lua_pushnumber(L, p.X);
lua_setfield(L, -2, "x");
lua_pushnumber(L, p.Y); lua_pushnumber(L, p.Y);
lua_setfield(L, -2, "y");
lua_pushnumber(L, p.Z); lua_pushnumber(L, p.Z);
lua_setfield(L, -2, "z"); lua_call(L, 3, 1);
set_vector_metatable(L);
} }
void push_v2f(lua_State *L, v2f p) void push_v2f(lua_State *L, v2f p)
@ -160,12 +159,12 @@ v2f check_v2f(lua_State *L, int index)
v2f p; v2f p;
CHECK_POS_TAB(index); CHECK_POS_TAB(index);
lua_getfield(L, index, "x"); lua_getfield(L, index, "x");
CHECK_POS_COORD("x"); CHECK_POS_COORD(-1, "x");
p.X = lua_tonumber(L, -1); p.X = lua_tonumber(L, -1);
CHECK_FLOAT(p.X, "x"); CHECK_FLOAT(p.X, "x");
lua_pop(L, 1); lua_pop(L, 1);
lua_getfield(L, index, "y"); lua_getfield(L, index, "y");
CHECK_POS_COORD("y"); CHECK_POS_COORD(-1, "y");
p.Y = lua_tonumber(L, -1); p.Y = lua_tonumber(L, -1);
CHECK_FLOAT(p.Y, "y"); CHECK_FLOAT(p.Y, "y");
lua_pop(L, 1); lua_pop(L, 1);
@ -174,78 +173,48 @@ v2f check_v2f(lua_State *L, int index)
v3f read_v3f(lua_State *L, int index) v3f read_v3f(lua_State *L, int index)
{ {
v3f pos; read_v3_aux(L, index);
CHECK_POS_TAB(index); float x = lua_tonumber(L, -3);
lua_getfield(L, index, "x"); float y = lua_tonumber(L, -2);
pos.X = lua_tonumber(L, -1); float z = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 3);
lua_getfield(L, index, "y"); return v3f(x, y, z);
pos.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "z");
pos.Z = lua_tonumber(L, -1);
lua_pop(L, 1);
return pos;
} }
v3f check_v3f(lua_State *L, int index) v3f check_v3f(lua_State *L, int index)
{ {
v3f pos; read_v3_aux(L, index);
CHECK_POS_TAB(index); CHECK_POS_COORD(-3, "x");
lua_getfield(L, index, "x"); CHECK_POS_COORD(-2, "y");
CHECK_POS_COORD("x"); CHECK_POS_COORD(-1, "z");
pos.X = lua_tonumber(L, -1); float x = lua_tonumber(L, -3);
CHECK_FLOAT(pos.X, "x"); float y = lua_tonumber(L, -2);
lua_pop(L, 1); float z = lua_tonumber(L, -1);
lua_getfield(L, index, "y"); lua_pop(L, 3);
CHECK_POS_COORD("y"); return v3f(x, y, z);
pos.Y = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Y, "y");
lua_pop(L, 1);
lua_getfield(L, index, "z");
CHECK_POS_COORD("z");
pos.Z = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Z, "z");
lua_pop(L, 1);
return pos;
} }
v3d read_v3d(lua_State *L, int index) v3d read_v3d(lua_State *L, int index)
{ {
v3d pos; read_v3_aux(L, index);
CHECK_POS_TAB(index); double x = lua_tonumber(L, -3);
lua_getfield(L, index, "x"); double y = lua_tonumber(L, -2);
pos.X = lua_tonumber(L, -1); double z = lua_tonumber(L, -1);
lua_pop(L, 1); lua_pop(L, 3);
lua_getfield(L, index, "y"); return v3d(x, y, z);
pos.Y = lua_tonumber(L, -1);
lua_pop(L, 1);
lua_getfield(L, index, "z");
pos.Z = lua_tonumber(L, -1);
lua_pop(L, 1);
return pos;
} }
v3d check_v3d(lua_State *L, int index) v3d check_v3d(lua_State *L, int index)
{ {
v3d pos; read_v3_aux(L, index);
CHECK_POS_TAB(index); CHECK_POS_COORD(-3, "x");
lua_getfield(L, index, "x"); CHECK_POS_COORD(-2, "y");
CHECK_POS_COORD("x"); CHECK_POS_COORD(-1, "z");
pos.X = lua_tonumber(L, -1); double x = lua_tonumber(L, -3);
CHECK_FLOAT(pos.X, "x"); double y = lua_tonumber(L, -2);
lua_pop(L, 1); double z = lua_tonumber(L, -1);
lua_getfield(L, index, "y"); lua_pop(L, 3);
CHECK_POS_COORD("y"); return v3d(x, y, z);
pos.Y = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Y, "y");
lua_pop(L, 1);
lua_getfield(L, index, "z");
CHECK_POS_COORD("z");
pos.Z = lua_tonumber(L, -1);
CHECK_FLOAT(pos.Z, "z");
lua_pop(L, 1);
return pos;
} }
void push_ARGB8(lua_State *L, video::SColor color) void push_ARGB8(lua_State *L, video::SColor color)
@ -274,14 +243,11 @@ v3f checkFloatPos(lua_State *L, int index)
void push_v3s16(lua_State *L, v3s16 p) void push_v3s16(lua_State *L, v3s16 p)
{ {
lua_createtable(L, 0, 3); lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
lua_pushinteger(L, p.X); lua_pushinteger(L, p.X);
lua_setfield(L, -2, "x");
lua_pushinteger(L, p.Y); lua_pushinteger(L, p.Y);
lua_setfield(L, -2, "y");
lua_pushinteger(L, p.Z); lua_pushinteger(L, p.Z);
lua_setfield(L, -2, "z"); lua_call(L, 3, 1);
set_vector_metatable(L);
} }
v3s16 read_v3s16(lua_State *L, int index) v3s16 read_v3s16(lua_State *L, int index)

@ -56,8 +56,14 @@ enum {
CUSTOM_RIDX_CURRENT_MOD_NAME, CUSTOM_RIDX_CURRENT_MOD_NAME,
CUSTOM_RIDX_BACKTRACE, CUSTOM_RIDX_BACKTRACE,
CUSTOM_RIDX_HTTP_API_LUA, CUSTOM_RIDX_HTTP_API_LUA,
CUSTOM_RIDX_VECTOR_METATABLE,
CUSTOM_RIDX_METATABLE_MAP, CUSTOM_RIDX_METATABLE_MAP,
// The following four functions are implemented in Lua because LuaJIT can
// trace them and optimize tables/string better than from the C API.
CUSTOM_RIDX_READ_VECTOR,
CUSTOM_RIDX_PUSH_VECTOR,
CUSTOM_RIDX_READ_NODE,
CUSTOM_RIDX_PUSH_NODE,
}; };

@ -247,6 +247,7 @@ bool AsyncEngine::prepareEnvironment(lua_State* L, int top)
try { try {
script->loadMod(Server::getBuiltinLuaPath() + DIR_DELIM + "init.lua", script->loadMod(Server::getBuiltinLuaPath() + DIR_DELIM + "init.lua",
BUILTIN_MOD_NAME); BUILTIN_MOD_NAME);
script->checkSetByBuiltin();
} catch (const ModError &e) { } catch (const ModError &e) {
errorstream << "Execution of async base environment failed: " errorstream << "Execution of async base environment failed: "
<< e.what() << std::endl; << e.what() << std::endl;

@ -120,16 +120,32 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
#endif #endif
// Add basic globals // Add basic globals
lua_newtable(m_luastack);
lua_setglobal(m_luastack, "core");
// vector.metatable is stored in the registry for quick access from C++. // "core" table:
lua_newtable(m_luastack); lua_newtable(m_luastack);
lua_rawseti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE); // Populate with some internal functions which will be removed in Lua:
lua_newtable(m_luastack); lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawgeti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE); lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
lua_setfield(m_luastack, -2, "metatable"); return 0;
lua_setglobal(m_luastack, "vector"); });
lua_setfield(m_luastack, -2, "set_read_vector");
lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
return 0;
});
lua_setfield(m_luastack, -2, "set_push_vector");
lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_NODE);
return 0;
});
lua_setfield(m_luastack, -2, "set_read_node");
lua_pushcfunction(m_luastack, [](lua_State *L) -> int {
lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_NODE);
return 0;
});
lua_setfield(m_luastack, -2, "set_push_node");
// Finally, put the table into the global environment:
lua_setglobal(m_luastack, "core");
if (m_type == ScriptingType::Client) if (m_type == ScriptingType::Client)
lua_pushstring(m_luastack, "/"); lua_pushstring(m_luastack, "/");
@ -180,6 +196,29 @@ void ScriptApiBase::clientOpenLibs(lua_State *L)
} }
} }
void ScriptApiBase::checkSetByBuiltin()
{
lua_State *L = getStack();
if (m_gamedef) {
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_VECTOR);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing read_vector");
lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_VECTOR);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing push_vector");
lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_READ_NODE);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing read_node");
lua_pop(L, 1);
lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_PUSH_NODE);
FATAL_ERROR_IF(lua_type(L, -1) != LUA_TFUNCTION, "missing push_node");
lua_pop(L, 1);
}
}
void ScriptApiBase::loadMod(const std::string &script_path, void ScriptApiBase::loadMod(const std::string &script_path,
const std::string &mod_name) const std::string &mod_name)
{ {

@ -115,6 +115,9 @@ public:
void clientOpenLibs(lua_State *L); void clientOpenLibs(lua_State *L);
// Check things that should be set by the builtin mod.
void checkSetByBuiltin();
protected: protected:
friend class LuaABM; friend class LuaABM;
friend class LuaLBM; friend class LuaLBM;

@ -186,15 +186,13 @@ bool ScriptApiClient::on_dignode(v3s16 p, MapNode node)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
const NodeDefManager *ndef = getClient()->ndef();
// Get core.registered_on_dignode // Get core.registered_on_dignode
lua_getglobal(L, "core"); lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_dignode"); lua_getfield(L, -1, "registered_on_dignode");
// Push data // Push data
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, node, ndef); pushnode(L, node);
// Call functions // Call functions
try { try {
@ -210,15 +208,13 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
{ {
SCRIPTAPI_PRECHECKHEADER SCRIPTAPI_PRECHECKHEADER
const NodeDefManager *ndef = getClient()->ndef();
// Get core.registered_on_punchgnode // Get core.registered_on_punchgnode
lua_getglobal(L, "core"); lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_punchnode"); lua_getfield(L, -1, "registered_on_punchnode");
// Push data // Push data
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, node, ndef); pushnode(L, node);
// Call functions // Call functions
try { try {

@ -274,7 +274,6 @@ void ScriptApiEnv::on_liquid_transformed(
// Convert the list to a pos array and a node array for lua // Convert the list to a pos array and a node array for lua
int index = 1; int index = 1;
const NodeDefManager *ndef = getEnv()->getGameDef()->ndef();
lua_createtable(L, list.size(), 0); lua_createtable(L, list.size(), 0);
lua_createtable(L, list.size(), 0); lua_createtable(L, list.size(), 0);
for(std::pair<v3s16, MapNode> p : list) { for(std::pair<v3s16, MapNode> p : list) {
@ -282,7 +281,7 @@ void ScriptApiEnv::on_liquid_transformed(
push_v3s16(L, p.first); push_v3s16(L, p.first);
lua_rawset(L, -4); lua_rawset(L, -4);
lua_pushnumber(L, index++); lua_pushnumber(L, index++);
pushnode(L, p.second, ndef); pushnode(L, p.second);
lua_rawset(L, -3); lua_rawset(L, -3);
} }

@ -119,7 +119,7 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
// Call function // Call function
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, node, ndef); pushnode(L, node);
objectrefGetOrCreate(L, puncher); objectrefGetOrCreate(L, puncher);
pushPointedThing(pointed); pushPointedThing(pointed);
PCALL_RES(lua_pcall(L, 4, 0, error_handler)); PCALL_RES(lua_pcall(L, 4, 0, error_handler));
@ -142,7 +142,7 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
// Call function // Call function
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, node, ndef); pushnode(L, node);
objectrefGetOrCreate(L, digger); objectrefGetOrCreate(L, digger);
PCALL_RES(lua_pcall(L, 3, 1, error_handler)); PCALL_RES(lua_pcall(L, 3, 1, error_handler));
@ -204,8 +204,8 @@ bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode)
// Call function // Call function
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, node, ndef); pushnode(L, node);
pushnode(L, newnode, ndef); pushnode(L, newnode);
PCALL_RES(lua_pcall(L, 3, 1, error_handler)); PCALL_RES(lua_pcall(L, 3, 1, error_handler));
lua_remove(L, error_handler); lua_remove(L, error_handler);
return readParam<bool>(L, -1, false); return readParam<bool>(L, -1, false);
@ -225,7 +225,7 @@ void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
// Call function // Call function
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, node, ndef); pushnode(L, node);
PCALL_RES(lua_pcall(L, 2, 0, error_handler)); PCALL_RES(lua_pcall(L, 2, 0, error_handler));
lua_pop(L, 1); // Pop error handler lua_pop(L, 1); // Pop error handler
} }

@ -98,7 +98,6 @@ void ScriptApiSecurity::initializeSecurity()
"type", "type",
"unpack", "unpack",
"_VERSION", "_VERSION",
"vector",
"xpcall", "xpcall",
}; };
static const char *whitelist_tables[] = { static const char *whitelist_tables[] = {
@ -253,10 +252,6 @@ void ScriptApiSecurity::initializeSecurity()
lua_pushnil(L); lua_pushnil(L);
lua_setfield(L, old_globals, "core"); lua_setfield(L, old_globals, "core");
// 'vector' as well.
lua_pushnil(L);
lua_setfield(L, old_globals, "vector");
lua_pop(L, 1); // Pop globals_backup lua_pop(L, 1); // Pop globals_backup
@ -299,7 +294,6 @@ void ScriptApiSecurity::initializeSecurityClient()
"type", "type",
"unpack", "unpack",
"_VERSION", "_VERSION",
"vector",
"xpcall", "xpcall",
// Completely safe libraries // Completely safe libraries
"coroutine", "coroutine",

@ -220,7 +220,7 @@ int ModApiClient::l_get_node_or_nil(lua_State *L)
MapNode n = getClient(L)->CSMGetNode(pos, &pos_ok); MapNode n = getClient(L)->CSMGetNode(pos, &pos_ok);
if (pos_ok) { if (pos_ok) {
// Return node // Return node
pushnode(L, n, getClient(L)->ndef()); pushnode(L, n);
} else { } else {
lua_pushnil(L); lua_pushnil(L);
} }

@ -98,7 +98,7 @@ void LuaABM::trigger(ServerEnvironment *env, v3s16 p, MapNode n,
luaL_checktype(L, -1, LUA_TFUNCTION); luaL_checktype(L, -1, LUA_TFUNCTION);
lua_remove(L, -2); // Remove registered_abms[m_id] lua_remove(L, -2); // Remove registered_abms[m_id]
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, n, env->getGameDef()->ndef()); pushnode(L, n);
lua_pushnumber(L, active_object_count); lua_pushnumber(L, active_object_count);
lua_pushnumber(L, active_object_count_wider); lua_pushnumber(L, active_object_count_wider);
@ -140,7 +140,7 @@ void LuaLBM::trigger(ServerEnvironment *env, v3s16 p, MapNode n)
luaL_checktype(L, -1, LUA_TFUNCTION); luaL_checktype(L, -1, LUA_TFUNCTION);
lua_remove(L, -2); // Remove registered_lbms[m_id] lua_remove(L, -2); // Remove registered_lbms[m_id]
push_v3s16(L, p); push_v3s16(L, p);
pushnode(L, n, env->getGameDef()->ndef()); pushnode(L, n);
int result = lua_pcall(L, 2, 0, error_handler); int result = lua_pcall(L, 2, 0, error_handler);
if (result) if (result)
@ -247,10 +247,9 @@ int ModApiEnvMod::l_set_node(lua_State *L)
{ {
GET_ENV_PTR; GET_ENV_PTR;
const NodeDefManager *ndef = env->getGameDef()->ndef();
// parameters // parameters
v3s16 pos = read_v3s16(L, 1); v3s16 pos = read_v3s16(L, 1);
MapNode n = readnode(L, 2, ndef); MapNode n = readnode(L, 2);
// Do it // Do it
bool succeeded = env->setNode(pos, n); bool succeeded = env->setNode(pos, n);
lua_pushboolean(L, succeeded); lua_pushboolean(L, succeeded);
@ -263,7 +262,6 @@ int ModApiEnvMod::l_bulk_set_node(lua_State *L)
{ {
GET_ENV_PTR; GET_ENV_PTR;
const NodeDefManager *ndef = env->getGameDef()->ndef();
// parameters // parameters
if (!lua_istable(L, 1)) { if (!lua_istable(L, 1)) {
return 0; return 0;
@ -275,7 +273,7 @@ int ModApiEnvMod::l_bulk_set_node(lua_State *L)
return 1; return 1;
} }
MapNode n = readnode(L, 2, ndef); MapNode n = readnode(L, 2);
// Do it // Do it
bool succeeded = true; bool succeeded = true;
@ -315,10 +313,9 @@ int ModApiEnvMod::l_swap_node(lua_State *L)
{ {
GET_ENV_PTR; GET_ENV_PTR;
const NodeDefManager *ndef = env->getGameDef()->ndef();
// parameters // parameters
v3s16 pos = read_v3s16(L, 1); v3s16 pos = read_v3s16(L, 1);
MapNode n = readnode(L, 2, ndef); MapNode n = readnode(L, 2);
// Do it // Do it
bool succeeded = env->swapNode(pos, n); bool succeeded = env->swapNode(pos, n);
lua_pushboolean(L, succeeded); lua_pushboolean(L, succeeded);
@ -336,7 +333,7 @@ int ModApiEnvMod::l_get_node(lua_State *L)
// Do it // Do it
MapNode n = env->getMap().getNode(pos); MapNode n = env->getMap().getNode(pos);
// Return node // Return node
pushnode(L, n, env->getGameDef()->ndef()); pushnode(L, n);
return 1; return 1;
} }
@ -353,7 +350,7 @@ int ModApiEnvMod::l_get_node_or_nil(lua_State *L)
MapNode n = env->getMap().getNode(pos, &pos_ok); MapNode n = env->getMap().getNode(pos, &pos_ok);
if (pos_ok) { if (pos_ok) {
// Return node // Return node
pushnode(L, n, env->getGameDef()->ndef()); pushnode(L, n);
} else { } else {
lua_pushnil(L); lua_pushnil(L);
} }
@ -438,7 +435,7 @@ int ModApiEnvMod::l_place_node(lua_State *L)
IItemDefManager *idef = server->idef(); IItemDefManager *idef = server->idef();
v3s16 pos = read_v3s16(L, 1); v3s16 pos = read_v3s16(L, 1);
MapNode n = readnode(L, 2, ndef); MapNode n = readnode(L, 2);
// Don't attempt to load non-loaded area as of now // Don't attempt to load non-loaded area as of now
MapNode n_old = env->getMap().getNode(pos); MapNode n_old = env->getMap().getNode(pos);

@ -721,3 +721,10 @@ void ModApiItemMod::InitializeAsync(lua_State *L, int top)
API_FCT(get_content_id); API_FCT(get_content_id);
API_FCT(get_name_from_content_id); API_FCT(get_name_from_content_id);
} }
void ModApiItemMod::InitializeClient(lua_State *L, int top)
{
// all read-only functions
API_FCT(get_content_id);
API_FCT(get_name_from_content_id);
}

@ -174,4 +174,5 @@ private:
public: public:
static void Initialize(lua_State *L, int top); static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top); static void InitializeAsync(lua_State *L, int top);
static void InitializeClient(lua_State *L, int top);
}; };

@ -138,7 +138,7 @@ int ModApiParticles::l_add_particle(lua_State *L)
lua_getfield(L, 1, "node"); lua_getfield(L, 1, "node");
if (lua_istable(L, -1)) if (lua_istable(L, -1))
p.node = readnode(L, -1, getGameDef(L)->ndef()); p.node = readnode(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile); p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);
@ -289,7 +289,7 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
lua_getfield(L, 1, "node"); lua_getfield(L, 1, "node");
if (lua_istable(L, -1)) if (lua_istable(L, -1))
p.node = readnode(L, -1, getGameDef(L)->ndef()); p.node = readnode(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile); p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);

@ -87,7 +87,7 @@ int ModApiParticlesLocal::l_add_particle(lua_State *L)
lua_getfield(L, 1, "node"); lua_getfield(L, 1, "node");
if (lua_istable(L, -1)) if (lua_istable(L, -1))
p.node = readnode(L, -1, getGameDef(L)->ndef()); p.node = readnode(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile); p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);
@ -185,7 +185,7 @@ int ModApiParticlesLocal::l_add_particlespawner(lua_State *L)
lua_getfield(L, 1, "node"); lua_getfield(L, 1, "node");
if (lua_istable(L, -1)) if (lua_istable(L, -1))
p.node = readnode(L, -1, getGameDef(L)->ndef()); p.node = readnode(L, -1);
lua_pop(L, 1); lua_pop(L, 1);
p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile); p.node_tile = getintfield_default(L, 1, "node_tile", p.node_tile);

@ -139,12 +139,10 @@ int LuaVoxelManip::l_get_node_at(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
LuaVoxelManip *o = checkObject<LuaVoxelManip>(L, 1); LuaVoxelManip *o = checkObject<LuaVoxelManip>(L, 1);
v3s16 pos = check_v3s16(L, 2); v3s16 pos = check_v3s16(L, 2);
pushnode(L, o->vm->getNodeNoExNoEmerge(pos), ndef); pushnode(L, o->vm->getNodeNoExNoEmerge(pos));
return 1; return 1;
} }
@ -152,11 +150,9 @@ int LuaVoxelManip::l_set_node_at(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
LuaVoxelManip *o = checkObject<LuaVoxelManip>(L, 1); LuaVoxelManip *o = checkObject<LuaVoxelManip>(L, 1);
v3s16 pos = check_v3s16(L, 2); v3s16 pos = check_v3s16(L, 2);
MapNode n = readnode(L, 3, ndef); MapNode n = readnode(L, 3);
o->vm->setNodeNoEmerge(pos, n); o->vm->setNodeNoEmerge(pos, n);

@ -78,6 +78,7 @@ void ClientScripting::InitializeModApi(lua_State *L, int top)
ModApiUtil::InitializeClient(L, top); ModApiUtil::InitializeClient(L, top);
ModApiClient::Initialize(L, top); ModApiClient::Initialize(L, top);
ModApiItemMod::InitializeClient(L, top);
ModApiStorage::Initialize(L, top); ModApiStorage::Initialize(L, top);
ModApiEnvMod::InitializeClient(L, top); ModApiEnvMod::InitializeClient(L, top);
ModApiChannels::Initialize(L, top); ModApiChannels::Initialize(L, top);

@ -447,6 +447,7 @@ void Server::init()
m_inventory_mgr = std::make_unique<ServerInventoryManager>(); m_inventory_mgr = std::make_unique<ServerInventoryManager>();
m_script->loadMod(getBuiltinLuaPath() + DIR_DELIM "init.lua", BUILTIN_MOD_NAME); m_script->loadMod(getBuiltinLuaPath() + DIR_DELIM "init.lua", BUILTIN_MOD_NAME);
m_script->checkSetByBuiltin();
m_gamespec.checkAndLog(); m_gamespec.checkAndLog();
m_modmgr->loadMods(m_script); m_modmgr->loadMods(m_script);