From ea5d6312c1ab6ff3e859fcd7a429a384f0f0ff91 Mon Sep 17 00:00:00 2001 From: Zughy <63455151+Zughy@users.noreply.github.com> Date: Thu, 21 Jan 2021 17:37:38 +0000 Subject: ObjectRef: fix some v3f checks (#10602) --- src/script/lua_api/l_object.cpp | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index f52e4892e..ba201a9d3 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -399,7 +399,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 +463,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(L, 2, v3f(0, 0, 0)); + v3f offset_third = readParam(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 +537,9 @@ int ObjectRef::l_set_bone_position(lua_State *L) if (sao == nullptr) return 0; - std::string bone = readParam(L, 2); - v3f position = check_v3f(L, 3); - v3f rotation = check_v3f(L, 4); + std::string bone = readParam(L, 2, ""); + v3f position = readParam(L, 3, v3f(0, 0, 0)); + v3f rotation = readParam(L, 4, v3f(0, 0, 0)); sao->setBonePosition(bone, position, rotation); return 0; @@ -554,7 +554,7 @@ int ObjectRef::l_get_bone_position(lua_State *L) if (sao == nullptr) return 0; - std::string bone = readParam(L, 2); + std::string bone = readParam(L, 2, ""); v3f position = v3f(0, 0, 0); v3f rotation = v3f(0, 0, 0); @@ -578,10 +578,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 +590,9 @@ int ObjectRef::l_set_attach(lua_State *L) old_parent->removeAttachmentChild(sao->getId()); } - bone = readParam(L, 3, ""); - position = read_v3f(L, 4); - rotation = read_v3f(L, 5); + bone = readParam(L, 3, ""); + position = readParam(L, 4, v3f(0, 0, 0)); + rotation = readParam(L, 5, v3f(0, 0, 0)); force_visible = readParam(L, 6, false); sao->setAttachment(parent->getId(), bone, position, rotation, force_visible); @@ -609,10 +609,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); @@ -892,9 +892,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(L, 2) * core::RADTODEG; entitysao->setRotation(v3f(0, yaw, 0)); @@ -2199,7 +2196,7 @@ int ObjectRef::l_set_minimap_modes(lua_State *L) luaL_checktype(L, 2, LUA_TTABLE); std::vector modes; - s16 selected_mode = luaL_checkint(L, 3); + s16 selected_mode = readParam(L, 3); lua_pushnil(L); while (lua_next(L, 2) != 0) { -- cgit v1.2.3 From 45ccfe26fb6e0a130e4925ec362cccb1f045a829 Mon Sep 17 00:00:00 2001 From: Zughy <63455151+Zughy@users.noreply.github.com> Date: Thu, 21 Jan 2021 18:17:09 +0000 Subject: Removed some obsolete code (#10562) Co-authored-by: Zughy <4279489-marco_a@users.noreply.gitlab.com> --- builtin/game/deprecated.lua | 25 +------------------------ builtin/game/item.lua | 4 ---- builtin/game/register.lua | 7 ------- doc/world_format.txt | 11 ----------- src/activeobject.h | 10 +++++----- src/mapgen/mapgen.h | 2 -- src/script/common/c_content.cpp | 17 ----------------- src/script/lua_api/l_mapgen.cpp | 3 --- src/server/player_sao.cpp | 2 +- 9 files changed, 7 insertions(+), 74 deletions(-) (limited to 'src/script/lua_api') diff --git a/builtin/game/deprecated.lua b/builtin/game/deprecated.lua index 20f0482eb..c5c7848f5 100644 --- a/builtin/game/deprecated.lua +++ b/builtin/game/deprecated.lua @@ -1,28 +1,5 @@ -- Minetest: builtin/deprecated.lua --- --- Default material types --- -local function digprop_err() - core.log("deprecated", "The core.digprop_* functions are obsolete and need to be replaced by item groups.") -end - -core.digprop_constanttime = digprop_err -core.digprop_stonelike = digprop_err -core.digprop_dirtlike = digprop_err -core.digprop_gravellike = digprop_err -core.digprop_woodlike = digprop_err -core.digprop_leaveslike = digprop_err -core.digprop_glasslike = digprop_err - -function core.node_metadata_inventory_move_allow_all() - core.log("deprecated", "core.node_metadata_inventory_move_allow_all is obsolete and does nothing.") -end - -function core.add_to_creative_inventory(itemstring) - core.log("deprecated", "core.add_to_creative_inventory is obsolete and does nothing.") -end - -- -- EnvRef -- @@ -77,7 +54,7 @@ core.setting_save = setting_proxy("write") function core.register_on_auth_fail(func) core.log("deprecated", "core.register_on_auth_fail " .. - "is obsolete and should be replaced by " .. + "is deprecated and should be replaced by " .. "core.register_on_authplayer instead.") core.register_on_authplayer(function (player_name, ip, is_success) diff --git a/builtin/game/item.lua b/builtin/game/item.lua index 109712b42..0df25b455 100644 --- a/builtin/game/item.lua +++ b/builtin/game/item.lua @@ -705,10 +705,6 @@ core.nodedef_default = { on_receive_fields = nil, - on_metadata_inventory_move = core.node_metadata_inventory_move_allow_all, - on_metadata_inventory_offer = core.node_metadata_inventory_offer_allow_all, - on_metadata_inventory_take = core.node_metadata_inventory_take_allow_all, - -- Node properties drawtype = "normal", visual_scale = 1.0, diff --git a/builtin/game/register.lua b/builtin/game/register.lua index 93e1dad12..b006957e9 100644 --- a/builtin/game/register.lua +++ b/builtin/game/register.lua @@ -324,13 +324,6 @@ for name in pairs(forbidden_item_names) do register_alias_raw(name, "") end - --- Obsolete: --- Aliases for core.register_alias (how ironic...) --- core.alias_node = core.register_alias --- core.alias_tool = core.register_alias --- core.alias_craftitem = core.register_alias - -- -- Built-in node definitions. Also defined in C. -- diff --git a/doc/world_format.txt b/doc/world_format.txt index 73a03e5ee..a8a9e463e 100644 --- a/doc/world_format.txt +++ b/doc/world_format.txt @@ -493,19 +493,8 @@ Static objects are persistent freely moving objects in the world. Object types: 1: Test object -2: Item -3: Rat (obsolete) -4: Oerkki (obsolete) -5: Firefly (obsolete) -6: MobV2 (obsolete) 7: LuaEntity -1: Item: - u8 version - version 0: - u16 len - u8[len] itemstring - 7: LuaEntity: u8 compatibility_byte (always 1) u16 len diff --git a/src/activeobject.h b/src/activeobject.h index 0829858ad..1d8a3712b 100644 --- a/src/activeobject.h +++ b/src/activeobject.h @@ -28,11 +28,11 @@ enum ActiveObjectType { ACTIVEOBJECT_TYPE_INVALID = 0, ACTIVEOBJECT_TYPE_TEST = 1, // Obsolete stuff - ACTIVEOBJECT_TYPE_ITEM = 2, -// ACTIVEOBJECT_TYPE_RAT = 3, -// ACTIVEOBJECT_TYPE_OERKKI1 = 4, -// ACTIVEOBJECT_TYPE_FIREFLY = 5, - ACTIVEOBJECT_TYPE_MOBV2 = 6, +// ACTIVEOBJECT_TYPE_ITEM = 2, +// ACTIVEOBJECT_TYPE_RAT = 3, +// ACTIVEOBJECT_TYPE_OERKKI1 = 4, +// ACTIVEOBJECT_TYPE_FIREFLY = 5, +// ACTIVEOBJECT_TYPE_MOBV2 = 6, // End obsolete stuff ACTIVEOBJECT_TYPE_LUAENTITY = 7, // Special type, not stored as a static object diff --git a/src/mapgen/mapgen.h b/src/mapgen/mapgen.h index 1487731e2..61db4f3b9 100644 --- a/src/mapgen/mapgen.h +++ b/src/mapgen/mapgen.h @@ -30,10 +30,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #define MAPGEN_DEFAULT_NAME "v7" /////////////////// Mapgen flags -#define MG_TREES 0x01 // Obsolete. Moved into mgv6 flags #define MG_CAVES 0x02 #define MG_DUNGEONS 0x04 -#define MG_FLAT 0x08 // Obsolete. Moved into mgv6 flags #define MG_LIGHT 0x10 #define MG_DECORATIONS 0x20 #define MG_BIOMES 0x40 diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index e3cb9042e..4316f412d 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -83,9 +83,6 @@ void read_item_definition(lua_State* L, int index, getboolfield(L, index, "liquids_pointable", def.liquids_pointable); - warn_if_field_exists(L, index, "tool_digging_properties", - "Obsolete; use tool_capabilities"); - lua_getfield(L, index, "tool_capabilities"); if(lua_istable(L, -1)){ def.tool_capabilities = new ToolCapabilities( @@ -653,20 +650,6 @@ void read_content_features(lua_State *L, ContentFeatures &f, int index) warningstream << "Node " << f.name.c_str() << " has a palette, but not a suitable paramtype2." << std::endl; - // Warn about some obsolete fields - warn_if_field_exists(L, index, "wall_mounted", - "Obsolete; use paramtype2 = 'wallmounted'"); - warn_if_field_exists(L, index, "light_propagates", - "Obsolete; determined from paramtype"); - warn_if_field_exists(L, index, "dug_item", - "Obsolete; use 'drop' field"); - warn_if_field_exists(L, index, "extra_dug_item", - "Obsolete; use 'drop' field"); - warn_if_field_exists(L, index, "extra_dug_item_rarity", - "Obsolete; use 'drop' field"); - warn_if_field_exists(L, index, "metadata_name", - "Obsolete; use on_add and metadata callbacks"); - // True for all ground-like things like stone and mud, false for eg. trees getboolfield(L, index, "is_ground_content", f.is_ground_content); f.light_propagates = (f.param_type == CPT_LIGHT); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 834938e56..498859f14 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -873,9 +873,6 @@ int ModApiMapgen::l_set_mapgen_params(lua_State *L) if (lua_isnumber(L, -1)) settingsmgr->setMapSetting("chunksize", readParam(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(L, -1), true); diff --git a/src/server/player_sao.cpp b/src/server/player_sao.cpp index c1b1401e6..110d2010d 100644 --- a/src/server/player_sao.cpp +++ b/src/server/player_sao.cpp @@ -148,7 +148,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version) void PlayerSAO::getStaticData(std::string * result) const { - FATAL_ERROR("Obsolete function"); + FATAL_ERROR("This function shall not be called for PlayerSAO"); } void PlayerSAO::step(float dtime, bool send_recommended) -- cgit v1.2.3 From 4fcd000e20a26120349184cb9d40342b7876e6b8 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Thu, 21 Jan 2021 19:08:06 +0000 Subject: MgOre: Fix invalid field polymorphism (#10846) --- src/mapgen/mg_ore.h | 47 ++++++++++++++++++----------------------- src/script/lua_api/l_mapgen.cpp | 2 +- 2 files changed, 21 insertions(+), 28 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/mapgen/mg_ore.h b/src/mapgen/mg_ore.h index 76420fab4..a58fa9bfe 100644 --- a/src/mapgen/mg_ore.h +++ b/src/mapgen/mg_ore.h @@ -52,7 +52,7 @@ extern FlagDesc flagdesc_ore[]; class Ore : public ObjDef, public NodeResolver { public: - static const bool NEEDS_NOISE = false; + const bool needs_noise; content_t c_ore; // the node to place std::vector c_wherein; // the nodes to be placed in @@ -68,7 +68,7 @@ public: Noise *noise = nullptr; std::unordered_set biomes; - Ore() = default;; + explicit Ore(bool needs_noise): needs_noise(needs_noise) {} virtual ~Ore(); virtual void resolveNodeNames(); @@ -83,17 +83,17 @@ protected: class OreScatter : public Ore { public: - static const bool NEEDS_NOISE = false; + OreScatter() : Ore(false) {} ObjDef *clone() const; - virtual void generate(MMVManip *vm, int mapseed, u32 blockseed, - v3s16 nmin, v3s16 nmax, biome_t *biomemap); + void generate(MMVManip *vm, int mapseed, u32 blockseed, + v3s16 nmin, v3s16 nmax, biome_t *biomemap) override; }; class OreSheet : public Ore { public: - static const bool NEEDS_NOISE = true; + OreSheet() : Ore(true) {} ObjDef *clone() const; @@ -101,14 +101,12 @@ public: u16 column_height_max; float column_midpoint_factor; - virtual void generate(MMVManip *vm, int mapseed, u32 blockseed, - v3s16 nmin, v3s16 nmax, biome_t *biomemap); + void generate(MMVManip *vm, int mapseed, u32 blockseed, + v3s16 nmin, v3s16 nmax, biome_t *biomemap) override; }; class OrePuff : public Ore { public: - static const bool NEEDS_NOISE = true; - ObjDef *clone() const; NoiseParams np_puff_top; @@ -116,55 +114,50 @@ public: Noise *noise_puff_top = nullptr; Noise *noise_puff_bottom = nullptr; - OrePuff() = default; + OrePuff() : Ore(true) {} virtual ~OrePuff(); - virtual void generate(MMVManip *vm, int mapseed, u32 blockseed, - v3s16 nmin, v3s16 nmax, biome_t *biomemap); + void generate(MMVManip *vm, int mapseed, u32 blockseed, + v3s16 nmin, v3s16 nmax, biome_t *biomemap) override; }; class OreBlob : public Ore { public: - static const bool NEEDS_NOISE = true; - ObjDef *clone() const; - virtual void generate(MMVManip *vm, int mapseed, u32 blockseed, - v3s16 nmin, v3s16 nmax, biome_t *biomemap); + OreBlob() : Ore(true) {} + void generate(MMVManip *vm, int mapseed, u32 blockseed, + v3s16 nmin, v3s16 nmax, biome_t *biomemap) override; }; class OreVein : public Ore { public: - static const bool NEEDS_NOISE = true; - ObjDef *clone() const; float random_factor; Noise *noise2 = nullptr; int sizey_prev = 0; - OreVein() = default; + OreVein() : Ore(true) {} virtual ~OreVein(); - virtual void generate(MMVManip *vm, int mapseed, u32 blockseed, - v3s16 nmin, v3s16 nmax, biome_t *biomemap); + void generate(MMVManip *vm, int mapseed, u32 blockseed, + v3s16 nmin, v3s16 nmax, biome_t *biomemap) override; }; class OreStratum : public Ore { public: - static const bool NEEDS_NOISE = false; - ObjDef *clone() const; NoiseParams np_stratum_thickness; Noise *noise_stratum_thickness = nullptr; u16 stratum_thickness; - OreStratum() = default; + OreStratum() : Ore(false) {} virtual ~OreStratum(); - virtual void generate(MMVManip *vm, int mapseed, u32 blockseed, - v3s16 nmin, v3s16 nmax, biome_t *biomemap); + void generate(MMVManip *vm, int mapseed, u32 blockseed, + v3s16 nmin, v3s16 nmax, biome_t *biomemap) override; }; class OreManager : public ObjDefManager { diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 498859f14..fad08e1f6 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -1335,7 +1335,7 @@ 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) { + } else if (ore->needs_noise) { errorstream << "register_ore: specified ore type requires valid " "'noise_params' parameter" << std::endl; delete ore; -- cgit v1.2.3 From 67aa75d444d0e5cfff2728dbbcffd6f95b2fe88b Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 22 Jan 2021 15:08:57 +0000 Subject: Use JSON for favorites, move server list code to Lua (#10085) Co-authored-by: sfan5 --- builtin/mainmenu/common.lua | 53 ------ builtin/mainmenu/init.lua | 1 + builtin/mainmenu/serverlistmgr.lua | 241 ++++++++++++++++++++++++ builtin/mainmenu/tab_online.lua | 115 +++++------ builtin/mainmenu/tests/favorites_wellformed.txt | 29 +++ builtin/mainmenu/tests/serverlistmgr_spec.lua | 36 ++++ builtin/settingtypes.txt | 2 +- doc/menu_lua_api.txt | 26 --- minetest.conf.example | 2 +- src/client/clientlauncher.cpp | 8 - src/defaultsettings.cpp | 2 +- src/script/lua_api/l_mainmenu.cpp | 206 +------------------- src/script/lua_api/l_mainmenu.h | 4 - src/script/lua_api/l_util.cpp | 13 ++ src/script/lua_api/l_util.h | 3 + src/serverlist.cpp | 162 ---------------- src/serverlist.h | 13 -- 17 files changed, 388 insertions(+), 528 deletions(-) create mode 100644 builtin/mainmenu/serverlistmgr.lua create mode 100644 builtin/mainmenu/tests/favorites_wellformed.txt create mode 100644 builtin/mainmenu/tests/serverlistmgr_spec.lua (limited to 'src/script/lua_api') diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua index 2bd8aa8a5..01f9a30b9 100644 --- a/builtin/mainmenu/common.lua +++ b/builtin/mainmenu/common.lua @@ -62,24 +62,6 @@ function image_column(tooltip, flagname) "5=" .. core.formspec_escape(defaulttexturedir .. "server_ping_1.png") end --------------------------------------------------------------------------------- -function order_favorite_list(list) - local res = {} - --orders the favorite list after support - for i = 1, #list do - local fav = list[i] - if is_server_protocol_compat(fav.proto_min, fav.proto_max) then - res[#res + 1] = fav - end - end - for i = 1, #list do - local fav = list[i] - if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then - res[#res + 1] = fav - end - end - return res -end -------------------------------------------------------------------------------- function render_serverlist_row(spec, is_favorite) @@ -226,41 +208,6 @@ function menu_handle_key_up_down(fields, textlist, settingname) return false end --------------------------------------------------------------------------------- -function asyncOnlineFavourites() - if not menudata.public_known then - menudata.public_known = {{ - name = fgettext("Loading..."), - description = fgettext_ne("Try reenabling public serverlist and check your internet connection.") - }} - end - menudata.favorites = menudata.public_known - menudata.favorites_is_public = true - - if not menudata.public_downloading then - menudata.public_downloading = true - else - return - end - - core.handle_async( - function(param) - return core.get_favorites("online") - end, - nil, - function(result) - menudata.public_downloading = nil - local favs = order_favorite_list(result) - if favs[1] then - menudata.public_known = favs - menudata.favorites = menudata.public_known - menudata.favorites_is_public = true - end - core.event_handler("Refresh") - end - ) -end - -------------------------------------------------------------------------------- function text2textlist(xpos, ypos, width, height, tl_name, textlen, text, transparency) local textlines = core.wrap_text(text, textlen, true) diff --git a/builtin/mainmenu/init.lua b/builtin/mainmenu/init.lua index 656d1d149..45089c7c9 100644 --- a/builtin/mainmenu/init.lua +++ b/builtin/mainmenu/init.lua @@ -34,6 +34,7 @@ dofile(basepath .. "fstk" .. DIR_DELIM .. "ui.lua") dofile(menupath .. DIR_DELIM .. "async_event.lua") dofile(menupath .. DIR_DELIM .. "common.lua") dofile(menupath .. DIR_DELIM .. "pkgmgr.lua") +dofile(menupath .. DIR_DELIM .. "serverlistmgr.lua") dofile(menupath .. DIR_DELIM .. "textures.lua") dofile(menupath .. DIR_DELIM .. "dlg_config_world.lua") diff --git a/builtin/mainmenu/serverlistmgr.lua b/builtin/mainmenu/serverlistmgr.lua new file mode 100644 index 000000000..d98736e54 --- /dev/null +++ b/builtin/mainmenu/serverlistmgr.lua @@ -0,0 +1,241 @@ +--Minetest +--Copyright (C) 2020 rubenwardy +-- +--This program is free software; you can redistribute it and/or modify +--it under the terms of the GNU Lesser General Public License as published by +--the Free Software Foundation; either version 2.1 of the License, or +--(at your option) any later version. +-- +--This program is distributed in the hope that it will be useful, +--but WITHOUT ANY WARRANTY; without even the implied warranty of +--MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +--GNU Lesser General Public License for more details. +-- +--You should have received a copy of the GNU Lesser General Public License along +--with this program; if not, write to the Free Software Foundation, Inc., +--51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +serverlistmgr = {} + +-------------------------------------------------------------------------------- +local function order_server_list(list) + local res = {} + --orders the favorite list after support + for i = 1, #list do + local fav = list[i] + if is_server_protocol_compat(fav.proto_min, fav.proto_max) then + res[#res + 1] = fav + end + end + for i = 1, #list do + local fav = list[i] + if not is_server_protocol_compat(fav.proto_min, fav.proto_max) then + res[#res + 1] = fav + end + end + return res +end + +local public_downloading = false + +-------------------------------------------------------------------------------- +function serverlistmgr.sync() + if not serverlistmgr.servers then + serverlistmgr.servers = {{ + name = fgettext("Loading..."), + description = fgettext_ne("Try reenabling public serverlist and check your internet connection.") + }} + end + + if public_downloading then + return + end + public_downloading = true + + core.handle_async( + function(param) + local http = core.get_http_api() + local url = ("%s/list?proto_version_min=%d&proto_version_max=%d"):format( + core.settings:get("serverlist_url"), + core.get_min_supp_proto(), + core.get_max_supp_proto()) + + local response = http.fetch_sync({ url = url }) + if not response.succeeded then + return {} + end + + local retval = core.parse_json(response.data) + return retval and retval.list or {} + end, + nil, + function(result) + public_downloading = nil + local favs = order_server_list(result) + if favs[1] then + serverlistmgr.servers = favs + end + core.event_handler("Refresh") + end + ) +end + +-------------------------------------------------------------------------------- +local function get_favorites_path() + local base = core.get_user_path() .. DIR_DELIM .. "client" .. DIR_DELIM .. "serverlist" .. DIR_DELIM + return base .. core.settings:get("serverlist_file") +end + +-------------------------------------------------------------------------------- +local function save_favorites(favorites) + local filename = core.settings:get("serverlist_file") + -- If setting specifies legacy format change the filename to the new one + if filename:sub(#filename - 3):lower() == ".txt" then + core.settings:set("serverlist_file", filename:sub(1, #filename - 4) .. ".json") + end + + local path = get_favorites_path() + core.create_dir(path) + core.safe_file_write(path, core.write_json(favorites)) +end + +-------------------------------------------------------------------------------- +function serverlistmgr.read_legacy_favorites(path) + local file = io.open(path, "r") + if not file then + return nil + end + + local lines = {} + for line in file:lines() do + lines[#lines + 1] = line + end + file:close() + + local favorites = {} + + local i = 1 + while i < #lines do + local function pop() + local line = lines[i] + i = i + 1 + return line and line:trim() + end + + if pop():lower() == "[server]" then + local name = pop() + local address = pop() + local port = tonumber(pop()) + local description = pop() + + if name == "" then + name = nil + end + + if description == "" then + description = nil + end + + if not address or #address < 3 then + core.log("warning", "Malformed favorites file, missing address at line " .. i) + elseif not port or port < 1 or port > 65535 then + core.log("warning", "Malformed favorites file, missing port at line " .. i) + elseif (name and name:upper() == "[SERVER]") or + (address and address:upper() == "[SERVER]") or + (description and description:upper() == "[SERVER]") then + core.log("warning", "Potentially malformed favorites file, overran at line " .. i) + else + favorites[#favorites + 1] = { + name = name, + address = address, + port = port, + description = description + } + end + end + end + + return favorites +end + +-------------------------------------------------------------------------------- +local function read_favorites() + local path = get_favorites_path() + + -- If new format configured fall back to reading the legacy file + if path:sub(#path - 4):lower() == ".json" then + local file = io.open(path, "r") + if file then + local json = file:read("*all") + file:close() + return core.parse_json(json) + end + + path = path:sub(1, #path - 5) .. ".txt" + end + + local favs = serverlistmgr.read_legacy_favorites(path) + if favs then + save_favorites(favs) + os.remove(path) + end + return favs +end + +-------------------------------------------------------------------------------- +local function delete_favorite(favorites, del_favorite) + for i=1, #favorites do + local fav = favorites[i] + + if fav.address == del_favorite.address and fav.port == del_favorite.port then + table.remove(favorites, i) + return + end + end +end + +-------------------------------------------------------------------------------- +function serverlistmgr.get_favorites() + if serverlistmgr.favorites then + return serverlistmgr.favorites + end + + serverlistmgr.favorites = {} + + -- Add favorites, removing duplicates + local seen = {} + for _, fav in ipairs(read_favorites() or {}) do + local key = ("%s:%d"):format(fav.address:lower(), fav.port) + if not seen[key] then + seen[key] = true + serverlistmgr.favorites[#serverlistmgr.favorites + 1] = fav + end + end + + return serverlistmgr.favorites +end + +-------------------------------------------------------------------------------- +function serverlistmgr.add_favorite(new_favorite) + assert(type(new_favorite.port) == "number") + + -- Whitelist favorite keys + new_favorite = { + name = new_favorite.name, + address = new_favorite.address, + port = new_favorite.port, + description = new_favorite.description, + } + + local favorites = serverlistmgr.get_favorites() + delete_favorite(favorites, new_favorite) + table.insert(favorites, 1, new_favorite) + save_favorites(favorites) +end + +-------------------------------------------------------------------------------- +function serverlistmgr.delete_favorite(del_favorite) + local favorites = serverlistmgr.get_favorites() + delete_favorite(favorites, del_favorite) + save_favorites(favorites) +end diff --git a/builtin/mainmenu/tab_online.lua b/builtin/mainmenu/tab_online.lua index 8f1341161..e6748ed88 100644 --- a/builtin/mainmenu/tab_online.lua +++ b/builtin/mainmenu/tab_online.lua @@ -20,11 +20,11 @@ local function get_formspec(tabview, name, tabdata) -- Update the cached supported proto info, -- it may have changed after a change by the settings menu. common_update_cached_supp_proto() - local fav_selected + local selected if menudata.search_result then - fav_selected = menudata.search_result[tabdata.fav_selected] + selected = menudata.search_result[tabdata.selected] else - fav_selected = menudata.favorites[tabdata.fav_selected] + selected = serverlistmgr.servers[tabdata.selected] end if not tabdata.search_for then @@ -58,18 +58,18 @@ local function get_formspec(tabview, name, tabdata) -- Connect "button[9.88,4.9;2.3,1;btn_mp_connect;" .. fgettext("Connect") .. "]" - if tabdata.fav_selected and fav_selected then + if tabdata.selected and selected then if gamedata.fav then retval = retval .. "button[7.73,4.9;2.3,1;btn_delete_favorite;" .. fgettext("Del. Favorite") .. "]" end - if fav_selected.description then + if selected.description then retval = retval .. "textarea[8.1,2.3;4.23,2.9;;;" .. core.formspec_escape((gamedata.serverdescription or ""), true) .. "]" end end - --favourites + --favorites retval = retval .. "tablecolumns[" .. image_column(fgettext("Favorite"), "favorite") .. ";" .. image_column(fgettext("Ping")) .. ",padding=0.25;" .. @@ -83,13 +83,12 @@ local function get_formspec(tabview, name, tabdata) image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" .. "color,span=1;" .. "text,padding=1]" .. - "table[-0.15,0.6;7.75,5.15;favourites;" + "table[-0.15,0.6;7.75,5.15;favorites;" if menudata.search_result then + local favs = serverlistmgr.get_favorites() for i = 1, #menudata.search_result do - local favs = core.get_favorites("local") local server = menudata.search_result[i] - for fav_id = 1, #favs do if server.address == favs[fav_id].address and server.port == favs[fav_id].port then @@ -103,29 +102,30 @@ local function get_formspec(tabview, name, tabdata) retval = retval .. render_serverlist_row(server, server.is_favorite) end - elseif #menudata.favorites > 0 then - local favs = core.get_favorites("local") + elseif #serverlistmgr.servers > 0 then + local favs = serverlistmgr.get_favorites() if #favs > 0 then for i = 1, #favs do - for j = 1, #menudata.favorites do - if menudata.favorites[j].address == favs[i].address and - menudata.favorites[j].port == favs[i].port then - table.insert(menudata.favorites, i, table.remove(menudata.favorites, j)) + for j = 1, #serverlistmgr.servers do + if serverlistmgr.servers[j].address == favs[i].address and + serverlistmgr.servers[j].port == favs[i].port then + table.insert(serverlistmgr.servers, i, table.remove(serverlistmgr.servers, j)) + end end - end - if favs[i].address ~= menudata.favorites[i].address then - table.insert(menudata.favorites, i, favs[i]) + if favs[i].address ~= serverlistmgr.servers[i].address then + table.insert(serverlistmgr.servers, i, favs[i]) end end end - retval = retval .. render_serverlist_row(menudata.favorites[1], (#favs > 0)) - for i = 2, #menudata.favorites do - retval = retval .. "," .. render_serverlist_row(menudata.favorites[i], (i <= #favs)) + + retval = retval .. render_serverlist_row(serverlistmgr.servers[1], (#favs > 0)) + for i = 2, #serverlistmgr.servers do + retval = retval .. "," .. render_serverlist_row(serverlistmgr.servers[i], (i <= #favs)) end end - if tabdata.fav_selected then - retval = retval .. ";" .. tabdata.fav_selected .. "]" + if tabdata.selected then + retval = retval .. ";" .. tabdata.selected .. "]" else retval = retval .. ";0]" end @@ -135,21 +135,20 @@ end -------------------------------------------------------------------------------- local function main_button_handler(tabview, fields, name, tabdata) - local serverlist = menudata.search_result or menudata.favorites + local serverlist = menudata.search_result or serverlistmgr.servers if fields.te_name then gamedata.playername = fields.te_name core.settings:set("name", fields.te_name) end - if fields.favourites then - local event = core.explode_table_event(fields.favourites) + if fields.favorites then + local event = core.explode_table_event(fields.favorites) local fav = serverlist[event.row] if event.type == "DCL" then if event.row <= #serverlist then - if menudata.favorites_is_public and - not is_server_protocol_compat_or_error( + if not is_server_protocol_compat_or_error( fav.proto_min, fav.proto_max) then return true end @@ -178,7 +177,7 @@ local function main_button_handler(tabview, fields, name, tabdata) if event.type == "CHG" then if event.row <= #serverlist then gamedata.fav = false - local favs = core.get_favorites("local") + local favs = serverlistmgr.get_favorites() local address = fav.address local port = fav.port gamedata.serverdescription = fav.description @@ -194,28 +193,28 @@ local function main_button_handler(tabview, fields, name, tabdata) core.settings:set("address", address) core.settings:set("remote_port", port) end - tabdata.fav_selected = event.row + tabdata.selected = event.row end return true end end if fields.key_up or fields.key_down then - local fav_idx = core.get_table_index("favourites") + local fav_idx = core.get_table_index("favorites") local fav = serverlist[fav_idx] if fav_idx then if fields.key_up and fav_idx > 1 then fav_idx = fav_idx - 1 - elseif fields.key_down and fav_idx < #menudata.favorites then + elseif fields.key_down and fav_idx < #serverlistmgr.servers then fav_idx = fav_idx + 1 end else fav_idx = 1 end - if not menudata.favorites or not fav then - tabdata.fav_selected = 0 + if not serverlistmgr.servers or not fav then + tabdata.selected = 0 return true end @@ -227,17 +226,17 @@ local function main_button_handler(tabview, fields, name, tabdata) core.settings:set("remote_port", port) end - tabdata.fav_selected = fav_idx + tabdata.selected = fav_idx return true end if fields.btn_delete_favorite then - local current_favourite = core.get_table_index("favourites") - if not current_favourite then return end + local current_favorite = core.get_table_index("favorites") + if not current_favorite then return end - core.delete_favorite(current_favourite) - asyncOnlineFavourites() - tabdata.fav_selected = nil + serverlistmgr.delete_favorite(serverlistmgr.servers[current_favorite]) + serverlistmgr.sync() + tabdata.selected = nil core.settings:set("address", "") core.settings:set("remote_port", "30000") @@ -251,11 +250,11 @@ local function main_button_handler(tabview, fields, name, tabdata) end if fields.btn_mp_search or fields.key_enter_field == "te_search" then - tabdata.fav_selected = 1 + tabdata.selected = 1 local input = fields.te_search:lower() tabdata.search_for = fields.te_search - if #menudata.favorites < 2 then + if #serverlistmgr.servers < 2 then return true end @@ -275,8 +274,8 @@ local function main_button_handler(tabview, fields, name, tabdata) -- Search the serverlist local search_result = {} - for i = 1, #menudata.favorites do - local server = menudata.favorites[i] + for i = 1, #serverlistmgr.servers do + local server = serverlistmgr.servers[i] local found = 0 for k = 1, #keywords do local keyword = keywords[k] @@ -293,7 +292,7 @@ local function main_button_handler(tabview, fields, name, tabdata) end end if found > 0 then - local points = (#menudata.favorites - i) / 5 + found + local points = (#serverlistmgr.servers - i) / 5 + found server.points = points table.insert(search_result, server) end @@ -312,7 +311,7 @@ local function main_button_handler(tabview, fields, name, tabdata) end if fields.btn_mp_refresh then - asyncOnlineFavourites() + serverlistmgr.sync() return true end @@ -321,30 +320,36 @@ local function main_button_handler(tabview, fields, name, tabdata) gamedata.playername = fields.te_name gamedata.password = fields.te_pwd gamedata.address = fields.te_address - gamedata.port = fields.te_port + gamedata.port = tonumber(fields.te_port) gamedata.selected_world = 0 - local fav_idx = core.get_table_index("favourites") + local fav_idx = core.get_table_index("favorites") local fav = serverlist[fav_idx] if fav_idx and fav_idx <= #serverlist and - fav.address == fields.te_address and - fav.port == fields.te_port then + fav.address == gamedata.address and + fav.port == gamedata.port then + + serverlistmgr.add_favorite(fav) gamedata.servername = fav.name gamedata.serverdescription = fav.description - if menudata.favorites_is_public and - not is_server_protocol_compat_or_error( + if not is_server_protocol_compat_or_error( fav.proto_min, fav.proto_max) then return true end else gamedata.servername = "" gamedata.serverdescription = "" + + serverlistmgr.add_favorite({ + address = gamedata.address, + port = gamedata.port, + }) end - core.settings:set("address", fields.te_address) - core.settings:set("remote_port", fields.te_port) + core.settings:set("address", gamedata.address) + core.settings:set("remote_port", gamedata.port) core.start() return true @@ -354,7 +359,7 @@ end local function on_change(type, old_tab, new_tab) if type == "LEAVE" then return end - asyncOnlineFavourites() + serverlistmgr.sync() end -------------------------------------------------------------------------------- diff --git a/builtin/mainmenu/tests/favorites_wellformed.txt b/builtin/mainmenu/tests/favorites_wellformed.txt new file mode 100644 index 000000000..8b87b4398 --- /dev/null +++ b/builtin/mainmenu/tests/favorites_wellformed.txt @@ -0,0 +1,29 @@ +[server] + +127.0.0.1 +30000 + + +[server] + +localhost +30000 + + +[server] + +vps.rubenwardy.com +30001 + + +[server] + +gundul.ddnss.de +39155 + + +[server] +VanessaE's Dreambuilder creative Server +daconcepts.com +30000 +VanessaE's Dreambuilder creative-mode server. Lots of mods, whitelisted buckets. diff --git a/builtin/mainmenu/tests/serverlistmgr_spec.lua b/builtin/mainmenu/tests/serverlistmgr_spec.lua new file mode 100644 index 000000000..148e9b794 --- /dev/null +++ b/builtin/mainmenu/tests/serverlistmgr_spec.lua @@ -0,0 +1,36 @@ +_G.core = {} +_G.unpack = table.unpack +_G.serverlistmgr = {} + +dofile("builtin/common/misc_helpers.lua") +dofile("builtin/mainmenu/serverlistmgr.lua") + +local base = "builtin/mainmenu/tests/" + +describe("legacy favorites", function() + it("loads well-formed correctly", function() + local favs = serverlistmgr.read_legacy_favorites(base .. "favorites_wellformed.txt") + + local expected = { + { + address = "127.0.0.1", + port = 30000, + }, + + { address = "localhost", port = 30000 }, + + { address = "vps.rubenwardy.com", port = 30001 }, + + { address = "gundul.ddnss.de", port = 39155 }, + + { + address = "daconcepts.com", + port = 30000, + name = "VanessaE's Dreambuilder creative Server", + description = "VanessaE's Dreambuilder creative-mode server. Lots of mods, whitelisted buckets." + }, + } + + assert.same(expected, favs) + end) +end) diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 7e23b5641..21118134e 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -964,7 +964,7 @@ serverlist_url (Serverlist URL) string servers.minetest.net # File in client/serverlist/ that contains your favorite servers displayed in the # Multiplayer Tab. -serverlist_file (Serverlist file) string favoriteservers.txt +serverlist_file (Serverlist file) string favoriteservers.json # Maximum size of the out chat queue. # 0 to disable queueing and -1 to make the queue size unlimited. diff --git a/doc/menu_lua_api.txt b/doc/menu_lua_api.txt index 1bcf697e9..db49c1736 100644 --- a/doc/menu_lua_api.txt +++ b/doc/menu_lua_api.txt @@ -253,32 +253,6 @@ Package - content which is downloadable from the content db, may or may not be i } -Favorites ---------- - -core.get_favorites(location) -> list of favorites (possible in async calls) -^ location: "local" or "online" -^ returns { - [1] = { - clients = , - clients_max = , - version = , - password = , - creative = , - damage = , - pvp = , - description = , - name = , - address =
, - port = - clients_list = - mods = - }, - ... -} -core.delete_favorite(id, location) -> success - - Logging ------- diff --git a/minetest.conf.example b/minetest.conf.example index 086339037..3bb357813 100644 --- a/minetest.conf.example +++ b/minetest.conf.example @@ -1160,7 +1160,7 @@ # File in client/serverlist/ that contains your favorite servers displayed in the # Multiplayer Tab. # type: string -# serverlist_file = favoriteservers.txt +# serverlist_file = favoriteservers.json # Maximum size of the out chat queue. # 0 to disable queueing and -1 to make the queue size unlimited. diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index 29427f609..7245f29f0 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -487,14 +487,6 @@ bool ClientLauncher::launch_game(std::string &error_message, start_data.socket_port = myrand_range(49152, 65535); } else { g_settings->set("name", start_data.name); - if (!start_data.address.empty()) { - ServerListSpec server; - server["name"] = server_name; - server["address"] = start_data.address; - server["port"] = itos(start_data.socket_port); - server["description"] = server_description; - ServerList::insert(server); - } } if (start_data.name.length() > PLAYERNAME_SIZE - 1) { diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index e8fb18e05..114351d86 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -283,7 +283,7 @@ void set_default_settings(Settings *settings) // Main menu settings->setDefault("main_menu_path", ""); - settings->setDefault("serverlist_file", "favoriteservers.txt"); + settings->setDefault("serverlist_file", "favoriteservers.json"); #if USE_FREETYPE settings->setDefault("freetype", "true"); diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 5070ec7d4..4733c4003 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -274,207 +274,6 @@ int ModApiMainMenu::l_get_worlds(lua_State *L) return 1; } -/******************************************************************************/ -int ModApiMainMenu::l_get_favorites(lua_State *L) -{ - std::string listtype = "local"; - - if (!lua_isnone(L, 1)) { - listtype = luaL_checkstring(L, 1); - } - - std::vector 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 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) { @@ -1130,11 +929,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); @@ -1170,7 +967,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); @@ -1186,5 +982,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..580a0df72 100644 --- a/src/script/lua_api/l_mainmenu.h +++ b/src/script/lua_api/l_mainmenu.h @@ -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 diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 6490eb578..203a0dd28 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -250,6 +250,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) { @@ -486,6 +497,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); @@ -539,6 +551,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); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index b6c1b58af..dbdd62b99 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); diff --git a/src/serverlist.cpp b/src/serverlist.cpp index 80a8c2f1a..3bcab3d58 100644 --- a/src/serverlist.cpp +++ b/src/serverlist.cpp @@ -17,181 +17,19 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include #include -#include -#include - #include "version.h" #include "settings.h" #include "serverlist.h" #include "filesys.h" -#include "porting.h" #include "log.h" #include "network/networkprotocol.h" #include #include "convert_json.h" #include "httpfetch.h" -#include "util/string.h" namespace ServerList { - -std::string getFilePath() -{ - std::string serverlist_file = g_settings->get("serverlist_file"); - - std::string dir_path = "client" DIR_DELIM "serverlist" DIR_DELIM; - fs::CreateDir(porting::path_user + DIR_DELIM "client"); - fs::CreateDir(porting::path_user + DIR_DELIM + dir_path); - return porting::path_user + DIR_DELIM + dir_path + serverlist_file; -} - - -std::vector getLocal() -{ - std::string path = ServerList::getFilePath(); - std::string liststring; - fs::ReadFile(path, liststring); - - return deSerialize(liststring); -} - - -std::vector getOnline() -{ - std::ostringstream geturl; - - u16 proto_version_min = CLIENT_PROTOCOL_VERSION_MIN; - - geturl << g_settings->get("serverlist_url") << - "/list?proto_version_min=" << proto_version_min << - "&proto_version_max=" << CLIENT_PROTOCOL_VERSION_MAX; - Json::Value root = fetchJsonValue(geturl.str(), NULL); - - std::vector server_list; - - if (!root.isObject()) { - return server_list; - } - - root = root["list"]; - if (!root.isArray()) { - return server_list; - } - - for (const Json::Value &i : root) { - if (i.isObject()) { - server_list.push_back(i); - } - } - - return server_list; -} - - -// Delete a server from the local favorites list -bool deleteEntry(const ServerListSpec &server) -{ - std::vector serverlist = ServerList::getLocal(); - for (std::vector::iterator it = serverlist.begin(); - it != serverlist.end();) { - if ((*it)["address"] == server["address"] && - (*it)["port"] == server["port"]) { - it = serverlist.erase(it); - } else { - ++it; - } - } - - std::string path = ServerList::getFilePath(); - std::ostringstream ss(std::ios_base::binary); - ss << ServerList::serialize(serverlist); - if (!fs::safeWriteToFile(path, ss.str())) - return false; - return true; -} - -// Insert a server to the local favorites list -bool insert(const ServerListSpec &server) -{ - // Remove duplicates - ServerList::deleteEntry(server); - - std::vector serverlist = ServerList::getLocal(); - - // Insert new server at the top of the list - serverlist.insert(serverlist.begin(), server); - - std::string path = ServerList::getFilePath(); - std::ostringstream ss(std::ios_base::binary); - ss << ServerList::serialize(serverlist); - if (!fs::safeWriteToFile(path, ss.str())) - return false; - - return true; -} - -std::vector deSerialize(const std::string &liststring) -{ - std::vector serverlist; - std::istringstream stream(liststring); - std::string line, tmp; - while (std::getline(stream, line)) { - std::transform(line.begin(), line.end(), line.begin(), ::toupper); - if (line == "[SERVER]") { - ServerListSpec server; - std::getline(stream, tmp); - server["name"] = tmp; - std::getline(stream, tmp); - server["address"] = tmp; - std::getline(stream, tmp); - server["port"] = tmp; - bool unique = true; - for (const ServerListSpec &added : serverlist) { - if (server["name"] == added["name"] - && server["port"] == added["port"]) { - unique = false; - break; - } - } - if (!unique) - continue; - std::getline(stream, tmp); - server["description"] = tmp; - serverlist.push_back(server); - } - } - return serverlist; -} - -const std::string serialize(const std::vector &serverlist) -{ - std::string liststring; - for (const ServerListSpec &it : serverlist) { - liststring += "[server]\n"; - liststring += it["name"].asString() + '\n'; - liststring += it["address"].asString() + '\n'; - liststring += it["port"].asString() + '\n'; - liststring += it["description"].asString() + '\n'; - liststring += '\n'; - } - return liststring; -} - -const std::string serializeJson(const std::vector &serverlist) -{ - Json::Value root; - Json::Value list(Json::arrayValue); - for (const ServerListSpec &it : serverlist) { - list.append(it); - } - root["list"] = list; - - return fastWriteJson(root); -} - - #if USE_CURL void sendAnnounce(AnnounceAction action, const u16 port, diff --git a/src/serverlist.h b/src/serverlist.h index 2b82b7431..4a0bd5efa 100644 --- a/src/serverlist.h +++ b/src/serverlist.h @@ -24,21 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #pragma once -typedef Json::Value ServerListSpec; - namespace ServerList { -std::vector getLocal(); -std::vector getOnline(); - -bool deleteEntry(const ServerListSpec &server); -bool insert(const ServerListSpec &server); - -std::vector deSerialize(const std::string &liststring); -const std::string serialize(const std::vector &serverlist); -std::vector deSerializeJson(const std::string &liststring); -const std::string serializeJson(const std::vector &serverlist); - #if USE_CURL enum AnnounceAction {AA_START, AA_UPDATE, AA_DELETE}; void sendAnnounce(AnnounceAction, u16 port, -- cgit v1.2.3 From 37a05ec8d6cbf9ff4432225cffe78c16fdd0647d Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sun, 22 Nov 2020 17:49:30 +0100 Subject: Settings: Proper priority hierarchy Remove old defaults system Introduce priority-based fallback list Use new functions for map_meta special functions Change groups to use end tags Unittest changes: * Adapt unittest to the new code * Compare Settings objects --- src/content/subgames.cpp | 24 +-- src/database/database-files.cpp | 15 +- src/database/database-files.h | 4 +- src/defaultsettings.cpp | 4 +- src/defaultsettings.h | 9 +- src/gui/guiKeyChangeMenu.cpp | 2 +- src/main.cpp | 6 +- src/map.cpp | 2 +- src/map_settings_manager.cpp | 67 +++---- src/map_settings_manager.h | 5 +- src/remoteplayer.h | 1 - src/script/lua_api/l_mapgen.cpp | 2 +- src/script/lua_api/l_settings.cpp | 2 +- src/script/scripting_mainmenu.cpp | 2 +- src/server.cpp | 3 + src/server.h | 1 + src/serverenvironment.cpp | 7 +- src/settings.cpp | 300 ++++++++++++++--------------- src/settings.h | 43 +++-- src/unittest/test_map_settings_manager.cpp | 86 ++++++--- src/unittest/test_settings.cpp | 73 +++++-- 21 files changed, 359 insertions(+), 299 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/content/subgames.cpp b/src/content/subgames.cpp index c6350f2dd..e9dc609b0 100644 --- a/src/content/subgames.cpp +++ b/src/content/subgames.cpp @@ -329,18 +329,16 @@ void loadGameConfAndInitWorld(const std::string &path, const std::string &name, } } - // Override defaults with those provided by the game. - // We clear and reload the defaults because the defaults - // might have been overridden by other subgame config - // files that were loaded before. - g_settings->clearDefaults(); - set_default_settings(g_settings); - - Settings game_defaults; - getGameMinetestConfig(gamespec.path, game_defaults); - game_defaults.removeSecureSettings(); + Settings *game_settings = Settings::getLayer(SL_GAME); + const bool new_game_settings = (game_settings == nullptr); + if (new_game_settings) { + // Called by main-menu without a Server instance running + // -> create and free manually + game_settings = Settings::createLayer(SL_GAME); + } - g_settings->overrideDefaults(&game_defaults); + getGameMinetestConfig(gamespec.path, *game_settings); + game_settings->removeSecureSettings(); infostream << "Initializing world at " << final_path << std::endl; @@ -381,4 +379,8 @@ void loadGameConfAndInitWorld(const std::string &path, const std::string &name, fs::safeWriteToFile(map_meta_path, oss.str()); } + + // The Settings object is no longer needed for created worlds + if (new_game_settings) + delete game_settings; } diff --git a/src/database/database-files.cpp b/src/database/database-files.cpp index 529fb8763..d9e8f24ea 100644 --- a/src/database/database-files.cpp +++ b/src/database/database-files.cpp @@ -122,18 +122,17 @@ void PlayerDatabaseFiles::serialize(RemotePlayer *p, std::ostream &os) args.set("name", p->m_name); // This should not happen - assert(m_sao); - args.setU16("hp", p->m_sao->getHP()); - args.setV3F("position", p->m_sao->getBasePosition()); - args.setFloat("pitch", p->m_sao->getLookPitch()); - args.setFloat("yaw", p->m_sao->getRotation().Y); - args.setU16("breath", p->m_sao->getBreath()); + PlayerSAO *sao = p->getPlayerSAO(); + assert(sao); + args.setU16("hp", sao->getHP()); + args.setV3F("position", sao->getBasePosition()); + args.setFloat("pitch", sao->getLookPitch()); + args.setFloat("yaw", sao->getRotation().Y); + args.setU16("breath", sao->getBreath()); std::string extended_attrs; { // serializeExtraAttributes - PlayerSAO *sao = p->getPlayerSAO(); - assert(sao); Json::Value json_root; const StringMap &attrs = sao->getMeta().getStrings(); diff --git a/src/database/database-files.h b/src/database/database-files.h index a041cb1ff..e647a2e24 100644 --- a/src/database/database-files.h +++ b/src/database/database-files.h @@ -38,8 +38,8 @@ public: void listPlayers(std::vector &res); private: - void deSerialize(RemotePlayer *p, std::istream &is, - const std::string &playername, PlayerSAO *sao); + void deSerialize(RemotePlayer *p, std::istream &is, const std::string &playername, + PlayerSAO *sao); /* serialize() writes a bunch of text that can contain any characters except a '\0', and such an ending that diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index 114351d86..d34ec324b 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -27,8 +27,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen/mapgen.h" // Mapgen::setDefaultSettings #include "util/string.h" -void set_default_settings(Settings *settings) +void set_default_settings() { + Settings *settings = Settings::createLayer(SL_DEFAULTS); + // Client and server settings->setDefault("language", ""); settings->setDefault("name", ""); diff --git a/src/defaultsettings.h b/src/defaultsettings.h index c81e88502..c239b3c12 100644 --- a/src/defaultsettings.h +++ b/src/defaultsettings.h @@ -25,11 +25,4 @@ class Settings; * initialize basic default settings * @param settings pointer to settings */ -void set_default_settings(Settings *settings); - -/** - * override a default settings by settings from another settings element - * @param settings target settings pointer - * @param from source settings pointer - */ -void override_default_settings(Settings *settings, Settings *from); +void set_default_settings(); diff --git a/src/gui/guiKeyChangeMenu.cpp b/src/gui/guiKeyChangeMenu.cpp index eb641d952..4dcb47779 100644 --- a/src/gui/guiKeyChangeMenu.cpp +++ b/src/gui/guiKeyChangeMenu.cpp @@ -248,7 +248,7 @@ bool GUIKeyChangeMenu::acceptInput() { for (key_setting *k : key_settings) { std::string default_key; - g_settings->getDefaultNoEx(k->setting_name, default_key); + Settings::getLayer(SL_DEFAULTS)->getNoEx(k->setting_name, default_key); if (k->key.sym() != default_key) g_settings->set(k->setting_name, k->key.sym()); diff --git a/src/main.cpp b/src/main.cpp index f7238176b..57768dbb2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -487,12 +487,15 @@ static bool create_userdata_path() static bool init_common(const Settings &cmd_args, int argc, char *argv[]) { startup_message(); - set_default_settings(g_settings); + set_default_settings(); // Initialize sockets sockets_init(); atexit(sockets_cleanup); + // Initialize g_settings + Settings::createLayer(SL_GLOBAL); + if (!read_config_file(cmd_args)) return false; @@ -524,6 +527,7 @@ static bool read_config_file(const Settings &cmd_args) // Path of configuration file in use sanity_check(g_settings_path == ""); // Sanity check + if (cmd_args.exists("config")) { bool r = g_settings->readConfigFile(cmd_args.get("config").c_str()); if (!r) { diff --git a/src/map.cpp b/src/map.cpp index aff545921..7c59edbaa 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -1184,7 +1184,7 @@ bool Map::isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes) ServerMap::ServerMap(const std::string &savedir, IGameDef *gamedef, EmergeManager *emerge, MetricsBackend *mb): Map(gamedef), - settings_mgr(g_settings, savedir + DIR_DELIM + "map_meta.txt"), + settings_mgr(savedir + DIR_DELIM + "map_meta.txt"), m_emerge(emerge) { verbosestream<overrideDefaults(user_settings); + m_map_settings = Settings::createLayer(SL_MAP, "[end_of_params]"); + Mapgen::setDefaultSettings(Settings::getLayer(SL_DEFAULTS)); } @@ -49,22 +43,23 @@ MapSettingsManager::~MapSettingsManager() bool MapSettingsManager::getMapSetting( const std::string &name, std::string *value_out) { + // Get from map_meta.txt, then try from all other sources if (m_map_settings->getNoEx(name, *value_out)) return true; // Compatibility kludge - if (m_user_settings == g_settings && name == "seed") - return m_user_settings->getNoEx("fixed_map_seed", *value_out); + if (name == "seed") + return Settings::getLayer(SL_GLOBAL)->getNoEx("fixed_map_seed", *value_out); - return m_user_settings->getNoEx(name, *value_out); + return false; } bool MapSettingsManager::getMapSettingNoiseParams( const std::string &name, NoiseParams *value_out) { - return m_map_settings->getNoiseParams(name, *value_out) || - m_user_settings->getNoiseParams(name, *value_out); + // TODO: Rename to "getNoiseParams" + return m_map_settings->getNoiseParams(name, *value_out); } @@ -77,7 +72,7 @@ bool MapSettingsManager::setMapSetting( if (override_meta) m_map_settings->set(name, value); else - m_map_settings->setDefault(name, value); + Settings::getLayer(SL_GLOBAL)->set(name, value); return true; } @@ -89,7 +84,11 @@ bool MapSettingsManager::setMapSettingNoiseParams( if (mapgen_params) return false; - m_map_settings->setNoiseParams(name, *value, !override_meta); + if (override_meta) + m_map_settings->setNoiseParams(name, *value); + else + Settings::getLayer(SL_GLOBAL)->setNoiseParams(name, *value); + return true; } @@ -104,8 +103,8 @@ bool MapSettingsManager::loadMapMeta() return false; } - if (!m_map_settings->parseConfigLines(is, "[end_of_params]")) { - errorstream << "loadMapMeta: [end_of_params] not found!" << std::endl; + if (!m_map_settings->parseConfigLines(is)) { + errorstream << "loadMapMeta: Format error. '[end_of_params]' missing?" << std::endl; return false; } @@ -116,28 +115,22 @@ bool MapSettingsManager::loadMapMeta() bool MapSettingsManager::saveMapMeta() { // If mapgen params haven't been created yet; abort - if (!mapgen_params) + if (!mapgen_params) { + errorstream << "saveMapMeta: mapgen_params not present!" << std::endl; return false; + } + // Paths set up by subgames.cpp, but not in unittests if (!fs::CreateAllDirs(fs::RemoveLastPathComponent(m_map_meta_path))) { errorstream << "saveMapMeta: could not create dirs to " << m_map_meta_path; return false; } - std::ostringstream oss(std::ios_base::binary); - Settings conf; + mapgen_params->MapgenParams::writeParams(m_map_settings); + mapgen_params->writeParams(m_map_settings); - mapgen_params->MapgenParams::writeParams(&conf); - mapgen_params->writeParams(&conf); - conf.writeLines(oss); - - // NOTE: If there are ever types of map settings other than - // those relating to map generation, save them here - - oss << "[end_of_params]\n"; - - if (!fs::safeWriteToFile(m_map_meta_path, oss.str())) { + if (!m_map_settings->updateConfigFile(m_map_meta_path.c_str())) { errorstream << "saveMapMeta: could not write " << m_map_meta_path << std::endl; return false; @@ -152,23 +145,21 @@ MapgenParams *MapSettingsManager::makeMapgenParams() if (mapgen_params) return mapgen_params; - assert(m_user_settings != NULL); assert(m_map_settings != NULL); // At this point, we have (in order of precedence): - // 1). m_mapgen_settings->m_settings containing map_meta.txt settings or + // 1). SL_MAP containing map_meta.txt settings or // explicit overrides from scripts - // 2). m_mapgen_settings->m_defaults containing script-set mgparams without - // overrides - // 3). g_settings->m_settings containing all user-specified config file + // 2). SL_GLOBAL containing all user-specified config file // settings - // 4). g_settings->m_defaults containing any low-priority settings from + // 3). SL_DEFAULTS containing any low-priority settings from // scripts, e.g. mods using Lua as an enhanced config file) // Now, get the mapgen type so we can create the appropriate MapgenParams std::string mg_name; MapgenType mgtype = getMapSetting("mg_name", &mg_name) ? Mapgen::getMapgenType(mg_name) : MAPGEN_DEFAULT; + if (mgtype == MAPGEN_INVALID) { errorstream << "EmergeManager: mapgen '" << mg_name << "' not valid; falling back to " << diff --git a/src/map_settings_manager.h b/src/map_settings_manager.h index 5baa38455..9258d3032 100644 --- a/src/map_settings_manager.h +++ b/src/map_settings_manager.h @@ -44,8 +44,7 @@ struct MapgenParams; */ class MapSettingsManager { public: - MapSettingsManager(Settings *user_settings, - const std::string &map_meta_path); + MapSettingsManager(const std::string &map_meta_path); ~MapSettingsManager(); // Finalized map generation parameters @@ -71,6 +70,6 @@ public: private: std::string m_map_meta_path; + // TODO: Rename to "m_settings" Settings *m_map_settings; - Settings *m_user_settings; }; diff --git a/src/remoteplayer.h b/src/remoteplayer.h index b82cbc08e..8d086fc5a 100644 --- a/src/remoteplayer.h +++ b/src/remoteplayer.h @@ -140,7 +140,6 @@ public: void onSuccessfulSave(); private: - PlayerSAO *m_sao = nullptr; bool m_dirty = false; diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index fad08e1f6..183f20540 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -982,7 +982,7 @@ int ModApiMapgen::l_set_noiseparams(lua_State *L) bool set_default = !lua_isboolean(L, 3) || readParam(L, 3); - g_settings->setNoiseParams(name, np, set_default); + Settings::getLayer(set_default ? SL_DEFAULTS : SL_GLOBAL)->setNoiseParams(name, np); return 0; } diff --git a/src/script/lua_api/l_settings.cpp b/src/script/lua_api/l_settings.cpp index 33eb02392..bcbaf15fa 100644 --- a/src/script/lua_api/l_settings.cpp +++ b/src/script/lua_api/l_settings.cpp @@ -197,7 +197,7 @@ int LuaSettings::l_set_np_group(lua_State *L) SET_SECURITY_CHECK(L, key); - o->m_settings->setNoiseParams(key, value, false); + o->m_settings->setNoiseParams(key, value); return 0; } diff --git a/src/script/scripting_mainmenu.cpp b/src/script/scripting_mainmenu.cpp index 0f672f917..9b377366e 100644 --- a/src/script/scripting_mainmenu.cpp +++ b/src/script/scripting_mainmenu.cpp @@ -31,7 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., extern "C" { #include "lualib.h" } - +#include "settings.h" #define MAINMENU_NUM_ASYNC_THREADS 4 diff --git a/src/server.cpp b/src/server.cpp index b5352749c..aba7b6401 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -351,6 +351,7 @@ Server::~Server() // Deinitialize scripting infostream << "Server: Deinitializing scripting" << std::endl; delete m_script; + delete m_game_settings; while (!m_unsent_map_edit_queue.empty()) { delete m_unsent_map_edit_queue.front(); @@ -368,6 +369,8 @@ void Server::init() infostream << "- world: " << m_path_world << std::endl; infostream << "- game: " << m_gamespec.path << std::endl; + m_game_settings = Settings::createLayer(SL_GAME); + // Create world if it doesn't exist try { loadGameConfAndInitWorld(m_path_world, diff --git a/src/server.h b/src/server.h index a7e85d0e1..1dd181794 100644 --- a/src/server.h +++ b/src/server.h @@ -524,6 +524,7 @@ private: u16 m_max_chatmessage_length; // For "dedicated" server list flag bool m_dedicated; + Settings *m_game_settings = nullptr; // Thread can set; step() will throw as ServerError MutexedVariable m_async_fatal_error; diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 56dbb0632..3d9ba132b 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -632,7 +632,7 @@ void ServerEnvironment::saveMeta() // Open file and serialize std::ostringstream ss(std::ios_base::binary); - Settings args; + Settings args("EnvArgsEnd"); args.setU64("game_time", m_game_time); args.setU64("time_of_day", getTimeOfDay()); args.setU64("last_clear_objects_time", m_last_clear_objects_time); @@ -641,7 +641,6 @@ void ServerEnvironment::saveMeta() m_lbm_mgr.createIntroductionTimesString()); args.setU64("day_count", m_day_count); args.writeLines(ss); - ss<<"EnvArgsEnd\n"; if(!fs::safeWriteToFile(path, ss.str())) { @@ -676,9 +675,9 @@ void ServerEnvironment::loadMeta() throw SerializationError("Couldn't load env meta"); } - Settings args; + Settings args("EnvArgsEnd"); - if (!args.parseConfigLines(is, "EnvArgsEnd")) { + if (!args.parseConfigLines(is)) { throw SerializationError("ServerEnvironment::loadMeta(): " "EnvArgsEnd not found!"); } diff --git a/src/settings.cpp b/src/settings.cpp index f30ef34e9..cf2a16aa6 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -33,27 +33,50 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include -static Settings main_settings; -Settings *g_settings = &main_settings; +Settings *g_settings = nullptr; std::string g_settings_path; -Settings::~Settings() +Settings *Settings::s_layers[SL_TOTAL_COUNT] = {0}; // Zeroed by compiler +std::unordered_map Settings::s_flags; + + +Settings *Settings::createLayer(SettingsLayer sl, const std::string &end_tag) { - clear(); + if ((int)sl < 0 || sl >= SL_TOTAL_COUNT) + throw new BaseException("Invalid settings layer"); + + Settings *&pos = s_layers[(size_t)sl]; + if (pos) + throw new BaseException("Setting layer " + std::to_string(sl) + " already exists"); + + pos = new Settings(end_tag); + pos->m_settingslayer = sl; + + if (sl == SL_GLOBAL) + g_settings = pos; + return pos; } -Settings & Settings::operator += (const Settings &other) +Settings *Settings::getLayer(SettingsLayer sl) { - if (&other == this) - return *this; + sanity_check((int)sl >= 0 && sl < SL_TOTAL_COUNT); + return s_layers[(size_t)sl]; +} + +Settings::~Settings() +{ MutexAutoLock lock(m_mutex); - MutexAutoLock lock2(other.m_mutex); - updateNoLock(other); + if (m_settingslayer < SL_TOTAL_COUNT) + s_layers[(size_t)m_settingslayer] = nullptr; - return *this; + // Compatibility + if (m_settingslayer == SL_GLOBAL) + g_settings = nullptr; + + clearNoLock(); } @@ -62,11 +85,15 @@ Settings & Settings::operator = (const Settings &other) if (&other == this) return *this; + FATAL_ERROR_IF(m_settingslayer != SL_TOTAL_COUNT && other.m_settingslayer != SL_TOTAL_COUNT, + ("Tried to copy unique Setting layer " + std::to_string(m_settingslayer)).c_str()); + MutexAutoLock lock(m_mutex); MutexAutoLock lock2(other.m_mutex); clearNoLock(); - updateNoLock(other); + m_settings = other.m_settings; + m_callbacks = other.m_callbacks; return *this; } @@ -130,11 +157,11 @@ bool Settings::readConfigFile(const char *filename) if (!is.good()) return false; - return parseConfigLines(is, ""); + return parseConfigLines(is); } -bool Settings::parseConfigLines(std::istream &is, const std::string &end) +bool Settings::parseConfigLines(std::istream &is) { MutexAutoLock lock(m_mutex); @@ -142,7 +169,7 @@ bool Settings::parseConfigLines(std::istream &is, const std::string &end) while (is.good()) { std::getline(is, line); - SettingsParseEvent event = parseConfigObject(line, end, name, value); + SettingsParseEvent event = parseConfigObject(line, name, value); switch (event) { case SPE_NONE: @@ -155,8 +182,8 @@ bool Settings::parseConfigLines(std::istream &is, const std::string &end) case SPE_END: return true; case SPE_GROUP: { - Settings *group = new Settings; - if (!group->parseConfigLines(is, "}")) { + Settings *group = new Settings("}"); + if (!group->parseConfigLines(is)) { delete group; return false; } @@ -169,7 +196,8 @@ bool Settings::parseConfigLines(std::istream &is, const std::string &end) } } - return end.empty(); + // false (failure) if end tag not found + return m_end_tag.empty(); } @@ -179,6 +207,13 @@ void Settings::writeLines(std::ostream &os, u32 tab_depth) const for (const auto &setting_it : m_settings) printEntry(os, setting_it.first, setting_it.second, tab_depth); + + if (!m_end_tag.empty()) { + for (u32 i = 0; i < tab_depth; i++) + os << "\t"; + + os << m_end_tag << "\n"; + } } @@ -193,9 +228,7 @@ void Settings::printEntry(std::ostream &os, const std::string &name, entry.group->writeLines(os, tab_depth + 1); - for (u32 i = 0; i != tab_depth; i++) - os << "\t"; - os << "}\n"; + // Closing bracket handled by writeLines } else { os << name << " = "; @@ -207,8 +240,7 @@ void Settings::printEntry(std::ostream &os, const std::string &name, } -bool Settings::updateConfigObject(std::istream &is, std::ostream &os, - const std::string &end, u32 tab_depth) +bool Settings::updateConfigObject(std::istream &is, std::ostream &os, u32 tab_depth) { SettingEntries::const_iterator it; std::set present_entries; @@ -220,11 +252,11 @@ bool Settings::updateConfigObject(std::istream &is, std::ostream &os, // in the object if existing while (is.good() && !end_found) { std::getline(is, line); - SettingsParseEvent event = parseConfigObject(line, end, name, value); + SettingsParseEvent event = parseConfigObject(line, name, value); switch (event) { case SPE_END: - os << line << (is.eof() ? "" : "\n"); + // Skip end tag. Append later. end_found = true; break; case SPE_MULTILINE: @@ -252,14 +284,13 @@ bool Settings::updateConfigObject(std::istream &is, std::ostream &os, if (it != m_settings.end() && it->second.is_group) { os << line << "\n"; sanity_check(it->second.group != NULL); - was_modified |= it->second.group->updateConfigObject(is, os, - "}", tab_depth + 1); + was_modified |= it->second.group->updateConfigObject(is, os, tab_depth + 1); } else if (it == m_settings.end()) { // Remove by skipping was_modified = true; - Settings removed_group; // Move 'is' to group end + Settings removed_group("}"); // Move 'is' to group end std::stringstream ss; - removed_group.updateConfigObject(is, ss, "}", tab_depth + 1); + removed_group.updateConfigObject(is, ss, tab_depth + 1); break; } else { printEntry(os, name, it->second, tab_depth); @@ -273,6 +304,9 @@ bool Settings::updateConfigObject(std::istream &is, std::ostream &os, } } + if (!line.empty() && is.eof()) + os << "\n"; + // Add any settings in the object that don't exist in the config file yet for (it = m_settings.begin(); it != m_settings.end(); ++it) { if (present_entries.find(it->first) != present_entries.end()) @@ -282,6 +316,12 @@ bool Settings::updateConfigObject(std::istream &is, std::ostream &os, was_modified = true; } + // Append ending tag + if (!m_end_tag.empty()) { + os << m_end_tag << "\n"; + was_modified |= !end_found; + } + return was_modified; } @@ -293,7 +333,7 @@ bool Settings::updateConfigFile(const char *filename) std::ifstream is(filename); std::ostringstream os(std::ios_base::binary); - bool was_modified = updateConfigObject(is, os, ""); + bool was_modified = updateConfigObject(is, os); is.close(); if (!was_modified) @@ -366,29 +406,37 @@ bool Settings::parseCommandLine(int argc, char *argv[], * Getters * ***********/ - -const SettingsEntry &Settings::getEntry(const std::string &name) const +Settings *Settings::getParent() const { - MutexAutoLock lock(m_mutex); + // If the Settings object is within the hierarchy structure, + // iterate towards the origin (0) to find the next fallback layer + if (m_settingslayer >= SL_TOTAL_COUNT) + return nullptr; - SettingEntries::const_iterator n; - if ((n = m_settings.find(name)) == m_settings.end()) { - if ((n = m_defaults.find(name)) == m_defaults.end()) - throw SettingNotFoundException("Setting [" + name + "] not found."); + for (int i = (int)m_settingslayer - 1; i >= 0; --i) { + if (s_layers[i]) + return s_layers[i]; } - return n->second; + + // No parent + return nullptr; } -const SettingsEntry &Settings::getEntryDefault(const std::string &name) const +const SettingsEntry &Settings::getEntry(const std::string &name) const { - MutexAutoLock lock(m_mutex); + { + MutexAutoLock lock(m_mutex); - SettingEntries::const_iterator n; - if ((n = m_defaults.find(name)) == m_defaults.end()) { - throw SettingNotFoundException("Setting [" + name + "] not found."); + SettingEntries::const_iterator n; + if ((n = m_settings.find(name)) != m_settings.end()) + return n->second; } - return n->second; + + if (auto parent = getParent()) + return parent->getEntry(name); + + throw SettingNotFoundException("Setting [" + name + "] not found."); } @@ -412,10 +460,15 @@ const std::string &Settings::get(const std::string &name) const const std::string &Settings::getDefault(const std::string &name) const { - const SettingsEntry &entry = getEntryDefault(name); - if (entry.is_group) + const SettingsEntry *entry; + if (auto parent = getParent()) + entry = &parent->getEntry(name); + else + entry = &getEntry(name); // Bottom of the chain + + if (entry->is_group) throw SettingNotFoundException("Setting [" + name + "] is a group."); - return entry.value; + return entry->value; } @@ -491,29 +544,25 @@ u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc, u32 *flagmask) const { u32 flags = 0; - u32 mask_default = 0; - std::string value; // Read default value (if there is any) - if (getDefaultNoEx(name, value)) { - flags = std::isdigit(value[0]) - ? stoi(value) - : readFlagString(value, flagdesc, &mask_default); - } + if (auto parent = getParent()) + flags = parent->getFlagStr(name, flagdesc, flagmask); // Apply custom flags "on top" - value = get(name); - u32 flags_user; - u32 mask_user = U32_MAX; - flags_user = std::isdigit(value[0]) - ? stoi(value) // Override default - : readFlagString(value, flagdesc, &mask_user); - - flags &= ~mask_user; - flags |= flags_user; - - if (flagmask) - *flagmask = mask_default | mask_user; + if (m_settings.find(name) != m_settings.end()) { + std::string value = get(name); + u32 flags_user; + u32 mask_user = U32_MAX; + flags_user = std::isdigit(value[0]) + ? stoi(value) // Override default + : readFlagString(value, flagdesc, &mask_user); + + flags &= ~mask_user; + flags |= flags_user; + if (flagmask) + *flagmask |= mask_user; + } return flags; } @@ -521,7 +570,12 @@ u32 Settings::getFlagStr(const std::string &name, const FlagDesc *flagdesc, bool Settings::getNoiseParams(const std::string &name, NoiseParams &np) const { - return getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np); + if (getNoiseParamsFromGroup(name, np) || getNoiseParamsFromValue(name, np)) + return true; + if (auto parent = getParent()) + return parent->getNoiseParams(name, np); + + return false; } @@ -583,13 +637,18 @@ bool Settings::exists(const std::string &name) const { MutexAutoLock lock(m_mutex); - return (m_settings.find(name) != m_settings.end() || - m_defaults.find(name) != m_defaults.end()); + if (m_settings.find(name) != m_settings.end()) + return true; + if (auto parent = getParent()) + return parent->exists(name); + return false; } std::vector Settings::getNames() const { + MutexAutoLock lock(m_mutex); + std::vector names; for (const auto &settings_it : m_settings) { names.push_back(settings_it.first); @@ -625,17 +684,6 @@ bool Settings::getNoEx(const std::string &name, std::string &val) const } -bool Settings::getDefaultNoEx(const std::string &name, std::string &val) const -{ - try { - val = getDefault(name); - return true; - } catch (SettingNotFoundException &e) { - return false; - } -} - - bool Settings::getFlag(const std::string &name) const { try { @@ -746,24 +794,25 @@ bool Settings::getFlagStrNoEx(const std::string &name, u32 &val, ***********/ bool Settings::setEntry(const std::string &name, const void *data, - bool set_group, bool set_default) + bool set_group) { - Settings *old_group = NULL; - if (!checkNameValid(name)) return false; if (!set_group && !checkValueValid(*(const std::string *)data)) return false; + Settings *old_group = NULL; { MutexAutoLock lock(m_mutex); - SettingsEntry &entry = set_default ? m_defaults[name] : m_settings[name]; + SettingsEntry &entry = m_settings[name]; old_group = entry.group; entry.value = set_group ? "" : *(const std::string *)data; entry.group = set_group ? *(Settings **)data : NULL; entry.is_group = set_group; + if (set_group) + entry.group->m_end_tag = "}"; } delete old_group; @@ -774,7 +823,7 @@ bool Settings::setEntry(const std::string &name, const void *data, bool Settings::set(const std::string &name, const std::string &value) { - if (!setEntry(name, &value, false, false)) + if (!setEntry(name, &value, false)) return false; doCallbacks(name); @@ -782,9 +831,10 @@ bool Settings::set(const std::string &name, const std::string &value) } +// TODO: Remove this function bool Settings::setDefault(const std::string &name, const std::string &value) { - return setEntry(name, &value, false, true); + return getLayer(SL_DEFAULTS)->set(name, value); } @@ -794,17 +844,7 @@ bool Settings::setGroup(const std::string &name, const Settings &group) // avoid double-free by copying the source Settings *copy = new Settings(); *copy = group; - return setEntry(name, ©, true, false); -} - - -bool Settings::setGroupDefault(const std::string &name, const Settings &group) -{ - // Settings must own the group pointer - // avoid double-free by copying the source - Settings *copy = new Settings(); - *copy = group; - return setEntry(name, ©, true, true); + return setEntry(name, ©, true); } @@ -874,8 +914,7 @@ bool Settings::setFlagStr(const std::string &name, u32 flags, } -bool Settings::setNoiseParams(const std::string &name, - const NoiseParams &np, bool set_default) +bool Settings::setNoiseParams(const std::string &name, const NoiseParams &np) { Settings *group = new Settings; @@ -888,7 +927,7 @@ bool Settings::setNoiseParams(const std::string &name, group->setFloat("lacunarity", np.lacunarity); group->setFlagStr("flags", np.flags, flagdesc_noiseparams, np.flags); - return setEntry(name, &group, true, set_default); + return setEntry(name, &group, true); } @@ -912,20 +951,8 @@ bool Settings::remove(const std::string &name) } -void Settings::clear() -{ - MutexAutoLock lock(m_mutex); - clearNoLock(); -} - -void Settings::clearDefaults() -{ - MutexAutoLock lock(m_mutex); - clearDefaultsNoLock(); -} - SettingsParseEvent Settings::parseConfigObject(const std::string &line, - const std::string &end, std::string &name, std::string &value) + std::string &name, std::string &value) { std::string trimmed_line = trim(line); @@ -933,7 +960,7 @@ SettingsParseEvent Settings::parseConfigObject(const std::string &line, return SPE_NONE; if (trimmed_line[0] == '#') return SPE_COMMENT; - if (trimmed_line == end) + if (trimmed_line == m_end_tag) return SPE_END; size_t pos = trimmed_line.find('='); @@ -952,67 +979,26 @@ SettingsParseEvent Settings::parseConfigObject(const std::string &line, } -void Settings::updateNoLock(const Settings &other) -{ - m_settings.insert(other.m_settings.begin(), other.m_settings.end()); - m_defaults.insert(other.m_defaults.begin(), other.m_defaults.end()); -} - - void Settings::clearNoLock() { - for (SettingEntries::const_iterator it = m_settings.begin(); it != m_settings.end(); ++it) delete it->second.group; m_settings.clear(); - - clearDefaultsNoLock(); } -void Settings::clearDefaultsNoLock() -{ - for (SettingEntries::const_iterator it = m_defaults.begin(); - it != m_defaults.end(); ++it) - delete it->second.group; - m_defaults.clear(); -} void Settings::setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags) { - m_flags[name] = flagdesc; + s_flags[name] = flagdesc; setDefault(name, writeFlagString(flags, flagdesc, U32_MAX)); } -void Settings::overrideDefaults(Settings *other) -{ - for (const auto &setting : other->m_settings) { - if (setting.second.is_group) { - setGroupDefault(setting.first, *setting.second.group); - continue; - } - const FlagDesc *flagdesc = getFlagDescFallback(setting.first); - if (flagdesc) { - // Flags cannot be copied directly. - // 1) Get the current set flags - u32 flags = getFlagStr(setting.first, flagdesc, nullptr); - // 2) Set the flags as defaults - other->setDefault(setting.first, flagdesc, flags); - // 3) Get the newly set flags and override the default setting value - setDefault(setting.first, flagdesc, - other->getFlagStr(setting.first, flagdesc, nullptr)); - continue; - } - // Also covers FlagDesc settings - setDefault(setting.first, setting.second.value); - } -} - const FlagDesc *Settings::getFlagDescFallback(const std::string &name) const { - auto it = m_flags.find(name); - return it == m_flags.end() ? nullptr : it->second; + auto it = s_flags.find(name); + return it == s_flags.end() ? nullptr : it->second; } void Settings::registerChangedCallback(const std::string &name, diff --git a/src/settings.h b/src/settings.h index 6db2f9481..af4cae3cd 100644 --- a/src/settings.h +++ b/src/settings.h @@ -30,7 +30,7 @@ class Settings; struct NoiseParams; // Global objects -extern Settings *g_settings; +extern Settings *g_settings; // Same as Settings::getLayer(SL_GLOBAL); extern std::string g_settings_path; // Type for a settings changed callback function @@ -60,6 +60,14 @@ enum SettingsParseEvent { SPE_MULTILINE, }; +enum SettingsLayer { + SL_DEFAULTS, + SL_GAME, + SL_GLOBAL, + SL_MAP, + SL_TOTAL_COUNT +}; + struct ValueSpec { ValueSpec(ValueType a_type, const char *a_help=NULL) { @@ -92,8 +100,13 @@ typedef std::unordered_map SettingEntries; class Settings { public: - Settings() = default; + static Settings *createLayer(SettingsLayer sl, const std::string &end_tag = ""); + static Settings *getLayer(SettingsLayer sl); + SettingsLayer getLayerType() const { return m_settingslayer; } + Settings(const std::string &end_tag = "") : + m_end_tag(end_tag) + {} ~Settings(); Settings & operator += (const Settings &other); @@ -110,7 +123,7 @@ public: // NOTE: Types of allowed_options are ignored. Returns success. bool parseCommandLine(int argc, char *argv[], std::map &allowed_options); - bool parseConfigLines(std::istream &is, const std::string &end = ""); + bool parseConfigLines(std::istream &is); void writeLines(std::ostream &os, u32 tab_depth=0) const; /*********** @@ -146,7 +159,6 @@ public: bool getGroupNoEx(const std::string &name, Settings *&val) const; bool getNoEx(const std::string &name, std::string &val) const; - bool getDefaultNoEx(const std::string &name, std::string &val) const; bool getFlag(const std::string &name) const; bool getU16NoEx(const std::string &name, u16 &val) const; bool getS16NoEx(const std::string &name, s16 &val) const; @@ -170,11 +182,10 @@ public: // N.B. Groups not allocated with new must be set to NULL in the settings // tree before object destruction. bool setEntry(const std::string &name, const void *entry, - bool set_group, bool set_default); + bool set_group); bool set(const std::string &name, const std::string &value); bool setDefault(const std::string &name, const std::string &value); bool setGroup(const std::string &name, const Settings &group); - bool setGroupDefault(const std::string &name, const Settings &group); bool setBool(const std::string &name, bool value); bool setS16(const std::string &name, s16 value); bool setU16(const std::string &name, u16 value); @@ -185,21 +196,16 @@ public: bool setV3F(const std::string &name, v3f value); bool setFlagStr(const std::string &name, u32 flags, const FlagDesc *flagdesc = nullptr, u32 flagmask = U32_MAX); - bool setNoiseParams(const std::string &name, const NoiseParams &np, - bool set_default=false); + bool setNoiseParams(const std::string &name, const NoiseParams &np); // remove a setting bool remove(const std::string &name); - void clear(); - void clearDefaults(); /************** * Miscellany * **************/ void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags); - // Takes the provided setting values and uses them as new defaults - void overrideDefaults(Settings *other); const FlagDesc *getFlagDescFallback(const std::string &name) const; void registerChangedCallback(const std::string &name, @@ -215,9 +221,9 @@ private: ***********************/ SettingsParseEvent parseConfigObject(const std::string &line, - const std::string &end, std::string &name, std::string &value); + std::string &name, std::string &value); bool updateConfigObject(std::istream &is, std::ostream &os, - const std::string &end, u32 tab_depth=0); + u32 tab_depth=0); static bool checkNameValid(const std::string &name); static bool checkValueValid(const std::string &value); @@ -228,9 +234,9 @@ private: /*********** * Getters * ***********/ + Settings *getParent() const; const SettingsEntry &getEntry(const std::string &name) const; - const SettingsEntry &getEntryDefault(const std::string &name) const; // Allow TestSettings to run sanity checks using private functions. friend class TestSettings; @@ -242,14 +248,15 @@ private: void doCallbacks(const std::string &name) const; SettingEntries m_settings; - SettingEntries m_defaults; - std::unordered_map m_flags; - SettingsCallbackMap m_callbacks; + std::string m_end_tag; mutable std::mutex m_callback_mutex; // All methods that access m_settings/m_defaults directly should lock this. mutable std::mutex m_mutex; + static Settings *s_layers[SL_TOTAL_COUNT]; + SettingsLayer m_settingslayer = SL_TOTAL_COUNT; + static std::unordered_map s_flags; }; diff --git a/src/unittest/test_map_settings_manager.cpp b/src/unittest/test_map_settings_manager.cpp index 3d642a9b4..81ca68705 100644 --- a/src/unittest/test_map_settings_manager.cpp +++ b/src/unittest/test_map_settings_manager.cpp @@ -30,7 +30,7 @@ public: TestMapSettingsManager() { TestManager::registerTestModule(this); } const char *getName() { return "TestMapSettingsManager"; } - void makeUserConfig(Settings *conf); + void makeUserConfig(); std::string makeMetaFile(bool make_corrupt); void runTests(IGameDef *gamedef); @@ -65,8 +65,11 @@ void check_noise_params(const NoiseParams *np1, const NoiseParams *np2) } -void TestMapSettingsManager::makeUserConfig(Settings *conf) +void TestMapSettingsManager::makeUserConfig() { + delete Settings::getLayer(SL_GLOBAL); + Settings *conf = Settings::createLayer(SL_GLOBAL); + conf->set("mg_name", "v7"); conf->set("seed", "5678"); conf->set("water_level", "20"); @@ -103,12 +106,11 @@ std::string TestMapSettingsManager::makeMetaFile(bool make_corrupt) void TestMapSettingsManager::testMapSettingsManager() { - Settings user_settings; - makeUserConfig(&user_settings); + makeUserConfig(); std::string test_mapmeta_path = makeMetaFile(false); - MapSettingsManager mgr(&user_settings, test_mapmeta_path); + MapSettingsManager mgr(test_mapmeta_path); std::string value; UASSERT(mgr.getMapSetting("mg_name", &value)); @@ -140,6 +142,12 @@ void TestMapSettingsManager::testMapSettingsManager() mgr.setMapSettingNoiseParams("mgv5_np_height", &script_np_height); mgr.setMapSettingNoiseParams("mgv5_np_factor", &script_np_factor); + { + NoiseParams dummy; + mgr.getMapSettingNoiseParams("mgv5_np_factor", &dummy); + check_noise_params(&dummy, &script_np_factor); + } + // Now make our Params and see if the values are correctly sourced MapgenParams *params = mgr.makeMapgenParams(); UASSERT(params->mgtype == MAPGEN_V5); @@ -188,50 +196,66 @@ void TestMapSettingsManager::testMapSettingsManager() void TestMapSettingsManager::testMapMetaSaveLoad() { - Settings conf; std::string path = getTestTempDirectory() + DIR_DELIM + "foobar" + DIR_DELIM + "map_meta.txt"; + makeUserConfig(); + Settings &conf = *Settings::getLayer(SL_GLOBAL); + + // There cannot be two MapSettingsManager + // copy the mapgen params to compare them + MapgenParams params1, params2; // Create a set of mapgen params and save them to map meta - conf.set("seed", "12345"); - conf.set("water_level", "5"); - MapSettingsManager mgr1(&conf, path); - MapgenParams *params1 = mgr1.makeMapgenParams(); - UASSERT(params1); - UASSERT(mgr1.saveMapMeta()); + { + conf.set("seed", "12345"); + conf.set("water_level", "5"); + MapSettingsManager mgr(path); + MapgenParams *params = mgr.makeMapgenParams(); + UASSERT(params); + params1 = *params; + params1.bparams = nullptr; // No double-free + UASSERT(mgr.saveMapMeta()); + } // Now try loading the map meta to mapgen params - conf.set("seed", "67890"); - conf.set("water_level", "32"); - MapSettingsManager mgr2(&conf, path); - UASSERT(mgr2.loadMapMeta()); - MapgenParams *params2 = mgr2.makeMapgenParams(); - UASSERT(params2); + { + conf.set("seed", "67890"); + conf.set("water_level", "32"); + MapSettingsManager mgr(path); + UASSERT(mgr.loadMapMeta()); + MapgenParams *params = mgr.makeMapgenParams(); + UASSERT(params); + params2 = *params; + params2.bparams = nullptr; // No double-free + } // Check that both results are correct - UASSERTEQ(u64, params1->seed, 12345); - UASSERTEQ(s16, params1->water_level, 5); - UASSERTEQ(u64, params2->seed, 12345); - UASSERTEQ(s16, params2->water_level, 5); + UASSERTEQ(u64, params1.seed, 12345); + UASSERTEQ(s16, params1.water_level, 5); + UASSERTEQ(u64, params2.seed, 12345); + UASSERTEQ(s16, params2.water_level, 5); } void TestMapSettingsManager::testMapMetaFailures() { std::string test_mapmeta_path; - Settings conf; // Check to see if it'll fail on a non-existent map meta file - test_mapmeta_path = "woobawooba/fgdfg/map_meta.txt"; - UASSERT(!fs::PathExists(test_mapmeta_path)); + { + test_mapmeta_path = "woobawooba/fgdfg/map_meta.txt"; + UASSERT(!fs::PathExists(test_mapmeta_path)); - MapSettingsManager mgr1(&conf, test_mapmeta_path); - UASSERT(!mgr1.loadMapMeta()); + MapSettingsManager mgr1(test_mapmeta_path); + UASSERT(!mgr1.loadMapMeta()); + } // Check to see if it'll fail on a corrupt map meta file - test_mapmeta_path = makeMetaFile(true); - UASSERT(fs::PathExists(test_mapmeta_path)); + { + test_mapmeta_path = makeMetaFile(true); + UASSERT(fs::PathExists(test_mapmeta_path)); - MapSettingsManager mgr2(&conf, test_mapmeta_path); - UASSERT(!mgr2.loadMapMeta()); + MapSettingsManager mgr2(test_mapmeta_path); + UASSERT(!mgr2.loadMapMeta()); + } } diff --git a/src/unittest/test_settings.cpp b/src/unittest/test_settings.cpp index f91ba5b67..d2d35c357 100644 --- a/src/unittest/test_settings.cpp +++ b/src/unittest/test_settings.cpp @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include "settings.h" +#include "defaultsettings.h" #include "noise.h" class TestSettings : public TestBase { @@ -31,6 +32,7 @@ public: void runTests(IGameDef *gamedef); void testAllSettings(); + void testDefaults(); void testFlagDesc(); static const char *config_text_before; @@ -42,6 +44,7 @@ static TestSettings g_test_instance; void TestSettings::runTests(IGameDef *gamedef) { TEST(testAllSettings); + TEST(testDefaults); TEST(testFlagDesc); } @@ -70,7 +73,8 @@ const char *TestSettings::config_text_before = " with leading whitespace!\n" "\"\"\"\n" "np_terrain = 5, 40, (250, 250, 250), 12341, 5, 0.7, 2.4\n" - "zoop = true"; + "zoop = true\n" + "[dummy_eof_end_tag]\n"; const std::string TestSettings::config_text_after = "leet = 1337\n" @@ -111,12 +115,34 @@ const std::string TestSettings::config_text_after = " animals = cute\n" " num_apples = 4\n" " num_oranges = 53\n" - "}\n"; + "}\n" + "[dummy_eof_end_tag]"; + +void compare_settings(const std::string &name, Settings *a, Settings *b) +{ + auto keys = a->getNames(); + Settings *group1, *group2; + std::string value1, value2; + for (auto &key : keys) { + if (a->getGroupNoEx(key, group1)) { + UASSERT(b->getGroupNoEx(key, group2)); + + compare_settings(name + "->" + key, group1, group2); + continue; + } + + UASSERT(b->getNoEx(key, value1)); + // For identification + value1 = name + "->" + key + "=" + value1; + value2 = name + "->" + key + "=" + a->get(key); + UASSERTCMP(std::string, ==, value2, value1); + } +} void TestSettings::testAllSettings() { try { - Settings s; + Settings s("[dummy_eof_end_tag]"); // Test reading of settings std::istringstream is(config_text_before); @@ -197,21 +223,44 @@ void TestSettings::testAllSettings() is.clear(); is.seekg(0); - UASSERT(s.updateConfigObject(is, os, "", 0) == true); - //printf(">>>> expected config:\n%s\n", TEST_CONFIG_TEXT_AFTER); - //printf(">>>> actual config:\n%s\n", os.str().c_str()); -#if __cplusplus < 201103L - // This test only works in older C++ versions than C++11 because we use unordered_map - UASSERT(os.str() == config_text_after); -#endif + UASSERT(s.updateConfigObject(is, os, 0) == true); + + { + // Confirm settings + Settings s2("[dummy_eof_end_tag]"); + std::istringstream is(config_text_after, std::ios_base::binary); + s2.parseConfigLines(is); + + compare_settings("(main)", &s, &s2); + } + } catch (SettingNotFoundException &e) { UASSERT(!"Setting not found!"); } } +void TestSettings::testDefaults() +{ + Settings *game = Settings::createLayer(SL_GAME); + Settings *def = Settings::getLayer(SL_DEFAULTS); + + def->set("name", "FooBar"); + UASSERT(def->get("name") == "FooBar"); + UASSERT(game->get("name") == "FooBar"); + + game->set("name", "Baz"); + UASSERT(game->get("name") == "Baz"); + + delete game; + + // Restore default settings + delete Settings::getLayer(SL_DEFAULTS); + set_default_settings(); +} + void TestSettings::testFlagDesc() { - Settings s; + Settings &s = *Settings::createLayer(SL_GAME); FlagDesc flagdesc[] = { { "biomes", 0x01 }, { "trees", 0x02 }, @@ -242,4 +291,6 @@ void TestSettings::testFlagDesc() // Enabled: tables s.set("test_flags", "16"); UASSERT(s.getFlagStr("test_flags", flagdesc, nullptr) == 0x10); + + delete &s; } -- cgit v1.2.3 From 112a6adb10d3a5a2e55012a36580607d12ce9758 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sun, 31 Jan 2021 20:36:47 +0100 Subject: Cache client IP in RemoteClient so it can always be retrieved (#10887) specifically: after the peer has already disappeared --- src/clientiface.cpp | 3 -- src/clientiface.h | 15 ++++-- src/network/serverpackethandler.cpp | 11 ++-- src/script/lua_api/l_server.cpp | 101 ++++++++++++++---------------------- src/server.cpp | 38 ++++++-------- src/server.h | 15 ++++-- 6 files changed, 84 insertions(+), 99 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/clientiface.cpp b/src/clientiface.cpp index 01852c5d1..797afd3c1 100644 --- a/src/clientiface.cpp +++ b/src/clientiface.cpp @@ -452,9 +452,6 @@ void RemoteClient::notifyEvent(ClientStateEvent event) case CSE_Hello: m_state = CS_HelloSent; break; - case CSE_InitLegacy: - m_state = CS_AwaitingInit2; - break; case CSE_Disconnect: m_state = CS_Disconnecting; break; diff --git a/src/clientiface.h b/src/clientiface.h index eabffb0b6..cc5292b71 100644 --- a/src/clientiface.h +++ b/src/clientiface.h @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "serialization.h" // for SER_FMT_VER_INVALID #include "network/networkpacket.h" #include "network/networkprotocol.h" +#include "network/address.h" #include "porting.h" #include @@ -188,7 +189,6 @@ enum ClientStateEvent { CSE_Hello, CSE_AuthAccept, - CSE_InitLegacy, CSE_GotInit2, CSE_SetDenied, CSE_SetDefinitionsSent, @@ -338,17 +338,24 @@ public: u8 getMajor() const { return m_version_major; } u8 getMinor() const { return m_version_minor; } u8 getPatch() const { return m_version_patch; } - const std::string &getFull() const { return m_full_version; } + const std::string &getFullVer() const { return m_full_version; } void setLangCode(const std::string &code) { m_lang_code = code; } const std::string &getLangCode() const { return m_lang_code; } + + void setCachedAddress(const Address &addr) { m_addr = addr; } + const Address &getAddress() const { return m_addr; } + private: // Version is stored in here after INIT before INIT2 u8 m_pending_serialization_version = SER_FMT_VER_INVALID; /* current state of client */ ClientState m_state = CS_Created; - + + // Cached here so retrieval doesn't have to go to connection API + Address m_addr; + // Client sent language code std::string m_lang_code; @@ -412,7 +419,7 @@ private: /* client information - */ + */ u8 m_version_major = 0; u8 m_version_minor = 0; u8 m_version_patch = 0; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index c636d01e1..882cf71c1 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -56,12 +56,12 @@ void Server::handleCommand_Init(NetworkPacket* pkt) session_t peer_id = pkt->getPeerId(); RemoteClient *client = getClient(peer_id, CS_Created); + Address addr; std::string addr_s; try { - Address address = getPeerAddress(peer_id); - addr_s = address.serializeString(); - } - catch (con::PeerNotFoundException &e) { + addr = m_con->GetPeerAddress(peer_id); + addr_s = addr.serializeString(); + } catch (con::PeerNotFoundException &e) { /* * no peer for this packet found * most common reason is peer timeout, e.g. peer didn't @@ -73,13 +73,14 @@ void Server::handleCommand_Init(NetworkPacket* pkt) return; } - // If net_proto_version is set, this client has already been handled if (client->getState() > CS_Created) { verbosestream << "Server: Ignoring multiple TOSERVER_INITs from " << addr_s << " (peer_id=" << peer_id << ")" << std::endl; return; } + client->setCachedAddress(addr); + verbosestream << "Server: Got TOSERVER_INIT from " << addr_s << " (peer_id=" << peer_id << ")" << std::endl; diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 6f934bb9d..0ae699c9f 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -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(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(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(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; } diff --git a/src/server.cpp b/src/server.cpp index aba7b6401..76a817701 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1242,20 +1242,8 @@ bool Server::getClientConInfo(session_t peer_id, con::rtt_stat_type type, float* return *retval != -1; } -bool Server::getClientInfo( - session_t peer_id, - ClientState* state, - u32* uptime, - u8* ser_vers, - u16* prot_vers, - u8* major, - u8* minor, - u8* patch, - std::string* vers_string, - std::string* lang_code - ) -{ - *state = m_clients.getClientState(peer_id); +bool Server::getClientInfo(session_t peer_id, ClientInfo &ret) +{ m_clients.lock(); RemoteClient* client = m_clients.lockedGetClientNoEx(peer_id, CS_Invalid); @@ -1264,15 +1252,18 @@ bool Server::getClientInfo( return false; } - *uptime = client->uptime(); - *ser_vers = client->serialization_version; - *prot_vers = client->net_proto_version; + ret.state = client->getState(); + ret.addr = client->getAddress(); + ret.uptime = client->uptime(); + ret.ser_vers = client->serialization_version; + ret.prot_vers = client->net_proto_version; + + ret.major = client->getMajor(); + ret.minor = client->getMinor(); + ret.patch = client->getPatch(); + ret.vers_string = client->getFullVer(); - *major = client->getMajor(); - *minor = client->getMinor(); - *patch = client->getPatch(); - *vers_string = client->getFull(); - *lang_code = client->getLangCode(); + ret.lang_code = client->getLangCode(); m_clients.unlock(); @@ -3339,7 +3330,8 @@ void Server::hudSetHotbarSelectedImage(RemotePlayer *player, const std::string & Address Server::getPeerAddress(session_t peer_id) { - return m_con->GetPeerAddress(peer_id); + // Note that this is only set after Init was received in Server::handleCommand_Init + return getClient(peer_id, CS_Invalid)->getAddress(); } void Server::setLocalPlayerAnimations(RemotePlayer *player, diff --git a/src/server.h b/src/server.h index 1dd181794..7071d2d07 100644 --- a/src/server.h +++ b/src/server.h @@ -126,6 +126,17 @@ struct MinimapMode { u16 scale = 1; }; +// structure for everything getClientInfo returns, for convenience +struct ClientInfo { + ClientState state; + Address addr; + u32 uptime; + u8 ser_vers; + u16 prot_vers; + u8 major, minor, patch; + std::string vers_string, lang_code; +}; + class Server : public con::PeerHandler, public MapEventReceiver, public IGameDef { @@ -326,9 +337,7 @@ public: void DenyAccess_Legacy(session_t peer_id, const std::wstring &reason); void DisconnectPeer(session_t peer_id); bool getClientConInfo(session_t peer_id, con::rtt_stat_type type, float *retval); - bool getClientInfo(session_t peer_id, ClientState *state, u32 *uptime, - u8* ser_vers, u16* prot_vers, u8* major, u8* minor, u8* patch, - std::string* vers_string, std::string* lang_code); + bool getClientInfo(session_t peer_id, ClientInfo &ret); void printToConsoleOnly(const std::string &text); -- cgit v1.2.3 From a01a02f7a1ec915c207632085cd5f24eab28da17 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 30 Jan 2021 12:41:27 +0100 Subject: Preserve immortal group for players when damage is disabled --- builtin/settingtypes.txt | 2 +- doc/lua_api.txt | 5 +++-- src/script/lua_api/l_object.cpp | 9 +++++++++ src/server.cpp | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) (limited to 'src/script/lua_api') diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt index 21118134e..8b6227b37 100644 --- a/builtin/settingtypes.txt +++ b/builtin/settingtypes.txt @@ -1085,7 +1085,7 @@ default_stack_max (Default stack size) int 99 # Enable players getting damage and dying. enable_damage (Damage) bool false -# Enable creative mode for new created maps. +# Enable creative mode for all players creative_mode (Creative) bool false # A chosen map seed for a new map, leave empty for random. diff --git a/doc/lua_api.txt b/doc/lua_api.txt index df9e3f8b0..18499e15a 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -1745,8 +1745,9 @@ to games. ### `ObjectRef` groups * `immortal`: Skips all damage and breath handling for an object. This group - will also hide the integrated HUD status bars for players, and is - automatically set to all players when damage is disabled on the server. + will also hide the integrated HUD status bars for players. It is + automatically set to all players when damage is disabled on the server and + cannot be reset (subject to change). * `punch_operable`: For entities; disables the regular damage mechanism for players punching it by hand or a non-tool item, so that it can do something else than take damage. diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index ba201a9d3..07aa3f7c9 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; } diff --git a/src/server.cpp b/src/server.cpp index 76a817701..8a86dbd82 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1349,7 +1349,7 @@ void Server::SendPlayerHPOrDie(PlayerSAO *playersao, const PlayerHPChangeReason return; session_t peer_id = playersao->getPeerID(); - bool is_alive = playersao->getHP() > 0; + bool is_alive = !playersao->isDead(); if (is_alive) SendPlayerHP(peer_id); -- cgit v1.2.3 From 40ad9767531beb6cf2e8bd918c9c9ed5f2749320 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Sat, 30 Jan 2021 14:35:34 +0100 Subject: Revise dynamic_add_media API to better accomodate future changes --- builtin/game/misc.lua | 23 +++++++++++++++++++++++ doc/lua_api.txt | 22 ++++++++++++---------- src/script/lua_api/l_server.cpp | 21 ++++++++++++++++----- src/script/lua_api/l_server.h | 2 +- src/server.cpp | 20 +++++++++++++++----- src/server.h | 2 +- 6 files changed, 68 insertions(+), 22 deletions(-) (limited to 'src/script/lua_api') diff --git a/builtin/game/misc.lua b/builtin/game/misc.lua index 96a0a2dda..b8c5e16a9 100644 --- a/builtin/game/misc.lua +++ b/builtin/game/misc.lua @@ -266,3 +266,26 @@ end function core.cancel_shutdown_requests() core.request_shutdown("", false, -1) end + + +-- Callback handling for dynamic_add_media + +local dynamic_add_media_raw = core.dynamic_add_media_raw +core.dynamic_add_media_raw = nil +function core.dynamic_add_media(filepath, callback) + local ret = dynamic_add_media_raw(filepath) + if ret == false then + return ret + end + if callback == nil then + core.log("deprecated", "Calling minetest.dynamic_add_media without ".. + "a callback is deprecated and will stop working in future versions.") + else + -- At the moment async loading is not actually implemented, so we + -- immediately call the callback ourselves + for _, name in ipairs(ret) do + callback(name) + end + end + return true +end diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 18499e15a..9c2a0f131 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -5446,20 +5446,22 @@ Server * Returns a code (0: successful, 1: no such player, 2: player is connected) * `minetest.remove_player_auth(name)`: remove player authentication data * Returns boolean indicating success (false if player nonexistant) -* `minetest.dynamic_add_media(filepath)` - * Adds the file at the given path to the media sent to clients by the server - on startup and also pushes this file to already connected clients. +* `minetest.dynamic_add_media(filepath, callback)` + * `filepath`: path to a media file on the filesystem + * `callback`: function with arguments `name`, where name is a player name + (previously there was no callback argument; omitting it is deprecated) + * Adds the file to the media sent to clients by the server on startup + and also pushes this file to already connected clients. The file must be a supported image, sound or model format. It must not be modified, deleted, moved or renamed after calling this function. The list of dynamically added media is not persisted. - * Returns boolean indicating success (duplicate files count as error) - * The media will be ready to use (in e.g. entity textures, sound_play) - immediately after calling this function. + * Returns false on error, true if the request was accepted + * The given callback will be called for every player as soon as the + media is available on the client. Old clients that lack support for this feature will not see the media - unless they reconnect to the server. - * Since media transferred this way does not use client caching or HTTP - transfers, dynamic media should not be used with big files or performance - will suffer. + unless they reconnect to the server. (callback won't be called) + * Since media transferred this way currently does not use client caching + or HTTP transfers, dynamic media should not be used with big files. Bans ---- diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 0ae699c9f..78cf4b403 100644 --- a/src/script/lua_api/l_server.cpp +++ b/src/script/lua_api/l_server.cpp @@ -452,19 +452,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(L, 1); CHECK_SECURE_PATH(L, filepath.c_str(), false); - bool ok = getServer(L)->dynamicAddMedia(filepath); - lua_pushboolean(L, ok); + std::vector 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; } @@ -532,7 +543,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/server.cpp b/src/server.cpp index 8a86dbd82..90496129e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -3465,7 +3465,8 @@ void Server::deleteParticleSpawner(const std::string &playername, u32 id) SendDeleteParticleSpawner(peer_id, id); } -bool Server::dynamicAddMedia(const std::string &filepath) +bool Server::dynamicAddMedia(const std::string &filepath, + std::vector &sent_to) { std::string filename = fs::GetFilenameFromPath(filepath.c_str()); if (m_media.find(filename) != m_media.end()) { @@ -3485,9 +3486,17 @@ bool Server::dynamicAddMedia(const std::string &filepath) pkt << raw_hash << filename << (bool) true; pkt.putLongString(filedata); - auto client_ids = m_clients.getClientIDs(CS_DefinitionsSent); - for (session_t client_id : client_ids) { + m_clients.lock(); + for (auto &pair : m_clients.getClientList()) { + if (pair.second->getState() < CS_DefinitionsSent) + continue; + if (pair.second->net_proto_version < 39) + continue; + + if (auto player = m_env->getPlayer(pair.second->peer_id)) + sent_to.emplace_back(player); /* + FIXME: this is a very awful hack The network layer only guarantees ordered delivery inside a channel. Since the very next packet could be one that uses the media, we have to push the media over ALL channels to ensure it is processed before @@ -3496,9 +3505,10 @@ bool Server::dynamicAddMedia(const std::string &filepath) - channel 1 (HUD) - channel 0 (everything else: e.g. play_sound, object messages) */ - m_clients.send(client_id, 1, &pkt, true); - m_clients.send(client_id, 0, &pkt, true); + m_clients.send(pair.second->peer_id, 1, &pkt, true); + m_clients.send(pair.second->peer_id, 0, &pkt, true); } + m_clients.unlock(); return true; } diff --git a/src/server.h b/src/server.h index 7071d2d07..5c143a657 100644 --- a/src/server.h +++ b/src/server.h @@ -257,7 +257,7 @@ public: void deleteParticleSpawner(const std::string &playername, u32 id); - bool dynamicAddMedia(const std::string &filepath); + bool dynamicAddMedia(const std::string &filepath, std::vector &sent_to); ServerInventoryManager *getInventoryMgr() const { return m_inventory_mgr.get(); } void sendDetachedInventory(Inventory *inventory, const std::string &name, session_t peer_id); -- cgit v1.2.3 From c834d2ab25694ef2d67dc24f85f304269d202c8e Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 29 Jan 2021 14:03:27 +0100 Subject: Drop wide/narrow conversion functions The only valid usecase for these is interfacing with OS APIs that want a locale/OS-specific multibyte encoding. But they weren't used for that anywhere, instead UTF-8 is pretty much assumed when it comes to that. Since these are only a potential source of bugs and do not fulfil their purpose at all, drop them entirely. --- src/chat.cpp | 4 +-- src/client/client.cpp | 4 +-- src/client/keycode.cpp | 3 +- src/gui/guiConfirmRegistration.cpp | 3 +- src/network/serverpackethandler.cpp | 8 ++--- src/script/lua_api/l_server.cpp | 2 +- src/server.cpp | 61 ++++++++++++++----------------------- src/server.h | 8 ++--- src/unittest/test_utilities.cpp | 4 +-- src/util/string.cpp | 59 ++--------------------------------- src/util/string.h | 28 ----------------- 11 files changed, 41 insertions(+), 143 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/chat.cpp b/src/chat.cpp index 2f65e68b3..c9317a079 100644 --- a/src/chat.cpp +++ b/src/chat.cpp @@ -485,8 +485,8 @@ void ChatPrompt::nickCompletion(const std::list& names, bool backwa // find all names that start with the selected prefix std::vector completions; for (const std::string &name : names) { - if (str_starts_with(narrow_to_wide(name), prefix, true)) { - std::wstring completion = narrow_to_wide(name); + std::wstring completion = utf8_to_wide(name); + if (str_starts_with(completion, prefix, true)) { if (prefix_start == 0) completion += L": "; completions.push_back(completion); diff --git a/src/client/client.cpp b/src/client/client.cpp index 61888b913..ef4a3cdfc 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1196,7 +1196,7 @@ void Client::sendChatMessage(const std::wstring &message) if (canSendChatMessage()) { u32 now = time(NULL); float time_passed = now - m_last_chat_message_sent; - m_last_chat_message_sent = time(NULL); + m_last_chat_message_sent = now; m_chat_message_allowance += time_passed * (CLIENT_CHAT_MESSAGE_LIMIT_PER_10S / 8.0f); if (m_chat_message_allowance > CLIENT_CHAT_MESSAGE_LIMIT_PER_10S) @@ -1832,7 +1832,7 @@ void Client::makeScreenshot() sstr << "Failed to save screenshot '" << filename << "'"; } pushToChatQueue(new ChatMessage(CHATMESSAGE_TYPE_SYSTEM, - narrow_to_wide(sstr.str()))); + utf8_to_wide(sstr.str()))); infostream << sstr.str() << std::endl; image->drop(); } diff --git a/src/client/keycode.cpp b/src/client/keycode.cpp index 6a0e9f569..ce5214f54 100644 --- a/src/client/keycode.cpp +++ b/src/client/keycode.cpp @@ -316,7 +316,8 @@ KeyPress::KeyPress(const char *name) int chars_read = mbtowc(&Char, name, 1); FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character"); m_name = ""; - warningstream << "KeyPress: Unknown key '" << name << "', falling back to first char."; + warningstream << "KeyPress: Unknown key '" << name + << "', falling back to first char." << std::endl; } KeyPress::KeyPress(const irr::SEvent::SKeyInput &in, bool prefer_character) diff --git a/src/gui/guiConfirmRegistration.cpp b/src/gui/guiConfirmRegistration.cpp index 020a2796a..4a798c39b 100644 --- a/src/gui/guiConfirmRegistration.cpp +++ b/src/gui/guiConfirmRegistration.cpp @@ -192,8 +192,7 @@ void GUIConfirmRegistration::acceptInput() bool GUIConfirmRegistration::processInput() { - std::wstring m_password_ws = narrow_to_wide(m_password); - if (m_password_ws != m_pass_confirm) { + if (utf8_to_wide(m_password) != m_pass_confirm) { gui::IGUIElement *e = getElementFromId(ID_message); if (e) e->setVisible(true); diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 4d79f375c..02af06abc 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -778,15 +778,13 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt) return; } - // Get player name of this client std::string name = player->getName(); - std::wstring wname = narrow_to_wide(name); - std::wstring answer_to_sender = handleChat(name, wname, message, true, player); + std::wstring answer_to_sender = handleChat(name, message, true, player); if (!answer_to_sender.empty()) { // Send the answer to sender - SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_NORMAL, - answer_to_sender, wname)); + SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM, + answer_to_sender)); } } diff --git a/src/script/lua_api/l_server.cpp b/src/script/lua_api/l_server.cpp index 78cf4b403..bf5292521 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; } diff --git a/src/server.cpp b/src/server.cpp index 90496129e..907bc6d24 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2955,7 +2955,7 @@ void Server::handleChatInterfaceEvent(ChatEvent *evt) } } -std::wstring Server::handleChat(const std::string &name, const std::wstring &wname, +std::wstring Server::handleChat(const std::string &name, std::wstring wmessage, bool check_shout_priv, RemotePlayer *player) { // If something goes wrong, this player is to blame @@ -2993,7 +2993,7 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna auto message = trim(wide_to_utf8(wmessage)); if (message.find_first_of("\n\r") != std::wstring::npos) { - return L"New lines are not permitted in chat messages"; + return L"Newlines are not permitted in chat messages"; } // Run script hook, exit if script ate the chat message @@ -3014,10 +3014,10 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna the Cyrillic alphabet and some characters on older Android devices */ #ifdef __ANDROID__ - line += L"<" + wname + L"> " + wmessage; + line += L"<" + utf8_to_wide(name) + L"> " + wmessage; #else - line += narrow_to_wide(m_script->formatChatMessage(name, - wide_to_narrow(wmessage))); + line += utf8_to_wide(m_script->formatChatMessage(name, + wide_to_utf8(wmessage))); #endif } @@ -3030,35 +3030,23 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna /* Send the message to others */ - actionstream << "CHAT: " << wide_to_narrow(unescape_enriched(line)) << std::endl; + actionstream << "CHAT: " << wide_to_utf8(unescape_enriched(line)) << std::endl; - std::vector clients = m_clients.getClientIDs(); - - /* - Send the message back to the inital sender - if they are using protocol version >= 29 - */ - - session_t peer_id_to_avoid_sending = - (player ? player->getPeerId() : PEER_ID_INEXISTENT); + ChatMessage chatmsg(line); - if (player && player->protocol_version >= 29) - peer_id_to_avoid_sending = PEER_ID_INEXISTENT; + std::vector clients = m_clients.getClientIDs(); + for (u16 cid : clients) + SendChatMessage(cid, chatmsg); - for (u16 cid : clients) { - if (cid != peer_id_to_avoid_sending) - SendChatMessage(cid, ChatMessage(line)); - } return L""; } void Server::handleAdminChat(const ChatEventChat *evt) { std::string name = evt->nick; - std::wstring wname = utf8_to_wide(name); std::wstring wmessage = evt->evt_msg; - std::wstring answer = handleChat(name, wname, wmessage); + std::wstring answer = handleChat(name, wmessage); // If asked to send answer to sender if (!answer.empty()) { @@ -3095,46 +3083,43 @@ PlayerSAO *Server::getPlayerSAO(session_t peer_id) return player->getPlayerSAO(); } -std::wstring Server::getStatusString() +std::string Server::getStatusString() { - std::wostringstream os(std::ios_base::binary); - os << L"# Server: "; + std::ostringstream os(std::ios_base::binary); + os << "# Server: "; // Version - os << L"version=" << narrow_to_wide(g_version_string); + os << "version=" << g_version_string; // Uptime - os << L", uptime=" << m_uptime_counter->get(); + os << ", uptime=" << m_uptime_counter->get(); // Max lag estimate - os << L", max_lag=" << (m_env ? m_env->getMaxLagEstimate() : 0); + os << ", max_lag=" << (m_env ? m_env->getMaxLagEstimate() : 0); // Information about clients bool first = true; - os << L", clients={"; + os << ", clients={"; if (m_env) { std::vector clients = m_clients.getClientIDs(); for (session_t client_id : clients) { RemotePlayer *player = m_env->getPlayer(client_id); // Get name of player - std::wstring name = L"unknown"; - if (player) - name = narrow_to_wide(player->getName()); + const char *name = player ? player->getName() : ""; // Add name to information string if (!first) - os << L", "; + os << ", "; else first = false; - os << name; } } - os << L"}"; + os << "}"; if (m_env && !((ServerMap*)(&m_env->getMap()))->isSavingEnabled()) - os << std::endl << L"# Server: " << " WARNING: Map saving is disabled."; + os << std::endl << "# Server: " << " WARNING: Map saving is disabled."; if (!g_settings->get("motd").empty()) - os << std::endl << L"# Server: " << narrow_to_wide(g_settings->get("motd")); + os << std::endl << "# Server: " << g_settings->get("motd"); return os.str(); } diff --git a/src/server.h b/src/server.h index 5c143a657..0b4084aa9 100644 --- a/src/server.h +++ b/src/server.h @@ -219,7 +219,7 @@ public: void onMapEditEvent(const MapEditEvent &event); // Connection must be locked when called - std::wstring getStatusString(); + std::string getStatusString(); inline double getUptime() const { return m_uptime_counter->get(); } // read shutdown state @@ -495,10 +495,8 @@ private: void handleChatInterfaceEvent(ChatEvent *evt); // This returns the answer to the sender of wmessage, or "" if there is none - std::wstring handleChat(const std::string &name, const std::wstring &wname, - std::wstring wmessage_input, - bool check_shout_priv = false, - RemotePlayer *player = NULL); + std::wstring handleChat(const std::string &name, std::wstring wmessage_input, + bool check_shout_priv = false, RemotePlayer *player = nullptr); void handleAdminChat(const ChatEventChat *evt); // When called, connection mutex should be locked diff --git a/src/unittest/test_utilities.cpp b/src/unittest/test_utilities.cpp index 5559cdbf2..93ba3f844 100644 --- a/src/unittest/test_utilities.cpp +++ b/src/unittest/test_utilities.cpp @@ -247,8 +247,8 @@ void TestUtilities::testStartsWith() void TestUtilities::testStrEqual() { - UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc"))); - UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true)); + UASSERT(str_equal(utf8_to_wide("abc"), utf8_to_wide("abc"))); + UASSERT(str_equal(utf8_to_wide("ABC"), utf8_to_wide("abc"), true)); } diff --git a/src/util/string.cpp b/src/util/string.cpp index 7e6d6d3b3..611ad35cb 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -175,62 +175,6 @@ wchar_t *utf8_to_wide_c(const char *str) return ret_c; } -// You must free the returned string! -// The returned string is allocated using new -wchar_t *narrow_to_wide_c(const char *str) -{ - wchar_t *nstr = nullptr; -#if defined(_WIN32) - int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0); - if (nResult == 0) { - errorstream<<"gettext: MultiByteToWideChar returned null"< wcs(wcl + 1); - size_t len = mbstowcs(*wcs, mbs.c_str(), wcl); - if (len == (size_t)(-1)) - return L""; - wcs[len] = 0; - return *wcs; -#endif -} - - -std::string wide_to_narrow(const std::wstring &wcs) -{ -#ifdef __ANDROID__ - return wide_to_utf8(wcs); -#else - size_t mbl = wcs.size() * 4; - SharedBuffer mbs(mbl+1); - size_t len = wcstombs(*mbs, wcs.c_str(), mbl); - if (len == (size_t)(-1)) - return "Character conversion failed!"; - - mbs[len] = 0; - return *mbs; -#endif -} - std::string urlencode(const std::string &str) { @@ -757,7 +701,8 @@ void translate_string(const std::wstring &s, Translations *translations, } else { // This is an escape sequence *inside* the template string to translate itself. // This should not happen, show an error message. - errorstream << "Ignoring escape sequence '" << wide_to_narrow(escape_sequence) << "' in translation" << std::endl; + errorstream << "Ignoring escape sequence '" + << wide_to_utf8(escape_sequence) << "' in translation" << std::endl; } } diff --git a/src/util/string.h b/src/util/string.h index ec14e9a2d..d4afcaec8 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -73,16 +73,6 @@ std::string wide_to_utf8(const std::wstring &input); // The returned string is allocated using new[] wchar_t *utf8_to_wide_c(const char *str); -// NEVER use those two functions unless you have a VERY GOOD reason to -// they just convert between wide and multibyte encoding -// multibyte encoding depends on current locale, this is no good, especially on Windows - -// You must free the returned string! -// The returned string is allocated using new -wchar_t *narrow_to_wide_c(const char *str); -std::wstring narrow_to_wide(const std::string &mbs); -std::string wide_to_narrow(const std::wstring &wcs); - std::string urlencode(const std::string &str); std::string urldecode(const std::string &str); u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask); @@ -355,11 +345,6 @@ inline s32 mystoi(const std::string &str, s32 min, s32 max) return i; } - -// MSVC2010 includes it's own versions of these -//#if !defined(_MSC_VER) || _MSC_VER < 1600 - - /** * Returns a 32-bit value reprensented by the string \p str (decimal). * @see atoi(3) for further limitations @@ -369,17 +354,6 @@ inline s32 mystoi(const std::string &str) return atoi(str.c_str()); } - -/** - * Returns s 32-bit value represented by the wide string \p str (decimal). - * @see atoi(3) for further limitations - */ -inline s32 mystoi(const std::wstring &str) -{ - return mystoi(wide_to_narrow(str)); -} - - /** * Returns a float reprensented by the string \p str (decimal). * @see atof(3) @@ -389,8 +363,6 @@ inline float mystof(const std::string &str) return atof(str.c_str()); } -//#endif - #define stoi mystoi #define stof mystof -- cgit v1.2.3 From fbb9ef3818b17894843f967c0c23b5d57a1e3771 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sat, 6 Feb 2021 12:46:45 +0000 Subject: Reduce ore noise_parms error to deprecation warning (#10921) Fixes #10914 --- src/script/lua_api/l_mapgen.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/script/lua_api') diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 183f20540..12a497b1e 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -1336,10 +1336,8 @@ int ModApiMapgen::l_register_ore(lua_State *L) 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; + log_deprecated(L, + "register_ore: ore type requires 'noise_params' but it is not specified, falling back to defaults"); } lua_pop(L, 1); -- cgit v1.2.3 From 3a8c37181a9bf9624f3243e8e884f12ae7692609 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Sun, 7 Feb 2021 15:27:24 +0000 Subject: Use consistent temp folder path (#10892) --- builtin/mainmenu/common.lua | 36 ++++++++---------------------------- doc/menu_lua_api.txt | 1 + src/defaultsettings.cpp | 1 - src/filesys.cpp | 6 ++---- src/script/lua_api/l_mainmenu.cpp | 11 +++++++++++ src/script/lua_api/l_mainmenu.h | 2 ++ 6 files changed, 24 insertions(+), 33 deletions(-) (limited to 'src/script/lua_api') diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua index 01f9a30b9..cd896f9ec 100644 --- a/builtin/mainmenu/common.lua +++ b/builtin/mainmenu/common.lua @@ -146,35 +146,15 @@ end -------------------------------------------------------------------------------- os.tempfolder = function() - if core.settings:get("TMPFolder") then - return core.settings:get("TMPFolder") .. DIR_DELIM .. "MT_" .. math.random(0,10000) - end - - local filetocheck = os.tmpname() - os.remove(filetocheck) - - -- luacheck: ignore - -- https://blogs.msdn.microsoft.com/vcblog/2014/06/18/c-runtime-crt-features-fixes-and-breaking-changes-in-visual-studio-14-ctp1/ - -- The C runtime (CRT) function called by os.tmpname is tmpnam. - -- Microsofts tmpnam implementation in older CRT / MSVC releases is defective. - -- tmpnam return values starting with a backslash characterize this behavior. - -- https://sourceforge.net/p/mingw-w64/bugs/555/ - -- MinGW tmpnam implementation is forwarded to the CRT directly. - -- https://sourceforge.net/p/mingw-w64/discussion/723797/thread/55520785/ - -- MinGW links to an older CRT release (msvcrt.dll). - -- Due to legal concerns MinGW will never use a newer CRT. - -- - -- Make use of TEMP to compose the temporary filename if an old - -- style tmpnam return value is detected. - if filetocheck:sub(1, 1) == "\\" then - local tempfolder = os.getenv("TEMP") - return tempfolder .. filetocheck - end + local temp = core.get_temp_path() + return temp .. DIR_DELIM .. "MT_" .. math.random(0, 10000) +end - local randname = "MTTempModFolder_" .. math.random(0,10000) - local backstring = filetocheck:reverse() - return filetocheck:sub(0, filetocheck:len() - backstring:find(DIR_DELIM) + 1) .. - randname +-------------------------------------------------------------------------------- +os.tmpname = function() + local path = os.tempfolder() + io.open(path, "w"):close() + return path end -------------------------------------------------------------------------------- diff --git a/doc/menu_lua_api.txt b/doc/menu_lua_api.txt index db49c1736..b3975bc1d 100644 --- a/doc/menu_lua_api.txt +++ b/doc/menu_lua_api.txt @@ -85,6 +85,7 @@ core.get_video_drivers() core.get_mapgen_names([include_hidden=false]) -> table of map generator algorithms registered in the core (possible in async calls) core.get_cache_path() -> path of cache +core.get_temp_path() -> path of temp folder HTTP Requests diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp index d34ec324b..41c4922a4 100644 --- a/src/defaultsettings.cpp +++ b/src/defaultsettings.cpp @@ -461,7 +461,6 @@ void set_default_settings() settings->setDefault("screen_h", "0"); settings->setDefault("fullscreen", "true"); settings->setDefault("touchtarget", "true"); - settings->setDefault("TMPFolder", porting::path_cache); settings->setDefault("touchscreen_threshold","20"); settings->setDefault("fixed_virtual_joystick", "false"); settings->setDefault("virtual_joystick_triggers_aux", "false"); diff --git a/src/filesys.cpp b/src/filesys.cpp index eeba0c564..5ffb4506e 100644 --- a/src/filesys.cpp +++ b/src/filesys.cpp @@ -27,9 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "config.h" #include "porting.h" -#ifdef __ANDROID__ -#include "settings.h" // For g_settings -#endif namespace fs { @@ -359,8 +356,9 @@ std::string TempPath() compatible with lua's os.tmpname which under the default configuration hardcodes mkstemp("/tmp/lua_XXXXXX"). */ + #ifdef __ANDROID__ - return g_settings->get("TMPFolder"); + return porting::path_cache; #else return DIR_DELIM "tmp"; #endif diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index 4733c4003..ba7f708a4 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -529,6 +529,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( @@ -537,12 +538,20 @@ 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()); return 1; } +/******************************************************************************/ +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); @@ -942,6 +951,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); @@ -975,6 +985,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); diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h index 580a0df72..49ce7c251 100644 --- a/src/script/lua_api/l_mainmenu.h +++ b/src/script/lua_api/l_mainmenu.h @@ -122,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); -- cgit v1.2.3