aboutsummaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
Diffstat (limited to 'src/script')
-rw-r--r--src/script/common/CMakeLists.txt1
-rw-r--r--src/script/common/c_content.cpp91
-rw-r--r--src/script/common/c_content.h12
-rw-r--r--src/script/common/c_converter.cpp54
-rw-r--r--src/script/common/c_converter.h3
-rw-r--r--src/script/common/c_internal.cpp14
-rw-r--r--src/script/common/c_internal.h8
-rw-r--r--src/script/common/c_packer.cpp596
-rw-r--r--src/script/common/c_packer.h126
-rw-r--r--src/script/cpp_api/s_async.cpp183
-rw-r--r--src/script/cpp_api/s_async.h54
-rw-r--r--src/script/cpp_api/s_base.cpp15
-rw-r--r--src/script/cpp_api/s_base.h9
-rw-r--r--src/script/cpp_api/s_client.cpp2
-rw-r--r--src/script/cpp_api/s_entity.cpp2
-rw-r--r--src/script/cpp_api/s_entity.h2
-rw-r--r--src/script/cpp_api/s_env.cpp4
-rw-r--r--src/script/cpp_api/s_item.cpp21
-rw-r--r--src/script/cpp_api/s_item.h14
-rw-r--r--src/script/cpp_api/s_node.h1
-rw-r--r--src/script/cpp_api/s_player.cpp2
-rw-r--r--src/script/cpp_api/s_player.h2
-rw-r--r--src/script/cpp_api/s_security.cpp72
-rw-r--r--src/script/cpp_api/s_security.h15
-rw-r--r--src/script/cpp_api/s_server.cpp6
-rw-r--r--src/script/cpp_api/s_server.h2
-rw-r--r--src/script/lua_api/l_areastore.cpp49
-rw-r--r--src/script/lua_api/l_client.cpp2
-rw-r--r--src/script/lua_api/l_craft.cpp32
-rw-r--r--src/script/lua_api/l_craft.h1
-rw-r--r--src/script/lua_api/l_env.cpp35
-rw-r--r--src/script/lua_api/l_env.h2
-rw-r--r--src/script/lua_api/l_http.cpp69
-rw-r--r--src/script/lua_api/l_http.h4
-rw-r--r--src/script/lua_api/l_internal.h2
-rw-r--r--src/script/lua_api/l_inventory.cpp36
-rw-r--r--src/script/lua_api/l_inventory.h2
-rw-r--r--src/script/lua_api/l_item.cpp31
-rw-r--r--src/script/lua_api/l_item.h7
-rw-r--r--src/script/lua_api/l_localplayer.cpp21
-rw-r--r--src/script/lua_api/l_localplayer.h3
-rw-r--r--src/script/lua_api/l_mainmenu.cpp121
-rw-r--r--src/script/lua_api/l_mainmenu.h2
-rw-r--r--src/script/lua_api/l_metadata.cpp5
-rw-r--r--src/script/lua_api/l_nodemeta.cpp17
-rw-r--r--src/script/lua_api/l_noise.cpp67
-rw-r--r--src/script/lua_api/l_noise.h17
-rw-r--r--src/script/lua_api/l_object.cpp369
-rw-r--r--src/script/lua_api/l_object.h9
-rw-r--r--src/script/lua_api/l_server.cpp150
-rw-r--r--src/script/lua_api/l_server.h17
-rw-r--r--src/script/lua_api/l_storage.cpp18
-rw-r--r--src/script/lua_api/l_util.cpp105
-rw-r--r--src/script/lua_api/l_util.h15
-rw-r--r--src/script/lua_api/l_vmanip.cpp51
-rw-r--r--src/script/lua_api/l_vmanip.h5
-rw-r--r--src/script/scripting_server.cpp68
-rw-r--r--src/script/scripting_server.h17
58 files changed, 2060 insertions, 600 deletions
diff --git a/src/script/common/CMakeLists.txt b/src/script/common/CMakeLists.txt
index d07f6ab1b..3e84b46c7 100644
--- a/src/script/common/CMakeLists.txt
+++ b/src/script/common/CMakeLists.txt
@@ -3,6 +3,7 @@ set(common_SCRIPT_COMMON_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/c_converter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/c_types.cpp
${CMAKE_CURRENT_SOURCE_DIR}/c_internal.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/c_packer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/helper.cpp
PARENT_SCOPE)
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index 8ca3a722f..0bdeaab9e 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -751,6 +751,9 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
// the slowest possible
f.liquid_viscosity = getintfield_default(L, index,
"liquid_viscosity", f.liquid_viscosity);
+ // If move_resistance is not set explicitly,
+ // move_resistance is equal to liquid_viscosity
+ f.move_resistance = f.liquid_viscosity;
f.liquid_range = getintfield_default(L, index,
"liquid_range", f.liquid_range);
f.leveled = getintfield_default(L, index, "leveled", f.leveled);
@@ -854,6 +857,21 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
getstringfield(L, index, "node_dig_prediction",
f.node_dig_prediction);
+ // How much the node slows down players, ranging from 1 to 7,
+ // the higher, the slower.
+ f.move_resistance = getintfield_default(L, index,
+ "move_resistance", f.move_resistance);
+
+ // Whether e.g. players in this node will have liquid movement physics
+ lua_getfield(L, index, "liquid_move_physics");
+ if(lua_isboolean(L, -1)) {
+ f.liquid_move_physics = lua_toboolean(L, -1);
+ } else if(lua_isnil(L, -1)) {
+ f.liquid_move_physics = f.liquid_type != LIQUID_NONE;
+ } else {
+ errorstream << "Field \"liquid_move_physics\": Invalid type!" << std::endl;
+ }
+ lua_pop(L, 1);
}
void push_content_features(lua_State *L, const ContentFeatures &c)
@@ -1004,6 +1022,10 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
lua_setfield(L, -2, "legacy_wallmounted");
lua_pushstring(L, c.node_dig_prediction.c_str());
lua_setfield(L, -2, "node_dig_prediction");
+ lua_pushnumber(L, c.move_resistance);
+ lua_setfield(L, -2, "move_resistance");
+ lua_pushboolean(L, c.liquid_move_physics);
+ lua_setfield(L, -2, "liquid_move_physics");
}
/******************************************************************************/
@@ -1384,24 +1406,27 @@ void push_tool_capabilities(lua_State *L,
}
/******************************************************************************/
-void push_inventory(lua_State *L, Inventory *inventory)
+void push_inventory_list(lua_State *L, const InventoryList &invlist)
{
- if (! inventory)
- throw SerializationError("Attempt to push nonexistant inventory");
- std::vector<const InventoryList*> lists = inventory->getLists();
- std::vector<const InventoryList*>::iterator iter = lists.begin();
+ push_items(L, invlist.getItems());
+}
+
+/******************************************************************************/
+void push_inventory_lists(lua_State *L, const Inventory &inv)
+{
+ const auto &lists = inv.getLists();
lua_createtable(L, 0, lists.size());
- for (; iter != lists.end(); iter++) {
- const char* name = (*iter)->getName().c_str();
- lua_pushstring(L, name);
- push_inventory_list(L, inventory, name);
+ for(const InventoryList *list : lists) {
+ const std::string &name = list->getName();
+ lua_pushlstring(L, name.c_str(), name.size());
+ push_inventory_list(L, *list);
lua_rawset(L, -3);
}
}
/******************************************************************************/
void read_inventory_list(lua_State *L, int tableindex,
- Inventory *inv, const char *name, Server* srv, int forcesize)
+ Inventory *inv, const char *name, IGameDef *gdef, int forcesize)
{
if(tableindex < 0)
tableindex = lua_gettop(L) + 1 + tableindex;
@@ -1413,7 +1438,7 @@ void read_inventory_list(lua_State *L, int tableindex,
}
// Get Lua-specified items to insert into the list
- std::vector<ItemStack> items = read_items(L, tableindex,srv);
+ std::vector<ItemStack> items = read_items(L, tableindex, gdef);
size_t listsize = (forcesize >= 0) ? forcesize : items.size();
// Create or resize/clear list
@@ -1430,19 +1455,6 @@ void read_inventory_list(lua_State *L, int tableindex,
}
}
-void push_inventory_list(lua_State *L, Inventory *inv, const char *name)
-{
- InventoryList *invlist = inv->getList(name);
- if(invlist == NULL){
- lua_pushnil(L);
- return;
- }
- std::vector<ItemStack> items;
- for(u32 i=0; i<invlist->getSize(); i++)
- items.push_back(invlist->getItem(i));
- push_items(L, items);
-}
-
/******************************************************************************/
struct TileAnimationParams read_animation_definition(lua_State *L, int index)
{
@@ -1701,7 +1713,7 @@ void push_items(lua_State *L, const std::vector<ItemStack> &items)
}
/******************************************************************************/
-std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
+std::vector<ItemStack> read_items(lua_State *L, int index, IGameDef *gdef)
{
if(index < 0)
index = lua_gettop(L) + 1 + index;
@@ -1717,7 +1729,7 @@ std::vector<ItemStack> read_items(lua_State *L, int index, Server *srv)
if (items.size() < (u32) key) {
items.resize(key);
}
- items[key - 1] = read_item(L, -1, srv->idef());
+ items[key - 1] = read_item(L, -1, gdef->idef());
lua_pop(L, 1);
}
return items;
@@ -1768,24 +1780,19 @@ bool read_noiseparams(lua_State *L, int index, NoiseParams *np)
void push_noiseparams(lua_State *L, NoiseParams *np)
{
lua_newtable(L);
- push_float_string(L, np->offset);
- lua_setfield(L, -2, "offset");
- push_float_string(L, np->scale);
- lua_setfield(L, -2, "scale");
- push_float_string(L, np->persist);
- lua_setfield(L, -2, "persistence");
- push_float_string(L, np->lacunarity);
- lua_setfield(L, -2, "lacunarity");
- lua_pushnumber(L, np->seed);
- lua_setfield(L, -2, "seed");
- lua_pushnumber(L, np->octaves);
- lua_setfield(L, -2, "octaves");
+ setfloatfield(L, -1, "offset", np->offset);
+ setfloatfield(L, -1, "scale", np->scale);
+ setfloatfield(L, -1, "persist", np->persist);
+ setfloatfield(L, -1, "persistence", np->persist);
+ setfloatfield(L, -1, "lacunarity", np->lacunarity);
+ setintfield( L, -1, "seed", np->seed);
+ setintfield( L, -1, "octaves", np->octaves);
push_flags_string(L, flagdesc_noiseparams, np->flags,
np->flags);
lua_setfield(L, -2, "flags");
- push_v3_float_string(L, np->spread);
+ push_v3f(L, np->spread);
lua_setfield(L, -2, "spread");
}
@@ -2042,6 +2049,12 @@ void push_hud_element(lua_State *L, HudElement *elem)
lua_pushnumber(L, elem->number);
lua_setfield(L, -2, "number");
+ if (elem->type == HUD_ELEM_WAYPOINT) {
+ // waypoints reuse the item field to store precision, precision = item - 1
+ lua_pushnumber(L, elem->item - 1);
+ lua_setfield(L, -2, "precision");
+ }
+ // push the item field for waypoints as well for backwards compatibility
lua_pushnumber(L, elem->item);
lua_setfield(L, -2, "item");
diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h
index 1aed7901e..a6b96c012 100644
--- a/src/script/common/c_content.h
+++ b/src/script/common/c_content.h
@@ -55,10 +55,11 @@ struct ObjectProperties;
struct SimpleSoundSpec;
struct ServerSoundParams;
class Inventory;
+class InventoryList;
struct NodeBox;
struct ContentFeatures;
struct TileDef;
-class Server;
+class IGameDef;
struct DigParams;
struct HitParams;
struct EnumString;
@@ -124,11 +125,12 @@ void push_inventory (lua_State *L,
Inventory *inventory);
void push_inventory_list (lua_State *L,
- Inventory *inv,
- const char *name);
+ const InventoryList &invlist);
+void push_inventory_lists (lua_State *L,
+ const Inventory &inv);
void read_inventory_list (lua_State *L, int tableindex,
Inventory *inv, const char *name,
- Server *srv, int forcesize=-1);
+ IGameDef *gdef, int forcesize=-1);
MapNode readnode (lua_State *L, int index,
const NodeDefManager *ndef);
@@ -168,7 +170,7 @@ void push_items (lua_State *L,
std::vector<ItemStack> read_items (lua_State *L,
int index,
- Server* srv);
+ IGameDef* gdef);
void push_soundspec (lua_State *L,
const SimpleSoundSpec &spec);
diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp
index 19734b913..b5ff52f73 100644
--- a/src/script/common/c_converter.cpp
+++ b/src/script/common/c_converter.cpp
@@ -52,32 +52,12 @@ if (value < F1000_MIN || value > F1000_MAX) { \
/**
- * A helper which sets (if available) the vector metatable from builtin as metatable
- * for the table on top of the stack
+ * A helper which sets the vector metatable for the table on top of the stack
*/
static void set_vector_metatable(lua_State *L)
{
- // get vector.metatable
- lua_getglobal(L, "vector");
- if (!lua_istable(L, -1)) {
- // there is no global vector table
- lua_pop(L, 1);
- errorstream << "set_vector_metatable in c_converter.cpp: " <<
- "missing global vector table" << std::endl;
- return;
- }
- lua_getfield(L, -1, "metatable");
- // set the metatable
- lua_setmetatable(L, -3);
- // pop vector global
- lua_pop(L, 1);
-}
-
-
-void push_float_string(lua_State *L, float value)
-{
- auto str = ftos(value);
- lua_pushstring(L, str.c_str());
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE);
+ lua_setmetatable(L, -2);
}
void push_v3f(lua_State *L, v3f p)
@@ -101,26 +81,6 @@ void push_v2f(lua_State *L, v2f p)
lua_setfield(L, -2, "y");
}
-void push_v3_float_string(lua_State *L, v3f p)
-{
- lua_createtable(L, 0, 3);
- push_float_string(L, p.X);
- lua_setfield(L, -2, "x");
- push_float_string(L, p.Y);
- lua_setfield(L, -2, "y");
- push_float_string(L, p.Z);
- lua_setfield(L, -2, "z");
-}
-
-void push_v2_float_string(lua_State *L, v2f p)
-{
- lua_createtable(L, 0, 2);
- push_float_string(L, p.X);
- lua_setfield(L, -2, "x");
- push_float_string(L, p.Y);
- lua_setfield(L, -2, "y");
-}
-
v2s16 read_v2s16(lua_State *L, int index)
{
v2s16 p;
@@ -479,17 +439,9 @@ size_t read_stringlist(lua_State *L, int index, std::vector<std::string> *result
Table field getters
*/
-#if defined(__MINGW32__) && !defined(__MINGW64__)
-/* MinGW 32-bit somehow crashes in the std::set destructor when this
- * variable is thread-local, so just don't do that. */
-static std::set<u64> warned_msgs;
-#endif
-
bool check_field_or_nil(lua_State *L, int index, int type, const char *fieldname)
{
-#if !defined(__MINGW32__) || defined(__MINGW64__)
thread_local std::set<u64> warned_msgs;
-#endif
int t = lua_type(L, index);
if (t == LUA_TNIL)
diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h
index 6ad6f3212..a14eb9186 100644
--- a/src/script/common/c_converter.h
+++ b/src/script/common/c_converter.h
@@ -118,9 +118,6 @@ std::vector<aabb3f> read_aabb3f_vector (lua_State *L, int index, f32 scale);
size_t read_stringlist (lua_State *L, int index,
std::vector<std::string> *result);
-void push_float_string (lua_State *L, float value);
-void push_v3_float_string(lua_State *L, v3f p);
-void push_v2_float_string(lua_State *L, v2f p);
void push_v2s16 (lua_State *L, v2s16 p);
void push_v2s32 (lua_State *L, v2s32 p);
void push_v3s16 (lua_State *L, v3s16 p);
diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp
index df82dba14..ddd2d184c 100644
--- a/src/script/common/c_internal.cpp
+++ b/src/script/common/c_internal.cpp
@@ -166,3 +166,17 @@ void log_deprecated(lua_State *L, std::string message, int stack_depth)
infostream << script_get_backtrace(L) << std::endl;
}
+void call_string_dump(lua_State *L, int idx)
+{
+ // Retrieve string.dump from insecure env to avoid it being tampered with
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_GLOBALS_BACKUP);
+ if (!lua_isnil(L, -1))
+ lua_getfield(L, -1, "string");
+ else
+ lua_getglobal(L, "string");
+ lua_getfield(L, -1, "dump");
+ lua_remove(L, -2); // remove _G
+ lua_remove(L, -2); // remove 'string' table
+ lua_pushvalue(L, idx);
+ lua_call(L, 1, 1);
+}
diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h
index ab2d7b975..272a39941 100644
--- a/src/script/common/c_internal.h
+++ b/src/script/common/c_internal.h
@@ -54,6 +54,10 @@ extern "C" {
#define CUSTOM_RIDX_GLOBALS_BACKUP (CUSTOM_RIDX_BASE + 1)
#define CUSTOM_RIDX_CURRENT_MOD_NAME (CUSTOM_RIDX_BASE + 2)
#define CUSTOM_RIDX_BACKTRACE (CUSTOM_RIDX_BASE + 3)
+#define CUSTOM_RIDX_HTTP_API_LUA (CUSTOM_RIDX_BASE + 4)
+#define CUSTOM_RIDX_VECTOR_METATABLE (CUSTOM_RIDX_BASE + 5)
+#define CUSTOM_RIDX_METATABLE_MAP (CUSTOM_RIDX_BASE + 6)
+
// Determine if CUSTOM_RIDX_SCRIPTAPI will hold a light or full userdata
#if defined(__aarch64__) && USE_LUAJIT
@@ -136,3 +140,7 @@ DeprecatedHandlingMode get_deprecated_handling_mode();
* @param stack_depth How far on the stack to the first user function (ie: not builtin or core)
*/
void log_deprecated(lua_State *L, std::string message, int stack_depth = 1);
+
+// Safely call string.dump on a function value
+// (does not pop, leaves one value on stack)
+void call_string_dump(lua_State *L, int idx);
diff --git a/src/script/common/c_packer.cpp b/src/script/common/c_packer.cpp
new file mode 100644
index 000000000..ede00c758
--- /dev/null
+++ b/src/script/common/c_packer.cpp
@@ -0,0 +1,596 @@
+/*
+Minetest
+Copyright (C) 2022 sfan5 <sfan5@live.de>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include <cstdio>
+#include <cstring>
+#include <cmath>
+#include <cassert>
+#include <unordered_set>
+#include <unordered_map>
+#include "c_packer.h"
+#include "c_internal.h"
+#include "log.h"
+#include "debug.h"
+#include "threading/mutex_auto_lock.h"
+
+extern "C" {
+#include <lauxlib.h>
+}
+
+//
+// Helpers
+//
+
+// convert negative index to absolute position on Lua stack
+static inline int absidx(lua_State *L, int idx)
+{
+ assert(idx < 0);
+ return lua_gettop(L) + idx + 1;
+}
+
+// does the type put anything into PackedInstr::sdata?
+static inline bool uses_sdata(int type)
+{
+ switch (type) {
+ case LUA_TSTRING:
+ case LUA_TFUNCTION:
+ case LUA_TUSERDATA:
+ return true;
+ default:
+ return false;
+ }
+}
+
+// does the type put anything into PackedInstr::<union>?
+static inline bool uses_union(int type)
+{
+ switch (type) {
+ case LUA_TNIL:
+ case LUA_TSTRING:
+ case LUA_TFUNCTION:
+ return false;
+ default:
+ return true;
+ }
+}
+
+static inline bool can_set_into(int ktype, int vtype)
+{
+ switch (ktype) {
+ case LUA_TNUMBER:
+ return !uses_union(vtype);
+ case LUA_TSTRING:
+ return !uses_sdata(vtype);
+ default:
+ return false;
+ }
+}
+
+// is the key suitable for use with set_into?
+static inline bool suitable_key(lua_State *L, int idx)
+{
+ if (lua_type(L, idx) == LUA_TSTRING) {
+ // strings may not have a NULL byte (-> lua_setfield)
+ size_t len;
+ const char *str = lua_tolstring(L, idx, &len);
+ return strlen(str) == len;
+ } else {
+ assert(lua_type(L, idx) == LUA_TNUMBER);
+ // numbers must fit into an s32 and be integers (-> lua_rawseti)
+ lua_Number n = lua_tonumber(L, idx);
+ return std::floor(n) == n && n >= S32_MIN && n <= S32_MAX;
+ }
+}
+
+namespace {
+ // checks if you left any values on the stack, for debugging
+ class StackChecker {
+ lua_State *L;
+ int top;
+ public:
+ StackChecker(lua_State *L) : L(L), top(lua_gettop(L)) {}
+ ~StackChecker() {
+ assert(lua_gettop(L) >= top);
+ if (lua_gettop(L) > top) {
+ rawstream << "Lua stack not cleaned up: "
+ << lua_gettop(L) << " != " << top
+ << " (false-positive if exception thrown)" << std::endl;
+ }
+ }
+ };
+
+ // Since an std::vector may reallocate, this is the only safe way to keep
+ // a reference to a particular element.
+ template <typename T>
+ class VectorRef {
+ std::vector<T> *vec;
+ size_t idx;
+ VectorRef(std::vector<T> *vec, size_t idx) : vec(vec), idx(idx) {}
+ public:
+ constexpr VectorRef() : vec(nullptr), idx(0) {}
+ static VectorRef<T> front(std::vector<T> &vec) {
+ return VectorRef(&vec, 0);
+ }
+ static VectorRef<T> back(std::vector<T> &vec) {
+ return VectorRef(&vec, vec.size() - 1);
+ }
+ T &operator*() { return (*vec)[idx]; }
+ T *operator->() { return &(*vec)[idx]; }
+ operator bool() const { return vec != nullptr; }
+ };
+
+ struct Packer {
+ PackInFunc fin;
+ PackOutFunc fout;
+ };
+
+ typedef std::pair<std::string, Packer> PackerTuple;
+}
+
+static inline auto emplace(PackedValue &pv, s16 type)
+{
+ pv.i.emplace_back();
+ auto ref = VectorRef<PackedInstr>::back(pv.i);
+ ref->type = type;
+ // Initialize fields that may be left untouched
+ if (type == LUA_TTABLE) {
+ ref->uidata1 = 0;
+ ref->uidata2 = 0;
+ } else if (type == LUA_TUSERDATA) {
+ ref->ptrdata = nullptr;
+ } else if (type == INSTR_POP) {
+ ref->sidata2 = 0;
+ }
+ return ref;
+}
+
+//
+// Management of registered packers
+//
+
+static std::unordered_map<std::string, Packer> g_packers;
+static std::mutex g_packers_lock;
+
+void script_register_packer(lua_State *L, const char *regname,
+ PackInFunc fin, PackOutFunc fout)
+{
+ // Store away callbacks
+ {
+ MutexAutoLock autolock(g_packers_lock);
+ auto it = g_packers.find(regname);
+ if (it == g_packers.end()) {
+ auto &ref = g_packers[regname];
+ ref.fin = fin;
+ ref.fout = fout;
+ } else {
+ FATAL_ERROR_IF(it->second.fin != fin || it->second.fout != fout,
+ "Packer registered twice with mismatching callbacks");
+ }
+ }
+
+ // Save metatable so we can identify instances later
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_METATABLE_MAP);
+ if (lua_isnil(L, -1)) {
+ lua_newtable(L);
+ lua_pushvalue(L, -1);
+ lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_METATABLE_MAP);
+ }
+
+ luaL_getmetatable(L, regname);
+ FATAL_ERROR_IF(lua_isnil(L, -1), "No metatable registered with that name");
+
+ // CUSTOM_RIDX_METATABLE_MAP contains { [metatable] = "regname", ... }
+ // check first
+ lua_pushstring(L, regname);
+ lua_rawget(L, -3);
+ if (!lua_isnil(L, -1)) {
+ FATAL_ERROR_IF(lua_topointer(L, -1) != lua_topointer(L, -2),
+ "Packer registered twice with inconsistent metatable");
+ }
+ lua_pop(L, 1);
+ // then set
+ lua_pushstring(L, regname);
+ lua_rawset(L, -3);
+
+ lua_pop(L, 1);
+}
+
+static bool find_packer(const char *regname, PackerTuple &out)
+{
+ MutexAutoLock autolock(g_packers_lock);
+ auto it = g_packers.find(regname);
+ if (it == g_packers.end())
+ return false;
+ // copy data for thread safety
+ out.first = it->first;
+ out.second = it->second;
+ return true;
+}
+
+static bool find_packer(lua_State *L, int idx, PackerTuple &out)
+{
+#ifndef NDEBUG
+ StackChecker checker(L);
+#endif
+
+ // retrieve metatable of the object
+ if (lua_getmetatable(L, idx) != 1)
+ return false;
+
+ // use our global table to map it to the registry name
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_METATABLE_MAP);
+ assert(lua_istable(L, -1));
+ lua_pushvalue(L, -2);
+ lua_rawget(L, -2);
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 3);
+ return false;
+ }
+
+ // load the associated data
+ bool found = find_packer(lua_tostring(L, -1), out);
+ FATAL_ERROR_IF(!found, "Inconsistent internal state");
+ lua_pop(L, 3);
+ return true;
+}
+
+//
+// Packing implementation
+//
+
+static VectorRef<PackedInstr> record_object(lua_State *L, int idx, PackedValue &pv,
+ std::unordered_map<const void *, s32> &seen)
+{
+ const void *ptr = lua_topointer(L, idx);
+ assert(ptr);
+ auto found = seen.find(ptr);
+ if (found == seen.end()) {
+ seen[ptr] = pv.i.size();
+ return VectorRef<PackedInstr>();
+ }
+ s32 ref = found->second;
+ assert(ref < (s32)pv.i.size());
+ // reuse the value from first time
+ auto r = emplace(pv, INSTR_PUSHREF);
+ r->ref = ref;
+ pv.i[ref].keep_ref = true;
+ return r;
+}
+
+static VectorRef<PackedInstr> pack_inner(lua_State *L, int idx, int vidx, PackedValue &pv,
+ std::unordered_map<const void *, s32> &seen)
+{
+#ifndef NDEBUG
+ StackChecker checker(L);
+ assert(idx > 0);
+ assert(vidx > 0);
+#endif
+
+ switch (lua_type(L, idx)) {
+ case LUA_TNONE:
+ case LUA_TNIL:
+ return emplace(pv, LUA_TNIL);
+ case LUA_TBOOLEAN: {
+ auto r = emplace(pv, LUA_TBOOLEAN);
+ r->bdata = lua_toboolean(L, idx);
+ return r;
+ }
+ case LUA_TNUMBER: {
+ auto r = emplace(pv, LUA_TNUMBER);
+ r->ndata = lua_tonumber(L, idx);
+ return r;
+ }
+ case LUA_TSTRING: {
+ auto r = emplace(pv, LUA_TSTRING);
+ size_t len;
+ const char *str = lua_tolstring(L, idx, &len);
+ assert(str);
+ r->sdata.assign(str, len);
+ return r;
+ }
+ case LUA_TTABLE: {
+ auto r = record_object(L, idx, pv, seen);
+ if (r)
+ return r;
+ break; // execution continues
+ }
+ case LUA_TFUNCTION: {
+ auto r = record_object(L, idx, pv, seen);
+ if (r)
+ return r;
+ r = emplace(pv, LUA_TFUNCTION);
+ call_string_dump(L, idx);
+ size_t len;
+ const char *str = lua_tolstring(L, -1, &len);
+ assert(str);
+ r->sdata.assign(str, len);
+ lua_pop(L, 1);
+ return r;
+ }
+ case LUA_TUSERDATA: {
+ auto r = record_object(L, idx, pv, seen);
+ if (r)
+ return r;
+ PackerTuple ser;
+ if (!find_packer(L, idx, ser))
+ throw LuaError("Cannot serialize unsupported userdata");
+ pv.contains_userdata = true;
+ r = emplace(pv, LUA_TUSERDATA);
+ r->sdata = ser.first;
+ r->ptrdata = ser.second.fin(L, idx);
+ return r;
+ }
+ default: {
+ std::string err = "Cannot serialize type ";
+ err += lua_typename(L, lua_type(L, idx));
+ throw LuaError(err);
+ }
+ }
+
+ // LUA_TTABLE
+ lua_checkstack(L, 5);
+
+ auto rtable = emplace(pv, LUA_TTABLE);
+ const int vi_table = vidx++;
+
+ lua_pushnil(L);
+ while (lua_next(L, idx) != 0) {
+ // key at -2, value at -1
+ const int ktype = lua_type(L, -2), vtype = lua_type(L, -1);
+ if (ktype == LUA_TNUMBER)
+ rtable->uidata1++; // narr
+ else
+ rtable->uidata2++; // nrec
+
+ // check if we can use a shortcut
+ if (can_set_into(ktype, vtype) && suitable_key(L, -2)) {
+ // push only the value
+ auto rval = pack_inner(L, absidx(L, -1), vidx, pv, seen);
+ rval->pop = rval->type != LUA_TTABLE;
+ // and where to put it:
+ rval->set_into = vi_table;
+ if (ktype == LUA_TSTRING)
+ rval->sdata = lua_tostring(L, -2);
+ else
+ rval->sidata1 = lua_tointeger(L, -2);
+ // pop tables after the fact
+ if (!rval->pop) {
+ auto ri1 = emplace(pv, INSTR_POP);
+ ri1->sidata1 = vidx;
+ }
+ } else {
+ // push the key and value
+ pack_inner(L, absidx(L, -2), vidx, pv, seen);
+ vidx++;
+ pack_inner(L, absidx(L, -1), vidx, pv, seen);
+ vidx++;
+ // push an instruction to set them
+ auto ri1 = emplace(pv, INSTR_SETTABLE);
+ ri1->set_into = vi_table;
+ ri1->sidata1 = vidx - 2;
+ ri1->sidata2 = vidx - 1;
+ ri1->pop = true;
+ vidx -= 2;
+ }
+
+ lua_pop(L, 1);
+ }
+
+ assert(vidx == vi_table + 1);
+ return rtable;
+}
+
+PackedValue *script_pack(lua_State *L, int idx)
+{
+ if (idx < 0)
+ idx = absidx(L, idx);
+
+ PackedValue pv;
+ std::unordered_map<const void *, s32> seen;
+ pack_inner(L, idx, 1, pv, seen);
+
+ return new PackedValue(std::move(pv));
+}
+
+//
+// Unpacking implementation
+//
+
+void script_unpack(lua_State *L, PackedValue *pv)
+{
+ lua_newtable(L); // table at index top to track ref indices -> objects
+ const int top = lua_gettop(L);
+ int ctr = 0;
+
+ for (size_t packed_idx = 0; packed_idx < pv->i.size(); packed_idx++) {
+ auto &i = pv->i[packed_idx];
+
+ // If leaving values on stack make sure there's space (every 5th iteration)
+ if (!i.pop && (ctr++) >= 5) {
+ lua_checkstack(L, 5);
+ ctr = 0;
+ }
+
+ switch (i.type) {
+ /* Instructions */
+ case INSTR_SETTABLE:
+ lua_pushvalue(L, top + i.sidata1); // key
+ lua_pushvalue(L, top + i.sidata2); // value
+ lua_rawset(L, top + i.set_into);
+ if (i.pop) {
+ if (i.sidata1 != i.sidata2) {
+ // removing moves indices so pop higher index first
+ lua_remove(L, top + std::max(i.sidata1, i.sidata2));
+ lua_remove(L, top + std::min(i.sidata1, i.sidata2));
+ } else {
+ lua_remove(L, top + i.sidata1);
+ }
+ }
+ continue;
+ case INSTR_POP:
+ lua_remove(L, top + i.sidata1);
+ if (i.sidata2 > 0)
+ lua_remove(L, top + i.sidata2);
+ continue;
+ case INSTR_PUSHREF:
+ lua_pushinteger(L, i.ref);
+ lua_rawget(L, top);
+ break;
+
+ /* Lua types */
+ case LUA_TNIL:
+ lua_pushnil(L);
+ break;
+ case LUA_TBOOLEAN:
+ lua_pushboolean(L, i.bdata);
+ break;
+ case LUA_TNUMBER:
+ lua_pushnumber(L, i.ndata);
+ break;
+ case LUA_TSTRING:
+ lua_pushlstring(L, i.sdata.data(), i.sdata.size());
+ break;
+ case LUA_TTABLE:
+ lua_createtable(L, i.uidata1, i.uidata2);
+ break;
+ case LUA_TFUNCTION:
+ luaL_loadbuffer(L, i.sdata.data(), i.sdata.size(), nullptr);
+ break;
+ case LUA_TUSERDATA: {
+ PackerTuple ser;
+ sanity_check(find_packer(i.sdata.c_str(), ser));
+ ser.second.fout(L, i.ptrdata);
+ i.ptrdata = nullptr; // ownership taken by callback
+ break;
+ }
+
+ default:
+ assert(0);
+ break;
+ }
+
+ if (i.keep_ref) {
+ lua_pushinteger(L, packed_idx);
+ lua_pushvalue(L, -2);
+ lua_rawset(L, top);
+ }
+
+ if (i.set_into) {
+ if (!i.pop)
+ lua_pushvalue(L, -1);
+ if (uses_sdata(i.type))
+ lua_rawseti(L, top + i.set_into, i.sidata1);
+ else
+ lua_setfield(L, top + i.set_into, i.sdata.c_str());
+ } else {
+ if (i.pop)
+ lua_pop(L, 1);
+ }
+ }
+
+ // as part of the unpacking process we take ownership of all userdata
+ pv->contains_userdata = false;
+ // leave exactly one value on the stack
+ lua_settop(L, top+1);
+ lua_remove(L, top);
+}
+
+//
+// PackedValue
+//
+
+PackedValue::~PackedValue()
+{
+ if (!contains_userdata)
+ return;
+ for (auto &i : this->i) {
+ if (i.type == LUA_TUSERDATA && i.ptrdata) {
+ PackerTuple ser;
+ if (find_packer(i.sdata.c_str(), ser)) {
+ // tell it to deallocate object
+ ser.second.fout(nullptr, i.ptrdata);
+ } else {
+ assert(false);
+ }
+ }
+ }
+}
+
+//
+// script_dump_packed
+//
+
+#ifndef NDEBUG
+void script_dump_packed(const PackedValue *val)
+{
+ printf("instruction stream: [\n");
+ for (const auto &i : val->i) {
+ printf("\t(");
+ switch (i.type) {
+ case INSTR_SETTABLE:
+ printf("SETTABLE(%d, %d)", i.sidata1, i.sidata2);
+ break;
+ case INSTR_POP:
+ printf(i.sidata2 ? "POP(%d, %d)" : "POP(%d)", i.sidata1, i.sidata2);
+ break;
+ case INSTR_PUSHREF:
+ printf("PUSHREF(%d)", i.ref);
+ break;
+ case LUA_TNIL:
+ printf("nil");
+ break;
+ case LUA_TBOOLEAN:
+ printf(i.bdata ? "true" : "false");
+ break;
+ case LUA_TNUMBER:
+ printf("%f", i.ndata);
+ break;
+ case LUA_TSTRING:
+ printf("\"%s\"", i.sdata.c_str());
+ break;
+ case LUA_TTABLE:
+ printf("table(%d, %d)", i.uidata1, i.uidata2);
+ break;
+ case LUA_TFUNCTION:
+ printf("function(%d byte)", i.sdata.size());
+ break;
+ case LUA_TUSERDATA:
+ printf("userdata %s %p", i.sdata.c_str(), i.ptrdata);
+ break;
+ default:
+ printf("!!UNKNOWN!!");
+ break;
+ }
+ if (i.set_into) {
+ if (i.type >= 0 && uses_sdata(i.type))
+ printf(", k=%d, into=%d", i.sidata1, i.set_into);
+ else if (i.type >= 0)
+ printf(", k=\"%s\", into=%d", i.sdata.c_str(), i.set_into);
+ else
+ printf(", into=%d", i.set_into);
+ }
+ if (i.keep_ref)
+ printf(", keep_ref");
+ if (i.pop)
+ printf(", pop");
+ printf(")\n");
+ }
+ printf("]\n");
+}
+#endif
diff --git a/src/script/common/c_packer.h b/src/script/common/c_packer.h
new file mode 100644
index 000000000..fe072c10a
--- /dev/null
+++ b/src/script/common/c_packer.h
@@ -0,0 +1,126 @@
+/*
+Minetest
+Copyright (C) 2022 sfan5 <sfan5@live.de>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#pragma once
+
+#include <string>
+#include <vector>
+#include "irrlichttypes.h"
+#include "util/basic_macros.h"
+
+extern "C" {
+#include <lua.h>
+}
+
+/*
+ This file defines an in-memory representation of Lua objects including
+ support for functions and userdata. It it used to move data between Lua
+ states and cannot be used for persistence or network transfer.
+*/
+
+#define INSTR_SETTABLE (-10)
+#define INSTR_POP (-11)
+#define INSTR_PUSHREF (-12)
+
+/**
+ * Represents a single instruction that pushes a new value or works with existing ones.
+ */
+struct PackedInstr
+{
+ s16 type; // LUA_T* or INSTR_*
+ u16 set_into; // set into table on stack
+ bool keep_ref; // is referenced later by INSTR_PUSHREF?
+ bool pop; // remove from stack?
+ union {
+ bool bdata; // boolean: value
+ lua_Number ndata; // number: value
+ struct {
+ u16 uidata1, uidata2; // table: narr, nrec
+ };
+ struct {
+ /*
+ SETTABLE: key index, value index
+ POP: indices to remove
+ otherwise w/ set_into: numeric key, -
+ */
+ s32 sidata1, sidata2;
+ };
+ void *ptrdata; // userdata: implementation defined
+ s32 ref; // PUSHREF: index of referenced instr
+ };
+ /*
+ - string: value
+ - function: buffer
+ - w/ set_into: string key (no null bytes!)
+ - userdata: name in registry
+ */
+ std::string sdata;
+
+ PackedInstr() : type(0), set_into(0), keep_ref(false), pop(false) {}
+};
+
+/**
+ * A packed value can be a primitive like a string or number but also a table
+ * including all of its contents. It is made up of a linear stream of
+ * 'instructions' that build the final value when executed.
+ */
+struct PackedValue
+{
+ std::vector<PackedInstr> i;
+ // Indicates whether there are any userdata pointers that need to be deallocated
+ bool contains_userdata = false;
+
+ PackedValue() = default;
+ ~PackedValue();
+
+ DISABLE_CLASS_COPY(PackedValue)
+
+ ALLOW_CLASS_MOVE(PackedValue)
+};
+
+/*
+ * Packing callback: Turns a Lua value at given index into a void*
+ */
+typedef void *(*PackInFunc)(lua_State *L, int idx);
+/*
+ * Unpacking callback: Turns a void* back into the Lua value (left on top of stack)
+ *
+ * Note that this function must take ownership of the pointer, so make sure
+ * to free or keep the memory.
+ * `L` can be nullptr to indicate that data should just be discarded.
+ */
+typedef void (*PackOutFunc)(lua_State *L, void *ptr);
+/*
+ * Register a packable type with the name of its metatable.
+ *
+ * Even though the callbacks are global this must be called for every Lua state
+ * that supports objects of this type.
+ * This function is thread-safe.
+ */
+void script_register_packer(lua_State *L, const char *regname,
+ PackInFunc fin, PackOutFunc fout);
+
+// Pack a Lua value
+PackedValue *script_pack(lua_State *L, int idx);
+// Unpack a Lua value (left on top of stack)
+// Note that this may modify the PackedValue, reusability is not guaranteed!
+void script_unpack(lua_State *L, PackedValue *val);
+
+// Dump contents of PackedValue to stdout for debugging
+void script_dump_packed(const PackedValue *val);
diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp
index dacdcd75a..42a794ceb 100644
--- a/src/script/cpp_api/s_async.cpp
+++ b/src/script/cpp_api/s_async.cpp
@@ -21,9 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cstdlib>
extern "C" {
-#include "lua.h"
-#include "lauxlib.h"
-#include "lualib.h"
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
}
#include "server.h"
@@ -32,6 +32,7 @@ extern "C" {
#include "filesys.h"
#include "porting.h"
#include "common/c_internal.h"
+#include "common/c_packer.h"
#include "lua_api/l_base.h"
/******************************************************************************/
@@ -76,19 +77,34 @@ void AsyncEngine::initialize(unsigned int numEngines)
{
initDone = true;
- for (unsigned int i = 0; i < numEngines; i++) {
- AsyncWorkerThread *toAdd = new AsyncWorkerThread(this,
- std::string("AsyncWorker-") + itos(i));
- workerThreads.push_back(toAdd);
- toAdd->start();
+ if (numEngines == 0) {
+ // Leave one core for the main thread and one for whatever else
+ autoscaleMaxWorkers = Thread::getNumberOfProcessors();
+ if (autoscaleMaxWorkers >= 2)
+ autoscaleMaxWorkers -= 2;
+ infostream << "AsyncEngine: using at most " << autoscaleMaxWorkers
+ << " threads with automatic scaling" << std::endl;
+
+ addWorkerThread();
+ } else {
+ for (unsigned int i = 0; i < numEngines; i++)
+ addWorkerThread();
}
}
+void AsyncEngine::addWorkerThread()
+{
+ AsyncWorkerThread *toAdd = new AsyncWorkerThread(this,
+ std::string("AsyncWorker-") + itos(workerThreads.size()));
+ workerThreads.push_back(toAdd);
+ toAdd->start();
+}
+
/******************************************************************************/
u32 AsyncEngine::queueAsyncJob(std::string &&func, std::string &&params,
const std::string &mod_origin)
{
- jobQueueMutex.lock();
+ MutexAutoLock autolock(jobQueueMutex);
u32 jobId = jobIdCounter++;
jobQueue.emplace_back();
@@ -99,7 +115,23 @@ u32 AsyncEngine::queueAsyncJob(std::string &&func, std::string &&params,
to_add.mod_origin = mod_origin;
jobQueueCounter.post();
- jobQueueMutex.unlock();
+ return jobId;
+}
+
+u32 AsyncEngine::queueAsyncJob(std::string &&func, PackedValue *params,
+ const std::string &mod_origin)
+{
+ MutexAutoLock autolock(jobQueueMutex);
+ u32 jobId = jobIdCounter++;
+
+ jobQueue.emplace_back();
+ auto &to_add = jobQueue.back();
+ to_add.id = jobId;
+ to_add.function = std::move(func);
+ to_add.params_ext.reset(params);
+ to_add.mod_origin = mod_origin;
+
+ jobQueueCounter.post();
return jobId;
}
@@ -132,6 +164,12 @@ void AsyncEngine::putJobResult(LuaJobInfo &&result)
/******************************************************************************/
void AsyncEngine::step(lua_State *L)
{
+ stepJobResults(L);
+ stepAutoscale();
+}
+
+void AsyncEngine::stepJobResults(lua_State *L)
+{
int error_handler = PUSH_ERROR_HANDLER(L);
lua_getglobal(L, "core");
@@ -148,7 +186,10 @@ void AsyncEngine::step(lua_State *L)
luaL_checktype(L, -1, LUA_TFUNCTION);
lua_pushinteger(L, j.id);
- lua_pushlstring(L, j.result.data(), j.result.size());
+ if (j.result_ext)
+ script_unpack(L, j.result_ext.get());
+ else
+ lua_pushlstring(L, j.result.data(), j.result.size());
// Call handler
const char *origin = j.mod_origin.empty() ? nullptr : j.mod_origin.c_str();
@@ -161,12 +202,71 @@ void AsyncEngine::step(lua_State *L)
lua_pop(L, 2); // Pop core and error handler
}
+void AsyncEngine::stepAutoscale()
+{
+ if (workerThreads.size() >= autoscaleMaxWorkers)
+ return;
+
+ MutexAutoLock autolock(jobQueueMutex);
+
+ // 2) If the timer elapsed, check again
+ if (autoscaleTimer && porting::getTimeMs() >= autoscaleTimer) {
+ autoscaleTimer = 0;
+ // Determine overlap with previous snapshot
+ unsigned int n = 0;
+ for (const auto &it : jobQueue)
+ n += autoscaleSeenJobs.count(it.id);
+ autoscaleSeenJobs.clear();
+ infostream << "AsyncEngine: " << n << " jobs were still waiting after 1s" << std::endl;
+ // Start this many new threads
+ while (workerThreads.size() < autoscaleMaxWorkers && n > 0) {
+ addWorkerThread();
+ n--;
+ }
+ return;
+ }
+
+ // 1) Check if there's anything in the queue
+ if (!autoscaleTimer && !jobQueue.empty()) {
+ // Take a snapshot of all jobs we have seen
+ for (const auto &it : jobQueue)
+ autoscaleSeenJobs.emplace(it.id);
+ // and set a timer for 1 second
+ autoscaleTimer = porting::getTimeMs() + 1000;
+ }
+}
+
/******************************************************************************/
-void AsyncEngine::prepareEnvironment(lua_State* L, int top)
+bool AsyncEngine::prepareEnvironment(lua_State* L, int top)
{
for (StateInitializer &stateInitializer : stateInitializers) {
stateInitializer(L, top);
}
+
+ auto *script = ModApiBase::getScriptApiBase(L);
+ try {
+ script->loadMod(Server::getBuiltinLuaPath() + DIR_DELIM + "init.lua",
+ BUILTIN_MOD_NAME);
+ } catch (const ModError &e) {
+ errorstream << "Execution of async base environment failed: "
+ << e.what() << std::endl;
+ FATAL_ERROR("Execution of async base environment failed");
+ }
+
+ // Load per mod stuff
+ if (server) {
+ const auto &list = server->m_async_init_files;
+ try {
+ for (auto &it : list)
+ script->loadMod(it.second, it.first);
+ } catch (const ModError &e) {
+ errorstream << "Failed to load mod script inside async environment." << std::endl;
+ server->setAsyncFatalError(e.what());
+ return false;
+ }
+ }
+
+ return true;
}
/******************************************************************************/
@@ -178,15 +278,25 @@ AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher,
{
lua_State *L = getStack();
+ if (jobDispatcher->server) {
+ setGameDef(jobDispatcher->server);
+
+ if (g_settings->getBool("secure.enable_security"))
+ initializeSecurity();
+ }
+
// Prepare job lua environment
lua_getglobal(L, "core");
int top = lua_gettop(L);
// Push builtin initialization type
- lua_pushstring(L, "async");
+ lua_pushstring(L, jobDispatcher->server ? "async_game" : "async");
lua_setglobal(L, "INIT");
- jobDispatcher->prepareEnvironment(L, top);
+ if (!jobDispatcher->prepareEnvironment(L, top)) {
+ // can't throw from here so we're stuck with this
+ isErrored = true;
+ }
}
/******************************************************************************/
@@ -198,19 +308,20 @@ AsyncWorkerThread::~AsyncWorkerThread()
/******************************************************************************/
void* AsyncWorkerThread::run()
{
- lua_State *L = getStack();
+ if (isErrored)
+ return nullptr;
- try {
- loadMod(getServer()->getBuiltinLuaPath() + DIR_DELIM + "init.lua",
- BUILTIN_MOD_NAME);
- } catch (const ModError &e) {
- errorstream << "Execution of async base environment failed: "
- << e.what() << std::endl;
- FATAL_ERROR("Execution of async base environment failed");
- }
+ lua_State *L = getStack();
int error_handler = PUSH_ERROR_HANDLER(L);
+ auto report_error = [this] (const ModError &e) {
+ if (jobDispatcher->server)
+ jobDispatcher->server->setAsyncFatalError(e.what());
+ else
+ errorstream << e.what() << std::endl;
+ };
+
lua_getglobal(L, "core");
if (lua_isnil(L, -1)) {
FATAL_ERROR("Unable to find core within async environment!");
@@ -223,6 +334,8 @@ void* AsyncWorkerThread::run()
if (!jobDispatcher->getJob(&j) || stopRequested())
continue;
+ const bool use_ext = !!j.params_ext;
+
lua_getfield(L, -1, "job_processor");
if (lua_isnil(L, -1))
FATAL_ERROR("Unable to get async job processor!");
@@ -232,7 +345,10 @@ void* AsyncWorkerThread::run()
errorstream << "ASYNC WORKER: Unable to deserialize function" << std::endl;
lua_pushnil(L);
}
- lua_pushlstring(L, j.params.data(), j.params.size());
+ if (use_ext)
+ script_unpack(L, j.params_ext.get());
+ else
+ lua_pushlstring(L, j.params.data(), j.params.size());
// Call it
setOriginDirect(j.mod_origin.empty() ? nullptr : j.mod_origin.c_str());
@@ -241,19 +357,28 @@ void* AsyncWorkerThread::run()
try {
scriptError(result, "<async>");
} catch (const ModError &e) {
- errorstream << e.what() << std::endl;
+ report_error(e);
}
} else {
// Fetch result
- size_t length;
- const char *retval = lua_tolstring(L, -1, &length);
- j.result.assign(retval, length);
+ if (use_ext) {
+ try {
+ j.result_ext.reset(script_pack(L, -1));
+ } catch (const ModError &e) {
+ report_error(e);
+ result = LUA_ERRERR;
+ }
+ } else {
+ size_t length;
+ const char *retval = lua_tolstring(L, -1, &length);
+ j.result.assign(retval, length);
+ }
}
lua_pop(L, 1); // Pop retval
// Put job result
- if (!j.result.empty())
+ if (result == 0)
jobDispatcher->putJobResult(std::move(j));
}
diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h
index 697cb0221..1e34e40ea 100644
--- a/src/script/cpp_api/s_async.h
+++ b/src/script/cpp_api/s_async.h
@@ -21,11 +21,15 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <vector>
#include <deque>
+#include <unordered_set>
+#include <memory>
+#include <lua.h>
#include "threading/semaphore.h"
#include "threading/thread.h"
-#include "lua.h"
+#include "common/c_packer.h"
#include "cpp_api/s_base.h"
+#include "cpp_api/s_security.h"
// Forward declarations
class AsyncEngine;
@@ -42,8 +46,12 @@ struct LuaJobInfo
std::string function;
// Parameter to be passed to function (serialized)
std::string params;
+ // Alternative parameters
+ std::unique_ptr<PackedValue> params_ext;
// Result of function call (serialized)
std::string result;
+ // Alternative result
+ std::unique_ptr<PackedValue> result_ext;
// Name of the mod who invoked this call
std::string mod_origin;
// JobID used to identify a job and match it to callback
@@ -51,7 +59,8 @@ struct LuaJobInfo
};
// Asynchronous working environment
-class AsyncWorkerThread : public Thread, virtual public ScriptApiBase {
+class AsyncWorkerThread : public Thread,
+ virtual public ScriptApiBase, public ScriptApiSecurity {
friend class AsyncEngine;
public:
virtual ~AsyncWorkerThread();
@@ -63,6 +72,7 @@ protected:
private:
AsyncEngine *jobDispatcher = nullptr;
+ bool isErrored = false;
};
// Asynchornous thread and job management
@@ -71,6 +81,7 @@ class AsyncEngine {
typedef void (*StateInitializer)(lua_State *L, int top);
public:
AsyncEngine() = default;
+ AsyncEngine(Server *server) : server(server) {};
~AsyncEngine();
/**
@@ -81,7 +92,7 @@ public:
/**
* Create async engine tasks and lock function registration
- * @param numEngines Number of async threads to be started
+ * @param numEngines Number of worker threads, 0 for automatic scaling
*/
void initialize(unsigned int numEngines);
@@ -95,8 +106,16 @@ public:
const std::string &mod_origin = "");
/**
+ * Queue an async job
+ * @param func Serialized lua function
+ * @param params Serialized parameters (takes ownership!)
+ * @return ID of queued job
+ */
+ u32 queueAsyncJob(std::string &&func, PackedValue *params,
+ const std::string &mod_origin = "");
+
+ /**
* Engine step to process finished jobs
- * the engine step is one way to pass events back, PushFinishedJobs another
* @param L The Lua stack
*/
void step(lua_State *L);
@@ -117,18 +136,43 @@ protected:
void putJobResult(LuaJobInfo &&result);
/**
+ * Start an additional worker thread
+ */
+ void addWorkerThread();
+
+ /**
+ * Process finished jobs callbacks
+ */
+ void stepJobResults(lua_State *L);
+
+ /**
+ * Handle automatic scaling of worker threads
+ */
+ void stepAutoscale();
+
+ /**
* Initialize environment with current registred functions
* this function adds all functions registred by registerFunction to the
* passed lua stack
* @param L Lua stack to initialize
* @param top Stack position
+ * @return false if a mod error ocurred
*/
- void prepareEnvironment(lua_State* L, int top);
+ bool prepareEnvironment(lua_State* L, int top);
private:
// Variable locking the engine against further modification
bool initDone = false;
+ // Maximum number of worker threads for automatic scaling
+ // 0 if disabled
+ unsigned int autoscaleMaxWorkers = 0;
+ u64 autoscaleTimer = 0;
+ std::unordered_set<u32> autoscaleSeenJobs;
+
+ // Only set for the server async environment (duh)
+ Server *server = nullptr;
+
// Internal store for registred state initializers
std::vector<StateInitializer> stateInitializers;
diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index 27d730b1d..ae4a16771 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -38,6 +38,8 @@ extern "C" {
#include "lualib.h"
#if USE_LUAJIT
#include "luajit.h"
+#else
+ #include "bit.h"
#endif
}
@@ -89,6 +91,11 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
else*/
luaL_openlibs(m_luastack);
+ // Load bit library
+ lua_pushcfunction(m_luastack, luaopen_bit);
+ lua_pushstring(m_luastack, LUA_BITLIBNAME);
+ lua_call(m_luastack, 1, 0);
+
// Make the ScriptApiBase* accessible to ModApiBase
#if INDIRECT_SCRIPTAPI_RIDX
*(void **)(lua_newuserdata(m_luastack, sizeof(void *))) = this;
@@ -115,6 +122,14 @@ ScriptApiBase::ScriptApiBase(ScriptingType type):
lua_newtable(m_luastack);
lua_setglobal(m_luastack, "core");
+ // vector.metatable is stored in the registry for quick access from C++.
+ lua_newtable(m_luastack);
+ lua_rawseti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE);
+ lua_newtable(m_luastack);
+ lua_rawgeti(m_luastack, LUA_REGISTRYINDEX, CUSTOM_RIDX_VECTOR_METATABLE);
+ lua_setfield(m_luastack, -2, "metatable");
+ lua_setglobal(m_luastack, "vector");
+
if (m_type == ScriptingType::Client)
lua_pushstring(m_luastack, "/");
else
diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h
index e49745f4e..19ae8783b 100644
--- a/src/script/cpp_api/s_base.h
+++ b/src/script/cpp_api/s_base.h
@@ -128,6 +128,15 @@ protected:
friend class ModApiEnvMod;
friend class LuaVoxelManip;
+ /*
+ Subtle edge case with coroutines: If for whatever reason you have a
+ method in a subclass that's called from existing lua_CFunction
+ (any of the l_*.cpp files) then make it static and take the lua_State*
+ as an argument. This is REQUIRED because getStack() will not return the
+ correct state if called inside coroutines.
+
+ Also note that src/script/common/ is the better place for such helpers.
+ */
lua_State* getStack()
{ return m_luastack; }
diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp
index 5d20f547d..e54a1361d 100644
--- a/src/script/cpp_api/s_client.cpp
+++ b/src/script/cpp_api/s_client.cpp
@@ -399,7 +399,7 @@ bool ScriptApiClient::on_inventory_open(Inventory *inventory)
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_inventory_open");
- push_inventory(L, inventory);
+ push_inventory_lists(L, *inventory);
try {
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp
index 746f7013e..06337b9e8 100644
--- a/src/script/cpp_api/s_entity.cpp
+++ b/src/script/cpp_api/s_entity.cpp
@@ -240,7 +240,7 @@ void ScriptApiEntity::luaentity_Step(u16 id, float dtime,
// tool_capabilities, direction, damage)
bool ScriptApiEntity::luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch,
- const ToolCapabilities *toolcap, v3f dir, s16 damage)
+ const ToolCapabilities *toolcap, v3f dir, s32 damage)
{
SCRIPTAPI_PRECHECKHEADER
diff --git a/src/script/cpp_api/s_entity.h b/src/script/cpp_api/s_entity.h
index b52f6e447..7658ae922 100644
--- a/src/script/cpp_api/s_entity.h
+++ b/src/script/cpp_api/s_entity.h
@@ -42,7 +42,7 @@ public:
const collisionMoveResult *moveresult);
bool luaentity_Punch(u16 id,
ServerActiveObject *puncher, float time_from_last_punch,
- const ToolCapabilities *toolcap, v3f dir, s16 damage);
+ const ToolCapabilities *toolcap, v3f dir, s32 damage);
bool luaentity_on_death(u16 id, ServerActiveObject *killer);
void luaentity_Rightclick(u16 id, ServerActiveObject *clicker);
void luaentity_on_attach_child(u16 id, ServerActiveObject *child);
diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp
index 874c37b6e..af68f689f 100644
--- a/src/script/cpp_api/s_env.cpp
+++ b/src/script/cpp_api/s_env.cpp
@@ -140,10 +140,10 @@ void ScriptApiEnv::initializeEnvironment(ServerEnvironment *env)
bool simple_catch_up = true;
getboolfield(L, current_abm, "catch_up", simple_catch_up);
-
+
s16 min_y = INT16_MIN;
getintfield(L, current_abm, "min_y", min_y);
-
+
s16 max_y = INT16_MAX;
getintfield(L, current_abm, "max_y", max_y);
diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp
index 48dce14f3..b1916070e 100644
--- a/src/script/cpp_api/s_item.cpp
+++ b/src/script/cpp_api/s_item.cpp
@@ -59,13 +59,14 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
return true;
}
-bool ScriptApiItem::item_OnPlace(ItemStack &item,
+bool ScriptApiItem::item_OnPlace(Optional<ItemStack> &ret_item,
ServerActiveObject *placer, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
+ const ItemStack &item = *ret_item;
// Push callback function on stack
if (!getItemCallback(item.name.c_str(), "on_place"))
return false;
@@ -82,22 +83,25 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L, -1, getServer()->idef());
+ ret_item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw WRAP_LUAERROR(e, "item=" + item.name);
}
+ } else {
+ ret_item = nullopt;
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
-bool ScriptApiItem::item_OnUse(ItemStack &item,
+bool ScriptApiItem::item_OnUse(Optional<ItemStack> &ret_item,
ServerActiveObject *user, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
+ const ItemStack &item = *ret_item;
// Push callback function on stack
if (!getItemCallback(item.name.c_str(), "on_use"))
return false;
@@ -109,22 +113,25 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if(!lua_isnil(L, -1)) {
try {
- item = read_item(L, -1, getServer()->idef());
+ ret_item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw WRAP_LUAERROR(e, "item=" + item.name);
}
+ } else {
+ ret_item = nullopt;
}
lua_pop(L, 2); // Pop item and error handler
return true;
}
-bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item,
+bool ScriptApiItem::item_OnSecondaryUse(Optional<ItemStack> &ret_item,
ServerActiveObject *user, const PointedThing &pointed)
{
SCRIPTAPI_PRECHECKHEADER
int error_handler = PUSH_ERROR_HANDLER(L);
+ const ItemStack &item = *ret_item;
if (!getItemCallback(item.name.c_str(), "on_secondary_use"))
return false;
@@ -134,10 +141,12 @@ bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item,
PCALL_RES(lua_pcall(L, 3, 1, error_handler));
if (!lua_isnil(L, -1)) {
try {
- item = read_item(L, -1, getServer()->idef());
+ ret_item = read_item(L, -1, getServer()->idef());
} catch (LuaError &e) {
throw WRAP_LUAERROR(e, "item=" + item.name);
}
+ } else {
+ ret_item = nullopt;
}
lua_pop(L, 2); // Pop item and error handler
return true;
diff --git a/src/script/cpp_api/s_item.h b/src/script/cpp_api/s_item.h
index 25a3501f9..5015d8bd4 100644
--- a/src/script/cpp_api/s_item.h
+++ b/src/script/cpp_api/s_item.h
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_base.h"
#include "irr_v3d.h"
+#include "util/Optional.h"
struct PointedThing;
struct ItemStack;
@@ -35,13 +36,20 @@ class ScriptApiItem
: virtual public ScriptApiBase
{
public:
+ /*
+ * Functions with Optional<ItemStack> are for callbacks where Lua may
+ * want to prevent the engine from modifying the inventory after it's done.
+ * This has a longer backstory where on_use may need to empty the player's
+ * inventory without the engine interfering (see issue #6546).
+ */
+
bool item_OnDrop(ItemStack &item,
ServerActiveObject *dropper, v3f pos);
- bool item_OnPlace(ItemStack &item,
+ bool item_OnPlace(Optional<ItemStack> &item,
ServerActiveObject *placer, const PointedThing &pointed);
- bool item_OnUse(ItemStack &item,
+ bool item_OnUse(Optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
- bool item_OnSecondaryUse(ItemStack &item,
+ bool item_OnSecondaryUse(Optional<ItemStack> &item,
ServerActiveObject *user, const PointedThing &pointed);
bool item_OnCraft(ItemStack &item, ServerActiveObject *user,
const InventoryList *old_craft_grid, const InventoryLocation &craft_inv);
diff --git a/src/script/cpp_api/s_node.h b/src/script/cpp_api/s_node.h
index 3f771c838..3c6a8445b 100644
--- a/src/script/cpp_api/s_node.h
+++ b/src/script/cpp_api/s_node.h
@@ -53,6 +53,7 @@ public:
static struct EnumString es_ContentParamType[];
static struct EnumString es_ContentParamType2[];
static struct EnumString es_LiquidType[];
+ static struct EnumString es_LiquidMoveType[];
static struct EnumString es_NodeBoxType[];
static struct EnumString es_TextureAlphaMode[];
};
diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp
index d3e6138dc..22b24f363 100644
--- a/src/script/cpp_api/s_player.cpp
+++ b/src/script/cpp_api/s_player.cpp
@@ -60,7 +60,7 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
float time_from_last_punch,
const ToolCapabilities *toolcap,
v3f dir,
- s16 damage)
+ s32 damage)
{
SCRIPTAPI_PRECHECKHEADER
// Get core.registered_on_punchplayers
diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h
index c0f141862..e866aee46 100644
--- a/src/script/cpp_api/s_player.h
+++ b/src/script/cpp_api/s_player.h
@@ -46,7 +46,7 @@ public:
void on_cheat(ServerActiveObject *player, const std::string &cheat_type);
bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,
float time_from_last_punch, const ToolCapabilities *toolcap,
- v3f dir, s16 damage);
+ v3f dir, s32 damage);
void on_rightclickplayer(ServerActiveObject *player, ServerActiveObject *clicker);
s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change,
const PlayerHPChangeReason &reason);
diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp
index bd9c80e15..76509038f 100644
--- a/src/script/cpp_api/s_security.cpp
+++ b/src/script/cpp_api/s_security.cpp
@@ -27,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <cerrno>
#include <string>
+#include <algorithm>
#include <iostream>
@@ -97,6 +98,7 @@ void ScriptApiSecurity::initializeSecurity()
"type",
"unpack",
"_VERSION",
+ "vector",
"xpcall",
};
static const char *whitelist_tables[] = {
@@ -106,6 +108,7 @@ void ScriptApiSecurity::initializeSecurity()
"string",
"table",
"math",
+ "bit"
};
static const char *io_whitelist[] = {
"open",
@@ -120,21 +123,17 @@ void ScriptApiSecurity::initializeSecurity()
"date",
"difftime",
"getenv",
- "setlocale",
"time",
- "tmpname",
};
static const char *debug_whitelist[] = {
"gethook",
"traceback",
"getinfo",
"getmetatable",
- "setupvalue",
"setmetatable",
"upvalueid",
"sethook",
"debug",
- "setlocal",
};
static const char *package_whitelist[] = {
"config",
@@ -220,6 +219,7 @@ void ScriptApiSecurity::initializeSecurity()
// And replace unsafe ones
SECURE_API(os, remove);
SECURE_API(os, rename);
+ SECURE_API(os, setlocale);
lua_setglobal(L, "os");
lua_pop(L, 1); // Pop old OS
@@ -251,6 +251,15 @@ void ScriptApiSecurity::initializeSecurity()
lua_pop(L, 1); // Pop old jit
#endif
+ // Get rid of 'core' in the old globals, we don't want anyone thinking it's
+ // safe or even usable.
+ lua_pushnil(L);
+ 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
@@ -286,19 +295,21 @@ void ScriptApiSecurity::initializeSecurityClient()
"rawset",
"select",
"setfenv",
- // getmetatable can be used to escape the sandbox
+ // getmetatable can be used to escape the sandbox <- ???
"setmetatable",
"tonumber",
"tostring",
"type",
"unpack",
"_VERSION",
+ "vector",
"xpcall",
// Completely safe libraries
"coroutine",
"string",
"table",
"math",
+ "bit",
};
static const char *os_whitelist[] = {
"clock",
@@ -307,7 +318,7 @@ void ScriptApiSecurity::initializeSecurityClient()
"time"
};
static const char *debug_whitelist[] = {
- "getinfo",
+ "getinfo", // used by builtin and unset before mods load
"traceback"
};
#if USE_LUAJIT
@@ -607,6 +618,38 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path,
return false;
}
+bool ScriptApiSecurity::checkWhitelisted(lua_State *L, const std::string &setting)
+{
+ assert(str_starts_with(setting, "secure."));
+
+ // We have to make sure that this function is being called directly by
+ // a mod, otherwise a malicious mod could override this function and
+ // steal its return value.
+ lua_Debug info;
+
+ // Make sure there's only one item below this function on the stack...
+ if (lua_getstack(L, 2, &info))
+ return false;
+ FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
+ FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
+
+ // ...and that that item is the main file scope.
+ if (strcmp(info.what, "main") != 0)
+ return false;
+
+ // Mod must be listed in secure.http_mods or secure.trusted_mods
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
+ if (!lua_isstring(L, -1))
+ return false;
+ std::string mod_name = readParam<std::string>(L, -1);
+
+ std::string value = g_settings->get(setting);
+ value.erase(std::remove(value.begin(), value.end(), ' '), value.end());
+ auto mod_list = str_split(value, ',');
+
+ return CONTAINS(mod_list, mod_name);
+}
+
int ScriptApiSecurity::sl_g_dofile(lua_State *L)
{
@@ -838,3 +881,20 @@ int ScriptApiSecurity::sl_os_remove(lua_State *L)
return 2;
}
+
+int ScriptApiSecurity::sl_os_setlocale(lua_State *L)
+{
+ const bool cat = lua_gettop(L) > 1;
+ // Don't allow changes
+ if (!lua_isnoneornil(L, 1)) {
+ lua_pushnil(L);
+ return 1;
+ }
+
+ push_original(L, "os", "setlocale");
+ lua_pushnil(L);
+ if (cat)
+ lua_pushvalue(L, 2);
+ lua_call(L, cat ? 2 : 1, 1);
+ return 1;
+}
diff --git a/src/script/cpp_api/s_security.h b/src/script/cpp_api/s_security.h
index 73e763548..880ce1638 100644
--- a/src/script/cpp_api/s_security.h
+++ b/src/script/cpp_api/s_security.h
@@ -40,11 +40,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
class ScriptApiSecurity : virtual public ScriptApiBase
{
public:
- int getThread(lua_State *L);
- // creates an empty Lua environment
- void createEmptyEnv(lua_State *L);
- // sets the enviroment to the table thats on top of the stack
- void setLuaEnv(lua_State *L, int thread);
// Sets up security on the ScriptApi's Lua state
void initializeSecurity();
void initializeSecurityClient();
@@ -57,8 +52,17 @@ public:
// Checks if mods are allowed to read (and optionally write) to the path
static bool checkPath(lua_State *L, const char *path, bool write_required,
bool *write_allowed=NULL);
+ // Check if mod is whitelisted in the given setting
+ // This additionally checks that the mod's main file scope is executing.
+ static bool checkWhitelisted(lua_State *L, const std::string &setting);
private:
+ int getThread(lua_State *L);
+ // sets the enviroment to the table thats on top of the stack
+ void setLuaEnv(lua_State *L, int thread);
+ // creates an empty Lua environment
+ void createEmptyEnv(lua_State *L);
+
// Syntax: "sl_" <Library name or 'g' (global)> '_' <Function name>
// (sl stands for Secure Lua)
@@ -75,4 +79,5 @@ private:
static int sl_os_rename(lua_State *L);
static int sl_os_remove(lua_State *L);
+ static int sl_os_setlocale(lua_State *L);
};
diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp
index 6ddb2630d..c255b0c71 100644
--- a/src/script/cpp_api/s_server.cpp
+++ b/src/script/cpp_api/s_server.cpp
@@ -198,10 +198,8 @@ std::string ScriptApiServer::formatChatMessage(const std::string &name,
return ret;
}
-u32 ScriptApiServer::allocateDynamicMediaCallback(int f_idx)
+u32 ScriptApiServer::allocateDynamicMediaCallback(lua_State *L, int f_idx)
{
- lua_State *L = getStack();
-
if (f_idx < 0)
f_idx = lua_gettop(L) + f_idx + 1;
@@ -235,7 +233,7 @@ u32 ScriptApiServer::allocateDynamicMediaCallback(int f_idx)
void ScriptApiServer::freeDynamicMediaCallback(u32 token)
{
- lua_State *L = getStack();
+ SCRIPTAPI_PRECHECKHEADER
verbosestream << "freeDynamicMediaCallback(" << token << ")" << std::endl;
diff --git a/src/script/cpp_api/s_server.h b/src/script/cpp_api/s_server.h
index c5c3d5596..58c8c0e48 100644
--- a/src/script/cpp_api/s_server.h
+++ b/src/script/cpp_api/s_server.h
@@ -51,7 +51,7 @@ public:
const std::string &password);
/* dynamic media handling */
- u32 allocateDynamicMediaCallback(int f_idx);
+ static u32 allocateDynamicMediaCallback(lua_State *L, int f_idx);
void freeDynamicMediaCallback(u32 token);
void on_dynamic_media_added(u32 token, const char *playername);
diff --git a/src/script/lua_api/l_areastore.cpp b/src/script/lua_api/l_areastore.cpp
index 45724e604..ec2656c4a 100644
--- a/src/script/lua_api/l_areastore.cpp
+++ b/src/script/lua_api/l_areastore.cpp
@@ -27,26 +27,26 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "filesys.h"
#include <fstream>
-static inline void get_data_and_border_flags(lua_State *L, u8 start_i,
- bool *borders, bool *data)
+static inline void get_data_and_corner_flags(lua_State *L, u8 start_i,
+ bool *corners, bool *data)
{
if (!lua_isboolean(L, start_i))
return;
- *borders = lua_toboolean(L, start_i);
+ *corners = lua_toboolean(L, start_i);
if (!lua_isboolean(L, start_i + 1))
return;
*data = lua_toboolean(L, start_i + 1);
}
static void push_area(lua_State *L, const Area *a,
- bool include_borders, bool include_data)
+ bool include_corners, bool include_data)
{
- if (!include_borders && !include_data) {
+ if (!include_corners && !include_data) {
lua_pushboolean(L, true);
return;
}
lua_newtable(L);
- if (include_borders) {
+ if (include_corners) {
push_v3s16(L, a->minedge);
lua_setfield(L, -2, "min");
push_v3s16(L, a->maxedge);
@@ -59,13 +59,13 @@ static void push_area(lua_State *L, const Area *a,
}
static inline void push_areas(lua_State *L, const std::vector<Area *> &areas,
- bool borders, bool data)
+ bool corners, bool data)
{
lua_newtable(L);
size_t cnt = areas.size();
for (size_t i = 0; i < cnt; i++) {
lua_pushnumber(L, areas[i]->id);
- push_area(L, areas[i], borders, data);
+ push_area(L, areas[i], corners, data);
lua_settable(L, -3);
}
}
@@ -94,7 +94,7 @@ int LuaAreaStore::gc_object(lua_State *L)
return 0;
}
-// get_area(id, include_borders, include_data)
+// get_area(id, include_corners, include_data)
int LuaAreaStore::l_get_area(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
@@ -104,9 +104,9 @@ int LuaAreaStore::l_get_area(lua_State *L)
u32 id = luaL_checknumber(L, 2);
- bool include_borders = true;
+ bool include_corners = true;
bool include_data = false;
- get_data_and_border_flags(L, 3, &include_borders, &include_data);
+ get_data_and_corner_flags(L, 3, &include_corners, &include_data);
const Area *res;
@@ -114,12 +114,12 @@ int LuaAreaStore::l_get_area(lua_State *L)
if (!res)
return 0;
- push_area(L, res, include_borders, include_data);
+ push_area(L, res, include_corners, include_data);
return 1;
}
-// get_areas_for_pos(pos, include_borders, include_data)
+// get_areas_for_pos(pos, include_corners, include_data)
int LuaAreaStore::l_get_areas_for_pos(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
@@ -129,19 +129,19 @@ int LuaAreaStore::l_get_areas_for_pos(lua_State *L)
v3s16 pos = check_v3s16(L, 2);
- bool include_borders = true;
+ bool include_corners = true;
bool include_data = false;
- get_data_and_border_flags(L, 3, &include_borders, &include_data);
+ get_data_and_corner_flags(L, 3, &include_corners, &include_data);
std::vector<Area *> res;
ast->getAreasForPos(&res, pos);
- push_areas(L, res, include_borders, include_data);
+ push_areas(L, res, include_corners, include_data);
return 1;
}
-// get_areas_in_area(edge1, edge2, accept_overlap, include_borders, include_data)
+// get_areas_in_area(corner1, corner2, accept_overlap, include_corners, include_data)
int LuaAreaStore::l_get_areas_in_area(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
@@ -149,25 +149,26 @@ int LuaAreaStore::l_get_areas_in_area(lua_State *L)
LuaAreaStore *o = checkobject(L, 1);
AreaStore *ast = o->as;
- v3s16 minedge = check_v3s16(L, 2);
- v3s16 maxedge = check_v3s16(L, 3);
+ v3s16 minp = check_v3s16(L, 2);
+ v3s16 maxp = check_v3s16(L, 3);
+ sortBoxVerticies(minp, maxp);
- bool include_borders = true;
+ bool include_corners = true;
bool include_data = false;
bool accept_overlap = false;
if (lua_isboolean(L, 4)) {
accept_overlap = readParam<bool>(L, 4);
- get_data_and_border_flags(L, 5, &include_borders, &include_data);
+ get_data_and_corner_flags(L, 5, &include_corners, &include_data);
}
std::vector<Area *> res;
- ast->getAreasInArea(&res, minedge, maxedge, accept_overlap);
- push_areas(L, res, include_borders, include_data);
+ ast->getAreasInArea(&res, minp, maxp, accept_overlap);
+ push_areas(L, res, include_corners, include_data);
return 1;
}
-// insert_area(edge1, edge2, data, id)
+// insert_area(corner1, corner2, data, id)
int LuaAreaStore::l_insert_area(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp
index 1d769c73e..265c7d3fc 100644
--- a/src/script/lua_api/l_client.cpp
+++ b/src/script/lua_api/l_client.cpp
@@ -474,7 +474,7 @@ int ModApiClient::l_get_inventory(lua_State *L)
inventory = client->getInventory(inventory_location);
if (! inventory)
throw SerializationError(std::string("Attempt to access nonexistant inventory (") + location + ")");
- push_inventory(L, inventory);
+ push_inventory_lists(L, *inventory);
} catch (SerializationError &) {
lua_pushnil(L);
}
diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp
index 18622ee00..137b210be 100644
--- a/src/script/lua_api/l_craft.cpp
+++ b/src/script/lua_api/l_craft.cpp
@@ -371,8 +371,9 @@ int ModApiCraft::l_clear_craft(lua_State *L)
int ModApiCraft::l_get_craft_result(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
+ IGameDef *gdef = getGameDef(L);
- int input_i = 1;
+ const int input_i = 1;
std::string method_s = getstringfield_default(L, input_i, "method", "normal");
enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
es_CraftMethod, CRAFT_METHOD_NORMAL);
@@ -382,10 +383,9 @@ int ModApiCraft::l_get_craft_result(lua_State *L)
width = luaL_checkinteger(L, -1);
lua_pop(L, 1);
lua_getfield(L, input_i, "items");
- std::vector<ItemStack> items = read_items(L, -1,getServer(L));
+ std::vector<ItemStack> items = read_items(L, -1, gdef);
lua_pop(L, 1); // items
- IGameDef *gdef = getServer(L);
ICraftDefManager *cdef = gdef->cdef();
CraftInput input(method, width, items);
CraftOutput output;
@@ -465,13 +465,13 @@ static void push_craft_recipes(lua_State *L, IGameDef *gdef,
const std::vector<CraftDefinition*> &recipes,
const CraftOutput &output)
{
- lua_createtable(L, recipes.size(), 0);
-
if (recipes.empty()) {
lua_pushnil(L);
return;
}
+ lua_createtable(L, recipes.size(), 0);
+
std::vector<CraftDefinition*>::const_iterator it = recipes.begin();
for (unsigned i = 0; it != recipes.end(); ++it) {
lua_newtable(L);
@@ -487,10 +487,9 @@ int ModApiCraft::l_get_craft_recipe(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string item = luaL_checkstring(L, 1);
- Server *server = getServer(L);
+ IGameDef *gdef = getGameDef(L);
CraftOutput output(item, 0);
- std::vector<CraftDefinition*> recipes = server->cdef()
- ->getCraftRecipes(output, server, 1);
+ auto recipes = gdef->cdef()->getCraftRecipes(output, gdef, 1);
lua_createtable(L, 1, 0);
@@ -500,7 +499,7 @@ int ModApiCraft::l_get_craft_recipe(lua_State *L)
setintfield(L, -1, "width", 0);
return 1;
}
- push_craft_recipe(L, server, recipes[0], output);
+ push_craft_recipe(L, gdef, recipes[0], output);
return 1;
}
@@ -510,12 +509,11 @@ int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string item = luaL_checkstring(L, 1);
- Server *server = getServer(L);
+ IGameDef *gdef = getGameDef(L);
CraftOutput output(item, 0);
- std::vector<CraftDefinition*> recipes = server->cdef()
- ->getCraftRecipes(output, server);
+ auto recipes = gdef->cdef()->getCraftRecipes(output, gdef);
- push_craft_recipes(L, server, recipes, output);
+ push_craft_recipes(L, gdef, recipes, output);
return 1;
}
@@ -527,3 +525,11 @@ void ModApiCraft::Initialize(lua_State *L, int top)
API_FCT(register_craft);
API_FCT(clear_craft);
}
+
+void ModApiCraft::InitializeAsync(lua_State *L, int top)
+{
+ // all read-only functions
+ API_FCT(get_all_craft_recipes);
+ API_FCT(get_craft_recipe);
+ API_FCT(get_craft_result);
+}
diff --git a/src/script/lua_api/l_craft.h b/src/script/lua_api/l_craft.h
index 9002b23ef..5234af56f 100644
--- a/src/script/lua_api/l_craft.h
+++ b/src/script/lua_api/l_craft.h
@@ -45,4 +45,5 @@ private:
public:
static void Initialize(lua_State *L, int top);
+ static void InitializeAsync(lua_State *L, int top);
};
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index 876f84d53..a489d245c 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -477,7 +477,7 @@ int ModApiEnvMod::l_place_node(lua_State *L)
return 1;
}
// Create item to place
- ItemStack item(ndef->get(n).name, 1, 0, idef);
+ Optional<ItemStack> item = ItemStack(ndef->get(n).name, 1, 0, idef);
// Make pointed position
PointedThing pointed;
pointed.type = POINTEDTHING_NODE;
@@ -1038,6 +1038,21 @@ int ModApiEnvMod::l_find_nodes_near_under_air_except(lua_State *L)
return 2;
}
+static void checkArea(v3s16 &minp, v3s16 &maxp)
+{
+ auto volume = VoxelArea(minp, maxp).getVolume();
+ // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
+ if (volume > 4096000) {
+ throw LuaError("Area volume exceeds allowed value of 4096000");
+ }
+
+ // Clamp to map range to avoid problems
+#define CLAMP(arg) core::clamp(arg, (s16)-MAX_MAP_GENERATION_LIMIT, (s16)MAX_MAP_GENERATION_LIMIT)
+ minp = v3s16(CLAMP(minp.X), CLAMP(minp.Y), CLAMP(minp.Z));
+ maxp = v3s16(CLAMP(maxp.X), CLAMP(maxp.Y), CLAMP(maxp.Z));
+#undef CLAMP
+}
+
// find_nodes_in_area(minp, maxp, nodenames, [grouped])
int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
{
@@ -1057,13 +1072,7 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
}
#endif
- v3s16 cube = maxp - minp + 1;
- // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
- if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) {
- luaL_error(L, "find_nodes_in_area(): area volume"
- " exceeds allowed value of 4096000");
- return 0;
- }
+ checkArea(minp, maxp);
std::vector<content_t> filter;
collectNodeIds(L, 3, ndef, filter);
@@ -1168,13 +1177,7 @@ int ModApiEnvMod::l_find_nodes_in_area_under_air(lua_State *L)
}
#endif
- v3s16 cube = maxp - minp + 1;
- // Volume limit equal to 8 default mapchunks, (80 * 2) ^ 3 = 4,096,000
- if ((u64)cube.X * (u64)cube.Y * (u64)cube.Z > 4096000) {
- luaL_error(L, "find_nodes_in_area_under_air(): area volume"
- " exceeds allowed value of 4096000");
- return 0;
- }
+ checkArea(minp, maxp);
std::vector<content_t> filter;
collectNodeIds(L, 3, ndef, filter);
@@ -1541,7 +1544,7 @@ int ModApiEnvMod::l_transforming_liquid_add(lua_State *L)
GET_ENV_PTR;
v3s16 p0 = read_v3s16(L, 1);
- env->getMap().transforming_liquid_add(p0);
+ env->getServerMap().transforming_liquid_add(p0);
return 1;
}
diff --git a/src/script/lua_api/l_env.h b/src/script/lua_api/l_env.h
index a5ac21e21..70a8d2398 100644
--- a/src/script/lua_api/l_env.h
+++ b/src/script/lua_api/l_env.h
@@ -114,7 +114,7 @@ private:
// get_objects_inside_radius(pos, radius)
static int l_get_objects_inside_radius(lua_State *L);
-
+
// get_objects_in_area(pos, minp, maxp)
static int l_get_objects_in_area(lua_State *L);
diff --git a/src/script/lua_api/l_http.cpp b/src/script/lua_api/l_http.cpp
index 751ec9837..5566a8523 100644
--- a/src/script/lua_api/l_http.cpp
+++ b/src/script/lua_api/l_http.cpp
@@ -21,14 +21,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_converter.h"
#include "common/c_content.h"
#include "lua_api/l_http.h"
+#include "cpp_api/s_security.h"
#include "httpfetch.h"
#include "settings.h"
#include "debug.h"
#include "log.h"
-#include <algorithm>
#include <iomanip>
-#include <cctype>
#define HTTP_API(name) \
lua_pushstring(L, #name); \
@@ -167,54 +166,22 @@ int ModApiHttp::l_request_http_api(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- // We have to make sure that this function is being called directly by
- // a mod, otherwise a malicious mod could override this function and
- // steal its return value.
- lua_Debug info;
-
- // Make sure there's only one item below this function on the stack...
- if (lua_getstack(L, 2, &info)) {
- return 0;
- }
- FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
- FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
-
- // ...and that that item is the main file scope.
- if (strcmp(info.what, "main") != 0) {
- return 0;
- }
-
- // Mod must be listed in secure.http_mods or secure.trusted_mods
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
- if (!lua_isstring(L, -1)) {
- return 0;
- }
-
- std::string mod_name = readParam<std::string>(L, -1);
- std::string http_mods = g_settings->get("secure.http_mods");
- http_mods.erase(std::remove(http_mods.begin(), http_mods.end(), ' '), http_mods.end());
- std::vector<std::string> mod_list_http = str_split(http_mods, ',');
-
- std::string trusted_mods = g_settings->get("secure.trusted_mods");
- trusted_mods.erase(std::remove(trusted_mods.begin(), trusted_mods.end(), ' '), trusted_mods.end());
- std::vector<std::string> mod_list_trusted = str_split(trusted_mods, ',');
-
- mod_list_http.insert(mod_list_http.end(), mod_list_trusted.begin(), mod_list_trusted.end());
- if (std::find(mod_list_http.begin(), mod_list_http.end(), mod_name) == mod_list_http.end()) {
+ if (!ScriptApiSecurity::checkWhitelisted(L, "secure.http_mods") &&
+ !ScriptApiSecurity::checkWhitelisted(L, "secure.trusted_mods")) {
lua_pushnil(L);
return 1;
}
- lua_getglobal(L, "core");
- lua_getfield(L, -1, "http_add_fetch");
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_HTTP_API_LUA);
+ assert(lua_isfunction(L, -1));
lua_newtable(L);
HTTP_API(fetch_async);
HTTP_API(fetch_async_get);
// Stack now looks like this:
- // <core.http_add_fetch> <table with fetch_async, fetch_async_get>
- // Now call core.http_add_fetch to append .fetch(request, callback) to table
+ // <function> <table with fetch_async, fetch_async_get>
+ // Now call it to append .fetch(request, callback) to table
lua_call(L, 1, 1);
return 1;
@@ -234,6 +201,22 @@ int ModApiHttp::l_get_http_api(lua_State *L)
#endif
+int ModApiHttp::l_set_http_api_lua(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+#if USE_CURL
+ // This is called by builtin to give us a function that will later
+ // populate the http_api table with additional method(s).
+ // We need this because access to the HTTP api is security-relevant and
+ // any mod could just mess with a global variable.
+ luaL_checktype(L, 1, LUA_TFUNCTION);
+ lua_rawseti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_HTTP_API_LUA);
+#endif
+
+ return 0;
+}
+
void ModApiHttp::Initialize(lua_State *L, int top)
{
#if USE_CURL
@@ -247,8 +230,14 @@ void ModApiHttp::Initialize(lua_State *L, int top)
API_FCT(get_http_api);
} else {
API_FCT(request_http_api);
+ API_FCT(set_http_api_lua);
}
+#else
+
+ // Define this function anyway so builtin can call it without checking
+ API_FCT(set_http_api_lua);
+
#endif
}
diff --git a/src/script/lua_api/l_http.h b/src/script/lua_api/l_http.h
index c3a2a5276..8d084ecd9 100644
--- a/src/script/lua_api/l_http.h
+++ b/src/script/lua_api/l_http.h
@@ -48,6 +48,10 @@ private:
static int l_get_http_api(lua_State *L);
#endif
+ // set_http_api_lua() [internal]
+ static int l_set_http_api_lua(lua_State *L);
+
+
public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);
diff --git a/src/script/lua_api/l_internal.h b/src/script/lua_api/l_internal.h
index 672e535ca..de73ff42a 100644
--- a/src/script/lua_api/l_internal.h
+++ b/src/script/lua_api/l_internal.h
@@ -69,7 +69,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
// Retrieve Environment pointer as `env` (no map lock)
#define GET_PLAIN_ENV_PTR_NO_MAP_LOCK \
- Environment *env = (Environment *)getEnv(L); \
+ Environment *env = getEnv(L); \
if (env == NULL) \
return 0
diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp
index 0dd418462..175047e58 100644
--- a/src/script/lua_api/l_inventory.cpp
+++ b/src/script/lua_api/l_inventory.cpp
@@ -214,11 +214,16 @@ int InvRef::l_get_list(lua_State *L)
InvRef *ref = checkobject(L, 1);
const char *listname = luaL_checkstring(L, 2);
Inventory *inv = getinv(L, ref);
- if(inv){
- push_inventory_list(L, inv, listname);
- } else {
+ if (!inv) {
lua_pushnil(L);
+ return 1;
}
+ InventoryList *invlist = inv->getList(listname);
+ if (!invlist) {
+ lua_pushnil(L);
+ return 1;
+ }
+ push_inventory_list(L, *invlist);
return 1;
}
@@ -242,7 +247,7 @@ int InvRef::l_set_list(lua_State *L)
return 0;
}
-// get_lists(self) -> list of InventoryLists
+// get_lists(self) -> table that maps listnames to InventoryLists
int InvRef::l_get_lists(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
@@ -251,15 +256,7 @@ int InvRef::l_get_lists(lua_State *L)
if (!inv) {
return 0;
}
- std::vector<const InventoryList*> lists = inv->getLists();
- std::vector<const InventoryList*>::iterator iter = lists.begin();
- lua_createtable(L, 0, lists.size());
- for (; iter != lists.end(); iter++) {
- const char* name = (*iter)->getName().c_str();
- lua_pushstring(L, name);
- push_inventory_list(L, inv, name);
- lua_rawset(L, -3);
- }
+ push_inventory_lists(L, *inv);
return 1;
}
@@ -421,19 +418,6 @@ void InvRef::create(lua_State *L, const InventoryLocation &loc)
luaL_getmetatable(L, className);
lua_setmetatable(L, -2);
}
-void InvRef::createPlayer(lua_State *L, RemotePlayer *player)
-{
- NO_MAP_LOCK_REQUIRED;
- InventoryLocation loc;
- loc.setPlayer(player->getName());
- create(L, loc);
-}
-void InvRef::createNodeMeta(lua_State *L, v3s16 p)
-{
- InventoryLocation loc;
- loc.setNodeMeta(p);
- create(L, loc);
-}
void InvRef::Register(lua_State *L)
{
diff --git a/src/script/lua_api/l_inventory.h b/src/script/lua_api/l_inventory.h
index 94f670c9d..6a75bac0f 100644
--- a/src/script/lua_api/l_inventory.h
+++ b/src/script/lua_api/l_inventory.h
@@ -111,8 +111,6 @@ public:
// Creates an InvRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
static void create(lua_State *L, const InventoryLocation &loc);
- static void createPlayer(lua_State *L, RemotePlayer *player);
- static void createNodeMeta(lua_State *L, v3s16 p);
static void Register(lua_State *L);
};
diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp
index 2ea2bd4f6..9220259ff 100644
--- a/src/script/lua_api/l_item.cpp
+++ b/src/script/lua_api/l_item.cpp
@@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_internal.h"
#include "common/c_converter.h"
#include "common/c_content.h"
+#include "common/c_packer.h"
#include "itemdef.h"
#include "nodedef.h"
#include "server.h"
@@ -446,6 +447,7 @@ int LuaItemStack::create_object(lua_State *L)
lua_setmetatable(L, -2);
return 1;
}
+
// Not callable from Lua
int LuaItemStack::create(lua_State *L, const ItemStack &item)
{
@@ -462,6 +464,20 @@ LuaItemStack *LuaItemStack::checkobject(lua_State *L, int narg)
return *(LuaItemStack **)luaL_checkudata(L, narg, className);
}
+void *LuaItemStack::packIn(lua_State *L, int idx)
+{
+ LuaItemStack *o = checkobject(L, idx);
+ return new ItemStack(o->getItem());
+}
+
+void LuaItemStack::packOut(lua_State *L, void *ptr)
+{
+ ItemStack *stack = reinterpret_cast<ItemStack*>(ptr);
+ if (L)
+ create(L, *stack);
+ delete stack;
+}
+
void LuaItemStack::Register(lua_State *L)
{
lua_newtable(L);
@@ -493,6 +509,8 @@ void LuaItemStack::Register(lua_State *L)
// Can be created from Lua (ItemStack(itemstack or itemstring or table or nil))
lua_register(L, className, create_object);
+
+ script_register_packer(L, className, packIn, packOut);
}
const char LuaItemStack::className[] = "ItemStack";
@@ -638,8 +656,8 @@ int ModApiItemMod::l_get_content_id(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1);
- const IItemDefManager *idef = getGameDef(L)->getItemDefManager();
- const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
+ const IItemDefManager *idef = getGameDef(L)->idef();
+ const NodeDefManager *ndef = getGameDef(L)->ndef();
// If this is called at mod load time, NodeDefManager isn't aware of
// aliases yet, so we need to handle them manually
@@ -664,7 +682,7 @@ int ModApiItemMod::l_get_name_from_content_id(lua_State *L)
NO_MAP_LOCK_REQUIRED;
content_t c = luaL_checkint(L, 1);
- const NodeDefManager *ndef = getGameDef(L)->getNodeDefManager();
+ const NodeDefManager *ndef = getGameDef(L)->ndef();
const char *name = ndef->get(c).name.c_str();
lua_pushstring(L, name);
@@ -679,3 +697,10 @@ void ModApiItemMod::Initialize(lua_State *L, int top)
API_FCT(get_content_id);
API_FCT(get_name_from_content_id);
}
+
+void ModApiItemMod::InitializeAsync(lua_State *L, int top)
+{
+ // all read-only functions
+ API_FCT(get_content_id);
+ API_FCT(get_name_from_content_id);
+}
diff --git a/src/script/lua_api/l_item.h b/src/script/lua_api/l_item.h
index 16878c101..180975061 100644
--- a/src/script/lua_api/l_item.h
+++ b/src/script/lua_api/l_item.h
@@ -141,8 +141,11 @@ public:
// Not callable from Lua
static int create(lua_State *L, const ItemStack &item);
static LuaItemStack* checkobject(lua_State *L, int narg);
- static void Register(lua_State *L);
+ static void *packIn(lua_State *L, int idx);
+ static void packOut(lua_State *L, void *ptr);
+
+ static void Register(lua_State *L);
};
class ModApiItemMod : public ModApiBase {
@@ -152,6 +155,8 @@ private:
static int l_register_alias_raw(lua_State *L);
static int l_get_content_id(lua_State *L);
static int l_get_name_from_content_id(lua_State *L);
+
public:
static void Initialize(lua_State *L, int top);
+ static void InitializeAsync(lua_State *L, int top);
};
diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp
index 769b3ef2b..1da0679d6 100644
--- a/src/script/lua_api/l_localplayer.cpp
+++ b/src/script/lua_api/l_localplayer.cpp
@@ -196,11 +196,11 @@ int LuaLocalPlayer::l_is_in_liquid_stable(lua_State *L)
return 1;
}
-int LuaLocalPlayer::l_get_liquid_viscosity(lua_State *L)
+int LuaLocalPlayer::l_get_move_resistance(lua_State *L)
{
LocalPlayer *player = getobject(L, 1);
- lua_pushinteger(L, player->liquid_viscosity);
+ lua_pushinteger(L, player->move_resistance);
return 1;
}
@@ -302,13 +302,15 @@ int LuaLocalPlayer::l_get_control(lua_State *L)
set("dig", c.dig);
set("place", c.place);
// Player movement in polar coordinates and non-binary speed
- set("movement_speed", c.movement_speed);
- set("movement_direction", c.movement_direction);
+ lua_pushnumber(L, c.movement_speed);
+ lua_setfield(L, -2, "movement_speed");
+ lua_pushnumber(L, c.movement_direction);
+ lua_setfield(L, -2, "movement_direction");
// Provide direction keys to ensure compatibility
- set("up", player->keyPressed & (1 << 0)); // Up, down, left, and right were removed in favor of
- set("down", player->keyPressed & (1 << 1)); // analog direction indicators and are therefore not
- set("left", player->keyPressed & (1 << 2)); // available as booleans anymore. The corresponding values
- set("right", player->keyPressed & (1 << 3)); // can still be read from the keyPressed bits though.
+ set("up", c.direction_keys & (1 << 0));
+ set("down", c.direction_keys & (1 << 1));
+ set("left", c.direction_keys & (1 << 2));
+ set("right", c.direction_keys & (1 << 3));
return 1;
}
@@ -576,7 +578,6 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, is_touching_ground),
luamethod(LuaLocalPlayer, is_in_liquid),
luamethod(LuaLocalPlayer, is_in_liquid_stable),
- luamethod(LuaLocalPlayer, get_liquid_viscosity),
luamethod(LuaLocalPlayer, is_climbing),
luamethod(LuaLocalPlayer, swimming_vertical),
luamethod(LuaLocalPlayer, get_physics_override),
@@ -602,5 +603,7 @@ const luaL_Reg LuaLocalPlayer::methods[] = {
luamethod(LuaLocalPlayer, get_object),
luamethod(LuaLocalPlayer, get_hotbar_size),
+ luamethod(LuaLocalPlayer, get_move_resistance),
+
{0, 0}
};
diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h
index bb5a294ca..458c824e6 100644
--- a/src/script/lua_api/l_localplayer.h
+++ b/src/script/lua_api/l_localplayer.h
@@ -72,7 +72,6 @@ private:
static int l_is_touching_ground(lua_State *L);
static int l_is_in_liquid(lua_State *L);
static int l_is_in_liquid_stable(lua_State *L);
- static int l_get_liquid_viscosity(lua_State *L);
static int l_is_climbing(lua_State *L);
static int l_swimming_vertical(lua_State *L);
@@ -121,6 +120,8 @@ private:
// hud_get(self, id)
static int l_hud_get(lua_State *L);
+ static int l_get_move_resistance(lua_State *L);
+
// get_object(self)
static int l_get_object(lua_State *L);
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index 8eb0e252a..2b46a4d51 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -323,9 +323,9 @@ int ModApiMainMenu::l_get_games(lua_State *L)
lua_newtable(L);
int table2 = lua_gettop(L);
int internal_index = 1;
- for (const std::string &addon_mods_path : game.addon_mods_paths) {
+ for (const auto &addon_mods_path : game.addon_mods_paths) {
lua_pushnumber(L, internal_index);
- lua_pushstring(L, addon_mods_path.c_str());
+ lua_pushstring(L, addon_mods_path.second.c_str());
lua_settable(L, table2);
internal_index++;
}
@@ -414,25 +414,53 @@ int ModApiMainMenu::l_create_world(lua_State *L)
const char *name = luaL_checkstring(L, 1);
int gameidx = luaL_checkinteger(L,2) -1;
+ StringMap use_settings;
+ luaL_checktype(L, 3, LUA_TTABLE);
+ lua_pushnil(L);
+ while (lua_next(L, 3) != 0) {
+ // key at index -2 and value at index -1
+ use_settings[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
+ lua_pop(L, 1);
+ }
+ lua_pop(L, 1);
+
std::string path = porting::path_user + DIR_DELIM
"worlds" + DIR_DELIM
+ sanitizeDirName(name, "world_");
std::vector<SubgameSpec> games = getAvailableGames();
+ if (gameidx < 0 || gameidx >= (int) games.size()) {
+ lua_pushstring(L, "Invalid game index");
+ return 1;
+ }
- if ((gameidx >= 0) &&
- (gameidx < (int) games.size())) {
+ // Set the settings for world creation
+ // this is a bad hack but the best we have right now..
+ StringMap backup;
+ for (auto it : use_settings) {
+ if (g_settings->existsLocal(it.first))
+ backup[it.first] = g_settings->get(it.first);
+ g_settings->set(it.first, it.second);
+ }
- // Create world if it doesn't exist
- try {
- loadGameConfAndInitWorld(path, name, games[gameidx], true);
- lua_pushnil(L);
- } catch (const BaseException &e) {
- lua_pushstring(L, (std::string("Failed to initialize world: ") + e.what()).c_str());
- }
- } else {
- lua_pushstring(L, "Invalid game index");
+ // Create world if it doesn't exist
+ try {
+ loadGameConfAndInitWorld(path, name, games[gameidx], true);
+ lua_pushnil(L);
+ } catch (const BaseException &e) {
+ auto err = std::string("Failed to initialize world: ") + e.what();
+ lua_pushstring(L, err.c_str());
+ }
+
+ // Restore previous settings
+ for (auto it : use_settings) {
+ auto it2 = backup.find(it.first);
+ if (it2 == backup.end())
+ g_settings->remove(it.first); // wasn't set before
+ else
+ g_settings->set(it.first, it2->second); // was set before
}
+
return 1;
}
@@ -503,6 +531,21 @@ int ModApiMainMenu::l_get_modpath(lua_State *L)
}
/******************************************************************************/
+int ModApiMainMenu::l_get_modpaths(lua_State *L)
+{
+ lua_newtable(L);
+
+ ModApiMainMenu::l_get_modpath(L);
+ lua_setfield(L, -2, "mods");
+
+ for (const std::string &component : getEnvModPaths()) {
+ lua_pushstring(L, component.c_str());
+ lua_setfield(L, -2, fs::AbsolutePath(component).c_str());
+ }
+ return 1;
+}
+
+/******************************************************************************/
int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
{
std::string modpath = fs::RemoveRelativePathComponents(
@@ -548,7 +591,10 @@ int ModApiMainMenu::l_get_cache_path(lua_State *L)
/******************************************************************************/
int ModApiMainMenu::l_get_temp_path(lua_State *L)
{
- lua_pushstring(L, fs::TempPath().c_str());
+ if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
+ lua_pushstring(L, fs::TempPath().c_str());
+ else
+ lua_pushstring(L, fs::CreateTempFile().c_str());
return 1;
}
@@ -588,26 +634,24 @@ int ModApiMainMenu::l_copy_dir(lua_State *L)
const char *destination = luaL_checkstring(L, 2);
bool keep_source = true;
+ if (!lua_isnoneornil(L, 3))
+ keep_source = readParam<bool>(L, 3);
- if ((!lua_isnone(L,3)) &&
- (!lua_isnil(L,3))) {
- keep_source = readParam<bool>(L,3);
- }
-
- std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
- std::string absolute_source = fs::RemoveRelativePathComponents(source);
+ std::string abs_destination = fs::RemoveRelativePathComponents(destination);
+ std::string abs_source = fs::RemoveRelativePathComponents(source);
- if ((ModApiMainMenu::mayModifyPath(absolute_destination))) {
- bool retval = fs::CopyDir(absolute_source,absolute_destination);
-
- if (retval && (!keep_source)) {
-
- retval &= fs::RecursiveDelete(absolute_source);
- }
- lua_pushboolean(L,retval);
+ if (!ModApiMainMenu::mayModifyPath(abs_destination) ||
+ (!keep_source && !ModApiMainMenu::mayModifyPath(abs_source))) {
+ lua_pushboolean(L, false);
return 1;
}
- lua_pushboolean(L,false);
+
+ bool retval;
+ if (keep_source)
+ retval = fs::CopyDir(abs_source, abs_destination);
+ else
+ retval = fs::MoveDir(abs_source, abs_destination);
+ lua_pushboolean(L, retval);
return 1;
}
@@ -629,9 +673,9 @@ int ModApiMainMenu::l_extract_zip(lua_State *L)
std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
- auto rendering_engine = getGuiEngine(L)->m_rendering_engine;
- fs::CreateAllDirs(absolute_destination);
- lua_pushboolean(L, fs::extractZipFile(rendering_engine->get_filesystem(), zipfile, destination));
+ auto fs = RenderingEngine::get_raw_device()->getFileSystem();
+ bool ok = fs::extractZipFile(fs, zipfile, destination);
+ lua_pushboolean(L, ok);
return 1;
}
@@ -759,8 +803,9 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
/******************************************************************************/
int ModApiMainMenu::l_gettext(lua_State *L)
{
- std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
- lua_pushstring(L, text.c_str());
+ const char *srctext = luaL_checkstring(L, 1);
+ const char *text = *srctext ? gettext(srctext) : "";
+ lua_pushstring(L, text);
return 1;
}
@@ -860,6 +905,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_mapgen_names);
API_FCT(get_user_path);
API_FCT(get_modpath);
+ API_FCT(get_modpaths);
API_FCT(get_clientmodpath);
API_FCT(get_gamepath);
API_FCT(get_texturepath);
@@ -893,6 +939,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
API_FCT(get_mapgen_names);
API_FCT(get_user_path);
API_FCT(get_modpath);
+ API_FCT(get_modpaths);
API_FCT(get_clientmodpath);
API_FCT(get_gamepath);
API_FCT(get_texturepath);
@@ -903,10 +950,10 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
API_FCT(delete_dir);
API_FCT(copy_dir);
API_FCT(is_dir);
- //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
+ API_FCT(extract_zip);
API_FCT(may_modify_path);
API_FCT(download_file);
API_FCT(get_min_supp_proto);
API_FCT(get_max_supp_proto);
- //API_FCT(gettext); (gettext lib isn't threadsafe)
+ API_FCT(gettext);
}
diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h
index ec2d20da2..781185425 100644
--- a/src/script/lua_api/l_mainmenu.h
+++ b/src/script/lua_api/l_mainmenu.h
@@ -112,6 +112,8 @@ private:
static int l_get_modpath(lua_State *L);
+ static int l_get_modpaths(lua_State *L);
+
static int l_get_clientmodpath(lua_State *L);
static int l_get_gamepath(lua_State *L);
diff --git a/src/script/lua_api/l_metadata.cpp b/src/script/lua_api/l_metadata.cpp
index 21002e6a7..d00cb4daa 100644
--- a/src/script/lua_api/l_metadata.cpp
+++ b/src/script/lua_api/l_metadata.cpp
@@ -82,9 +82,10 @@ int MetaDataRef::l_get(lua_State *L)
std::string str;
if (meta->getStringToRef(name, str)) {
lua_pushlstring(L, str.c_str(), str.size());
- return 1;
+ } else {
+ lua_pushnil(L);
}
- return 0;
+ return 1;
}
// get_string(self, name)
diff --git a/src/script/lua_api/l_nodemeta.cpp b/src/script/lua_api/l_nodemeta.cpp
index 60d14f8f2..1d052685e 100644
--- a/src/script/lua_api/l_nodemeta.cpp
+++ b/src/script/lua_api/l_nodemeta.cpp
@@ -89,7 +89,10 @@ int NodeMetaRef::l_get_inventory(lua_State *L)
NodeMetaRef *ref = checkobject(L, 1);
ref->getmeta(true); // try to ensure the metadata exists
- InvRef::createNodeMeta(L, ref->m_p);
+
+ InventoryLocation loc;
+ loc.setNodeMeta(ref->m_p);
+ InvRef::create(L, loc);
return 1;
}
@@ -124,18 +127,14 @@ void NodeMetaRef::handleToTable(lua_State *L, Metadata *_meta)
// fields
MetaDataRef::handleToTable(L, _meta);
- NodeMetadata *meta = (NodeMetadata*) _meta;
+ NodeMetadata *meta = (NodeMetadata *) _meta;
// inventory
- lua_newtable(L);
Inventory *inv = meta->getInventory();
if (inv) {
- std::vector<const InventoryList *> lists = inv->getLists();
- for(std::vector<const InventoryList *>::const_iterator
- i = lists.begin(); i != lists.end(); ++i) {
- push_inventory_list(L, inv, (*i)->getName().c_str());
- lua_setfield(L, -2, (*i)->getName().c_str());
- }
+ push_inventory_lists(L, *inv);
+ } else {
+ lua_newtable(L);
}
lua_setfield(L, -2, "inventory");
}
diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp
index f43ba837a..5561eaebf 100644
--- a/src/script/lua_api/l_noise.cpp
+++ b/src/script/lua_api/l_noise.cpp
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_internal.h"
#include "common/c_converter.h"
#include "common/c_content.h"
+#include "common/c_packer.h"
#include "log.h"
#include "porting.h"
#include "util/numeric.h"
@@ -30,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
LuaPerlinNoise
*/
-LuaPerlinNoise::LuaPerlinNoise(NoiseParams *params) :
+LuaPerlinNoise::LuaPerlinNoise(const NoiseParams *params) :
np(*params)
{
}
@@ -101,6 +102,25 @@ LuaPerlinNoise *LuaPerlinNoise::checkobject(lua_State *L, int narg)
}
+void *LuaPerlinNoise::packIn(lua_State *L, int idx)
+{
+ LuaPerlinNoise *o = checkobject(L, idx);
+ return new NoiseParams(o->np);
+}
+
+void LuaPerlinNoise::packOut(lua_State *L, void *ptr)
+{
+ NoiseParams *np = reinterpret_cast<NoiseParams*>(ptr);
+ if (L) {
+ LuaPerlinNoise *o = new LuaPerlinNoise(np);
+ *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
+ luaL_getmetatable(L, className);
+ lua_setmetatable(L, -2);
+ }
+ delete np;
+}
+
+
void LuaPerlinNoise::Register(lua_State *L)
{
lua_newtable(L);
@@ -126,6 +146,8 @@ void LuaPerlinNoise::Register(lua_State *L)
lua_pop(L, 1);
lua_register(L, className, create_object);
+
+ script_register_packer(L, className, packIn, packOut);
}
@@ -141,12 +163,10 @@ luaL_Reg LuaPerlinNoise::methods[] = {
LuaPerlinNoiseMap
*/
-LuaPerlinNoiseMap::LuaPerlinNoiseMap(NoiseParams *params, s32 seed, v3s16 size)
+LuaPerlinNoiseMap::LuaPerlinNoiseMap(const NoiseParams *np, s32 seed, v3s16 size)
{
- m_is3d = size.Z > 1;
- np = *params;
try {
- noise = new Noise(&np, seed, size.X, size.Y, size.Z);
+ noise = new Noise(np, seed, size.X, size.Y, size.Z);
} catch (InvalidNoiseParamsException &e) {
throw LuaError(e.what());
}
@@ -217,7 +237,7 @@ int LuaPerlinNoiseMap::l_get_3d_map(lua_State *L)
LuaPerlinNoiseMap *o = checkobject(L, 1);
v3f p = check_v3f(L, 2);
- if (!o->m_is3d)
+ if (!o->is3D())
return 0;
Noise *n = o->noise;
@@ -248,7 +268,7 @@ int LuaPerlinNoiseMap::l_get_3d_map_flat(lua_State *L)
v3f p = check_v3f(L, 2);
bool use_buffer = lua_istable(L, 3);
- if (!o->m_is3d)
+ if (!o->is3D())
return 0;
Noise *n = o->noise;
@@ -289,7 +309,7 @@ int LuaPerlinNoiseMap::l_calc_3d_map(lua_State *L)
LuaPerlinNoiseMap *o = checkobject(L, 1);
v3f p = check_v3f(L, 2);
- if (!o->m_is3d)
+ if (!o->is3D())
return 0;
Noise *n = o->noise;
@@ -359,6 +379,35 @@ LuaPerlinNoiseMap *LuaPerlinNoiseMap::checkobject(lua_State *L, int narg)
}
+struct NoiseMapParams {
+ NoiseParams np;
+ s32 seed;
+ v3s16 size;
+};
+
+void *LuaPerlinNoiseMap::packIn(lua_State *L, int idx)
+{
+ LuaPerlinNoiseMap *o = checkobject(L, idx);
+ NoiseMapParams *ret = new NoiseMapParams();
+ ret->np = o->noise->np;
+ ret->seed = o->noise->seed;
+ ret->size = v3s16(o->noise->sx, o->noise->sy, o->noise->sz);
+ return ret;
+}
+
+void LuaPerlinNoiseMap::packOut(lua_State *L, void *ptr)
+{
+ NoiseMapParams *p = reinterpret_cast<NoiseMapParams*>(ptr);
+ if (L) {
+ LuaPerlinNoiseMap *o = new LuaPerlinNoiseMap(&p->np, p->seed, p->size);
+ *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
+ luaL_getmetatable(L, className);
+ lua_setmetatable(L, -2);
+ }
+ delete p;
+}
+
+
void LuaPerlinNoiseMap::Register(lua_State *L)
{
lua_newtable(L);
@@ -384,6 +433,8 @@ void LuaPerlinNoiseMap::Register(lua_State *L)
lua_pop(L, 1);
lua_register(L, className, create_object);
+
+ script_register_packer(L, className, packIn, packOut);
}
diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h
index 9f50dfd3f..5d34a479b 100644
--- a/src/script/lua_api/l_noise.h
+++ b/src/script/lua_api/l_noise.h
@@ -30,6 +30,7 @@ class LuaPerlinNoise : public ModApiBase
{
private:
NoiseParams np;
+
static const char className[];
static luaL_Reg methods[];
@@ -42,7 +43,7 @@ private:
static int l_get_3d(lua_State *L);
public:
- LuaPerlinNoise(NoiseParams *params);
+ LuaPerlinNoise(const NoiseParams *params);
~LuaPerlinNoise() = default;
// LuaPerlinNoise(seed, octaves, persistence, scale)
@@ -51,6 +52,9 @@ public:
static LuaPerlinNoise *checkobject(lua_State *L, int narg);
+ static void *packIn(lua_State *L, int idx);
+ static void packOut(lua_State *L, void *ptr);
+
static void Register(lua_State *L);
};
@@ -59,9 +63,8 @@ public:
*/
class LuaPerlinNoiseMap : public ModApiBase
{
- NoiseParams np;
Noise *noise;
- bool m_is3d;
+
static const char className[];
static luaL_Reg methods[];
@@ -80,16 +83,20 @@ class LuaPerlinNoiseMap : public ModApiBase
static int l_get_map_slice(lua_State *L);
public:
- LuaPerlinNoiseMap(NoiseParams *np, s32 seed, v3s16 size);
-
+ LuaPerlinNoiseMap(const NoiseParams *np, s32 seed, v3s16 size);
~LuaPerlinNoiseMap();
+ inline bool is3D() const { return noise->sz > 1; }
+
// LuaPerlinNoiseMap(np, size)
// Creates an LuaPerlinNoiseMap and leaves it on top of stack
static int create_object(lua_State *L);
static LuaPerlinNoiseMap *checkobject(lua_State *L, int narg);
+ static void *packIn(lua_State *L, int idx);
+ static void packOut(lua_State *L, void *ptr);
+
static void Register(lua_State *L);
};
diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp
index b7185f7ec..39b19364e 100644
--- a/src/script/lua_api/l_object.cpp
+++ b/src/script/lua_api/l_object.cpp
@@ -174,7 +174,7 @@ int ObjectRef::l_punch(lua_State *L)
v3f dir = readParam<v3f>(L, 5, sao->getBasePosition() - puncher->getBasePosition());
dir.normalize();
- u16 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch);
+ u32 wear = sao->punch(dir, &toolcap, puncher, time_from_last_punch);
lua_pushnumber(L, wear);
return 1;
@@ -420,8 +420,7 @@ int ObjectRef::l_set_local_animation(lua_State *L)
float frame_speed = readParam<float>(L, 6, 30.0f);
getServer(L)->setLocalPlayerAnimations(player, frames, frame_speed);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_local_animation(self)
@@ -464,8 +463,7 @@ int ObjectRef::l_set_eye_offset(lua_State *L)
offset_third.Y = rangelim(offset_third.Y,-10,15); //1.5*BS
getServer(L)->setPlayerEyeOffset(player, offset_first, offset_third);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_eye_offset(self)
@@ -737,8 +735,7 @@ int ObjectRef::l_set_nametag_attributes(lua_State *L)
prop->validate();
sao->notifyObjectPropertiesModified();
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_nametag_attributes(self)
@@ -1116,7 +1113,7 @@ int ObjectRef::l_set_look_vertical(lua_State *L)
float pitch = readParam<float>(L, 2) * core::RADTODEG;
playersao->setLookPitchAndSend(pitch);
- return 1;
+ return 0;
}
// set_look_horizontal(self, radians)
@@ -1131,7 +1128,7 @@ int ObjectRef::l_set_look_horizontal(lua_State *L)
float yaw = readParam<float>(L, 2) * core::RADTODEG;
playersao->setPlayerYawAndSend(yaw);
- return 1;
+ return 0;
}
// DEPRECATED
@@ -1151,7 +1148,7 @@ int ObjectRef::l_set_look_pitch(lua_State *L)
float pitch = readParam<float>(L, 2) * core::RADTODEG;
playersao->setLookPitchAndSend(pitch);
- return 1;
+ return 0;
}
// DEPRECATED
@@ -1171,7 +1168,7 @@ int ObjectRef::l_set_look_yaw(lua_State *L)
float yaw = readParam<float>(L, 2) * core::RADTODEG;
playersao->setPlayerYawAndSend(yaw);
- return 1;
+ return 0;
}
// set_fov(self, degrees, is_multiplier, transition_time)
@@ -1310,8 +1307,7 @@ int ObjectRef::l_set_inventory_formspec(lua_State *L)
player->inventory_formspec = formspec;
getServer(L)->reportInventoryFormspecModified(player->getName());
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_inventory_formspec(self) -> formspec
@@ -1342,8 +1338,7 @@ int ObjectRef::l_set_formspec_prepend(lua_State *L)
player->formspec_prepend = formspec;
getServer(L)->reportFormspecPrependModified(player->getName());
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_formspec_prepend(self)
@@ -1367,20 +1362,19 @@ int ObjectRef::l_get_player_control(lua_State *L)
NO_MAP_LOCK_REQUIRED;
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
- if (player == nullptr) {
- lua_pushlstring(L, "", 0);
- return 1;
- }
- const PlayerControl &control = player->getPlayerControl();
lua_newtable(L);
- lua_pushboolean(L, player->keyPressed & (1 << 0));
+ if (player == nullptr)
+ return 1;
+
+ const PlayerControl &control = player->getPlayerControl();
+ lua_pushboolean(L, control.direction_keys & (1 << 0));
lua_setfield(L, -2, "up");
- lua_pushboolean(L, player->keyPressed & (1 << 1));
+ lua_pushboolean(L, control.direction_keys & (1 << 1));
lua_setfield(L, -2, "down");
- lua_pushboolean(L, player->keyPressed & (1 << 2));
+ lua_pushboolean(L, control.direction_keys & (1 << 2));
lua_setfield(L, -2, "left");
- lua_pushboolean(L, player->keyPressed & (1 << 3));
+ lua_pushboolean(L, control.direction_keys & (1 << 3));
lua_setfield(L, -2, "right");
lua_pushboolean(L, control.jump);
lua_setfield(L, -2, "jump");
@@ -1409,11 +1403,25 @@ int ObjectRef::l_get_player_control_bits(lua_State *L)
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
if (player == nullptr) {
- lua_pushlstring(L, "", 0);
+ lua_pushinteger(L, 0);
return 1;
}
- lua_pushnumber(L, player->keyPressed);
+ const auto &c = player->getPlayerControl();
+
+ // This is very close to PlayerControl::getKeysPressed() but duplicated
+ // here so the encoding in the API is not inadvertedly changed.
+ u32 keypress_bits =
+ c.direction_keys |
+ ( (u32)(c.jump & 1) << 4) |
+ ( (u32)(c.aux1 & 1) << 5) |
+ ( (u32)(c.sneak & 1) << 6) |
+ ( (u32)(c.dig & 1) << 7) |
+ ( (u32)(c.place & 1) << 8) |
+ ( (u32)(c.zoom & 1) << 9)
+ ;
+
+ lua_pushinteger(L, keypress_bits);
return 1;
}
@@ -1590,8 +1598,7 @@ int ObjectRef::l_hud_set_flags(lua_State *L)
if (!getServer(L)->hudSetFlags(player, flags, mask))
return 0;
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// hud_get_flags(self)
@@ -1604,20 +1611,11 @@ int ObjectRef::l_hud_get_flags(lua_State *L)
return 0;
lua_newtable(L);
- lua_pushboolean(L, player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE);
- lua_setfield(L, -2, "hotbar");
- lua_pushboolean(L, player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE);
- lua_setfield(L, -2, "healthbar");
- lua_pushboolean(L, player->hud_flags & HUD_FLAG_CROSSHAIR_VISIBLE);
- lua_setfield(L, -2, "crosshair");
- lua_pushboolean(L, player->hud_flags & HUD_FLAG_WIELDITEM_VISIBLE);
- lua_setfield(L, -2, "wielditem");
- lua_pushboolean(L, player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE);
- lua_setfield(L, -2, "breathbar");
- lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE);
- lua_setfield(L, -2, "minimap");
- lua_pushboolean(L, player->hud_flags & HUD_FLAG_MINIMAP_RADAR_VISIBLE);
- lua_setfield(L, -2, "minimap_radar");
+ const EnumString *esp = es_HudBuiltinElement;
+ for (int i = 0; esp[i].str; i++) {
+ lua_pushboolean(L, (player->hud_flags & esp[i].num) != 0);
+ lua_setfield(L, -2, esp[i].str);
+ }
return 1;
}
@@ -1722,9 +1720,11 @@ int ObjectRef::l_set_sky(lua_State *L)
return 0;
SkyboxParams sky_params = player->getSkyParams();
- bool is_colorspec = is_color_table(L, 2);
- if (lua_istable(L, 2) && !is_colorspec) {
+ // reset if empty
+ if (lua_isnoneornil(L, 2) && lua_isnone(L, 3)) {
+ sky_params = SkyboxDefaults::getSkyDefaults();
+ } else if (lua_istable(L, 2) && !is_color_table(L, 2)) {
lua_getfield(L, 2, "base_color");
if (!lua_isnil(L, -1))
read_color(L, -1, &sky_params.bgcolor);
@@ -1748,17 +1748,11 @@ int ObjectRef::l_set_sky(lua_State *L)
}
lua_pop(L, 1);
- /*
- We want to avoid crashes, so we're checking even if we're not using them.
- However, we want to ensure that the skybox can be set to nil when
- using "regular" or "plain" skybox modes as textures aren't needed.
- */
-
- if (sky_params.textures.size() != 6 && sky_params.textures.size() > 0)
+ // Validate that we either have six or zero textures
+ if (sky_params.textures.size() != 6 && !sky_params.textures.empty())
throw LuaError("Skybox expects 6 textures!");
- sky_params.clouds = getboolfield_default(L, 2,
- "clouds", sky_params.clouds);
+ sky_params.clouds = getboolfield_default(L, 2, "clouds", sky_params.clouds);
lua_getfield(L, 2, "sky_color");
if (lua_istable(L, -1)) {
@@ -1806,7 +1800,7 @@ int ObjectRef::l_set_sky(lua_State *L)
sky_params.fog_tint_type = luaL_checkstring(L, -1);
lua_pop(L, 1);
- // Because we need to leave the "sky_color" table.
+ // pop "sky_color" table
lua_pop(L, 1);
}
} else {
@@ -1842,11 +1836,8 @@ int ObjectRef::l_set_sky(lua_State *L)
if (lua_istable(L, 4)) {
lua_pushnil(L);
while (lua_next(L, 4) != 0) {
- // Key at index -2, and value at index -1
- if (lua_isstring(L, -1))
- sky_params.textures.emplace_back(readParam<std::string>(L, -1));
- else
- sky_params.textures.emplace_back("");
+ // Key at index -2, and value at index -1
+ sky_params.textures.emplace_back(readParam<std::string>(L, -1));
// Remove the value, keep the key for the next iteration
lua_pop(L, 1);
}
@@ -1862,12 +1853,39 @@ int ObjectRef::l_set_sky(lua_State *L)
getServer(L)->setMoon(player, moon_params);
getServer(L)->setStars(player, star_params);
}
+
getServer(L)->setSky(player, sky_params);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
+}
+
+static void push_sky_color(lua_State *L, const SkyboxParams &params)
+{
+ lua_newtable(L);
+ if (params.type == "regular") {
+ push_ARGB8(L, params.sky_color.day_sky);
+ lua_setfield(L, -2, "day_sky");
+ push_ARGB8(L, params.sky_color.day_horizon);
+ lua_setfield(L, -2, "day_horizon");
+ push_ARGB8(L, params.sky_color.dawn_sky);
+ lua_setfield(L, -2, "dawn_sky");
+ push_ARGB8(L, params.sky_color.dawn_horizon);
+ lua_setfield(L, -2, "dawn_horizon");
+ push_ARGB8(L, params.sky_color.night_sky);
+ lua_setfield(L, -2, "night_sky");
+ push_ARGB8(L, params.sky_color.night_horizon);
+ lua_setfield(L, -2, "night_horizon");
+ push_ARGB8(L, params.sky_color.indoors);
+ lua_setfield(L, -2, "indoors");
+ }
+ push_ARGB8(L, params.fog_sun_tint);
+ lua_setfield(L, -2, "fog_sun_tint");
+ push_ARGB8(L, params.fog_moon_tint);
+ lua_setfield(L, -2, "fog_moon_tint");
+ lua_pushstring(L, params.fog_tint_type.c_str());
+ lua_setfield(L, -2, "fog_tint_type");
}
-// get_sky(self)
+// get_sky(self, as_table)
int ObjectRef::l_get_sky(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
@@ -1876,10 +1894,30 @@ int ObjectRef::l_get_sky(lua_State *L)
if (player == nullptr)
return 0;
- SkyboxParams skybox_params = player->getSkyParams();
+ const SkyboxParams &skybox_params = player->getSkyParams();
+
+ // handle the deprecated version
+ if (!readParam<bool>(L, 2, false)) {
+ log_deprecated(L, "Deprecated call to get_sky, please check lua_api.txt");
+ push_ARGB8(L, skybox_params.bgcolor);
+ lua_pushlstring(L, skybox_params.type.c_str(), skybox_params.type.size());
+
+ lua_newtable(L);
+ s16 i = 1;
+ for (const std::string &texture : skybox_params.textures) {
+ lua_pushlstring(L, texture.c_str(), texture.size());
+ lua_rawseti(L, -2, i++);
+ }
+ lua_pushboolean(L, skybox_params.clouds);
+ return 4;
+ }
+
+ lua_newtable(L);
push_ARGB8(L, skybox_params.bgcolor);
+ lua_setfield(L, -2, "base_color");
lua_pushlstring(L, skybox_params.type.c_str(), skybox_params.type.size());
+ lua_setfield(L, -2, "type");
lua_newtable(L);
s16 i = 1;
@@ -1887,44 +1925,30 @@ int ObjectRef::l_get_sky(lua_State *L)
lua_pushlstring(L, texture.c_str(), texture.size());
lua_rawseti(L, -2, i++);
}
+ lua_setfield(L, -2, "textures");
lua_pushboolean(L, skybox_params.clouds);
- return 4;
+ lua_setfield(L, -2, "clouds");
+
+ push_sky_color(L, skybox_params);
+ lua_setfield(L, -2, "sky_color");
+ return 1;
}
+// DEPRECATED
// get_sky_color(self)
int ObjectRef::l_get_sky_color(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
+
+ log_deprecated(L, "Deprecated call to get_sky_color, use get_sky instead");
+
ObjectRef *ref = checkobject(L, 1);
RemotePlayer *player = getplayer(ref);
if (player == nullptr)
return 0;
const SkyboxParams &skybox_params = player->getSkyParams();
-
- lua_newtable(L);
- if (skybox_params.type == "regular") {
- push_ARGB8(L, skybox_params.sky_color.day_sky);
- lua_setfield(L, -2, "day_sky");
- push_ARGB8(L, skybox_params.sky_color.day_horizon);
- lua_setfield(L, -2, "day_horizon");
- push_ARGB8(L, skybox_params.sky_color.dawn_sky);
- lua_setfield(L, -2, "dawn_sky");
- push_ARGB8(L, skybox_params.sky_color.dawn_horizon);
- lua_setfield(L, -2, "dawn_horizon");
- push_ARGB8(L, skybox_params.sky_color.night_sky);
- lua_setfield(L, -2, "night_sky");
- push_ARGB8(L, skybox_params.sky_color.night_horizon);
- lua_setfield(L, -2, "night_horizon");
- push_ARGB8(L, skybox_params.sky_color.indoors);
- lua_setfield(L, -2, "indoors");
- }
- push_ARGB8(L, skybox_params.fog_sun_tint);
- lua_setfield(L, -2, "fog_sun_tint");
- push_ARGB8(L, skybox_params.fog_moon_tint);
- lua_setfield(L, -2, "fog_moon_tint");
- lua_pushstring(L, skybox_params.fog_tint_type.c_str());
- lua_setfield(L, -2, "fog_tint_type");
+ push_sky_color(L, skybox_params);
return 1;
}
@@ -1937,25 +1961,23 @@ int ObjectRef::l_set_sun(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
SunParams sun_params = player->getSunParams();
- sun_params.visible = getboolfield_default(L, 2,
- "visible", sun_params.visible);
- sun_params.texture = getstringfield_default(L, 2,
- "texture", sun_params.texture);
- sun_params.tonemap = getstringfield_default(L, 2,
- "tonemap", sun_params.tonemap);
- sun_params.sunrise = getstringfield_default(L, 2,
- "sunrise", sun_params.sunrise);
- sun_params.sunrise_visible = getboolfield_default(L, 2,
- "sunrise_visible", sun_params.sunrise_visible);
- sun_params.scale = getfloatfield_default(L, 2,
- "scale", sun_params.scale);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ sun_params = SkyboxDefaults::getSunDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ sun_params.visible = getboolfield_default(L, 2, "visible", sun_params.visible);
+ sun_params.texture = getstringfield_default(L, 2, "texture", sun_params.texture);
+ sun_params.tonemap = getstringfield_default(L, 2, "tonemap", sun_params.tonemap);
+ sun_params.sunrise = getstringfield_default(L, 2, "sunrise", sun_params.sunrise);
+ sun_params.sunrise_visible = getboolfield_default(L, 2, "sunrise_visible", sun_params.sunrise_visible);
+ sun_params.scale = getfloatfield_default(L, 2, "scale", sun_params.scale);
+ }
getServer(L)->setSun(player, sun_params);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
//get_sun(self)
@@ -1994,21 +2016,21 @@ int ObjectRef::l_set_moon(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
MoonParams moon_params = player->getMoonParams();
- moon_params.visible = getboolfield_default(L, 2,
- "visible", moon_params.visible);
- moon_params.texture = getstringfield_default(L, 2,
- "texture", moon_params.texture);
- moon_params.tonemap = getstringfield_default(L, 2,
- "tonemap", moon_params.tonemap);
- moon_params.scale = getfloatfield_default(L, 2,
- "scale", moon_params.scale);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ moon_params = SkyboxDefaults::getMoonDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ moon_params.visible = getboolfield_default(L, 2, "visible", moon_params.visible);
+ moon_params.texture = getstringfield_default(L, 2, "texture", moon_params.texture);
+ moon_params.tonemap = getstringfield_default(L, 2, "tonemap", moon_params.tonemap);
+ moon_params.scale = getfloatfield_default(L, 2, "scale", moon_params.scale);
+ }
getServer(L)->setMoon(player, moon_params);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_moon(self)
@@ -2043,25 +2065,27 @@ int ObjectRef::l_set_stars(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
StarParams star_params = player->getStarParams();
- star_params.visible = getboolfield_default(L, 2,
- "visible", star_params.visible);
- star_params.count = getintfield_default(L, 2,
- "count", star_params.count);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ star_params = SkyboxDefaults::getStarDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ star_params.visible = getboolfield_default(L, 2, "visible", star_params.visible);
+ star_params.count = getintfield_default(L, 2, "count", star_params.count);
- lua_getfield(L, 2, "star_color");
- if (!lua_isnil(L, -1))
- read_color(L, -1, &star_params.starcolor);
- lua_pop(L, 1);
+ lua_getfield(L, 2, "star_color");
+ if (!lua_isnil(L, -1))
+ read_color(L, -1, &star_params.starcolor);
+ lua_pop(L, 1);
- star_params.scale = getfloatfield_default(L, 2,
- "scale", star_params.scale);
+ star_params.scale = getfloatfield_default(L, 2,
+ "scale", star_params.scale);
+ }
getServer(L)->setStars(player, star_params);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_stars(self)
@@ -2096,35 +2120,39 @@ int ObjectRef::l_set_clouds(lua_State *L)
if (player == nullptr)
return 0;
- luaL_checktype(L, 2, LUA_TTABLE);
CloudParams cloud_params = player->getCloudParams();
- cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
+ // reset if empty
+ if (lua_isnoneornil(L, 2)) {
+ cloud_params = SkyboxDefaults::getCloudDefaults();
+ } else {
+ luaL_checktype(L, 2, LUA_TTABLE);
+ cloud_params.density = getfloatfield_default(L, 2, "density", cloud_params.density);
- lua_getfield(L, 2, "color");
- if (!lua_isnil(L, -1))
- read_color(L, -1, &cloud_params.color_bright);
- lua_pop(L, 1);
- lua_getfield(L, 2, "ambient");
- if (!lua_isnil(L, -1))
- read_color(L, -1, &cloud_params.color_ambient);
- lua_pop(L, 1);
+ lua_getfield(L, 2, "color");
+ if (!lua_isnil(L, -1))
+ read_color(L, -1, &cloud_params.color_bright);
+ lua_pop(L, 1);
+ lua_getfield(L, 2, "ambient");
+ if (!lua_isnil(L, -1))
+ read_color(L, -1, &cloud_params.color_ambient);
+ lua_pop(L, 1);
- cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height );
- cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
+ cloud_params.height = getfloatfield_default(L, 2, "height", cloud_params.height);
+ cloud_params.thickness = getfloatfield_default(L, 2, "thickness", cloud_params.thickness);
- lua_getfield(L, 2, "speed");
- if (lua_istable(L, -1)) {
- v2f new_speed;
- new_speed.X = getfloatfield_default(L, -1, "x", 0);
- new_speed.Y = getfloatfield_default(L, -1, "z", 0);
- cloud_params.speed = new_speed;
+ lua_getfield(L, 2, "speed");
+ if (lua_istable(L, -1)) {
+ v2f new_speed;
+ new_speed.X = getfloatfield_default(L, -1, "x", 0);
+ new_speed.Y = getfloatfield_default(L, -1, "z", 0);
+ cloud_params.speed = new_speed;
+ }
+ lua_pop(L, 1);
}
- lua_pop(L, 1);
getServer(L)->setClouds(player, cloud_params);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
int ObjectRef::l_get_clouds(lua_State *L)
@@ -2178,8 +2206,7 @@ int ObjectRef::l_override_day_night_ratio(lua_State *L)
}
getServer(L)->overrideDayNightRatio(player, do_override, ratio);
- lua_pushboolean(L, true);
- return 1;
+ return 0;
}
// get_day_night_ratio(self)
@@ -2256,6 +2283,46 @@ int ObjectRef::l_set_minimap_modes(lua_State *L)
return 0;
}
+// set_lighting(self, lighting)
+int ObjectRef::l_set_lighting(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ RemotePlayer *player = getplayer(ref);
+ if (player == nullptr)
+ return 0;
+
+ luaL_checktype(L, 2, LUA_TTABLE);
+ Lighting lighting = player->getLighting();
+ lua_getfield(L, 2, "shadows");
+ if (lua_istable(L, -1)) {
+ lighting.shadow_intensity = getfloatfield_default(L, -1, "intensity", lighting.shadow_intensity);
+ }
+ lua_pop(L, -1);
+
+ getServer(L)->setLighting(player, lighting);
+ return 0;
+}
+
+// get_lighting(self)
+int ObjectRef::l_get_lighting(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ObjectRef *ref = checkobject(L, 1);
+ RemotePlayer *player = getplayer(ref);
+ if (player == nullptr)
+ return 0;
+
+ const Lighting &lighting = player->getLighting();
+
+ lua_newtable(L); // result
+ lua_newtable(L); // "shadows"
+ lua_pushnumber(L, lighting.shadow_intensity);
+ lua_setfield(L, -2, "intensity");
+ lua_setfield(L, -2, "shadows");
+ return 1;
+}
+
ObjectRef::ObjectRef(ServerActiveObject *object):
m_object(object)
{}
@@ -2409,5 +2476,7 @@ luaL_Reg ObjectRef::methods[] = {
luamethod(ObjectRef, get_eye_offset),
luamethod(ObjectRef, send_mapblock),
luamethod(ObjectRef, set_minimap_modes),
+ luamethod(ObjectRef, set_lighting),
+ luamethod(ObjectRef, get_lighting),
{0,0}
};
diff --git a/src/script/lua_api/l_object.h b/src/script/lua_api/l_object.h
index db3a3a7cf..3e4e6681a 100644
--- a/src/script/lua_api/l_object.h
+++ b/src/script/lua_api/l_object.h
@@ -316,9 +316,10 @@ private:
// set_sky(self, sky_parameters)
static int l_set_sky(lua_State *L);
- // get_sky(self)
+ // get_sky(self, as_table)
static int l_get_sky(lua_State *L);
+ // DEPRECATED
// get_sky_color(self)
static int l_get_sky_color(lua_State* L);
@@ -375,4 +376,10 @@ private:
// set_minimap_modes(self, modes, wanted_mode)
static int l_set_minimap_modes(lua_State *L);
+
+ // set_lighting(self, lighting)
+ static int l_set_lighting(lua_State *L);
+
+ // get_lighting(self)
+ static int l_get_lighting(lua_State *L);
};
diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp
index b4672fe2a..e8105dd75 100644
--- a/src/script/lua_api/l_server.cpp
+++ b/src/script/lua_api/l_server.cpp
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_internal.h"
#include "common/c_converter.h"
#include "common/c_content.h"
+#include "common/c_packer.h"
#include "cpp_api/s_base.h"
#include "cpp_api/s_security.h"
#include "scripting_server.h"
@@ -57,6 +58,14 @@ int ModApiServer::l_get_server_uptime(lua_State *L)
return 1;
}
+// get_server_max_lag()
+int ModApiServer::l_get_server_max_lag(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ GET_ENV_PTR;
+ lua_pushnumber(L, env->getMaxLagEstimate());
+ return 1;
+}
// print(text)
int ModApiServer::l_print(lua_State *L)
@@ -282,8 +291,10 @@ int ModApiServer::l_ban_player(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- Server *server = getServer(L);
+ if (!getEnv(L))
+ throw LuaError("Can't ban player before server has started up");
+ Server *server = getServer(L);
const char *name = luaL_checkstring(L, 1);
RemotePlayer *player = server->getEnv().getPlayer(name);
if (!player) {
@@ -297,23 +308,30 @@ int ModApiServer::l_ban_player(lua_State *L)
return 1;
}
-// kick_player(name, [reason]) -> success
-int ModApiServer::l_kick_player(lua_State *L)
+// disconnect_player(name, [reason]) -> success
+int ModApiServer::l_disconnect_player(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
+
+ if (!getEnv(L))
+ throw LuaError("Can't kick player before server has started up");
+
const char *name = luaL_checkstring(L, 1);
- std::string message("Kicked");
+ std::string message;
if (lua_isstring(L, 2))
- message.append(": ").append(readParam<std::string>(L, 2));
+ message.append(readParam<std::string>(L, 2));
else
- message.append(".");
+ message.append("Disconnected.");
+
+ Server *server = getServer(L);
- RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name);
- if (player == NULL) {
+ RemotePlayer *player = server->getEnv().getPlayer(name);
+ if (!player) {
lua_pushboolean(L, false); // No such player
return 1;
}
- getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));
+
+ server->DenyAccess(player->getPeerId(), SERVER_ACCESSDENIED_CUSTOM_STRING, message);
lua_pushboolean(L, true);
return 1;
}
@@ -324,7 +342,8 @@ int ModApiServer::l_remove_player(lua_State *L)
NO_MAP_LOCK_REQUIRED;
std::string name = luaL_checkstring(L, 1);
ServerEnvironment *s_env = dynamic_cast<ServerEnvironment *>(getEnv(L));
- assert(s_env);
+ if (!s_env)
+ throw LuaError("Can't remove player before server has started up");
RemotePlayer *player = s_env->getPlayer(name.c_str());
if (!player)
@@ -375,12 +394,11 @@ int ModApiServer::l_get_modpath(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
std::string modname = luaL_checkstring(L, 1);
- const ModSpec *mod = getServer(L)->getModSpec(modname);
- if (!mod) {
+ const ModSpec *mod = getGameDef(L)->getModSpec(modname);
+ if (!mod)
lua_pushnil(L);
- return 1;
- }
- lua_pushstring(L, mod->path.c_str());
+ else
+ lua_pushstring(L, mod->path.c_str());
return 1;
}
@@ -392,13 +410,14 @@ int ModApiServer::l_get_modnames(lua_State *L)
// Get a list of mods
std::vector<std::string> modlist;
- getServer(L)->getModNames(modlist);
+ for (auto &it : getGameDef(L)->getMods())
+ modlist.emplace_back(it.name);
std::sort(modlist.begin(), modlist.end());
// Package them up for Lua
lua_createtable(L, modlist.size(), 0);
- std::vector<std::string>::iterator iter = modlist.begin();
+ auto iter = modlist.begin();
for (u16 i = 0; iter != modlist.end(); ++iter) {
lua_pushstring(L, iter->c_str());
lua_rawseti(L, -2, ++i);
@@ -410,8 +429,8 @@ int ModApiServer::l_get_modnames(lua_State *L)
int ModApiServer::l_get_worldpath(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- std::string worldpath = getServer(L)->getWorldPath();
- lua_pushstring(L, worldpath.c_str());
+ const Server *srv = getServer(L);
+ lua_pushstring(L, srv->getWorldPath().c_str());
return 1;
}
@@ -479,7 +498,7 @@ int ModApiServer::l_dynamic_add_media(lua_State *L)
CHECK_SECURE_PATH(L, filepath.c_str(), false);
- u32 token = server->getScriptIface()->allocateDynamicMediaCallback(2);
+ u32 token = server->getScriptIface()->allocateDynamicMediaCallback(L, 2);
bool ok = server->dynamicAddMedia(filepath, token, to_player, ephemeral);
if (!ok)
@@ -493,7 +512,8 @@ int ModApiServer::l_dynamic_add_media(lua_State *L)
int ModApiServer::l_is_singleplayer(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- lua_pushboolean(L, getServer(L)->isSingleplayer());
+ const Server *srv = getServer(L);
+ lua_pushboolean(L, srv->isSingleplayer());
return 1;
}
@@ -508,11 +528,82 @@ int ModApiServer::l_notify_authentication_modified(lua_State *L)
return 0;
}
+// do_async_callback(func, params, mod_origin)
+int ModApiServer::l_do_async_callback(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ ServerScripting *script = getScriptApi<ServerScripting>(L);
+
+ luaL_checktype(L, 1, LUA_TFUNCTION);
+ luaL_checktype(L, 2, LUA_TTABLE);
+ luaL_checktype(L, 3, LUA_TSTRING);
+
+ call_string_dump(L, 1);
+ size_t func_length;
+ const char *serialized_func_raw = lua_tolstring(L, -1, &func_length);
+
+ PackedValue *param = script_pack(L, 2);
+
+ std::string mod_origin = readParam<std::string>(L, 3);
+
+ u32 jobId = script->queueAsync(
+ std::string(serialized_func_raw, func_length),
+ param, mod_origin);
+
+ lua_settop(L, 0);
+ lua_pushinteger(L, jobId);
+ return 1;
+}
+
+// register_async_dofile(path)
+int ModApiServer::l_register_async_dofile(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ std::string path = readParam<std::string>(L, 1);
+ CHECK_SECURE_PATH(L, path.c_str(), false);
+
+ // Find currently running mod name (only at init time)
+ lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
+ if (!lua_isstring(L, -1))
+ return 0;
+ std::string modname = readParam<std::string>(L, -1);
+
+ getServer(L)->m_async_init_files.emplace_back(modname, path);
+ lua_pushboolean(L, true);
+ return 1;
+}
+
+// serialize_roundtrip(value)
+// Meant for unit testing the packer from Lua
+int ModApiServer::l_serialize_roundtrip(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ int top = lua_gettop(L);
+ auto *pv = script_pack(L, 1);
+ if (top != lua_gettop(L))
+ throw LuaError("stack values leaked");
+
+#ifndef NDEBUG
+ script_dump_packed(pv);
+#endif
+
+ top = lua_gettop(L);
+ script_unpack(L, pv);
+ delete pv;
+ if (top + 1 != lua_gettop(L))
+ throw LuaError("stack values leaked");
+
+ return 1;
+}
+
void ModApiServer::Initialize(lua_State *L, int top)
{
API_FCT(request_shutdown);
API_FCT(get_server_status);
API_FCT(get_server_uptime);
+ API_FCT(get_server_max_lag);
API_FCT(get_worldpath);
API_FCT(is_singleplayer);
@@ -536,7 +627,22 @@ void ModApiServer::Initialize(lua_State *L, int top)
API_FCT(get_ban_list);
API_FCT(get_ban_description);
API_FCT(ban_player);
- API_FCT(kick_player);
+ API_FCT(disconnect_player);
+ API_FCT(remove_player);
API_FCT(unban_player_or_ip);
API_FCT(notify_authentication_modified);
+
+ API_FCT(do_async_callback);
+ API_FCT(register_async_dofile);
+ API_FCT(serialize_roundtrip);
+}
+
+void ModApiServer::InitializeAsync(lua_State *L, int top)
+{
+ API_FCT(get_worldpath);
+ API_FCT(is_singleplayer);
+
+ API_FCT(get_current_modname);
+ API_FCT(get_modpath);
+ API_FCT(get_modnames);
}
diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h
index c688e494b..a4f38c34e 100644
--- a/src/script/lua_api/l_server.h
+++ b/src/script/lua_api/l_server.h
@@ -33,6 +33,9 @@ private:
// get_server_uptime()
static int l_get_server_uptime(lua_State *L);
+ // get_server_max_lag()
+ static int l_get_server_max_lag(lua_State *L);
+
// get_worldpath()
static int l_get_worldpath(lua_State *L);
@@ -94,8 +97,8 @@ private:
// unban_player_or_ip()
static int l_unban_player_or_ip(lua_State *L);
- // kick_player(name, [message]) -> success
- static int l_kick_player(lua_State *L);
+ // disconnect_player(name, [reason]) -> success
+ static int l_disconnect_player(lua_State *L);
// remove_player(name)
static int l_remove_player(lua_State *L);
@@ -103,6 +106,16 @@ private:
// notify_authentication_modified(name)
static int l_notify_authentication_modified(lua_State *L);
+ // do_async_callback(func, params, mod_origin)
+ static int l_do_async_callback(lua_State *L);
+
+ // register_async_dofile(path)
+ static int l_register_async_dofile(lua_State *L);
+
+ // serialize_roundtrip(obj)
+ static int l_serialize_roundtrip(lua_State *L);
+
public:
static void Initialize(lua_State *L, int top);
+ static void InitializeAsync(lua_State *L, int top);
};
diff --git a/src/script/lua_api/l_storage.cpp b/src/script/lua_api/l_storage.cpp
index 978b315d5..b8f4347a8 100644
--- a/src/script/lua_api/l_storage.cpp
+++ b/src/script/lua_api/l_storage.cpp
@@ -32,19 +32,23 @@ int ModApiStorage::l_get_mod_storage(lua_State *L)
std::string mod_name = readParam<std::string>(L, -1);
- ModMetadata *store = new ModMetadata(mod_name);
+ ModMetadata *store = nullptr;
+
if (IGameDef *gamedef = getGameDef(L)) {
- store->load(gamedef->getModStoragePath());
- gamedef->registerModStorage(store);
+ store = new ModMetadata(mod_name, gamedef->getModStorageDatabase());
+ if (gamedef->registerModStorage(store)) {
+ StorageRef::create(L, store);
+ int object = lua_gettop(L);
+ lua_pushvalue(L, object);
+ return 1;
+ }
} else {
- delete store;
assert(false); // this should not happen
}
- StorageRef::create(L, store);
- int object = lua_gettop(L);
+ delete store;
- lua_pushvalue(L, object);
+ lua_pushnil(L);
return 1;
}
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index d575eb603..fa749c2e5 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -41,7 +41,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/hex.h"
#include "util/sha1.h"
#include "util/png.h"
-#include <algorithm>
#include <cstdio>
// log([level,] text)
@@ -160,28 +159,33 @@ int ModApiUtil::l_write_json(lua_State *L)
return 1;
}
-// get_dig_params(groups, tool_capabilities)
+// get_dig_params(groups, tool_capabilities[, wear])
int ModApiUtil::l_get_dig_params(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
ItemGroupList groups;
read_groups(L, 1, groups);
ToolCapabilities tp = read_tool_capabilities(L, 2);
- push_dig_params(L, getDigParams(groups, &tp));
+ if (lua_isnoneornil(L, 3)) {
+ push_dig_params(L, getDigParams(groups, &tp));
+ } else {
+ u16 wear = readParam<int>(L, 3);
+ push_dig_params(L, getDigParams(groups, &tp, wear));
+ }
return 1;
}
-// get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+// get_hit_params(groups, tool_capabilities[, time_from_last_punch, [, wear]])
int ModApiUtil::l_get_hit_params(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
std::unordered_map<std::string, int> groups;
read_groups(L, 1, groups);
ToolCapabilities tp = read_tool_capabilities(L, 2);
- if(lua_isnoneornil(L, 3))
- push_hit_params(L, getHitParams(groups, &tp));
- else
- push_hit_params(L, getHitParams(groups, &tp, readParam<float>(L, 3)));
+ float time_from_last_punch = readParam<float>(L, 3, 1000000);
+ int wear = readParam<int>(L, 4, 0);
+ push_hit_params(L, getHitParams(groups, &tp,
+ time_from_last_punch, wear));
return 1;
}
@@ -344,6 +348,49 @@ int ModApiUtil::l_mkdir(lua_State *L)
return 1;
}
+// rmdir(path, recursive)
+int ModApiUtil::l_rmdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *path = luaL_checkstring(L, 1);
+ CHECK_SECURE_PATH(L, path, true);
+
+ bool recursive = readParam<bool>(L, 2, false);
+
+ if (recursive)
+ lua_pushboolean(L, fs::RecursiveDelete(path));
+ else
+ lua_pushboolean(L, fs::DeleteSingleFileOrEmptyDirectory(path));
+
+ return 1;
+}
+
+// cpdir(source, destination)
+int ModApiUtil::l_cpdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *source = luaL_checkstring(L, 1);
+ const char *destination = luaL_checkstring(L, 2);
+ CHECK_SECURE_PATH(L, source, false);
+ CHECK_SECURE_PATH(L, destination, true);
+
+ lua_pushboolean(L, fs::CopyDir(source, destination));
+ return 1;
+}
+
+// mpdir(source, destination)
+int ModApiUtil::l_mvdir(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+ const char *source = luaL_checkstring(L, 1);
+ const char *destination = luaL_checkstring(L, 2);
+ CHECK_SECURE_PATH(L, source, true);
+ CHECK_SECURE_PATH(L, destination, true);
+
+ lua_pushboolean(L, fs::MoveDir(source, destination));
+ return 1;
+}
+
// get_dir_list(path, is_dir)
int ModApiUtil::l_get_dir_list(lua_State *L)
{
@@ -396,36 +443,7 @@ int ModApiUtil::l_request_insecure_environment(lua_State *L)
return 1;
}
- // We have to make sure that this function is being called directly by
- // a mod, otherwise a malicious mod could override this function and
- // steal its return value.
- lua_Debug info;
- // Make sure there's only one item below this function on the stack...
- if (lua_getstack(L, 2, &info)) {
- return 0;
- }
- FATAL_ERROR_IF(!lua_getstack(L, 1, &info), "lua_getstack() failed");
- FATAL_ERROR_IF(!lua_getinfo(L, "S", &info), "lua_getinfo() failed");
- // ...and that that item is the main file scope.
- if (strcmp(info.what, "main") != 0) {
- return 0;
- }
-
- // Get mod name
- lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
- if (!lua_isstring(L, -1)) {
- return 0;
- }
-
- // Check secure.trusted_mods
- std::string mod_name = readParam<std::string>(L, -1);
- std::string trusted_mods = g_settings->get("secure.trusted_mods");
- trusted_mods.erase(std::remove_if(trusted_mods.begin(),
- trusted_mods.end(), static_cast<int(*)(int)>(&std::isspace)),
- trusted_mods.end());
- std::vector<std::string> mod_list = str_split(trusted_mods, ',');
- if (std::find(mod_list.begin(), mod_list.end(), mod_name) ==
- mod_list.end()) {
+ if (!ScriptApiSecurity::checkWhitelisted(L, "secure.trusted_mods")) {
return 0;
}
@@ -583,6 +601,9 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(decompress);
API_FCT(mkdir);
+ API_FCT(rmdir);
+ API_FCT(cpdir);
+ API_FCT(mvdir);
API_FCT(get_dir_list);
API_FCT(safe_file_write);
@@ -651,7 +672,13 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(decompress);
API_FCT(mkdir);
+ API_FCT(rmdir);
+ API_FCT(cpdir);
+ API_FCT(mvdir);
API_FCT(get_dir_list);
+ API_FCT(safe_file_write);
+
+ API_FCT(request_insecure_environment);
API_FCT(encode_base64);
API_FCT(decode_base64);
@@ -661,6 +688,8 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(colorspec_to_colorstring);
API_FCT(colorspec_to_bytes);
+ API_FCT(encode_png);
+
API_FCT(get_last_run_mod);
API_FCT(set_last_run_mod);
diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h
index cc91e8d39..cc5563577 100644
--- a/src/script/lua_api/l_util.h
+++ b/src/script/lua_api/l_util.h
@@ -50,10 +50,10 @@ private:
// write_json(data[, styled])
static int l_write_json(lua_State *L);
- // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
+ // get_dig_params(groups, tool_capabilities[, wear])
static int l_get_dig_params(lua_State *L);
- // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
+ // get_hit_params(groups, tool_capabilities[, time_from_last_punch[, wear]])
static int l_get_hit_params(lua_State *L);
// check_password_entry(name, entry, password)
@@ -80,6 +80,15 @@ private:
// mkdir(path)
static int l_mkdir(lua_State *L);
+ // rmdir(path, recursive)
+ static int l_rmdir(lua_State *L);
+
+ // cpdir(source, destination, remove_source)
+ static int l_cpdir(lua_State *L);
+
+ // mvdir(source, destination)
+ static int l_mvdir(lua_State *L);
+
// get_dir_list(path, is_dir)
static int l_get_dir_list(lua_State *L);
@@ -120,6 +129,4 @@ public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);
static void InitializeClient(lua_State *L, int top);
-
- static void InitializeAsync(AsyncEngine &engine);
};
diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp
index e040e545b..6187a47db 100644
--- a/src/script/lua_api/l_vmanip.cpp
+++ b/src/script/lua_api/l_vmanip.cpp
@@ -17,11 +17,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
-
+#include <map>
#include "lua_api/l_vmanip.h"
#include "lua_api/l_internal.h"
#include "common/c_content.h"
#include "common/c_converter.h"
+#include "common/c_packer.h"
#include "emerge.h"
#include "environment.h"
#include "map.h"
@@ -45,6 +46,8 @@ int LuaVoxelManip::l_read_from_map(lua_State *L)
LuaVoxelManip *o = checkobject(L, 1);
MMVManip *vm = o->vm;
+ if (vm->isOrphan())
+ return 0;
v3s16 bp1 = getNodeBlockPos(check_v3s16(L, 2));
v3s16 bp2 = getNodeBlockPos(check_v3s16(L, 3));
@@ -112,23 +115,23 @@ int LuaVoxelManip::l_write_to_map(lua_State *L)
LuaVoxelManip *o = checkobject(L, 1);
bool update_light = !lua_isboolean(L, 2) || readParam<bool>(L, 2);
+
GET_ENV_PTR;
ServerMap *map = &(env->getServerMap());
+
+ std::map<v3s16, MapBlock*> modified_blocks;
if (o->is_mapgen_vm || !update_light) {
- o->vm->blitBackAll(&(o->modified_blocks));
+ o->vm->blitBackAll(&modified_blocks);
} else {
- voxalgo::blit_back_with_light(map, o->vm,
- &(o->modified_blocks));
+ voxalgo::blit_back_with_light(map, o->vm, &modified_blocks);
}
MapEditEvent event;
event.type = MEET_OTHER;
- for (const auto &modified_block : o->modified_blocks)
- event.modified_blocks.insert(modified_block.first);
-
+ for (const auto &it : modified_blocks)
+ event.modified_blocks.insert(it.first);
map->dispatchEvent(event);
- o->modified_blocks.clear();
return 0;
}
@@ -166,7 +169,7 @@ int LuaVoxelManip::l_update_liquids(lua_State *L)
LuaVoxelManip *o = checkobject(L, 1);
- Map *map = &(env->getMap());
+ ServerMap *map = &(env->getServerMap());
const NodeDefManager *ndef = getServer(L)->getNodeDefManager();
MMVManip *vm = o->vm;
@@ -429,6 +432,34 @@ LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg)
return *(LuaVoxelManip **)ud; // unbox pointer
}
+void *LuaVoxelManip::packIn(lua_State *L, int idx)
+{
+ LuaVoxelManip *o = checkobject(L, idx);
+
+ if (o->is_mapgen_vm)
+ throw LuaError("nope");
+ return o->vm->clone();
+}
+
+void LuaVoxelManip::packOut(lua_State *L, void *ptr)
+{
+ MMVManip *vm = reinterpret_cast<MMVManip*>(ptr);
+ if (!L) {
+ delete vm;
+ return;
+ }
+
+ // Associate vmanip with map if the Lua env has one
+ Environment *env = getEnv(L);
+ if (env)
+ vm->reparent(&(env->getMap()));
+
+ LuaVoxelManip *o = new LuaVoxelManip(vm, false);
+ *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
+ luaL_getmetatable(L, className);
+ lua_setmetatable(L, -2);
+}
+
void LuaVoxelManip::Register(lua_State *L)
{
lua_newtable(L);
@@ -455,6 +486,8 @@ void LuaVoxelManip::Register(lua_State *L)
// Can be created from Lua (VoxelManip())
lua_register(L, className, create_object);
+
+ script_register_packer(L, className, packIn, packOut);
}
const char LuaVoxelManip::className[] = "VoxelManip";
diff --git a/src/script/lua_api/l_vmanip.h b/src/script/lua_api/l_vmanip.h
index 15ab9eef8..005133335 100644
--- a/src/script/lua_api/l_vmanip.h
+++ b/src/script/lua_api/l_vmanip.h
@@ -19,7 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once
-#include <map>
#include "irr_v3d.h"
#include "lua_api/l_base.h"
@@ -33,7 +32,6 @@ class MMVManip;
class LuaVoxelManip : public ModApiBase
{
private:
- std::map<v3s16, MapBlock *> modified_blocks;
bool is_mapgen_vm = false;
static const char className[];
@@ -77,5 +75,8 @@ public:
static LuaVoxelManip *checkobject(lua_State *L, int narg);
+ static void *packIn(lua_State *L, int idx);
+ static void packOut(lua_State *L, void *ptr);
+
static void Register(lua_State *L);
};
diff --git a/src/script/scripting_server.cpp b/src/script/scripting_server.cpp
index 85411ded4..b462141b0 100644
--- a/src/script/scripting_server.cpp
+++ b/src/script/scripting_server.cpp
@@ -47,11 +47,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_storage.h"
extern "C" {
-#include "lualib.h"
+#include <lualib.h>
}
ServerScripting::ServerScripting(Server* server):
- ScriptApiBase(ScriptingType::Server)
+ ScriptApiBase(ScriptingType::Server),
+ asyncEngine(server)
{
setGameDef(server);
@@ -88,6 +89,48 @@ ServerScripting::ServerScripting(Server* server):
infostream << "SCRIPTAPI: Initialized game modules" << std::endl;
}
+void ServerScripting::initAsync()
+{
+ // Save globals to transfer
+ {
+ lua_State *L = getStack();
+ lua_getglobal(L, "core");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ lua_getfield(L, -1, "get_globals_to_transfer");
+ lua_call(L, 0, 1);
+ auto *data = script_pack(L, -1);
+ assert(!data->contains_userdata);
+ getServer()->m_async_globals_data.reset(data);
+ lua_pushnil(L);
+ lua_setfield(L, -3, "get_globals_to_transfer"); // unset function too
+ lua_pop(L, 2); // pop 'core', return value
+ }
+
+ infostream << "SCRIPTAPI: Initializing async engine" << std::endl;
+ asyncEngine.registerStateInitializer(InitializeAsync);
+ asyncEngine.registerStateInitializer(ModApiUtil::InitializeAsync);
+ asyncEngine.registerStateInitializer(ModApiCraft::InitializeAsync);
+ asyncEngine.registerStateInitializer(ModApiItemMod::InitializeAsync);
+ asyncEngine.registerStateInitializer(ModApiServer::InitializeAsync);
+ // not added: ModApiMapgen is a minefield for thread safety
+ // not added: ModApiHttp async api can't really work together with our jobs
+ // not added: ModApiStorage is probably not thread safe(?)
+
+ asyncEngine.initialize(0);
+}
+
+void ServerScripting::stepAsync()
+{
+ asyncEngine.step(getStack());
+}
+
+u32 ServerScripting::queueAsync(std::string &&serialized_func,
+ PackedValue *param, const std::string &mod_origin)
+{
+ return asyncEngine.queueAsyncJob(std::move(serialized_func),
+ param, mod_origin);
+}
+
void ServerScripting::InitializeModApi(lua_State *L, int top)
{
// Register reference classes (userdata)
@@ -125,3 +168,24 @@ void ServerScripting::InitializeModApi(lua_State *L, int top)
ModApiStorage::Initialize(L, top);
ModApiChannels::Initialize(L, top);
}
+
+void ServerScripting::InitializeAsync(lua_State *L, int top)
+{
+ // classes
+ LuaItemStack::Register(L);
+ LuaPerlinNoise::Register(L);
+ LuaPerlinNoiseMap::Register(L);
+ LuaPseudoRandom::Register(L);
+ LuaPcgRandom::Register(L);
+ LuaSecureRandom::Register(L);
+ LuaVoxelManip::Register(L);
+ LuaSettings::Register(L);
+
+ // globals data
+ lua_getglobal(L, "core");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ auto *data = ModApiBase::getServer(L)->m_async_globals_data.get();
+ script_unpack(L, data);
+ lua_setfield(L, -2, "transferred_globals");
+ lua_pop(L, 1); // pop 'core'
+}
diff --git a/src/script/scripting_server.h b/src/script/scripting_server.h
index bf06ab197..9803397c5 100644
--- a/src/script/scripting_server.h
+++ b/src/script/scripting_server.h
@@ -27,6 +27,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_player.h"
#include "cpp_api/s_server.h"
#include "cpp_api/s_security.h"
+#include "cpp_api/s_async.h"
+
+struct PackedValue;
/*****************************************************************************/
/* Scripting <-> Server Game Interface */
@@ -48,6 +51,20 @@ public:
// use ScriptApiBase::loadMod() to load mods
+ // Initialize async engine, call this AFTER loading all mods
+ void initAsync();
+
+ // Global step handler to collect async results
+ void stepAsync();
+
+ // Pass job to async threads
+ u32 queueAsync(std::string &&serialized_func,
+ PackedValue *param, const std::string &mod_origin);
+
private:
void InitializeModApi(lua_State *L, int top);
+
+ static void InitializeAsync(lua_State *L, int top);
+
+ AsyncEngine asyncEngine;
};