diff options
author | Minetest-j45 <55553015+Minetest-j45@users.noreply.github.com> | 2021-08-30 15:22:40 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-30 15:22:40 +0100 |
commit | 7824a4956bf489b4e2cc35e0c97272eee06be6ba (patch) | |
tree | 70243765dc1743a83596f9c6eec122fb417ef92c /src/script/lua_api | |
parent | 607add326feb44e078b843464ce4a8de09f28743 (diff) | |
parent | 35445d24f425c6291a0580b468919ca83de716fd (diff) | |
download | dragonfireclient-7824a4956bf489b4e2cc35e0c97272eee06be6ba.tar.xz |
Merge pull request #1 from EliasFleckenstein03/master
update
Diffstat (limited to 'src/script/lua_api')
-rw-r--r-- | src/script/lua_api/l_client.cpp | 48 | ||||
-rw-r--r-- | src/script/lua_api/l_client.h | 6 | ||||
-rw-r--r-- | src/script/lua_api/l_clientobject.cpp | 126 | ||||
-rw-r--r-- | src/script/lua_api/l_clientobject.h | 16 | ||||
-rw-r--r-- | src/script/lua_api/l_http.cpp | 14 | ||||
-rw-r--r-- | src/script/lua_api/l_localplayer.cpp | 33 | ||||
-rw-r--r-- | src/script/lua_api/l_localplayer.h | 3 | ||||
-rw-r--r-- | src/script/lua_api/l_mainmenu.cpp | 334 | ||||
-rw-r--r-- | src/script/lua_api/l_mainmenu.h | 8 | ||||
-rw-r--r-- | src/script/lua_api/l_mapgen.cpp | 134 | ||||
-rw-r--r-- | src/script/lua_api/l_object.cpp | 75 | ||||
-rw-r--r-- | src/script/lua_api/l_server.cpp | 124 | ||||
-rw-r--r-- | src/script/lua_api/l_server.h | 2 | ||||
-rw-r--r-- | src/script/lua_api/l_settings.cpp | 80 | ||||
-rw-r--r-- | src/script/lua_api/l_util.cpp | 37 | ||||
-rw-r--r-- | src/script/lua_api/l_util.h | 6 |
16 files changed, 503 insertions, 543 deletions
diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index dac2febae..916983982 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -440,7 +440,7 @@ int ModApiClient::l_place_node(lua_State *L) pointed.node_abovesurface = pos; pointed.node_undersurface = pos; NodeMetadata *meta = map.getNodeMetadata(pos); - g_game->nodePlacement(selected_def, selected_item, pos, pos, pointed, meta); + g_game->nodePlacement(selected_def, selected_item, pos, pos, pointed, meta, true); return 0; } @@ -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; @@ -621,6 +621,48 @@ int ModApiClient::l_interact(lua_State *L) return 1; } +StringMap *table_to_stringmap(lua_State *L, int index) +{ + StringMap *m = new StringMap; + + lua_pushvalue(L, index); + lua_pushnil(L); + + while (lua_next(L, -2)) { + lua_pushvalue(L, -2); + std::basic_string<char> key = lua_tostring(L, -1); + std::basic_string<char> value = lua_tostring(L, -2); + (*m)[key] = value; + lua_pop(L, 2); + } + + lua_pop(L, 1); + + return m; +} + +// send_inventory_fields(formname, fields) +// Only works if the inventory form was opened beforehand. +int ModApiClient::l_send_inventory_fields(lua_State *L) +{ + std::string formname = luaL_checkstring(L, 1); + StringMap *fields = table_to_stringmap(L, 2); + + getClient(L)->sendInventoryFields(formname, *fields); + return 0; +} + +// send_nodemeta_fields(position, formname, fields) +int ModApiClient::l_send_nodemeta_fields(lua_State *L) +{ + v3s16 pos = check_v3s16(L, 1); + std::string formname = luaL_checkstring(L, 2); + StringMap *m = table_to_stringmap(L, 3); + + getClient(L)->sendNodemetaFields(pos, formname, *m); + return 0; +} + void ModApiClient::Initialize(lua_State *L, int top) { API_FCT(get_current_modname); @@ -657,4 +699,6 @@ void ModApiClient::Initialize(lua_State *L, int top) API_FCT(get_objects_inside_radius); API_FCT(make_screenshot); API_FCT(interact); + API_FCT(send_inventory_fields); + API_FCT(send_nodemeta_fields); } diff --git a/src/script/lua_api/l_client.h b/src/script/lua_api/l_client.h index 03a42e022..caf21f78e 100644 --- a/src/script/lua_api/l_client.h +++ b/src/script/lua_api/l_client.h @@ -132,6 +132,12 @@ private: // interact(action, pointed_thing) static int l_interact(lua_State *L); + // send_inventory_fields(formname, fields) + static int l_send_inventory_fields(lua_State *L); + + // send_nodemeta_fields(position, formname, fields) + static int l_send_nodemeta_fields(lua_State *L); + public: static void Initialize(lua_State *L, int top); }; diff --git a/src/script/lua_api/l_clientobject.cpp b/src/script/lua_api/l_clientobject.cpp index 521aba023..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,23 +229,53 @@ 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; } +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) @@ -192,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)); @@ -236,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 1ff22407f..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); @@ -89,4 +97,10 @@ private: // rightclick(self) static int l_rightclick(lua_State *L); + + // 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_http.cpp b/src/script/lua_api/l_http.cpp index 0bf9cfbad..5ea3b3f99 100644 --- a/src/script/lua_api/l_http.cpp +++ b/src/script/lua_api/l_http.cpp @@ -239,8 +239,18 @@ int ModApiHttp::l_get_http_api(lua_State *L) void ModApiHttp::Initialize(lua_State *L, int top) { #if USE_CURL - API_FCT(get_http_api); - API_FCT(request_http_api); + + bool isMainmenu = false; +#ifndef SERVER + isMainmenu = ModApiBase::getGuiEngine(L) != nullptr; +#endif + + if (isMainmenu) { + API_FCT(get_http_api); + } else { + API_FCT(request_http_api); + } + #endif } diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 3f4147227..4f57ee00f 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -66,10 +66,10 @@ int LuaLocalPlayer::l_get_velocity(lua_State *L) int LuaLocalPlayer::l_set_velocity(lua_State *L) { LocalPlayer *player = getobject(L, 1); - + v3f pos = checkFloatPos(L, 2); player->setSpeed(pos); - + return 0; } @@ -84,12 +84,12 @@ 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; } - + return 0; } @@ -104,12 +104,12 @@ 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; } - + return 0; } @@ -144,7 +144,7 @@ int LuaLocalPlayer::l_set_wield_index(lua_State *L) { LocalPlayer *player = getobject(L, 1); u32 index = luaL_checkinteger(L, 2) - 1; - + player->setWieldIndex(index); g_game->processItemSelection(&g_game->runData.new_playeritem); ItemStack selected_item, hand_item; @@ -226,7 +226,7 @@ int LuaLocalPlayer::l_get_physics_override(lua_State *L) LocalPlayer *player = getobject(L, 1); push_physics_override(L, player->physics_override_speed, player->physics_override_jump, player->physics_override_gravity, player->physics_override_sneak, player->physics_override_sneak_glitch, player->physics_override_new_move); - + return 1; } @@ -234,7 +234,7 @@ int LuaLocalPlayer::l_get_physics_override(lua_State *L) int LuaLocalPlayer::l_set_physics_override(lua_State *L) { LocalPlayer *player = getobject(L, 1); - + player->physics_override_speed = getfloatfield_default( L, 2, "speed", player->physics_override_speed); player->physics_override_jump = getfloatfield_default( @@ -331,7 +331,7 @@ int LuaLocalPlayer::l_get_pos(lua_State *L) int LuaLocalPlayer::l_set_pos(lua_State *L) { LocalPlayer *player = getobject(L, 1); - + v3f pos = checkFloatPos(L, 2); player->setPosition(pos); getClient(L)->sendPlayerPos(); @@ -476,13 +476,23 @@ int LuaLocalPlayer::l_hud_get(lua_State *L) return 1; } +// get_object(self) int LuaLocalPlayer::l_get_object(lua_State *L) { LocalPlayer *player = getobject(L, 1); ClientEnvironment &env = getClient(L)->getEnv(); ClientActiveObject *obj = env.getGenericCAO(player->getCAO()->getId()); - ClientObjectRef::create(L, obj); + push_objectRef(L, obj->getId()); + + return 1; +} + +// get_hotbar_size(self) +int LuaLocalPlayer::l_get_hotbar_size(lua_State *L) +{ + LocalPlayer *player = getobject(L, 1); + lua_pushnumber(L, player->hud_hotbar_itemcount); return 1; } @@ -585,6 +595,7 @@ const luaL_Reg LuaLocalPlayer::methods[] = { luamethod(LuaLocalPlayer, hud_change), luamethod(LuaLocalPlayer, hud_get), luamethod(LuaLocalPlayer, get_object), + luamethod(LuaLocalPlayer, get_hotbar_size), {0, 0} }; diff --git a/src/script/lua_api/l_localplayer.h b/src/script/lua_api/l_localplayer.h index 33e23d178..bb5a294ca 100644 --- a/src/script/lua_api/l_localplayer.h +++ b/src/script/lua_api/l_localplayer.h @@ -65,6 +65,9 @@ private: // get_wielded_item(self) static int l_get_wielded_item(lua_State *L); + // get_hotbar_size(self) + static int l_get_hotbar_size(lua_State *L); + static int l_is_attached(lua_State *L); static int l_is_touching_ground(lua_State *L); static int l_is_in_liquid(lua_State *L); diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 3ea5eb4ba..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" @@ -275,207 +273,6 @@ int ModApiMainMenu::l_get_worlds(lua_State *L) } /******************************************************************************/ -int ModApiMainMenu::l_get_favorites(lua_State *L) -{ - std::string listtype = "local"; - - if (!lua_isnone(L, 1)) { - listtype = luaL_checkstring(L, 1); - } - - std::vector<ServerListSpec> servers; - - if(listtype == "online") { - servers = ServerList::getOnline(); - } else { - servers = ServerList::getLocal(); - } - - lua_newtable(L); - int top = lua_gettop(L); - unsigned int index = 1; - - for (const Json::Value &server : servers) { - - lua_pushnumber(L, index); - - lua_newtable(L); - int top_lvl2 = lua_gettop(L); - - if (!server["clients"].asString().empty()) { - std::string clients_raw = server["clients"].asString(); - char* endptr = 0; - int numbervalue = strtol(clients_raw.c_str(), &endptr,10); - - if ((!clients_raw.empty()) && (*endptr == 0)) { - lua_pushstring(L, "clients"); - lua_pushnumber(L, numbervalue); - lua_settable(L, top_lvl2); - } - } - - if (!server["clients_max"].asString().empty()) { - - std::string clients_max_raw = server["clients_max"].asString(); - char* endptr = 0; - int numbervalue = strtol(clients_max_raw.c_str(), &endptr,10); - - if ((!clients_max_raw.empty()) && (*endptr == 0)) { - lua_pushstring(L, "clients_max"); - lua_pushnumber(L, numbervalue); - lua_settable(L, top_lvl2); - } - } - - if (!server["version"].asString().empty()) { - lua_pushstring(L, "version"); - std::string topush = server["version"].asString(); - lua_pushstring(L, topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (!server["proto_min"].asString().empty()) { - lua_pushstring(L, "proto_min"); - lua_pushinteger(L, server["proto_min"].asInt()); - lua_settable(L, top_lvl2); - } - - if (!server["proto_max"].asString().empty()) { - lua_pushstring(L, "proto_max"); - lua_pushinteger(L, server["proto_max"].asInt()); - lua_settable(L, top_lvl2); - } - - if (!server["password"].asString().empty()) { - lua_pushstring(L, "password"); - lua_pushboolean(L, server["password"].asBool()); - lua_settable(L, top_lvl2); - } - - if (!server["creative"].asString().empty()) { - lua_pushstring(L, "creative"); - lua_pushboolean(L, server["creative"].asBool()); - lua_settable(L, top_lvl2); - } - - if (!server["damage"].asString().empty()) { - lua_pushstring(L, "damage"); - lua_pushboolean(L, server["damage"].asBool()); - lua_settable(L, top_lvl2); - } - - if (!server["pvp"].asString().empty()) { - lua_pushstring(L, "pvp"); - lua_pushboolean(L, server["pvp"].asBool()); - lua_settable(L, top_lvl2); - } - - if (!server["description"].asString().empty()) { - lua_pushstring(L, "description"); - std::string topush = server["description"].asString(); - lua_pushstring(L, topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (!server["name"].asString().empty()) { - lua_pushstring(L, "name"); - std::string topush = server["name"].asString(); - lua_pushstring(L, topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (!server["address"].asString().empty()) { - lua_pushstring(L, "address"); - std::string topush = server["address"].asString(); - lua_pushstring(L, topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (!server["port"].asString().empty()) { - lua_pushstring(L, "port"); - std::string topush = server["port"].asString(); - lua_pushstring(L, topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (server.isMember("ping")) { - float ping = server["ping"].asFloat(); - lua_pushstring(L, "ping"); - lua_pushnumber(L, ping); - lua_settable(L, top_lvl2); - } - - if (server["clients_list"].isArray()) { - unsigned int index_lvl2 = 1; - lua_pushstring(L, "clients_list"); - lua_newtable(L); - int top_lvl3 = lua_gettop(L); - for (const Json::Value &client : server["clients_list"]) { - lua_pushnumber(L, index_lvl2); - std::string topush = client.asString(); - lua_pushstring(L, topush.c_str()); - lua_settable(L, top_lvl3); - index_lvl2++; - } - lua_settable(L, top_lvl2); - } - - if (server["mods"].isArray()) { - unsigned int index_lvl2 = 1; - lua_pushstring(L, "mods"); - lua_newtable(L); - int top_lvl3 = lua_gettop(L); - for (const Json::Value &mod : server["mods"]) { - - lua_pushnumber(L, index_lvl2); - std::string topush = mod.asString(); - lua_pushstring(L, topush.c_str()); - lua_settable(L, top_lvl3); - index_lvl2++; - } - lua_settable(L, top_lvl2); - } - - lua_settable(L, top); - index++; - } - return 1; -} - -/******************************************************************************/ -int ModApiMainMenu::l_delete_favorite(lua_State *L) -{ - std::vector<ServerListSpec> servers; - - std::string listtype = "local"; - - if (!lua_isnone(L,2)) { - listtype = luaL_checkstring(L,2); - } - - if ((listtype != "local") && - (listtype != "online")) - return 0; - - - if(listtype == "online") { - servers = ServerList::getOnline(); - } else { - servers = ServerList::getLocal(); - } - - int fav_idx = luaL_checkinteger(L,1) -1; - - if ((fav_idx >= 0) && - (fav_idx < (int) servers.size())) { - - ServerList::deleteEntry(servers[fav_idx]); - } - - return 0; -} - -/******************************************************************************/ int ModApiMainMenu::l_get_games(lua_State *L) { std::vector<SubgameSpec> games = getAvailableGames(); @@ -600,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, @@ -730,6 +528,7 @@ int ModApiMainMenu::l_get_texturepath(lua_State *L) return 1; } +/******************************************************************************/ int ModApiMainMenu::l_get_texturepath_share(lua_State *L) { std::string gamepath = fs::RemoveRelativePathComponents( @@ -738,6 +537,7 @@ int ModApiMainMenu::l_get_texturepath_share(lua_State *L) return 1; } +/******************************************************************************/ int ModApiMainMenu::l_get_cache_path(lua_State *L) { lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str()); @@ -745,6 +545,13 @@ 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()); + return 1; +} + +/******************************************************************************/ int ModApiMainMenu::l_create_dir(lua_State *L) { const char *path = luaL_checkstring(L, 1); @@ -821,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; } @@ -908,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))) @@ -955,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, @@ -1051,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); @@ -1067,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; } @@ -1133,11 +874,9 @@ void ModApiMainMenu::Initialize(lua_State *L, int top) API_FCT(get_content_info); API_FCT(start); API_FCT(close); - API_FCT(get_favorites); API_FCT(show_keys_menu); API_FCT(create_world); API_FCT(delete_world); - API_FCT(delete_favorite); API_FCT(set_background); API_FCT(set_topleft_text); API_FCT(get_mapgen_names); @@ -1148,6 +887,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top) API_FCT(get_texturepath); API_FCT(get_texturepath_share); API_FCT(get_cache_path); + API_FCT(get_temp_path); API_FCT(create_dir); API_FCT(delete_dir); API_FCT(copy_dir); @@ -1173,7 +913,6 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top) { API_FCT(get_worlds); API_FCT(get_games); - API_FCT(get_favorites); API_FCT(get_mapgen_names); API_FCT(get_user_path); API_FCT(get_modpath); @@ -1182,6 +921,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top) API_FCT(get_texturepath); API_FCT(get_texturepath_share); API_FCT(get_cache_path); + API_FCT(get_temp_path); API_FCT(create_dir); API_FCT(delete_dir); API_FCT(copy_dir); @@ -1189,5 +929,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top) //API_FCT(extract_zip); //TODO remove dependency to GuiEngine 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) } diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h index 0b02ed892..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 @@ -74,10 +74,6 @@ private: static int l_get_mapgen_names(lua_State *L); - static int l_get_favorites(lua_State *L); - - static int l_delete_favorite(lua_State *L); - static int l_gettext(lua_State *L); //packages @@ -126,6 +122,8 @@ private: static int l_get_cache_path(lua_State *L); + static int l_get_temp_path(lua_State *L); + static int l_create_dir(lua_State *L); static int l_delete_dir(lua_State *L); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 834938e56..eb3d49a5e 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -482,9 +482,7 @@ int ModApiMapgen::l_get_biome_id(lua_State *L) { NO_MAP_LOCK_REQUIRED; - const char *biome_str = lua_tostring(L, 1); - if (!biome_str) - return 0; + const char *biome_str = luaL_checkstring(L, 1); const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager(); if (!bmgr) @@ -527,30 +525,12 @@ int ModApiMapgen::l_get_heat(lua_State *L) v3s16 pos = read_v3s16(L, 1); - NoiseParams np_heat; - NoiseParams np_heat_blend; - - MapSettingsManager *settingsmgr = - getServer(L)->getEmergeManager()->map_settings_mgr; - - if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat", - &np_heat) || - !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend", - &np_heat_blend)) - return 0; - - std::string value; - if (!settingsmgr->getMapSetting("seed", &value)) - return 0; - std::istringstream ss(value); - u64 seed; - ss >> seed; + const BiomeGen *biomegen = getServer(L)->getEmergeManager()->getBiomeGen(); - const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager(); - if (!bmgr) + if (!biomegen || biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; - float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed); + float heat = ((BiomeGenOriginal*) biomegen)->calcHeatAtPoint(pos); lua_pushnumber(L, heat); @@ -566,31 +546,12 @@ int ModApiMapgen::l_get_humidity(lua_State *L) v3s16 pos = read_v3s16(L, 1); - NoiseParams np_humidity; - NoiseParams np_humidity_blend; + const BiomeGen *biomegen = getServer(L)->getEmergeManager()->getBiomeGen(); - MapSettingsManager *settingsmgr = - getServer(L)->getEmergeManager()->map_settings_mgr; - - if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity", - &np_humidity) || - !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend", - &np_humidity_blend)) - return 0; - - std::string value; - if (!settingsmgr->getMapSetting("seed", &value)) - return 0; - std::istringstream ss(value); - u64 seed; - ss >> seed; - - const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager(); - if (!bmgr) + if (!biomegen || biomegen->getType() != BIOMEGEN_ORIGINAL) return 0; - float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity, - np_humidity_blend, seed); + float humidity = ((BiomeGenOriginal*) biomegen)->calcHumidityAtPoint(pos); lua_pushnumber(L, humidity); @@ -606,45 +567,11 @@ int ModApiMapgen::l_get_biome_data(lua_State *L) v3s16 pos = read_v3s16(L, 1); - NoiseParams np_heat; - NoiseParams np_heat_blend; - NoiseParams np_humidity; - NoiseParams np_humidity_blend; - - MapSettingsManager *settingsmgr = - getServer(L)->getEmergeManager()->map_settings_mgr; - - if (!settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat", - &np_heat) || - !settingsmgr->getMapSettingNoiseParams("mg_biome_np_heat_blend", - &np_heat_blend) || - !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity", - &np_humidity) || - !settingsmgr->getMapSettingNoiseParams("mg_biome_np_humidity_blend", - &np_humidity_blend)) - return 0; - - std::string value; - if (!settingsmgr->getMapSetting("seed", &value)) - return 0; - std::istringstream ss(value); - u64 seed; - ss >> seed; - - const BiomeManager *bmgr = getServer(L)->getEmergeManager()->getBiomeManager(); - if (!bmgr) + const BiomeGen *biomegen = getServer(L)->getEmergeManager()->getBiomeGen(); + if (!biomegen) return 0; - float heat = bmgr->getHeatAtPosOriginal(pos, np_heat, np_heat_blend, seed); - if (!heat) - return 0; - - float humidity = bmgr->getHumidityAtPosOriginal(pos, np_humidity, - np_humidity_blend, seed); - if (!humidity) - return 0; - - const Biome *biome = bmgr->getBiomeFromNoiseOriginal(heat, humidity, pos); + const Biome *biome = biomegen->calcBiomeAtPoint(pos); if (!biome || biome->index == OBJDEF_INVALID_INDEX) return 0; @@ -653,11 +580,16 @@ int ModApiMapgen::l_get_biome_data(lua_State *L) lua_pushinteger(L, biome->index); lua_setfield(L, -2, "biome"); - lua_pushnumber(L, heat); - lua_setfield(L, -2, "heat"); + if (biomegen->getType() == BIOMEGEN_ORIGINAL) { + float heat = ((BiomeGenOriginal*) biomegen)->calcHeatAtPoint(pos); + float humidity = ((BiomeGenOriginal*) biomegen)->calcHumidityAtPoint(pos); - lua_pushnumber(L, humidity); - lua_setfield(L, -2, "humidity"); + lua_pushnumber(L, heat); + lua_setfield(L, -2, "heat"); + + lua_pushnumber(L, humidity); + lua_setfield(L, -2, "humidity"); + } return 1; } @@ -873,9 +805,6 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L) if (lua_isnumber(L, -1)) settingsmgr->setMapSetting("chunksize", readParam<std::string>(L, -1), true); - warn_if_field_exists(L, 1, "flagmask", - "Obsolete: flags field now includes unset flags."); - lua_getfield(L, 1, "flags"); if (lua_isstring(L, -1)) settingsmgr->setMapSetting("mg_flags", readParam<std::string>(L, -1), true); @@ -985,7 +914,7 @@ int ModApiMapgen::l_set_noiseparams(lua_State *L) bool set_default = !lua_isboolean(L, 3) || readParam<bool>(L, 3); - g_settings->setNoiseParams(name, np, set_default); + Settings::getLayer(set_default ? SL_DEFAULTS : SL_GLOBAL)->setNoiseParams(name, np); return 0; } @@ -1338,11 +1267,9 @@ int ModApiMapgen::l_register_ore(lua_State *L) lua_getfield(L, index, "noise_params"); if (read_noiseparams(L, -1, &ore->np)) { ore->flags |= OREFLAG_USE_NOISE; - } else if (ore->NEEDS_NOISE) { - errorstream << "register_ore: specified ore type requires valid " - "'noise_params' parameter" << std::endl; - delete ore; - return 0; + } else if (ore->needs_noise) { + log_deprecated(L, + "register_ore: ore type requires 'noise_params' but it is not specified, falling back to defaults"); } lua_pop(L, 1); @@ -1498,9 +1425,12 @@ int ModApiMapgen::l_generate_ores(lua_State *L) NO_MAP_LOCK_REQUIRED; EmergeManager *emerge = getServer(L)->getEmergeManager(); + if (!emerge || !emerge->mgparams) + return 0; Mapgen mg; - mg.seed = emerge->mgparams->seed; + // Intentionally truncates to s32, see Mapgen::Mapgen() + mg.seed = (s32)emerge->mgparams->seed; mg.vm = LuaVoxelManip::checkobject(L, 1)->vm; mg.ndef = getServer(L)->getNodeDefManager(); @@ -1524,9 +1454,12 @@ int ModApiMapgen::l_generate_decorations(lua_State *L) NO_MAP_LOCK_REQUIRED; EmergeManager *emerge = getServer(L)->getEmergeManager(); + if (!emerge || !emerge->mgparams) + return 0; Mapgen mg; - mg.seed = emerge->mgparams->seed; + // Intentionally truncates to s32, see Mapgen::Mapgen() + mg.seed = (s32)emerge->mgparams->seed; mg.vm = LuaVoxelManip::checkobject(L, 1)->vm; mg.ndef = getServer(L)->getNodeDefManager(); @@ -1739,11 +1672,10 @@ int ModApiMapgen::l_serialize_schematic(lua_State *L) std::ostringstream os(std::ios_base::binary); switch (schem_format) { case SCHEM_FMT_MTS: - schem->serializeToMts(&os, schem->m_nodenames); + schem->serializeToMts(&os); break; case SCHEM_FMT_LUA: - schem->serializeToLua(&os, schem->m_nodenames, - use_comments, indent_spaces); + schem->serializeToLua(&os, use_comments, indent_spaces); break; default: return 0; diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index f52e4892e..8ae99b929 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -355,6 +355,15 @@ int ObjectRef::l_set_armor_groups(lua_State *L) ItemGroupList groups; read_groups(L, 2, groups); + if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER) { + if (!g_settings->getBool("enable_damage") && !itemgroup_get(groups, "immortal")) { + warningstream << "Mod tried to enable damage for a player, but it's " + "disabled globally. Ignoring." << std::endl; + infostream << script_get_backtrace(L) << std::endl; + groups["immortal"] = 1; + } + } + sao->setArmorGroups(groups); return 0; } @@ -399,7 +408,7 @@ int ObjectRef::l_get_animation(lua_State *L) if (sao == nullptr) return 0; - v2f frames = v2f(1,1); + v2f frames = v2f(1, 1); float frame_speed = 15; float frame_blend = 0; bool frame_loop = true; @@ -463,8 +472,8 @@ int ObjectRef::l_set_eye_offset(lua_State *L) if (player == nullptr) return 0; - v3f offset_first = read_v3f(L, 2); - v3f offset_third = read_v3f(L, 3); + v3f offset_first = readParam<v3f>(L, 2, v3f(0, 0, 0)); + v3f offset_third = readParam<v3f>(L, 3, v3f(0, 0, 0)); // Prevent abuse of offset values (keep player always visible) offset_third.X = rangelim(offset_third.X,-10,10); @@ -537,9 +546,9 @@ int ObjectRef::l_set_bone_position(lua_State *L) if (sao == nullptr) return 0; - std::string bone = readParam<std::string>(L, 2); - v3f position = check_v3f(L, 3); - v3f rotation = check_v3f(L, 4); + std::string bone = readParam<std::string>(L, 2, ""); + v3f position = readParam<v3f>(L, 3, v3f(0, 0, 0)); + v3f rotation = readParam<v3f>(L, 4, v3f(0, 0, 0)); sao->setBonePosition(bone, position, rotation); return 0; @@ -554,7 +563,7 @@ int ObjectRef::l_get_bone_position(lua_State *L) if (sao == nullptr) return 0; - std::string bone = readParam<std::string>(L, 2); + std::string bone = readParam<std::string>(L, 2, ""); v3f position = v3f(0, 0, 0); v3f rotation = v3f(0, 0, 0); @@ -578,10 +587,10 @@ int ObjectRef::l_set_attach(lua_State *L) if (sao == parent) throw LuaError("ObjectRef::set_attach: attaching object to itself is not allowed."); - int parent_id = 0; + int parent_id; std::string bone; - v3f position = v3f(0, 0, 0); - v3f rotation = v3f(0, 0, 0); + v3f position; + v3f rotation; bool force_visible; sao->getAttachment(&parent_id, &bone, &position, &rotation, &force_visible); @@ -590,9 +599,9 @@ int ObjectRef::l_set_attach(lua_State *L) old_parent->removeAttachmentChild(sao->getId()); } - bone = readParam<std::string>(L, 3, ""); - position = read_v3f(L, 4); - rotation = read_v3f(L, 5); + bone = readParam<std::string>(L, 3, ""); + position = readParam<v3f>(L, 4, v3f(0, 0, 0)); + rotation = readParam<v3f>(L, 5, v3f(0, 0, 0)); force_visible = readParam<bool>(L, 6, false); sao->setAttachment(parent->getId(), bone, position, rotation, force_visible); @@ -609,10 +618,10 @@ int ObjectRef::l_get_attach(lua_State *L) if (sao == nullptr) return 0; - int parent_id = 0; + int parent_id; std::string bone; - v3f position = v3f(0, 0, 0); - v3f rotation = v3f(0, 0, 0); + v3f position; + v3f rotation; bool force_visible; sao->getAttachment(&parent_id, &bone, &position, &rotation, &force_visible); @@ -728,6 +737,18 @@ int ObjectRef::l_set_nametag_attributes(lua_State *L) } lua_pop(L, 1); + lua_getfield(L, -1, "bgcolor"); + if (!lua_isnil(L, -1)) { + if (lua_toboolean(L, -1)) { + video::SColor color; + if (read_color(L, -1, &color)) + prop->nametag_bgcolor = color; + } else { + prop->nametag_bgcolor = nullopt; + } + } + lua_pop(L, 1); + std::string nametag = getstringfield_default(L, 2, "text", ""); prop->nametag = nametag; @@ -749,13 +770,24 @@ int ObjectRef::l_get_nametag_attributes(lua_State *L) if (!prop) return 0; - video::SColor color = prop->nametag_color; - lua_newtable(L); - push_ARGB8(L, color); + + push_ARGB8(L, prop->nametag_color); lua_setfield(L, -2, "color"); + + if (prop->nametag_bgcolor) { + push_ARGB8(L, prop->nametag_bgcolor.value()); + lua_setfield(L, -2, "bgcolor"); + } else { + lua_pushboolean(L, false); + lua_setfield(L, -2, "bgcolor"); + } + lua_pushstring(L, prop->nametag.c_str()); lua_setfield(L, -2, "text"); + + + return 1; } @@ -892,9 +924,6 @@ int ObjectRef::l_set_yaw(lua_State *L) if (entitysao == nullptr) return 0; - if (isNaN(L, 2)) - throw LuaError("ObjectRef::set_yaw: NaN value is not allowed."); - float yaw = readParam<float>(L, 2) * core::RADTODEG; entitysao->setRotation(v3f(0, yaw, 0)); @@ -2199,7 +2228,7 @@ int ObjectRef::l_set_minimap_modes(lua_State *L) luaL_checktype(L, 2, LUA_TTABLE); std::vector<MinimapMode> modes; - s16 selected_mode = luaL_checkint(L, 3); + s16 selected_mode = readParam<s16>(L, 3); lua_pushnil(L); while (lua_next(L, 2) != 0) { diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 64ae924d2..026f5282c 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -44,7 +44,7 @@ int ModApiServer::l_request_shutdown(lua_State *L) int ModApiServer::l_get_server_status(lua_State *L) { NO_MAP_LOCK_REQUIRED; - lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str()); + lua_pushstring(L, getServer(L)->getStatusString().c_str()); return 1; } @@ -116,24 +116,18 @@ int ModApiServer::l_get_player_privs(lua_State *L) int ModApiServer::l_get_player_ip(lua_State *L) { NO_MAP_LOCK_REQUIRED; - const char * name = luaL_checkstring(L, 1); - RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name); - if(player == NULL) - { + + Server *server = getServer(L); + + const char *name = luaL_checkstring(L, 1); + RemotePlayer *player = server->getEnv().getPlayer(name); + if (!player) { lua_pushnil(L); // no such player return 1; } - try - { - Address addr = getServer(L)->getPeerAddress(player->getPeerId()); - std::string ip_str = addr.serializeString(); - lua_pushstring(L, ip_str.c_str()); - return 1; - } catch (const con::PeerNotFoundException &) { - dstream << FUNCTION_NAME << ": peer was not found" << std::endl; - lua_pushnil(L); // error - return 1; - } + + lua_pushstring(L, server->getPeerAddress(player->getPeerId()).serializeString().c_str()); + return 1; } // get_player_information(name) @@ -150,26 +144,18 @@ int ModApiServer::l_get_player_information(lua_State *L) return 1; } - Address addr; - try { - addr = server->getPeerAddress(player->getPeerId()); - } catch (const con::PeerNotFoundException &) { - dstream << FUNCTION_NAME << ": peer was not found" << std::endl; - lua_pushnil(L); // error - return 1; - } - - float min_rtt, max_rtt, avg_rtt, min_jitter, max_jitter, avg_jitter; - ClientState state; - u32 uptime; - u16 prot_vers; - u8 ser_vers, major, minor, patch; - std::string vers_string, lang_code; + /* + Be careful not to introduce a depdendency on the connection to + the peer here. This function is >>REQUIRED<< to still be able to return + values even when the peer unexpectedly disappears. + Hence all the ConInfo values here are optional. + */ auto getConInfo = [&] (con::rtt_stat_type type, float *value) -> bool { return server->getClientConInfo(player->getPeerId(), type, value); }; + float min_rtt, max_rtt, avg_rtt, min_jitter, max_jitter, avg_jitter; bool have_con_info = getConInfo(con::MIN_RTT, &min_rtt) && getConInfo(con::MAX_RTT, &max_rtt) && @@ -178,11 +164,9 @@ int ModApiServer::l_get_player_information(lua_State *L) getConInfo(con::MAX_JITTER, &max_jitter) && getConInfo(con::AVG_JITTER, &avg_jitter); - bool r = server->getClientInfo(player->getPeerId(), &state, &uptime, - &ser_vers, &prot_vers, &major, &minor, &patch, &vers_string, - &lang_code); - if (!r) { - dstream << FUNCTION_NAME << ": peer was not found" << std::endl; + ClientInfo info; + if (!server->getClientInfo(player->getPeerId(), info)) { + warningstream << FUNCTION_NAME << ": no client info?!" << std::endl; lua_pushnil(L); // error return 1; } @@ -191,13 +175,13 @@ int ModApiServer::l_get_player_information(lua_State *L) int table = lua_gettop(L); lua_pushstring(L,"address"); - lua_pushstring(L, addr.serializeString().c_str()); + lua_pushstring(L, info.addr.serializeString().c_str()); lua_settable(L, table); lua_pushstring(L,"ip_version"); - if (addr.getFamily() == AF_INET) { + if (info.addr.getFamily() == AF_INET) { lua_pushnumber(L, 4); - } else if (addr.getFamily() == AF_INET6) { + } else if (info.addr.getFamily() == AF_INET6) { lua_pushnumber(L, 6); } else { lua_pushnumber(L, 0); @@ -231,11 +215,11 @@ int ModApiServer::l_get_player_information(lua_State *L) } lua_pushstring(L,"connection_uptime"); - lua_pushnumber(L, uptime); + lua_pushnumber(L, info.uptime); lua_settable(L, table); lua_pushstring(L,"protocol_version"); - lua_pushnumber(L, prot_vers); + lua_pushnumber(L, info.prot_vers); lua_settable(L, table); lua_pushstring(L, "formspec_version"); @@ -243,32 +227,32 @@ int ModApiServer::l_get_player_information(lua_State *L) lua_settable(L, table); lua_pushstring(L, "lang_code"); - lua_pushstring(L, lang_code.c_str()); + lua_pushstring(L, info.lang_code.c_str()); lua_settable(L, table); #ifndef NDEBUG lua_pushstring(L,"serialization_version"); - lua_pushnumber(L, ser_vers); + lua_pushnumber(L, info.ser_vers); lua_settable(L, table); lua_pushstring(L,"major"); - lua_pushnumber(L, major); + lua_pushnumber(L, info.major); lua_settable(L, table); lua_pushstring(L,"minor"); - lua_pushnumber(L, minor); + lua_pushnumber(L, info.minor); lua_settable(L, table); lua_pushstring(L,"patch"); - lua_pushnumber(L, patch); + lua_pushnumber(L, info.patch); lua_settable(L, table); lua_pushstring(L,"version_string"); - lua_pushstring(L, vers_string.c_str()); + lua_pushstring(L, info.vers_string.c_str()); lua_settable(L, table); lua_pushstring(L,"state"); - lua_pushstring(L,ClientInterface::state2Name(state).c_str()); + lua_pushstring(L, ClientInterface::state2Name(info.state).c_str()); lua_settable(L, table); #endif @@ -296,23 +280,18 @@ int ModApiServer::l_get_ban_description(lua_State *L) int ModApiServer::l_ban_player(lua_State *L) { NO_MAP_LOCK_REQUIRED; - const char * name = luaL_checkstring(L, 1); - RemotePlayer *player = dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name); - if (player == NULL) { + + Server *server = getServer(L); + + const char *name = luaL_checkstring(L, 1); + RemotePlayer *player = server->getEnv().getPlayer(name); + if (!player) { lua_pushboolean(L, false); // no such player return 1; } - try - { - Address addr = getServer(L)->getPeerAddress( - dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->getPeerId()); - std::string ip_str = addr.serializeString(); - getServer(L)->setIpBanned(ip_str, name); - } catch(const con::PeerNotFoundException &) { - dstream << FUNCTION_NAME << ": peer was not found" << std::endl; - lua_pushboolean(L, false); // error - return 1; - } + + std::string ip_str = server->getPeerAddress(player->getPeerId()).serializeString(); + server->setIpBanned(ip_str, name); lua_pushboolean(L, true); return 1; } @@ -474,19 +453,30 @@ int ModApiServer::l_sound_fade(lua_State *L) } // dynamic_add_media(filepath) -int ModApiServer::l_dynamic_add_media(lua_State *L) +int ModApiServer::l_dynamic_add_media_raw(lua_State *L) { NO_MAP_LOCK_REQUIRED; - // Reject adding media before the server has started up if (!getEnv(L)) throw LuaError("Dynamic media cannot be added before server has started up"); std::string filepath = readParam<std::string>(L, 1); CHECK_SECURE_PATH(L, filepath.c_str(), false); - bool ok = getServer(L)->dynamicAddMedia(filepath); - lua_pushboolean(L, ok); + std::vector<RemotePlayer*> sent_to; + bool ok = getServer(L)->dynamicAddMedia(filepath, sent_to); + if (ok) { + // (see wrapper code in builtin) + lua_createtable(L, sent_to.size(), 0); + int i = 0; + for (RemotePlayer *player : sent_to) { + lua_pushstring(L, player->getName()); + lua_rawseti(L, -2, ++i); + } + } else { + lua_pushboolean(L, false); + } + return 1; } @@ -554,7 +544,7 @@ void ModApiServer::Initialize(lua_State *L, int top) API_FCT(sound_play); API_FCT(sound_stop); API_FCT(sound_fade); - API_FCT(dynamic_add_media); + API_FCT(dynamic_add_media_raw); API_FCT(get_player_information); API_FCT(get_player_privs); diff --git a/src/script/lua_api/l_server.h b/src/script/lua_api/l_server.h index 938bfa8ef..2df180b17 100644 --- a/src/script/lua_api/l_server.h +++ b/src/script/lua_api/l_server.h @@ -71,7 +71,7 @@ private: static int l_sound_fade(lua_State *L); // dynamic_add_media(filepath) - static int l_dynamic_add_media(lua_State *L); + static int l_dynamic_add_media_raw(lua_State *L); // get_player_privs(name, text) static int l_get_player_privs(lua_State *L); diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp index 33eb02392..a82073ed4 100644 --- a/src/script/lua_api/l_settings.cpp +++ b/src/script/lua_api/l_settings.cpp @@ -20,18 +20,43 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_settings.h" #include "lua_api/l_internal.h" #include "cpp_api/s_security.h" +#include "threading/mutex_auto_lock.h" #include "util/string.h" // FlagDesc #include "settings.h" #include "noise.h" #include "log.h" -#define SET_SECURITY_CHECK(L, name) \ - if (o->m_settings == g_settings && ScriptApiSecurity::isSecure(L) && \ - name.compare(0, 7, "secure.") == 0) { \ - throw LuaError("Attempt to set secure setting."); \ +/* This protects: + * 'secure.*' settings from being set + * some mapgen settings from being set + * (not security-criticial, just to avoid messing up user configs) + */ +#define CHECK_SETTING_SECURITY(L, name) \ + if (o->m_settings == g_settings) { \ + if (checkSettingSecurity(L, name) == -1) \ + return 0; \ } +static inline int checkSettingSecurity(lua_State* L, const std::string &name) +{ + if (ScriptApiSecurity::isSecure(L) && name.compare(0, 7, "secure.") == 0) + throw LuaError("Attempt to set secure setting."); + + bool is_mainmenu = false; +#ifndef SERVER + is_mainmenu = ModApiBase::getGuiEngine(L) != nullptr; +#endif + if (!is_mainmenu && (name == "mg_name" || name == "mg_flags")) { + errorstream << "Tried to set global setting " << name << ", ignoring. " + "minetest.set_mapgen_setting() should be used instead." << std::endl; + infostream << script_get_backtrace(L) << std::endl; + return -1; + } + + return 0; +} + LuaSettings::LuaSettings(Settings *settings, const std::string &filename) : m_settings(settings), m_filename(filename) @@ -129,6 +154,7 @@ int LuaSettings::l_get_np_group(lua_State *L) return 1; } +// get_flags(self, key) -> table or nil int LuaSettings::l_get_flags(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -161,7 +187,7 @@ int LuaSettings::l_set(lua_State* L) std::string key = std::string(luaL_checkstring(L, 2)); const char* value = luaL_checkstring(L, 3); - SET_SECURITY_CHECK(L, key); + CHECK_SETTING_SECURITY(L, key); if (!o->m_settings->set(key, value)) throw LuaError("Invalid sequence found in setting parameters"); @@ -178,14 +204,14 @@ int LuaSettings::l_set_bool(lua_State* L) std::string key = std::string(luaL_checkstring(L, 2)); bool value = readParam<bool>(L, 3); - SET_SECURITY_CHECK(L, key); + CHECK_SETTING_SECURITY(L, key); o->m_settings->setBool(key, value); - return 1; + return 0; } -// set(self, key, value) +// set_np_group(self, key, value) int LuaSettings::l_set_np_group(lua_State *L) { NO_MAP_LOCK_REQUIRED; @@ -195,9 +221,9 @@ int LuaSettings::l_set_np_group(lua_State *L) NoiseParams value; read_noiseparams(L, 3, &value); - SET_SECURITY_CHECK(L, key); + CHECK_SETTING_SECURITY(L, key); - o->m_settings->setNoiseParams(key, value, false); + o->m_settings->setNoiseParams(key, value); return 0; } @@ -210,7 +236,7 @@ int LuaSettings::l_remove(lua_State* L) std::string key = std::string(luaL_checkstring(L, 2)); - SET_SECURITY_CHECK(L, key); + CHECK_SETTING_SECURITY(L, key); bool success = o->m_settings->remove(key); lua_pushboolean(L, success); @@ -253,20 +279,36 @@ int LuaSettings::l_write(lua_State* L) return 1; } -// to_table(self) -> {[key1]=value1,...} -int LuaSettings::l_to_table(lua_State* L) +static void push_settings_table(lua_State *L, const Settings *settings) { - NO_MAP_LOCK_REQUIRED; - LuaSettings* o = checkobject(L, 1); - - std::vector<std::string> keys = o->m_settings->getNames(); - + std::vector<std::string> keys = settings->getNames(); lua_newtable(L); for (const std::string &key : keys) { - lua_pushstring(L, o->m_settings->get(key).c_str()); + std::string value; + Settings *group = nullptr; + + if (settings->getNoEx(key, value)) { + lua_pushstring(L, value.c_str()); + } else if (settings->getGroupNoEx(key, group)) { + // Recursively push tables + push_settings_table(L, group); + } else { + // Impossible case (multithreading) due to MutexAutoLock + continue; + } + lua_setfield(L, -2, key.c_str()); } +} + +// to_table(self) -> {[key1]=value1,...} +int LuaSettings::l_to_table(lua_State* L) +{ + NO_MAP_LOCK_REQUIRED; + LuaSettings* o = checkobject(L, 1); + MutexAutoLock(o->m_settings->m_mutex); + push_settings_table(L, o->m_settings); return 1; } diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index e2730c6d9..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. @@ -250,6 +251,17 @@ int ModApiUtil::l_get_builtin_path(lua_State *L) return 1; } +// get_user_path() +int ModApiUtil::l_get_user_path(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + std::string path = porting::path_user; + lua_pushstring(L, path.c_str()); + + return 1; +} + // compress(data, method, level) int ModApiUtil::l_compress(lua_State *L) { @@ -468,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); @@ -486,6 +515,7 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(is_yes); API_FCT(get_builtin_path); + API_FCT(get_user_path); API_FCT(compress); API_FCT(decompress); @@ -501,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"); @@ -527,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"); @@ -544,6 +576,7 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top) API_FCT(is_yes); API_FCT(get_builtin_path); + API_FCT(get_user_path); API_FCT(compress); API_FCT(decompress); @@ -556,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 b6c1b58af..6943a6afb 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -68,6 +68,9 @@ private: // get_builtin_path() static int l_get_builtin_path(lua_State *L); + // get_user_path() + static int l_get_user_path(lua_State *L); + // compress(data, method, ...) static int l_compress(lua_State *L); @@ -98,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); |