diff options
author | Loïc Blot <nerzhul@users.noreply.github.com> | 2017-08-20 13:30:50 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-20 13:30:50 +0200 |
commit | 1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3 (patch) | |
tree | 03dd0c39e323c7f0b1f06014ff30e74f429bfa01 /src/script | |
parent | 50669cd2822a11570ae462972194eeb2d585a8c1 (diff) | |
download | minetest-1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3.tar.gz minetest-1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3.tar.bz2 minetest-1c1c97cbd1d7913ac12bf550ec02c97f843a0fd3.zip |
Modernize source code: last part (#6285)
* Modernize source code: last par
* Use empty when needed
* Use emplace_back instead of push_back when needed
* For range-based loops
* Initializers fixes
* constructors, destructors default
* c++ C stl includes
Diffstat (limited to 'src/script')
27 files changed, 160 insertions, 209 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 8d94235ce..ddcdd803d 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -91,7 +91,7 @@ void read_item_definition(lua_State* L, int index, // If name is "" (hand), ensure there are ToolCapabilities // because it will be looked up there whenever any other item has // no ToolCapabilities - if(def.name == "" && def.tool_capabilities == NULL){ + if (def.name.empty() && def.tool_capabilities == NULL){ def.tool_capabilities = new ToolCapabilities(); } @@ -212,9 +212,9 @@ void read_object_properties(lua_State *L, int index, while(lua_next(L, table) != 0){ // key at index -2 and value at index -1 if(lua_isstring(L, -1)) - prop->textures.push_back(lua_tostring(L, -1)); + prop->textures.emplace_back(lua_tostring(L, -1)); else - prop->textures.push_back(""); + prop->textures.emplace_back(""); // removes value, keeps key for next iteration lua_pop(L, 1); } @@ -303,18 +303,16 @@ void push_object_properties(lua_State *L, ObjectProperties *prop) lua_newtable(L); u16 i = 1; - for (std::vector<std::string>::iterator it = prop->textures.begin(); - it != prop->textures.end(); ++it) { - lua_pushlstring(L, it->c_str(), it->size()); + for (const std::string &texture : prop->textures) { + lua_pushlstring(L, texture.c_str(), texture.size()); lua_rawseti(L, -2, i); } lua_setfield(L, -2, "textures"); lua_newtable(L); i = 1; - for (std::vector<video::SColor>::iterator it = prop->colors.begin(); - it != prop->colors.end(); ++it) { - push_ARGB8(L, *it); + for (const video::SColor &color : prop->colors) { + push_ARGB8(L, color); lua_rawseti(L, -2, i); } lua_setfield(L, -2, "colors"); @@ -564,7 +562,7 @@ ContentFeatures read_content_features(lua_State *L, int index) f.param_type_2 = (ContentParamType2)getenumfield(L, index, "paramtype2", ScriptApiNode::es_ContentParamType2, CPT2_NONE); - if (f.palette_name != "" && + if (!f.palette_name.empty() && !(f.param_type_2 == CPT2_COLOR || f.param_type_2 == CPT2_COLORED_FACEDIR || f.param_type_2 == CPT2_COLORED_WALLMOUNTED)) @@ -646,7 +644,7 @@ ContentFeatures read_content_features(lua_State *L, int index) lua_pushnil(L); while (lua_next(L, table) != 0) { // Value at -1 - f.connects_to.push_back(lua_tostring(L, -1)); + f.connects_to.emplace_back(lua_tostring(L, -1)); lua_pop(L, 1); } } @@ -771,9 +769,8 @@ void push_content_features(lua_State *L, const ContentFeatures &c) lua_newtable(L); u16 i = 1; - for (std::vector<std::string>::const_iterator it = c.connects_to.begin(); - it != c.connects_to.end(); ++it) { - lua_pushlstring(L, it->c_str(), it->size()); + for (const std::string &it : c.connects_to) { + lua_pushlstring(L, it.c_str(), it.size()); lua_rawseti(L, -2, i); } lua_setfield(L, -2, "connects_to"); @@ -893,9 +890,8 @@ void push_box(lua_State *L, const std::vector<aabb3f> &box) { lua_newtable(L); u8 i = 1; - for (std::vector<aabb3f>::const_iterator it = box.begin(); - it != box.end(); ++it) { - push_aabb3f(L, (*it)); + for (const aabb3f &it : box) { + push_aabb3f(L, it); lua_rawseti(L, -2, i); } } @@ -987,21 +983,19 @@ NodeBox read_nodebox(lua_State *L, int index) nodebox.type = (NodeBoxType)getenumfield(L, index, "type", ScriptApiNode::es_NodeBoxType, NODEBOX_REGULAR); -#define NODEBOXREAD(n, s) \ - do { \ +#define NODEBOXREAD(n, s){ \ lua_getfield(L, index, (s)); \ if (lua_istable(L, -1)) \ (n) = read_aabb3f(L, -1, BS); \ lua_pop(L, 1); \ - } while (0) + } #define NODEBOXREADVEC(n, s) \ - do { \ lua_getfield(L, index, (s)); \ if (lua_istable(L, -1)) \ (n) = read_aabb3f_vector(L, -1, BS); \ - lua_pop(L, 1); \ - } while (0) + lua_pop(L, 1); + NODEBOXREADVEC(nodebox.fixed, "fixed"); NODEBOXREAD(nodebox.wall_top, "wall_top"); NODEBOXREAD(nodebox.wall_bottom, "wall_bottom"); @@ -1037,7 +1031,7 @@ MapNode readnode(lua_State *L, int index, INodeDefManager *ndef) param2 = lua_tonumber(L, -1); lua_pop(L, 1); - return MapNode(ndef, name, param1, param2); + return {ndef, name, param1, param2}; } /******************************************************************************/ @@ -1096,18 +1090,17 @@ ItemStack read_item(lua_State* L, int index, IItemDefManager *idef) if(index < 0) index = lua_gettop(L) + 1 + index; - if(lua_isnil(L, index)) - { + if (lua_isnil(L, index)) { return ItemStack(); } - else if(lua_isuserdata(L, index)) - { + + if (lua_isuserdata(L, index)) { // Convert from LuaItemStack LuaItemStack *o = LuaItemStack::checkobject(L, index); return o->getItem(); } - else if(lua_isstring(L, index)) - { + + if (lua_isstring(L, index)) { // Convert from itemstring std::string itemstring = lua_tostring(L, index); try @@ -1168,18 +1161,16 @@ void push_tool_capabilities(lua_State *L, // Create groupcaps table lua_newtable(L); // For each groupcap - for (ToolGCMap::const_iterator i = toolcap.groupcaps.begin(); - i != toolcap.groupcaps.end(); ++i) { + for (const auto &gc_it : toolcap.groupcaps) { // Create groupcap table lua_newtable(L); - const std::string &name = i->first; - const ToolGroupCap &groupcap = i->second; + const std::string &name = gc_it.first; + const ToolGroupCap &groupcap = gc_it.second; // Create subtable "times" lua_newtable(L); - for (std::unordered_map<int, float>::const_iterator - i = groupcap.times.begin(); i != groupcap.times.end(); ++i) { - lua_pushinteger(L, i->first); - lua_pushnumber(L, i->second); + for (auto time : groupcap.times) { + lua_pushinteger(L, time.first); + lua_pushnumber(L, time.second); lua_settable(L, -3); } // Set subtable "times" @@ -1195,11 +1186,10 @@ void push_tool_capabilities(lua_State *L, //Create damage_groups table lua_newtable(L); // For each damage group - for (DamageGroup::const_iterator i = toolcap.damageGroups.begin(); - i != toolcap.damageGroups.end(); ++i) { + for (const auto &damageGroup : toolcap.damageGroups) { // Create damage group table - lua_pushinteger(L, i->second); - lua_setfield(L, -2, i->first.c_str()); + lua_pushinteger(L, damageGroup.second); + lua_setfield(L, -2, damageGroup.first.c_str()); } lua_setfield(L, -2, "damage_groups"); } @@ -1459,9 +1449,9 @@ void read_groups(lua_State *L, int index, ItemGroupList &result) void push_groups(lua_State *L, const ItemGroupList &groups) { lua_newtable(L); - for (ItemGroupList::const_iterator it = groups.begin(); it != groups.end(); ++it) { - lua_pushnumber(L, it->second); - lua_setfield(L, -2, it->first.c_str()); + for (const auto &group : groups) { + lua_pushnumber(L, group.second); + lua_setfield(L, -2, group.first.c_str()); } } @@ -1572,9 +1562,8 @@ static int push_json_value_getdepth(const Json::Value &value) return 1; int maxdepth = 0; - for (Json::Value::const_iterator it = value.begin(); - it != value.end(); ++it) { - int elemdepth = push_json_value_getdepth(*it); + for (const auto &it : value) { + int elemdepth = push_json_value_getdepth(it); if (elemdepth > maxdepth) maxdepth = elemdepth; } @@ -1646,8 +1635,8 @@ bool push_json_value(lua_State *L, const Json::Value &value, int nullindex) // of push_json_value_helper is 2, so make sure there a depth * 2 slots if (lua_checkstack(L, depth * 2)) return push_json_value_helper(L, value, nullindex); - else - return false; + + return false; } // Converts Lua table --> JSON diff --git a/src/script/common/c_content.h b/src/script/common/c_content.h index c4440c5d9..80a96e327 100644 --- a/src/script/common/c_content.h +++ b/src/script/common/c_content.h @@ -123,7 +123,6 @@ MapNode readnode (lua_State *L, int index, void pushnode (lua_State *L, const MapNode &n, INodeDefManager *ndef); -NodeBox read_nodebox (lua_State *L, int index); void read_groups (lua_State *L, int index, ItemGroupList &result); @@ -159,9 +158,6 @@ std::vector<ItemStack> read_items (lua_State *L, int index, Server* srv); -void read_soundspec (lua_State *L, - int index, - SimpleSoundSpec &spec); void push_soundspec (lua_State *L, const SimpleSoundSpec &spec); diff --git a/src/script/common/c_converter.cpp b/src/script/common/c_converter.cpp index 3426a9677..e5d89dea9 100644 --- a/src/script/common/c_converter.cpp +++ b/src/script/common/c_converter.cpp @@ -30,7 +30,7 @@ extern "C" { #include "constants.h" -#define CHECK_TYPE(index, name, type) do { \ +#define CHECK_TYPE(index, name, type) { \ int t = lua_type(L, (index)); \ if (t != (type)) { \ std::string traceback = script_get_backtrace(L); \ @@ -38,7 +38,7 @@ extern "C" { " (expected " + lua_typename(L, (type)) + \ " got " + lua_typename(L, t) + ").\n" + traceback); \ } \ - } while(0) + } #define CHECK_POS_COORD(name) CHECK_TYPE(-1, "position coordinate '" name "'", LUA_TNUMBER) #define CHECK_FLOAT_RANGE(value, name) \ if (value < F1000_MIN || value > F1000_MAX) { \ diff --git a/src/script/common/c_converter.h b/src/script/common/c_converter.h index bba01f545..f94996c88 100644 --- a/src/script/common/c_converter.h +++ b/src/script/common/c_converter.h @@ -76,8 +76,6 @@ void setfloatfield(lua_State *L, int table, const char *fieldname, float value); void setboolfield(lua_State *L, int table, const char *fieldname, bool value); -void setstringfield(lua_State *L, int table, - const char *fieldname, const char *value); v3f checkFloatPos (lua_State *L, int index); v2f check_v2f (lua_State *L, int index); diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h index 2548ac979..35477375b 100644 --- a/src/script/common/c_internal.h +++ b/src/script/common/c_internal.h @@ -58,12 +58,12 @@ extern "C" { #define PUSH_ERROR_HANDLER(L) \ (lua_rawgeti((L), LUA_REGISTRYINDEX, CUSTOM_RIDX_BACKTRACE), lua_gettop((L))) -#define PCALL_RESL(L, RES) do { \ +#define PCALL_RESL(L, RES) { \ int result_ = (RES); \ if (result_ != 0) { \ script_error((L), result_, NULL, __FUNCTION__); \ } \ -} while (0) +} #define script_run_callbacks(L, nargs, mode) \ script_run_callbacks_f((L), (nargs), (mode), __FUNCTION__) diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index 5cca5fc03..93a200c22 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -17,8 +17,8 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include <stdio.h> -#include <stdlib.h> +#include <cstdio> +#include <cstdlib> extern "C" { #include "lua.h" @@ -38,9 +38,8 @@ AsyncEngine::~AsyncEngine() { // Request all threads to stop - for (std::vector<AsyncWorkerThread *>::iterator it = workerThreads.begin(); - it != workerThreads.end(); ++it) { - (*it)->stop(); + for (AsyncWorkerThread *workerThread : workerThreads) { + workerThread->stop(); } @@ -51,15 +50,13 @@ AsyncEngine::~AsyncEngine() } // Wait for threads to finish - for (std::vector<AsyncWorkerThread *>::iterator it = workerThreads.begin(); - it != workerThreads.end(); ++it) { - (*it)->wait(); + for (AsyncWorkerThread *workerThread : workerThreads) { + workerThread->wait(); } // Force kill all threads - for (std::vector<AsyncWorkerThread *>::iterator it = workerThreads.begin(); - it != workerThreads.end(); ++it) { - delete *it; + for (AsyncWorkerThread *workerThread : workerThreads) { + delete workerThread; } jobQueueMutex.lock(); @@ -192,9 +189,8 @@ void AsyncEngine::pushFinishedJobs(lua_State* L) { /******************************************************************************/ void AsyncEngine::prepareEnvironment(lua_State* L, int top) { - for (std::vector<StateInitializer>::iterator it = stateInitializers.begin(); - it != stateInitializers.end(); it++) { - (*it)(L, top); + for (StateInitializer &stateInitializer : stateInitializers) { + stateInitializer(L, top); } } @@ -202,7 +198,6 @@ void AsyncEngine::prepareEnvironment(lua_State* L, int top) AsyncWorkerThread::AsyncWorkerThread(AsyncEngine* jobDispatcher, const std::string &name) : Thread(name), - ScriptApiBase(), jobDispatcher(jobDispatcher) { lua_State *L = getStack(); diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h index 38eb4800a..b1f4bf45f 100644 --- a/src/script/cpp_api/s_async.h +++ b/src/script/cpp_api/s_async.h @@ -68,7 +68,7 @@ class AsyncEngine { friend class AsyncWorkerThread; typedef void (*StateInitializer)(lua_State *L, int top); public: - AsyncEngine() {}; + AsyncEngine() = default; ~AsyncEngine(); /** diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp index 6bea8230b..32a99826e 100644 --- a/src/script/cpp_api/s_base.cpp +++ b/src/script/cpp_api/s_base.cpp @@ -40,7 +40,7 @@ extern "C" { #endif } -#include <stdio.h> +#include <cstdio> #include <cstdarg> #include "script/common/c_content.h" #include <sstream> diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h index b2c8b4a18..a170f82dc 100644 --- a/src/script/cpp_api/s_base.h +++ b/src/script/cpp_api/s_base.h @@ -40,12 +40,12 @@ extern "C" { // use that name to bypass security! #define BUILTIN_MOD_NAME "*builtin*" -#define PCALL_RES(RES) do { \ +#define PCALL_RES(RES) { \ int result_ = (RES); \ if (result_ != 0) { \ scriptError(result_, __FUNCTION__); \ } \ -} while (0) +} #define runCallbacks(nargs, mode) \ runCallbacksRaw((nargs), (mode), __FUNCTION__) diff --git a/src/script/cpp_api/s_inventory.cpp b/src/script/cpp_api/s_inventory.cpp index c90c7d4e2..251ddb221 100644 --- a/src/script/cpp_api/s_inventory.cpp +++ b/src/script/cpp_api/s_inventory.cpp @@ -218,8 +218,7 @@ bool ScriptApiDetached::getDetachedInventoryCallback( lua_getfield(L, -1, name.c_str()); lua_remove(L, -2); // Should be a table - if(lua_type(L, -1) != LUA_TTABLE) - { + if (lua_type(L, -1) != LUA_TTABLE) { errorstream<<"Detached inventory \""<<name<<"\" not defined"<<std::endl; lua_pop(L, 1); return false; @@ -230,20 +229,17 @@ bool ScriptApiDetached::getDetachedInventoryCallback( lua_getfield(L, -1, callbackname); lua_remove(L, -2); // Should be a function or nil - if(lua_type(L, -1) == LUA_TFUNCTION) - { + if (lua_type(L, -1) == LUA_TFUNCTION) { return true; } - else if(lua_isnil(L, -1)) - { - lua_pop(L, 1); - return false; - } - else - { - errorstream<<"Detached inventory \""<<name<<"\" callback \"" - <<callbackname<<"\" is not a function"<<std::endl; + + if (lua_isnil(L, -1)) { lua_pop(L, 1); return false; } + + errorstream << "Detached inventory \"" << name << "\" callback \"" + << callbackname << "\" is not a function" << std::endl; + lua_pop(L, 1); + return false; } diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp index 032018f2f..d48a3aee9 100644 --- a/src/script/cpp_api/s_item.cpp +++ b/src/script/cpp_api/s_item.cpp @@ -113,12 +113,12 @@ bool ScriptApiItem::item_OnUse(ItemStack &item, bool ScriptApiItem::item_OnSecondaryUse(ItemStack &item, ServerActiveObject *user) { SCRIPTAPI_PRECHECKHEADER - + int error_handler = PUSH_ERROR_HANDLER(L); - + if (!getItemCallback(item.name.c_str(), "on_secondary_use")) return false; - + LuaItemStack::create(L, item); objectrefGetOrCreate(L, user); PointedThing pointed; @@ -237,7 +237,9 @@ bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname) // Should be a function or nil if (lua_type(L, -1) == LUA_TFUNCTION) { return true; - } else if (!lua_isnil(L, -1)) { + } + + if (!lua_isnil(L, -1)) { errorstream << "Item \"" << name << "\" callback \"" << callbackname << "\" is not a function" << std::endl; } diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp index aa28e3fb5..591e26975 100644 --- a/src/script/cpp_api/s_node.cpp +++ b/src/script/cpp_api/s_node.cpp @@ -93,12 +93,6 @@ struct EnumString ScriptApiNode::es_NodeBoxType[] = {0, NULL}, }; -ScriptApiNode::ScriptApiNode() { -} - -ScriptApiNode::~ScriptApiNode() { -} - bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node, ServerActiveObject *puncher, PointedThing pointed) { @@ -198,7 +192,7 @@ bool ScriptApiNode::node_on_flood(v3s16 p, MapNode node, MapNode newnode) pushnode(L, newnode, ndef); PCALL_RES(lua_pcall(L, 3, 1, error_handler)); lua_remove(L, error_handler); - return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true; + return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1); } void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node) @@ -237,7 +231,7 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime) lua_pushnumber(L,dtime); PCALL_RES(lua_pcall(L, 2, 1, error_handler)); lua_remove(L, error_handler); - return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true; + return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1); } void ScriptApiNode::node_on_receive_fields(v3s16 p, diff --git a/src/script/cpp_api/s_node.h b/src/script/cpp_api/s_node.h index 92f07cee7..5b6509c83 100644 --- a/src/script/cpp_api/s_node.h +++ b/src/script/cpp_api/s_node.h @@ -32,8 +32,8 @@ class ScriptApiNode public ScriptApiNodemeta { public: - ScriptApiNode(); - virtual ~ScriptApiNode(); + ScriptApiNode() = default; + virtual ~ScriptApiNode() = default; bool node_on_punch(v3s16 p, MapNode node, ServerActiveObject *puncher, PointedThing pointed); diff --git a/src/script/cpp_api/s_nodemeta.cpp b/src/script/cpp_api/s_nodemeta.cpp index d050c0bc9..2e6d3a373 100644 --- a/src/script/cpp_api/s_nodemeta.cpp +++ b/src/script/cpp_api/s_nodemeta.cpp @@ -232,12 +232,3 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p, PCALL_RES(lua_pcall(L, 5, 0, error_handler)); lua_pop(L, 1); // Pop error handler } - -ScriptApiNodemeta::ScriptApiNodemeta() -{ -} - -ScriptApiNodemeta::~ScriptApiNodemeta() -{ -} - diff --git a/src/script/cpp_api/s_nodemeta.h b/src/script/cpp_api/s_nodemeta.h index 51b8a5eb2..4d3257dcf 100644 --- a/src/script/cpp_api/s_nodemeta.h +++ b/src/script/cpp_api/s_nodemeta.h @@ -30,8 +30,8 @@ class ScriptApiNodemeta public ScriptApiItem { public: - ScriptApiNodemeta(); - virtual ~ScriptApiNodemeta(); + ScriptApiNodemeta() = default; + virtual ~ScriptApiNodemeta() = default; // Return number of accepted items to be moved int nodemeta_inventory_AllowMove(v3s16 p, diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp index a8c07476c..b7f2f10f9 100644 --- a/src/script/cpp_api/s_player.cpp +++ b/src/script/cpp_api/s_player.cpp @@ -192,8 +192,3 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player, runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC); } -ScriptApiPlayer::~ScriptApiPlayer() -{ -} - - diff --git a/src/script/cpp_api/s_player.h b/src/script/cpp_api/s_player.h index 70b06bfc7..faf394de5 100644 --- a/src/script/cpp_api/s_player.h +++ b/src/script/cpp_api/s_player.h @@ -28,7 +28,7 @@ struct ToolCapabilities; class ScriptApiPlayer : virtual public ScriptApiBase { public: - virtual ~ScriptApiPlayer(); + virtual ~ScriptApiPlayer() = default; void on_newplayer(ServerActiveObject *player); void on_dieplayer(ServerActiveObject *player); diff --git a/src/script/cpp_api/s_security.cpp b/src/script/cpp_api/s_security.cpp index 761597701..690a3b47f 100644 --- a/src/script/cpp_api/s_security.cpp +++ b/src/script/cpp_api/s_security.cpp @@ -260,7 +260,7 @@ void ScriptApiSecurity::initializeSecurityClient() static const char *os_whitelist[] = { "clock", "date", - "difftime", + "difftime", "time", "setlocale", }; @@ -504,7 +504,7 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path, // by the operating system anyways. return false; } - removed = component + (removed.empty() ? "" : DIR_DELIM + removed); + removed.append(component).append(removed.empty() ? "" : DIR_DELIM + removed); abs_path = fs::AbsolutePath(cur_path); } if (abs_path.empty()) @@ -550,9 +550,9 @@ bool ScriptApiSecurity::checkPath(lua_State *L, const char *path, // Allow read-only access to all mod directories if (!write_required) { - const std::vector<ModSpec> mods = gamedef->getMods(); - for (size_t i = 0; i < mods.size(); ++i) { - str = fs::AbsolutePath(mods[i].path); + const std::vector<ModSpec> &mods = gamedef->getMods(); + for (const ModSpec &mod : mods) { + str = fs::AbsolutePath(mod.path); if (!str.empty() && fs::PathStartsWith(abs_path, str)) { return true; } @@ -617,7 +617,9 @@ int ScriptApiSecurity::sl_g_load(lua_State *L) int t = lua_type(L, -1); if (t == LUA_TNIL) { break; - } else if (t != LUA_TSTRING) { + } + + if (t != LUA_TSTRING) { lua_pushnil(L); lua_pushliteral(L, "Loader didn't return a string"); return 2; diff --git a/src/script/lua_api/l_client.cpp b/src/script/lua_api/l_client.cpp index 6a87b20e9..81bf49329 100644 --- a/src/script/lua_api/l_client.cpp +++ b/src/script/lua_api/l_client.cpp @@ -34,7 +34,6 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/string.h" #include "nodedef.h" - int ModApiClient::l_get_current_modname(lua_State *L) { lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME); diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp index 94edf201e..07d33a4eb 100644 --- a/src/script/lua_api/l_env.cpp +++ b/src/script/lua_api/l_env.cpp @@ -743,7 +743,7 @@ int ModApiEnvMod::l_find_node_near(lua_State *L) for (int d = start_radius; d <= radius; d++) { std::vector<v3s16> list = FacePositionCache::getFacePositions(d); - for (v3s16 i : list) { + for (const v3s16 &i : list) { v3s16 p = pos + i; content_t c = env->getMap().getNodeNoEx(p).getContent(); if (filter.count(c) != 0) { @@ -1127,7 +1127,7 @@ int ModApiEnvMod::l_find_path(lua_State *L) lua_newtable(L); int top = lua_gettop(L); unsigned int index = 1; - for (v3s16 i : path) { + for (const v3s16 &i : path) { lua_pushnumber(L,index); push_v3s16(L, i); lua_settable(L, top); diff --git a/src/script/lua_api/l_inventory.cpp b/src/script/lua_api/l_inventory.cpp index b0d43090c..f3097582e 100644 --- a/src/script/lua_api/l_inventory.cpp +++ b/src/script/lua_api/l_inventory.cpp @@ -495,28 +495,29 @@ int ModApiInventory::l_get_inventory(lua_State *L) v3s16 pos = check_v3s16(L, -1); loc.setNodeMeta(pos); - if(getServer(L)->getInventory(loc) != NULL) + if (getServer(L)->getInventory(loc) != NULL) InvRef::create(L, loc); else lua_pushnil(L); return 1; - } else { - NO_MAP_LOCK_REQUIRED; - if(type == "player"){ - std::string name = checkstringfield(L, 1, "name"); - loc.setPlayer(name); - } else if(type == "detached"){ - std::string name = checkstringfield(L, 1, "name"); - loc.setDetached(name); - } + } - if(getServer(L)->getInventory(loc) != NULL) - InvRef::create(L, loc); - else - lua_pushnil(L); - return 1; - // END NO_MAP_LOCK_REQUIRED; + NO_MAP_LOCK_REQUIRED; + if (type == "player") { + std::string name = checkstringfield(L, 1, "name"); + loc.setPlayer(name); + } else if (type == "detached") { + std::string name = checkstringfield(L, 1, "name"); + loc.setDetached(name); } + + if (getServer(L)->getInventory(loc) != NULL) + InvRef::create(L, loc); + else + lua_pushnil(L); + return 1; + // END NO_MAP_LOCK_REQUIRED; + } // create_detached_inventory_raw(name, [player_name]) diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index b0ce6b740..86305de82 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -233,23 +233,22 @@ int ModApiMainMenu::l_get_worlds(lua_State *L) int top = lua_gettop(L); unsigned int index = 1; - for (unsigned int i = 0; i < worlds.size(); i++) - { + for (const WorldSpec &world : worlds) { lua_pushnumber(L,index); lua_newtable(L); int top_lvl2 = lua_gettop(L); lua_pushstring(L,"path"); - lua_pushstring(L,worlds[i].path.c_str()); + lua_pushstring(L, world.path.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"name"); - lua_pushstring(L,worlds[i].name.c_str()); + lua_pushstring(L, world.name.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"gameid"); - lua_pushstring(L,worlds[i].gameid.c_str()); + lua_pushstring(L, world.gameid.c_str()); lua_settable(L, top_lvl2); lua_settable(L, top); @@ -267,40 +266,38 @@ int ModApiMainMenu::l_get_games(lua_State *L) int top = lua_gettop(L); unsigned int index = 1; - for (unsigned int i = 0; i < games.size(); i++) - { + for (const SubgameSpec &game : games) { lua_pushnumber(L,index); lua_newtable(L); int top_lvl2 = lua_gettop(L); lua_pushstring(L,"id"); - lua_pushstring(L,games[i].id.c_str()); + lua_pushstring(L, game.id.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"path"); - lua_pushstring(L,games[i].path.c_str()); + lua_pushstring(L, game.path.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"gamemods_path"); - lua_pushstring(L,games[i].gamemods_path.c_str()); + lua_pushstring(L, game.gamemods_path.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"name"); - lua_pushstring(L,games[i].name.c_str()); + lua_pushstring(L, game.name.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"menuicon_path"); - lua_pushstring(L,games[i].menuicon_path.c_str()); + lua_pushstring(L, game.menuicon_path.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"addon_mods_paths"); lua_newtable(L); int table2 = lua_gettop(L); int internal_index=1; - for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin(); - iter != games[i].addon_mods_paths.end(); ++iter) { + for (const std::string &addon_mods_path : game.addon_mods_paths) { lua_pushnumber(L,internal_index); - lua_pushstring(L,(*iter).c_str()); + lua_pushstring(L, addon_mods_path.c_str()); lua_settable(L, table2); internal_index++; } @@ -331,112 +328,111 @@ int ModApiMainMenu::l_get_favorites(lua_State *L) int top = lua_gettop(L); unsigned int index = 1; - for (unsigned int i = 0; i < servers.size(); i++) - { + for (const Json::Value &server : servers) { lua_pushnumber(L,index); lua_newtable(L); int top_lvl2 = lua_gettop(L); - if (servers[i]["clients"].asString().size()) { - std::string clients_raw = servers[i]["clients"].asString(); + 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 != "") && (*endptr == 0)) { + if ((!clients_raw.empty()) && (*endptr == 0)) { lua_pushstring(L,"clients"); lua_pushnumber(L,numbervalue); lua_settable(L, top_lvl2); } } - if (servers[i]["clients_max"].asString().size()) { + if (!server["clients_max"].asString().empty()) { - std::string clients_max_raw = servers[i]["clients_max"].asString(); + 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 != "") && (*endptr == 0)) { + if ((!clients_max_raw.empty()) && (*endptr == 0)) { lua_pushstring(L,"clients_max"); lua_pushnumber(L,numbervalue); lua_settable(L, top_lvl2); } } - if (servers[i]["version"].asString().size()) { + if (!server["version"].asString().empty()) { lua_pushstring(L,"version"); - std::string topush = servers[i]["version"].asString(); + std::string topush = server["version"].asString(); lua_pushstring(L,topush.c_str()); lua_settable(L, top_lvl2); } - if (servers[i]["proto_min"].asString().size()) { + if (!server["proto_min"].asString().empty()) { lua_pushstring(L,"proto_min"); - lua_pushinteger(L,servers[i]["proto_min"].asInt()); + lua_pushinteger(L, server["proto_min"].asInt()); lua_settable(L, top_lvl2); } - if (servers[i]["proto_max"].asString().size()) { + if (!server["proto_max"].asString().empty()) { lua_pushstring(L,"proto_max"); - lua_pushinteger(L,servers[i]["proto_max"].asInt()); + lua_pushinteger(L, server["proto_max"].asInt()); lua_settable(L, top_lvl2); } - if (servers[i]["password"].asString().size()) { + if (!server["password"].asString().empty()) { lua_pushstring(L,"password"); - lua_pushboolean(L,servers[i]["password"].asBool()); + lua_pushboolean(L, server["password"].asBool()); lua_settable(L, top_lvl2); } - if (servers[i]["creative"].asString().size()) { + if (!server["creative"].asString().empty()) { lua_pushstring(L,"creative"); - lua_pushboolean(L,servers[i]["creative"].asBool()); + lua_pushboolean(L, server["creative"].asBool()); lua_settable(L, top_lvl2); } - if (servers[i]["damage"].asString().size()) { + if (!server["damage"].asString().empty()) { lua_pushstring(L,"damage"); - lua_pushboolean(L,servers[i]["damage"].asBool()); + lua_pushboolean(L, server["damage"].asBool()); lua_settable(L, top_lvl2); } - if (servers[i]["pvp"].asString().size()) { + if (!server["pvp"].asString().empty()) { lua_pushstring(L,"pvp"); - lua_pushboolean(L,servers[i]["pvp"].asBool()); + lua_pushboolean(L, server["pvp"].asBool()); lua_settable(L, top_lvl2); } - if (servers[i]["description"].asString().size()) { + if (!server["description"].asString().empty()) { lua_pushstring(L,"description"); - std::string topush = servers[i]["description"].asString(); + std::string topush = server["description"].asString(); lua_pushstring(L,topush.c_str()); lua_settable(L, top_lvl2); } - if (servers[i]["name"].asString().size()) { + if (!server["name"].asString().empty()) { lua_pushstring(L,"name"); - std::string topush = servers[i]["name"].asString(); + std::string topush = server["name"].asString(); lua_pushstring(L,topush.c_str()); lua_settable(L, top_lvl2); } - if (servers[i]["address"].asString().size()) { + if (!server["address"].asString().empty()) { lua_pushstring(L,"address"); - std::string topush = servers[i]["address"].asString(); + std::string topush = server["address"].asString(); lua_pushstring(L,topush.c_str()); lua_settable(L, top_lvl2); } - if (servers[i]["port"].asString().size()) { + if (!server["port"].asString().empty()) { lua_pushstring(L,"port"); - std::string topush = servers[i]["port"].asString(); + std::string topush = server["port"].asString(); lua_pushstring(L,topush.c_str()); lua_settable(L, top_lvl2); } - if (servers[i].isMember("ping")) { - float ping = servers[i]["ping"].asFloat(); + if (server.isMember("ping")) { + float ping = server["ping"].asFloat(); lua_pushstring(L, "ping"); lua_pushnumber(L, ping); lua_settable(L, top_lvl2); @@ -558,7 +554,7 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L) GUIEngine* engine = getGuiEngine(L); sanity_check(engine != NULL); - std::string text = ""; + std::string text; if (!lua_isnone(L,1) && !lua_isnil(L,1)) text = luaL_checkstring(L, 1); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 454e8f719..a5e122ddb 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -847,9 +847,8 @@ int ModApiMapgen::l_get_gen_notify(lua_State *L) lua_newtable(L); int i = 1; - for (std::set<u32>::iterator it = emerge->gen_notify_on_deco_ids.begin(); - it != emerge->gen_notify_on_deco_ids.end(); ++it) { - lua_pushnumber(L, *it); + for (u32 gen_notify_on_deco_id : emerge->gen_notify_on_deco_ids) { + lua_pushnumber(L, gen_notify_on_deco_id); lua_rawseti(L, -2, i); i++; } @@ -1322,7 +1321,7 @@ int ModApiMapgen::l_create_schematic(lua_State *L) lua_pop(L, 1); u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS); - prob_list.push_back(std::make_pair(pos, prob)); + prob_list.emplace_back(pos, prob); } lua_pop(L, 1); @@ -1336,7 +1335,7 @@ int ModApiMapgen::l_create_schematic(lua_State *L) if (lua_istable(L, -1)) { s16 ypos = getintfield_default(L, -1, "ypos", 0); u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS); - slice_prob_list.push_back(std::make_pair(ypos, prob)); + slice_prob_list.emplace_back(ypos, prob); } lua_pop(L, 1); diff --git a/src/script/lua_api/l_metadata.cpp b/src/script/lua_api/l_metadata.cpp index 5f4e984cb..494d2dce0 100644 --- a/src/script/lua_api/l_metadata.cpp +++ b/src/script/lua_api/l_metadata.cpp @@ -218,10 +218,9 @@ void MetaDataRef::handleToTable(lua_State *L, Metadata *meta) lua_newtable(L); { const StringMap &fields = meta->getStrings(); - for (StringMap::const_iterator - it = fields.begin(); it != fields.end(); ++it) { - const std::string &name = it->first; - const std::string &value = it->second; + for (const auto &field : fields) { + const std::string &name = field.first; + const std::string &value = field.second; lua_pushlstring(L, name.c_str(), name.size()); lua_pushlstring(L, value.c_str(), value.size()); lua_settable(L, -3); diff --git a/src/script/lua_api/l_metadata.h b/src/script/lua_api/l_metadata.h index 5a7c7e7f7..e0e9696cb 100644 --- a/src/script/lua_api/l_metadata.h +++ b/src/script/lua_api/l_metadata.h @@ -31,7 +31,7 @@ class Metadata; class MetaDataRef : public ModApiBase { public: - virtual ~MetaDataRef() {} + virtual ~MetaDataRef() = default; protected: static MetaDataRef *checkobject(lua_State *L, int narg); diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index bad5ec8af..a65a5e88f 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -551,7 +551,7 @@ int ObjectRef::l_get_local_animation(lua_State *L) float frame_speed; player->getLocalAnimations(frames, &frame_speed); - for (v2s32 frame : frames) { + for (const v2s32 &frame : frames) { push_v2s32(L, frame); } diff --git a/src/script/scripting_client.cpp b/src/script/scripting_client.cpp index 904f735f3..b121f3712 100644 --- a/src/script/scripting_client.cpp +++ b/src/script/scripting_client.cpp @@ -32,8 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_localplayer.h" #include "lua_api/l_camera.h" -ClientScripting::ClientScripting(Client *client): - ScriptApiBase() +ClientScripting::ClientScripting(Client *client) { setGameDef(client); setType(ScriptingType::Client); |