1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

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
This commit is contained in:
Loïc Blot 2017-08-20 13:30:50 +02:00 committed by GitHub
parent 50669cd282
commit 1c1c97cbd1
72 changed files with 446 additions and 584 deletions

View file

@ -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

View file

@ -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);

View file

@ -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) { \

View file

@ -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);

View file

@ -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__)

View file

@ -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();

View file

@ -68,7 +68,7 @@ class AsyncEngine {
friend class AsyncWorkerThread;
typedef void (*StateInitializer)(lua_State *L, int top);
public:
AsyncEngine() {};
AsyncEngine() = default;
~AsyncEngine();
/**

View file

@ -40,7 +40,7 @@ extern "C" {
#endif
}
#include <stdio.h>
#include <cstdio>
#include <cstdarg>
#include "script/common/c_content.h"
#include <sstream>

View file

@ -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__)

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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,

View file

@ -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);

View file

@ -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()
{
}

View file

@ -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,

View file

@ -192,8 +192,3 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
runCallbacks(3, RUN_CALLBACKS_MODE_OR_SC);
}
ScriptApiPlayer::~ScriptApiPlayer()
{
}

View file

@ -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);

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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])

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);

View file

@ -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);
}

View file

@ -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);