aboutsummaryrefslogtreecommitdiff
path: root/src/script
diff options
context:
space:
mode:
Diffstat (limited to 'src/script')
-rw-r--r--src/script/common/c_content.cpp70
-rw-r--r--src/script/common/c_content.h4
-rw-r--r--src/script/cpp_api/s_base.cpp20
-rw-r--r--src/script/cpp_api/s_base.h5
-rw-r--r--src/script/cpp_api/s_client.cpp87
-rw-r--r--src/script/cpp_api/s_client.h5
-rw-r--r--src/script/cpp_api/s_node.cpp1
-rw-r--r--src/script/cpp_api/s_security.cpp44
-rw-r--r--src/script/lua_api/l_client.cpp2
-rw-r--r--src/script/lua_api/l_clientobject.cpp117
-rw-r--r--src/script/lua_api/l_clientobject.h13
-rw-r--r--src/script/lua_api/l_localplayer.cpp6
-rw-r--r--src/script/lua_api/l_mainmenu.cpp117
-rw-r--r--src/script/lua_api/l_mainmenu.h2
-rw-r--r--src/script/lua_api/l_util.cpp24
-rw-r--r--src/script/lua_api/l_util.h3
16 files changed, 376 insertions, 144 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index eeaf69da1..897ca2862 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -200,7 +200,7 @@ void read_object_properties(lua_State *L, int index,
if (getintfield(L, -1, "hp_max", hp_max)) {
prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);
- if (prop->hp_max < sao->getHP()) {
+ if (sao && prop->hp_max < sao->getHP()) {
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
sao->setHP(prop->hp_max, reason);
if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
@@ -209,7 +209,7 @@ void read_object_properties(lua_State *L, int index,
}
if (getintfield(L, -1, "breath_max", prop->breath_max)) {
- if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
+ if (sao && sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
PlayerSAO *player = (PlayerSAO *)sao;
if (prop->breath_max < player->getBreath())
player->setBreath(prop->breath_max);
@@ -516,6 +516,35 @@ TileDef read_tiledef(lua_State *L, int index, u8 drawtype)
}
/******************************************************************************/
+void push_tiledef(lua_State *L, TileDef tiledef)
+{
+ lua_newtable(L);
+ setstringfield(L, -1, "name", tiledef.name);
+ setboolfield(L, -1, "backface_culling", tiledef.backface_culling);
+ setboolfield(L, -1, "tileable_horizontal", tiledef.tileable_horizontal);
+ setboolfield(L, -1, "tileable_vertical", tiledef.tileable_vertical);
+ std::string align_style;
+ switch (tiledef.align_style) {
+ case ALIGN_STYLE_USER_DEFINED:
+ align_style = "user";
+ break;
+ case ALIGN_STYLE_WORLD:
+ align_style = "world";
+ break;
+ default:
+ align_style = "node";
+ }
+ setstringfield(L, -1, "align_style", align_style);
+ setintfield(L, -1, "scale", tiledef.scale);
+ if (tiledef.has_color) {
+ push_ARGB8(L, tiledef.color);
+ lua_setfield(L, -2, "color");
+ }
+ push_animation_definition(L, tiledef.animation);
+ lua_setfield(L, -2, "animation");
+}
+
+/******************************************************************************/
void read_content_features(lua_State *L, ContentFeatures &f, int index)
{
if(index < 0)
@@ -688,7 +717,8 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index)
if (!f.palette_name.empty() &&
!(f.param_type_2 == CPT2_COLOR ||
f.param_type_2 == CPT2_COLORED_FACEDIR ||
- f.param_type_2 == CPT2_COLORED_WALLMOUNTED))
+ f.param_type_2 == CPT2_COLORED_WALLMOUNTED ||
+ f.param_type_2 == CPT2_COLORED_DEGROTATE))
warningstream << "Node " << f.name.c_str()
<< " has a palette, but not a suitable paramtype2." << std::endl;
@@ -835,9 +865,32 @@ void push_content_features(lua_State *L, const ContentFeatures &c)
std::string drawtype(ScriptApiNode::es_DrawType[(int)c.drawtype].str);
std::string liquid_type(ScriptApiNode::es_LiquidType[(int)c.liquid_type].str);
- /* Missing "tiles" because I don't see a usecase (at least not yet). */
+ lua_newtable(L);
+ // tiles
lua_newtable(L);
+ for (int i = 0; i < 6; i++) {
+ push_tiledef(L, c.tiledef[i]);
+ lua_rawseti(L, -2, i + 1);
+ }
+ lua_setfield(L, -2, "tiles");
+
+ // overlay_tiles
+ lua_newtable(L);
+ for (int i = 0; i < 6; i++) {
+ push_tiledef(L, c.tiledef_overlay[i]);
+ lua_rawseti(L, -2, i + 1);
+ }
+ lua_setfield(L, -2, "overlay_tiles");
+
+ // special_tiles
+ lua_newtable(L);
+ for (int i = 0; i < CF_SPECIAL_COUNT; i++) {
+ push_tiledef(L, c.tiledef_special[i]);
+ lua_rawseti(L, -2, i + 1);
+ }
+ lua_setfield(L, -2, "special_tiles");
+
lua_pushboolean(L, c.has_on_construct);
lua_setfield(L, -2, "has_on_construct");
lua_pushboolean(L, c.has_on_destruct);
@@ -1886,14 +1939,7 @@ void push_pointed_thing(lua_State *L, const PointedThing &pointed, bool csm,
} else if (pointed.type == POINTEDTHING_OBJECT) {
lua_pushstring(L, "object");
lua_setfield(L, -2, "type");
- if (csm) {
-#ifndef SERVER
- ClientObjectRef::create(L, pointed.object_id);
-#endif
- } else {
- push_objectRef(L, pointed.object_id);
- }
-
+ push_objectRef(L, pointed.object_id);
lua_setfield(L, -2, "ref");
} else {
lua_pushstring(L, "nothing");
diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h
index 10b77a116..dc8f19ef3 100644
--- a/src/script/common/c_content.h
+++ b/src/script/common/c_content.h
@@ -39,7 +39,9 @@ extern "C" {
#include "itemgroup.h"
#include "itemdef.h"
#include "c_types.h"
-#include "hud.h"
+// We do a explicit path include because by default c_content.h include src/client/hud.h
+// prior to the src/hud.h, which is not good on server only build
+#include "../../hud.h"
namespace Json { class Value; }
diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index 1d62d8b65..5711ccbfd 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "cpp_api/s_internal.h"
#include "cpp_api/s_security.h"
#include "lua_api/l_object.h"
+#include "lua_api/l_clientobject.h"
#include "common/c_converter.h"
#include "server/player_sao.h"
#include "filesys.h"
@@ -354,13 +355,18 @@ void ScriptApiBase::setOriginFromTableRaw(int index, const char *fxn)
* since we lose control over the ref and the contained pointer.
*/
-void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
+void ScriptApiBase::addObjectReference(ActiveObject *cobj)
{
SCRIPTAPI_PRECHECKHEADER
//infostream<<"scriptapi_add_object_reference: id="<<cobj->getId()<<std::endl;
// Create object on stack
- ObjectRef::create(L, cobj); // Puts ObjectRef (as userdata) on stack
+#ifndef SERVER
+ if (m_type == ScriptingType::Client)
+ ClientObjectRef::create(L, dynamic_cast<ClientActiveObject *>(cobj));
+ else
+#endif
+ ObjectRef::create(L, dynamic_cast<ServerActiveObject *>(cobj)); // Puts ObjectRef (as userdata) on stack
int object = lua_gettop(L);
// Get core.object_refs table
@@ -375,7 +381,7 @@ void ScriptApiBase::addObjectReference(ServerActiveObject *cobj)
lua_settable(L, objectstable);
}
-void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
+void ScriptApiBase::removeObjectReference(ActiveObject *cobj)
{
SCRIPTAPI_PRECHECKHEADER
//infostream<<"scriptapi_rm_object_reference: id="<<cobj->getId()<<std::endl;
@@ -390,7 +396,12 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
lua_pushnumber(L, cobj->getId()); // Push id
lua_gettable(L, objectstable);
// Set object reference to NULL
- ObjectRef::set_null(L);
+#ifndef SERVER
+ if (m_type == ScriptingType::Client)
+ ClientObjectRef::set_null(L);
+ else
+#endif
+ ObjectRef::set_null(L);
lua_pop(L, 1); // pop object
// Set object_refs[id] = nil
@@ -413,7 +424,6 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
<< ", this is probably a bug." << std::endl;
}
}
-
void ScriptApiBase::pushPlayerHPChangeReason(lua_State *L, const PlayerHPChangeReason &reason)
{
if (reason.hasLuaReference())
diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h
index 36331ad37..a7a2c7203 100644
--- a/src/script/cpp_api/s_base.h
+++ b/src/script/cpp_api/s_base.h
@@ -73,6 +73,7 @@ class Game;
class IGameDef;
class Environment;
class GUIEngine;
+class ActiveObject;
class ServerActiveObject;
struct PlayerHPChangeReason;
@@ -99,8 +100,8 @@ public:
RunCallbacksMode mode, const char *fxn);
/* object */
- void addObjectReference(ServerActiveObject *cobj);
- void removeObjectReference(ServerActiveObject *cobj);
+ void addObjectReference(ActiveObject *cobj);
+ void removeObjectReference(ActiveObject *cobj);
IGameDef *getGameDef() { return m_gamedef; }
Server* getServer();
diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp
index b90decfb5..b0e7a073e 100644
--- a/src/script/cpp_api/s_client.cpp
+++ b/src/script/cpp_api/s_client.cpp
@@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/client.h"
#include "common/c_converter.h"
#include "common/c_content.h"
+#include "lua_api/l_clientobject.h"
#include "s_item.h"
void ScriptApiClient::on_mods_loaded()
@@ -176,7 +177,7 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
const NodeDefManager *ndef = getClient()->ndef();
- // Get core.registered_on_punchgnode
+ // Get core.registered_on_punchnode
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_punchnode");
@@ -260,7 +261,7 @@ bool ScriptApiClient::on_spawn_particle(struct ParticleParameters param)
SCRIPTAPI_PRECHECKHEADER
// Get core.registered_on_play_sound
-
+
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_spawn_particle");
@@ -285,13 +286,58 @@ bool ScriptApiClient::on_spawn_particle(struct ParticleParameters param)
pushnode(L, param.node, getGameDef()->ndef());
lua_setfield(L, -2, "node");
}
- setintfield(L, -1, "node_tile", param.node_tile);
-
+ setintfield(L, -1, "node_tile", param.node_tile);
+
// Call functions
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
return readParam<bool>(L, -1);
}
+void ScriptApiClient::on_object_properties_change(s16 id)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ // Get core.registered_on_object_properties_change
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "registered_on_object_properties_change");
+
+ // Push data
+ push_objectRef(L, id);
+
+ // Call functions
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
+}
+
+void ScriptApiClient::on_object_hp_change(s16 id)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ // Get core.registered_on_object_hp_change
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "registered_on_object_hp_change");
+
+ // Push data
+ push_objectRef(L, id);
+
+ // Call functions
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
+}
+
+void ScriptApiClient::on_object_add(s16 id)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ // Get core.registered_on_object_add
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "registered_on_object_add");
+
+ // Push data
+ push_objectRef(L, id);
+
+ // Call functions
+ runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
+}
+
bool ScriptApiClient::on_inventory_open(Inventory *inventory)
{
SCRIPTAPI_PRECHECKHEADER
@@ -308,24 +354,45 @@ bool ScriptApiClient::on_inventory_open(Inventory *inventory)
void ScriptApiClient::open_enderchest()
{
SCRIPTAPI_PRECHECKHEADER
-
+
PUSH_ERROR_HANDLER(L);
int error_handler = lua_gettop(L) - 1;
lua_insert(L, error_handler);
-
+
lua_getglobal(L, "core");
lua_getfield(L, -1, "open_enderchest");
if (lua_isfunction(L, -1))
lua_pcall(L, 0, 0, error_handler);
}
+v3f ScriptApiClient::get_send_speed(v3f speed)
+{
+ SCRIPTAPI_PRECHECKHEADER
+
+ PUSH_ERROR_HANDLER(L);
+ int error_handler = lua_gettop(L) - 1;
+ lua_insert(L, error_handler);
+
+ lua_getglobal(L, "core");
+ lua_getfield(L, -1, "get_send_speed");
+ if (lua_isfunction(L, -1)) {
+ speed /= BS;
+ push_v3f(L, speed);
+ lua_pcall(L, 1, 1, error_handler);
+ speed = read_v3f(L, -1);
+ speed *= BS;
+ }
+
+ return speed;
+}
+
void ScriptApiClient::set_node_def(const ContentFeatures &f)
{
SCRIPTAPI_PRECHECKHEADER
-
+
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_nodes");
-
+
push_content_features(L, f);
lua_setfield(L, -2, f.name.c_str());
}
@@ -333,10 +400,10 @@ void ScriptApiClient::set_node_def(const ContentFeatures &f)
void ScriptApiClient::set_item_def(const ItemDefinition &i)
{
SCRIPTAPI_PRECHECKHEADER
-
+
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_items");
-
+
push_item_definition(L, i);
lua_setfield(L, -2, i.name.c_str());
}
diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h
index 9f68a14fc..2f9ce7aa3 100644
--- a/src/script/cpp_api/s_client.h
+++ b/src/script/cpp_api/s_client.h
@@ -63,10 +63,15 @@ public:
bool new_move);
bool on_play_sound(SimpleSoundSpec spec);
bool on_spawn_particle(struct ParticleParameters param);
+ void on_object_properties_change(s16 id);
+ void on_object_hp_change(s16 id);
+ void on_object_add(s16 id);
bool on_inventory_open(Inventory *inventory);
void open_enderchest();
+ v3f get_send_speed(v3f speed);
+
void set_node_def(const ContentFeatures &f);
void set_item_def(const ItemDefinition &i);
diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp
index f23fbfbde..029cb6308 100644
--- a/src/script/cpp_api/s_node.cpp
+++ b/src/script/cpp_api/s_node.cpp
@@ -65,6 +65,7 @@ struct EnumString ScriptApiNode::es_ContentParamType2[] =
{CPT2_COLORED_FACEDIR, "colorfacedir"},
{CPT2_COLORED_WALLMOUNTED, "colorwallmounted"},
{CPT2_GLASSLIKE_LIQUID_LEVEL, "glasslikeliquidlevel"},
+ {CPT2_COLORED_DEGROTATE, "colordegrotate"},
{0, NULL},
};
diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp
index 37c5b61dc..8fe5d2c5a 100644
--- a/src/script/cpp_api/s_security.cpp
+++ b/src/script/cpp_api/s_security.cpp
@@ -45,6 +45,21 @@ static inline void copy_safe(lua_State *L, const char *list[], unsigned len, int
}
}
+static void shallow_copy_table(lua_State *L, int from=-2, int to=-1)
+{
+ if (from < 0) from = lua_gettop(L) + from + 1;
+ if (to < 0) to = lua_gettop(L) + to + 1;
+ lua_pushnil(L);
+ while (lua_next(L, from) != 0) {
+ assert(lua_type(L, -1) != LUA_TTABLE);
+ // duplicate key and value for lua_rawset
+ lua_pushvalue(L, -2);
+ lua_pushvalue(L, -2);
+ lua_rawset(L, to);
+ lua_pop(L, 1);
+ }
+}
+
// Pushes the original version of a library function on the stack, from the old version
static inline void push_original(lua_State *L, const char *lib, const char *func)
{
@@ -83,7 +98,10 @@ void ScriptApiSecurity::initializeSecurity()
"unpack",
"_VERSION",
"xpcall",
- // Completely safe libraries
+ };
+ static const char *whitelist_tables[] = {
+ // These libraries are completely safe BUT we need to duplicate their table
+ // to ensure the sandbox can't affect the insecure env
"coroutine",
"string",
"table",
@@ -168,6 +186,17 @@ void ScriptApiSecurity::initializeSecurity()
lua_pop(L, 1);
+ // Copy safe libraries
+ for (const char *libname : whitelist_tables) {
+ lua_getfield(L, old_globals, libname);
+ lua_newtable(L);
+ shallow_copy_table(L);
+
+ lua_setglobal(L, libname);
+ lua_pop(L, 1);
+ }
+
+
// Copy safe IO functions
lua_getfield(L, old_globals, "io");
lua_newtable(L);
@@ -223,6 +252,19 @@ void ScriptApiSecurity::initializeSecurity()
#endif
lua_pop(L, 1); // Pop globals_backup
+
+
+ /*
+ * In addition to copying the tables in whitelist_tables, we also need to
+ * replace the string metatable. Otherwise old_globals.string would
+ * be accessible via getmetatable("").__index from inside the sandbox.
+ */
+ lua_pushliteral(L, "");
+ lua_newtable(L);
+ lua_getglobal(L, "string");
+ lua_setfield(L, -2, "__index");
+ lua_setmetatable(L, -2);
+ lua_pop(L, 1); // Pop empty string
}
void ScriptApiSecurity::initializeSecurityClient()
diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp
index 484af2ec3..916983982 100644
--- a/src/script/lua_api/l_client.cpp
+++ b/src/script/lua_api/l_client.cpp
@@ -520,7 +520,7 @@ int ModApiClient::l_get_objects_inside_radius(lua_State *L)
int i = 0;
lua_createtable(L, objs.size(), 0);
for (const auto obj : objs) {
- ClientObjectRef::create(L, obj.obj); // TODO: getObjectRefOrCreate
+ push_objectRef(L, obj.obj->getId());
lua_rawseti(L, -2, ++i);
}
return 1;
diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp
index 76d0d65ab..c093e754e 100644
--- a/src/script/lua_api/l_clientobject.cpp
+++ b/src/script/lua_api/l_clientobject.cpp
@@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "lua_api/l_clientobject.h"
#include "l_internal.h"
#include "common/c_converter.h"
+#include "common/c_content.h"
#include "client/client.h"
#include "object_properties.h"
#include "util/pointedthing.h"
@@ -47,6 +48,8 @@ ClientActiveObject *ClientObjectRef::get_cao(ClientObjectRef *ref)
GenericCAO *ClientObjectRef::get_generic_cao(ClientObjectRef *ref, lua_State *L)
{
ClientActiveObject *obj = get_cao(ref);
+ if (!obj)
+ return nullptr;
ClientEnvironment &env = getClient(L)->getEnv();
GenericCAO *gcao = env.getGenericCAO(obj->getId());
return gcao;
@@ -56,6 +59,8 @@ int ClientObjectRef::l_get_pos(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
ClientActiveObject *cao = get_cao(ref);
+ if (!cao)
+ return 0;
push_v3f(L, cao->getPosition() / BS);
return 1;
}
@@ -64,6 +69,8 @@ int ClientObjectRef::l_get_velocity(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
push_v3f(L, gcao->getVelocity() / BS);
return 1;
}
@@ -72,6 +79,8 @@ int ClientObjectRef::l_get_acceleration(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
push_v3f(L, gcao->getAcceleration() / BS);
return 1;
}
@@ -80,6 +89,8 @@ int ClientObjectRef::l_get_rotation(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
push_v3f(L, gcao->getRotation());
return 1;
}
@@ -88,6 +99,8 @@ int ClientObjectRef::l_is_player(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
lua_pushboolean(L, gcao->isPlayer());
return 1;
}
@@ -96,6 +109,8 @@ int ClientObjectRef::l_is_local_player(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
lua_pushboolean(L, gcao->isLocalPlayer());
return 1;
}
@@ -104,6 +119,8 @@ int ClientObjectRef::l_get_name(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
lua_pushstring(L, gcao->getName().c_str());
return 1;
}
@@ -112,14 +129,23 @@ int ClientObjectRef::l_get_attach(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
- create(L, gcao->getParent());
+ if (!gcao)
+ return 0;
+ ClientActiveObject *parent = gcao->getParent();
+ if (!parent)
+ return 0;
+ push_objectRef(L, parent->getId());
return 1;
}
int ClientObjectRef::l_get_nametag(lua_State *L)
{
+ log_deprecated(L, "Deprecated call to get_nametag, use get_properties().nametag "
+ "instead");
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
ObjectProperties *props = gcao->getProperties();
lua_pushstring(L, props->nametag.c_str());
return 1;
@@ -127,8 +153,12 @@ int ClientObjectRef::l_get_nametag(lua_State *L)
int ClientObjectRef::l_get_item_textures(lua_State *L)
{
+ log_deprecated(L, "Deprecated call to get_item_textures, use "
+ "get_properties().textures instead");
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
ObjectProperties *props = gcao->getProperties();
lua_newtable(L);
@@ -138,20 +168,49 @@ int ClientObjectRef::l_get_item_textures(lua_State *L)
return 1;
}
-int ClientObjectRef::l_get_hp(lua_State *L)
+int ClientObjectRef::l_get_max_hp(lua_State *L)
{
+ log_deprecated(L, "Deprecated call to get_max_hp, use get_properties().hp_max "
+ "instead");
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
- lua_pushnumber(L, gcao->getHp());
+ if (!gcao)
+ return 0;
+ ObjectProperties *props = gcao->getProperties();
+ lua_pushnumber(L, props->hp_max);
return 1;
}
-int ClientObjectRef::l_get_max_hp(lua_State *L)
+int ClientObjectRef::l_get_properties(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
- ObjectProperties *props = gcao->getProperties();
- lua_pushnumber(L, props->hp_max);
+ if (!gcao)
+ return 0;
+ ObjectProperties *prop = gcao->getProperties();
+ push_object_properties(L, prop);
+ return 1;
+}
+
+int ClientObjectRef::l_set_properties(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
+ ObjectProperties prop = *gcao->getProperties();
+ read_object_properties(L, 2, nullptr, &prop, getClient(L)->idef());
+ gcao->setProperties(prop);
+ return 1;
+}
+
+int ClientObjectRef::l_get_hp(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
+ lua_pushnumber(L, gcao->getHp());
return 1;
}
@@ -159,6 +218,8 @@ int ClientObjectRef::l_punch(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
PointedThing pointed(gcao->getId(), v3f(0, 0, 0), v3s16(0, 0, 0), 0);
getClient(L)->interact(INTERACT_START_DIGGING, pointed);
return 0;
@@ -168,6 +229,8 @@ int ClientObjectRef::l_rightclick(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
PointedThing pointed(gcao->getId(), v3f(0, 0, 0), v3s16(0, 0, 0), 0);
getClient(L)->interact(INTERACT_PLACE, pointed);
return 0;
@@ -177,23 +240,42 @@ int ClientObjectRef::l_remove(lua_State *L)
{
ClientObjectRef *ref = checkobject(L, 1);
ClientActiveObject *cao = get_cao(ref);
+ if (!cao)
+ return 0;
getClient(L)->getEnv().removeActiveObject(cao->getId());
return 0;
}
+int ClientObjectRef::l_set_nametag_images(lua_State *L)
+{
+ ClientObjectRef *ref = checkobject(L, 1);
+ GenericCAO *gcao = get_generic_cao(ref, L);
+ if (!gcao)
+ return 0;
+ gcao->nametag_images.clear();
+ if (lua_istable(L, 2)) {
+ lua_pushnil(L);
+ while (lua_next(L, 2) != 0) {
+ gcao->nametag_images.push_back(lua_tostring(L, -1));
+ lua_pop(L, 1);
+ }
+ }
+ gcao->updateNametag();
+
+ return 0;
+}
+
ClientObjectRef::ClientObjectRef(ClientActiveObject *object) : m_object(object)
{
}
void ClientObjectRef::create(lua_State *L, ClientActiveObject *object)
{
- if (object) {
- ClientObjectRef *o = new ClientObjectRef(object);
- *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
- luaL_getmetatable(L, className);
- lua_setmetatable(L, -2);
- }
+ ClientObjectRef *o = new ClientObjectRef(object);
+ *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
+ luaL_getmetatable(L, className);
+ lua_setmetatable(L, -2);
}
void ClientObjectRef::create(lua_State *L, s16 id)
@@ -201,6 +283,12 @@ void ClientObjectRef::create(lua_State *L, s16 id)
create(L, ((ClientEnvironment *)getEnv(L))->getActiveObject(id));
}
+void ClientObjectRef::set_null(lua_State *L)
+{
+ ClientObjectRef *obj = checkobject(L, -1);
+ obj->m_object = nullptr;
+}
+
int ClientObjectRef::gc_object(lua_State *L)
{
ClientObjectRef *obj = *(ClientObjectRef **)(lua_touserdata(L, 1));
@@ -245,6 +333,9 @@ luaL_Reg ClientObjectRef::methods[] = {luamethod(ClientObjectRef, get_pos),
luamethod(ClientObjectRef, get_attach),
luamethod(ClientObjectRef, get_nametag),
luamethod(ClientObjectRef, get_item_textures),
+ luamethod(ClientObjectRef, get_properties),
+ luamethod(ClientObjectRef, set_properties),
luamethod(ClientObjectRef, get_hp),
luamethod(ClientObjectRef, get_max_hp), luamethod(ClientObjectRef, punch),
- luamethod(ClientObjectRef, rightclick), {0, 0}};
+ luamethod(ClientObjectRef, rightclick),
+ luamethod(ClientObjectRef, set_nametag_images), {0, 0}};
diff --git a/src/script/lua_api/l_clientobject.h b/src/script/lua_api/l_clientobject.h
index ebc0f2a90..c4c95cb41 100644
--- a/src/script/lua_api/l_clientobject.h
+++ b/src/script/lua_api/l_clientobject.h
@@ -35,6 +35,8 @@ public:
static void create(lua_State *L, ClientActiveObject *object);
static void create(lua_State *L, s16 id);
+ static void set_null(lua_State *L);
+
static ClientObjectRef *checkobject(lua_State *L, int narg);
private:
@@ -75,9 +77,15 @@ private:
// get_nametag(self)
static int l_get_nametag(lua_State *L);
- // get_textures(self)
+ // get_item_textures(self)
static int l_get_item_textures(lua_State *L);
+ // get_properties(self)
+ static int l_get_properties(lua_State *L);
+
+ // set_properties(self, properties)
+ static int l_set_properties(lua_State *L);
+
// get_hp(self)
static int l_get_hp(lua_State *L);
@@ -92,4 +100,7 @@ private:
// remove(self)
static int l_remove(lua_State *L);
+
+ // set_nametag_images(self, images)
+ static int l_set_nametag_images(lua_State *L);
};
diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp
index 747657016..4f57ee00f 100644
--- a/src/script/lua_api/l_localplayer.cpp
+++ b/src/script/lua_api/l_localplayer.cpp
@@ -84,7 +84,7 @@ int LuaLocalPlayer::l_set_yaw(lua_State *L)
LocalPlayer *player = getobject(L, 1);
if (lua_isnumber(L, 2)) {
- int yaw = lua_tonumber(L, 2);
+ double yaw = lua_tonumber(L, 2);
player->setYaw(yaw);
g_game->cam_view.camera_yaw = yaw;
g_game->cam_view_target.camera_yaw = yaw;
@@ -104,7 +104,7 @@ int LuaLocalPlayer::l_set_pitch(lua_State *L)
LocalPlayer *player = getobject(L, 1);
if (lua_isnumber(L, 2)) {
- int pitch = lua_tonumber(L, 2);
+ double pitch = lua_tonumber(L, 2);
player->setPitch(pitch);
g_game->cam_view.camera_pitch = pitch;
g_game->cam_view_target.camera_pitch = pitch;
@@ -483,7 +483,7 @@ int LuaLocalPlayer::l_get_object(lua_State *L)
ClientEnvironment &env = getClient(L)->getEnv();
ClientActiveObject *obj = env.getGenericCAO(player->getCAO()->getId());
- ClientObjectRef::create(L, obj);
+ push_objectRef(L, obj->getId());
return 1;
}
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index 4d437b967..d88bf31c1 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -34,9 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serverlist.h"
#include "mapgen/mapgen.h"
#include "settings.h"
-
-#include <IFileArchive.h>
-#include <IFileSystem.h>
+#include "client/client.h"
#include "client/renderingengine.h"
#include "network/networkprotocol.h"
@@ -399,7 +397,8 @@ int ModApiMainMenu::l_show_keys_menu(lua_State *L)
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
- GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
+ GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(
+ engine->m_rendering_engine->get_gui_env(),
engine->m_parent,
-1,
engine->m_menumanager,
@@ -629,75 +628,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);
-
- io::IFileSystem *fs = RenderingEngine::get_filesystem();
-
- if (!fs->addFileArchive(zipfile, false, false, io::EFAT_ZIP)) {
- lua_pushboolean(L,false);
- return 1;
- }
-
- sanity_check(fs->getFileArchiveCount() > 0);
-
- /**********************************************************************/
- /* WARNING this is not threadsafe!! */
- /**********************************************************************/
- io::IFileArchive* opened_zip =
- fs->getFileArchive(fs->getFileArchiveCount()-1);
-
- const io::IFileList* files_in_zip = opened_zip->getFileList();
-
- unsigned int number_of_files = files_in_zip->getFileCount();
-
- for (unsigned int i=0; i < number_of_files; i++) {
- std::string fullpath = destination;
- fullpath += DIR_DELIM;
- fullpath += files_in_zip->getFullFileName(i).c_str();
- std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
-
- if (!files_in_zip->isDirectory(i)) {
- if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,false);
- return 1;
- }
-
- io::IReadFile* toread = opened_zip->createAndOpenFile(i);
-
- FILE *targetfile = fopen(fullpath.c_str(),"wb");
-
- if (targetfile == NULL) {
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,false);
- return 1;
- }
-
- char read_buffer[1024];
- long total_read = 0;
-
- while (total_read < toread->getSize()) {
-
- unsigned int bytes_read =
- toread->read(read_buffer,sizeof(read_buffer));
- if ((bytes_read == 0 ) ||
- (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
- {
- fclose(targetfile);
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,false);
- return 1;
- }
- total_read += bytes_read;
- }
-
- fclose(targetfile);
- }
-
- }
-
- fs->removeFileArchive(fs->getFileArchiveCount()-1);
- lua_pushboolean(L,true);
+ lua_pushboolean(L, fs::extractZipFile(rendering_engine->get_filesystem(), zipfile, destination));
return 1;
}
@@ -716,24 +649,28 @@ int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
}
/******************************************************************************/
-bool ModApiMainMenu::mayModifyPath(const std::string &path)
+bool ModApiMainMenu::mayModifyPath(std::string path)
{
+ path = fs::RemoveRelativePathComponents(path);
+
if (fs::PathStartsWith(path, fs::TempPath()))
return true;
- if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "games")))
- return true;
+ std::string path_user = fs::RemoveRelativePathComponents(porting::path_user);
- if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "mods")))
+ if (fs::PathStartsWith(path, path_user + DIR_DELIM "client"))
return true;
-
- if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "clientmods")))
+ if (fs::PathStartsWith(path, path_user + DIR_DELIM "clientmods"))
return true;
-
- if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "textures")))
+ if (fs::PathStartsWith(path, path_user + DIR_DELIM "textures"))
return true;
-
- if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM "worlds")))
+ if (fs::PathStartsWith(path, path_user + DIR_DELIM "games"))
+ return true;
+ if (fs::PathStartsWith(path, path_user + DIR_DELIM "mods"))
+ return true;
+ if (fs::PathStartsWith(path, path_user + DIR_DELIM "textures"))
+ return true;
+ if (fs::PathStartsWith(path, path_user + DIR_DELIM "worlds"))
return true;
if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_cache)))
@@ -763,7 +700,7 @@ int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
bool is_file_select = readParam<bool>(L, 3);
GUIFileSelectMenu* fileOpenMenu =
- new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
+ new GUIFileSelectMenu(engine->m_rendering_engine->get_gui_env(),
engine->m_parent,
-1,
engine->m_menumanager,
@@ -859,15 +796,7 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
lua_pushnumber(L,RenderingEngine::getDisplayDensity());
lua_settable(L, top);
- lua_pushstring(L,"display_width");
- lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
- lua_settable(L, top);
-
- lua_pushstring(L,"display_height");
- lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
- lua_settable(L, top);
-
- const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
+ const v2u32 &window_size = RenderingEngine::getWindowSize();
lua_pushstring(L,"window_width");
lua_pushnumber(L, window_size.X);
lua_settable(L, top);
@@ -875,6 +804,10 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
lua_pushstring(L,"window_height");
lua_pushnumber(L, window_size.Y);
lua_settable(L, top);
+
+ lua_pushstring(L, "render_info");
+ lua_pushstring(L, wide_to_utf8(RenderingEngine::get_video_driver()->getName()).c_str());
+ lua_settable(L, top);
return 1;
}
diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h
index 49ce7c251..33ac9e721 100644
--- a/src/script/lua_api/l_mainmenu.h
+++ b/src/script/lua_api/l_mainmenu.h
@@ -58,7 +58,7 @@ private:
* @param path path to check
* @return true if the path may be modified
*/
- static bool mayModifyPath(const std::string &path);
+ static bool mayModifyPath(std::string path);
//api calls
diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp
index 60ae7871c..624828956 100644
--- a/src/script/lua_api/l_util.cpp
+++ b/src/script/lua_api/l_util.cpp
@@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
+#include "irrlichttypes_extrabloated.h"
#include "lua_api/l_util.h"
#include "lua_api/l_internal.h"
#include "lua_api/l_settings.h"
@@ -40,7 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/hex.h"
#include "util/sha1.h"
#include <algorithm>
-
+#include <cstdio>
// log([level,] text)
// Writes a line to the logger.
@@ -479,6 +480,23 @@ int ModApiUtil::l_sha1(lua_State *L)
return 1;
}
+// colorspec_to_colorstring(colorspec)
+int ModApiUtil::l_colorspec_to_colorstring(lua_State *L)
+{
+ NO_MAP_LOCK_REQUIRED;
+
+ video::SColor color(0);
+ if (read_color(L, 1, &color)) {
+ char colorstring[10];
+ snprintf(colorstring, 10, "#%02X%02X%02X%02X",
+ color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
+ lua_pushstring(L, colorstring);
+ return 1;
+ }
+
+ return 0;
+}
+
void ModApiUtil::Initialize(lua_State *L, int top)
{
API_FCT(log);
@@ -513,6 +531,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(get_version);
API_FCT(sha1);
+ API_FCT(colorspec_to_colorstring);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
@@ -539,6 +558,7 @@ void ModApiUtil::InitializeClient(lua_State *L, int top)
API_FCT(get_version);
API_FCT(sha1);
+ API_FCT(colorspec_to_colorstring);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
@@ -569,8 +589,8 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
API_FCT(get_version);
API_FCT(sha1);
+ API_FCT(colorspec_to_colorstring);
LuaSettings::create(L, g_settings, g_settings_path);
lua_setfield(L, top, "settings");
}
-
diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h
index dbdd62b99..6943a6afb 100644
--- a/src/script/lua_api/l_util.h
+++ b/src/script/lua_api/l_util.h
@@ -101,6 +101,9 @@ private:
// sha1(string, raw)
static int l_sha1(lua_State *L);
+ // colorspec_to_colorstring(colorspec)
+ static int l_colorspec_to_colorstring(lua_State *L);
+
public:
static void Initialize(lua_State *L, int top);
static void InitializeAsync(lua_State *L, int top);