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

Code modernization: subfolders (#6283)

* Code modernization: subfolders

Modernize various code on subfolders client, network, script, threading, unittests, util

* empty function
* default constructor/destructor
* for range-based loops
* use emplace_back instead of push_back
* C++ STL header style
* Make connection.cpp readable in a pointed place + typo
This commit is contained in:
Loïc Blot 2017-08-19 22:23:47 +02:00 committed by GitHub
parent 7528986e44
commit 88b436e6a9
49 changed files with 398 additions and 518 deletions

View file

@ -48,7 +48,7 @@ private:
public:
LuaCamera(Camera *m);
~LuaCamera() {}
~LuaCamera() = default;
static void create(lua_State *L, Camera *m);

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"
extern MainGameCallback *g_gamecallback;
int ModApiClient::l_get_current_modname(lua_State *L)
{

View file

@ -57,7 +57,7 @@ bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index,
// key at index -2 and value at index -1
if(!lua_isstring(L, -1))
return false;
recipe.push_back(lua_tostring(L, -1));
recipe.emplace_back(lua_tostring(L, -1));
// removes value, keeps key for next iteration
lua_pop(L, 1);
colcount++;
@ -90,7 +90,7 @@ bool ModApiCraft::readCraftRecipeShapeless(lua_State *L, int index,
// key at index -2 and value at index -1
if(!lua_isstring(L, -1))
return false;
recipe.push_back(lua_tostring(L, -1));
recipe.emplace_back(lua_tostring(L, -1));
// removes value, keeps key for next iteration
lua_pop(L, 1);
}
@ -122,8 +122,7 @@ bool ModApiCraft::readCraftReplacements(lua_State *L, int index,
return false;
std::string replace_to = lua_tostring(L, -1);
lua_pop(L, 1);
replacements.pairs.push_back(
std::make_pair(replace_from, replace_to));
replacements.pairs.emplace_back(replace_from, replace_to);
// removes value, keeps key for next iteration
lua_pop(L, 1);
}
@ -148,7 +147,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
*/
if(type == "shaped"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
if (output.empty())
throw LuaError("Crafting definition is missing an output");
int width = 0;
@ -179,7 +178,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
*/
else if(type == "shapeless"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
if (output.empty())
throw LuaError("Crafting definition (shapeless)"
" is missing an output");
@ -222,12 +221,12 @@ int ModApiCraft::l_register_craft(lua_State *L)
*/
else if(type == "cooking"){
std::string output = getstringfield_default(L, table, "output", "");
if(output == "")
if (output.empty())
throw LuaError("Crafting definition (cooking)"
" is missing an output");
std::string recipe = getstringfield_default(L, table, "recipe", "");
if(recipe == "")
if (recipe.empty())
throw LuaError("Crafting definition (cooking)"
" is missing a recipe"
" (output=\"" + output + "\")");
@ -252,7 +251,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
*/
else if(type == "fuel"){
std::string recipe = getstringfield_default(L, table, "recipe", "");
if(recipe == "")
if (recipe.empty())
throw LuaError("Crafting definition (fuel)"
" is missing a recipe");
@ -294,12 +293,12 @@ int ModApiCraft::l_clear_craft(lua_State *L)
std::string output = getstringfield_default(L, table, "output", "");
std::string type = getstringfield_default(L, table, "type", "shaped");
CraftOutput c_output(output, 0);
if (output != "") {
if (!output.empty()) {
if (craftdef->clearCraftRecipesByOutput(c_output, getServer(L)))
return 0;
else
throw LuaError("No craft recipe known for output"
" (output=\"" + output + "\")");
throw LuaError("No craft recipe known for output"
" (output=\"" + output + "\")");
}
std::vector<std::string> recipe;
int width = 0;
@ -330,7 +329,7 @@ int ModApiCraft::l_clear_craft(lua_State *L)
else if (type == "cooking") {
method = CRAFT_METHOD_COOKING;
std::string rec = getstringfield_default(L, table, "recipe", "");
if (rec == "")
if (rec.empty())
throw LuaError("Crafting definition (cooking)"
" is missing a recipe");
recipe.push_back(rec);
@ -341,7 +340,7 @@ int ModApiCraft::l_clear_craft(lua_State *L)
else if (type == "fuel") {
method = CRAFT_METHOD_FUEL;
std::string rec = getstringfield_default(L, table, "recipe", "");
if (rec == "")
if (rec.empty())
throw LuaError("Crafting definition (fuel)"
" is missing a recipe");
recipe.push_back(rec);

View file

@ -743,9 +743,8 @@ 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 (std::vector<v3s16>::iterator i = list.begin();
i != list.end(); ++i) {
v3s16 p = pos + (*i);
for (v3s16 i : list) {
v3s16 p = pos + i;
content_t c = env->getMap().getNodeNoEx(p).getContent();
if (filter.count(c) != 0) {
push_v3s16(L, p);
@ -809,10 +808,9 @@ int ModApiEnvMod::l_find_nodes_in_area(lua_State *L)
}
}
lua_newtable(L);
for (std::set<content_t>::const_iterator it = filter.begin();
it != filter.end(); ++it) {
lua_pushnumber(L, individual_count[*it]);
lua_setfield(L, -2, ndef->get(*it).name.c_str());
for (content_t it : filter) {
lua_pushnumber(L, individual_count[it]);
lua_setfield(L, -2, ndef->get(it).name.c_str());
}
return 2;
}
@ -1004,12 +1002,11 @@ int ModApiEnvMod::l_fix_light(lua_State *L)
for (blockpos.Z = blockpos1.Z; blockpos.Z <= blockpos2.Z; blockpos.Z++) {
success = success & map.repairBlockLight(blockpos, &modified_blocks);
}
if (modified_blocks.size() > 0) {
if (!modified_blocks.empty()) {
MapEditEvent event;
event.type = MEET_OTHER;
for (std::map<v3s16, MapBlock *>::iterator it = modified_blocks.begin();
it != modified_blocks.end(); ++it)
event.modified_blocks.insert(it->first);
for (auto &modified_block : modified_blocks)
event.modified_blocks.insert(modified_block.first);
map.dispatchEvent(&event);
}
@ -1126,14 +1123,13 @@ int ModApiEnvMod::l_find_path(lua_State *L)
std::vector<v3s16> path = get_path(env, pos1, pos2,
searchdistance, max_jump, max_drop, algo);
if (path.size() > 0)
{
if (!path.empty()) {
lua_newtable(L);
int top = lua_gettop(L);
unsigned int index = 1;
for (std::vector<v3s16>::iterator i = path.begin(); i != path.end(); ++i) {
for (v3s16 i : path) {
lua_pushnumber(L,index);
push_v3s16(L, *i);
push_v3s16(L, i);
lua_settable(L, top);
index++;
}
@ -1167,8 +1163,7 @@ int ModApiEnvMod::l_spawn_tree(lua_State *L)
tree_def.leavesnode=ndef->getId(leaves);
tree_def.leaves2_chance=0;
getstringfield(L, 2, "leaves2", leaves);
if (leaves !="")
{
if (!leaves.empty()) {
tree_def.leaves2node=ndef->getId(leaves);
getintfield(L, 2, "leaves2_chance", tree_def.leaves2_chance);
}
@ -1180,8 +1175,7 @@ int ModApiEnvMod::l_spawn_tree(lua_State *L)
getboolfield(L, 2, "thin_branches", tree_def.thin_branches);
tree_def.fruit_chance=0;
getstringfield(L, 2, "fruit", fruit);
if (fruit != "")
{
if (!fruit.empty()) {
tree_def.fruitnode=ndef->getId(fruit);
getintfield(L, 2, "fruit_chance",tree_def.fruit_chance);
}

View file

@ -69,7 +69,7 @@ void ModApiHttp::read_http_fetch_request(lua_State *L, HTTPFetchRequest &req)
while (lua_next(L, 2) != 0)
{
const char *header = luaL_checkstring(L, -1);
req.extra_headers.push_back(header);
req.extra_headers.emplace_back(header);
lua_pop(L, 1);
}
}

View file

@ -409,10 +409,6 @@ InvRef::InvRef(const InventoryLocation &loc):
{
}
InvRef::~InvRef()
{
}
// Creates an InvRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
void InvRef::create(lua_State *L, const InventoryLocation &loc)

View file

@ -106,7 +106,7 @@ private:
public:
InvRef(const InventoryLocation &loc);
~InvRef();
~InvRef() = default;
// Creates an InvRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.

View file

@ -67,7 +67,7 @@ int LuaItemStack::l_set_name(lua_State *L)
bool status = true;
item.name = luaL_checkstring(L, 2);
if (item.name == "" || item.empty()) {
if (item.name.empty() || item.empty()) {
item.clear();
status = false;
}
@ -231,12 +231,11 @@ int LuaItemStack::l_to_table(lua_State *L)
lua_newtable(L);
const StringMap &fields = item.metadata.getStrings();
for (StringMap::const_iterator it = fields.begin();
it != fields.end(); ++it) {
const std::string &name = it->first;
for (const auto &field : fields) {
const std::string &name = field.first;
if (name.empty())
continue;
const std::string &value = it->second;
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);
@ -391,10 +390,6 @@ LuaItemStack::LuaItemStack(const ItemStack &item):
{
}
LuaItemStack::~LuaItemStack()
{
}
const ItemStack& LuaItemStack::getItem() const
{
return m_stack;

View file

@ -121,7 +121,7 @@ private:
public:
LuaItemStack(const ItemStack &item);
~LuaItemStack();
~LuaItemStack() = default;
const ItemStack& getItem() const;
ItemStack& getItem();

View file

@ -46,7 +46,7 @@ private:
static int gc_object(lua_State *L);
public:
ItemStackMetaRef(ItemStack *istack): istack(istack) {}
~ItemStackMetaRef() {}
~ItemStackMetaRef() = default;
// Creates an ItemStackMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.

View file

@ -70,7 +70,7 @@ private:
public:
LuaLocalPlayer(LocalPlayer *m);
~LuaLocalPlayer() {}
~LuaLocalPlayer() = default;
static void create(lua_State *L, LocalPlayer *m);

View file

@ -51,7 +51,7 @@ private:
public:
LuaMinimap(Minimap *m);
~LuaMinimap() {}
~LuaMinimap() = default;
static void create(lua_State *L, Minimap *object);

View file

@ -182,10 +182,6 @@ NodeMetaRef::NodeMetaRef(Metadata *meta):
{
}
NodeMetaRef::~NodeMetaRef()
{
}
// Creates an NodeMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
void NodeMetaRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)

View file

@ -80,7 +80,7 @@ public:
NodeMetaRef(v3s16 p, ServerEnvironment *env);
NodeMetaRef(Metadata *meta);
~NodeMetaRef();
~NodeMetaRef() = default;
// Creates an NodeMetaRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.

View file

@ -113,10 +113,6 @@ NodeTimerRef::NodeTimerRef(v3s16 p, ServerEnvironment *env):
{
}
NodeTimerRef::~NodeTimerRef()
{
}
// Creates an NodeTimerRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
void NodeTimerRef::create(lua_State *L, v3s16 p, ServerEnvironment *env)

View file

@ -51,7 +51,7 @@ private:
public:
NodeTimerRef(v3s16 p, ServerEnvironment *env);
~NodeTimerRef();
~NodeTimerRef() = default;
// Creates an NodeTimerRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.

View file

@ -36,11 +36,6 @@ LuaPerlinNoise::LuaPerlinNoise(NoiseParams *params) :
}
LuaPerlinNoise::~LuaPerlinNoise()
{
}
int LuaPerlinNoise::l_get2d(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;

View file

@ -43,7 +43,7 @@ private:
public:
LuaPerlinNoise(NoiseParams *params);
~LuaPerlinNoise();
~LuaPerlinNoise() = default;
// LuaPerlinNoise(seed, octaves, persistence, scale)
// Creates an LuaPerlinNoise and leaves it on top of stack

View file

@ -551,8 +551,8 @@ int ObjectRef::l_get_local_animation(lua_State *L)
float frame_speed;
player->getLocalAnimations(frames, &frame_speed);
for (int i = 0; i < 4; i++) {
push_v2s32(L, frames[i]);
for (v2s32 frame : frames) {
push_v2s32(L, frame);
}
lua_pushnumber(L, frame_speed);
@ -611,7 +611,7 @@ int ObjectRef::l_set_bone_position(lua_State *L)
ServerActiveObject *co = getobject(ref);
if (co == NULL) return 0;
// Do it
std::string bone = "";
std::string bone;
if (!lua_isnil(L, 2))
bone = lua_tostring(L, 2);
v3f position = v3f(0, 0, 0);
@ -633,7 +633,7 @@ int ObjectRef::l_get_bone_position(lua_State *L)
if (co == NULL)
return 0;
// Do it
std::string bone = "";
std::string bone;
if (!lua_isnil(L, 2))
bone = lua_tostring(L, 2);
@ -661,7 +661,7 @@ int ObjectRef::l_set_attach(lua_State *L)
return 0;
// Do it
int parent_id = 0;
std::string bone = "";
std::string bone;
v3f position = v3f(0, 0, 0);
v3f rotation = v3f(0, 0, 0);
co->getAttachment(&parent_id, &bone, &position, &rotation);
@ -696,7 +696,7 @@ int ObjectRef::l_get_attach(lua_State *L)
// Do it
int parent_id = 0;
std::string bone = "";
std::string bone;
v3f position = v3f(0, 0, 0);
v3f rotation = v3f(0, 0, 0);
co->getAttachment(&parent_id, &bone, &position, &rotation);
@ -722,7 +722,7 @@ int ObjectRef::l_set_detach(lua_State *L)
return 0;
int parent_id = 0;
std::string bone = "";
std::string bone;
v3f position;
v3f rotation;
co->getAttachment(&parent_id, &bone, &position, &rotation);
@ -1223,7 +1223,7 @@ int ObjectRef::l_get_attribute(lua_State *L)
std::string attr = luaL_checkstring(L, 2);
std::string value = "";
std::string value;
if (co->getExtendedAttribute(attr, &value)) {
lua_pushstring(L, value.c_str());
return 1;
@ -1684,9 +1684,9 @@ int ObjectRef::l_set_sky(lua_State *L)
while (lua_next(L, 4) != 0) {
// key at index -2 and value at index -1
if (lua_isstring(L, -1))
params.push_back(lua_tostring(L, -1));
params.emplace_back(lua_tostring(L, -1));
else
params.push_back("");
params.emplace_back("");
// removes value, keeps key for next iteration
lua_pop(L, 1);
}
@ -1720,15 +1720,14 @@ int ObjectRef::l_get_sky(lua_State *L)
bool clouds;
player->getSky(&bgcolor, &type, &params, &clouds);
type = type == "" ? "regular" : type;
type = type.empty() ? "regular" : type;
push_ARGB8(L, bgcolor);
lua_pushlstring(L, type.c_str(), type.size());
lua_newtable(L);
s16 i = 1;
for (std::vector<std::string>::iterator it = params.begin();
it != params.end(); ++it) {
lua_pushlstring(L, it->c_str(), it->size());
for (const std::string &param : params) {
lua_pushlstring(L, param.c_str(), param.size());
lua_rawseti(L, -2, i);
i++;
}
@ -1865,15 +1864,6 @@ ObjectRef::ObjectRef(ServerActiveObject *object):
//infostream<<"ObjectRef created for id="<<m_object->getId()<<std::endl;
}
ObjectRef::~ObjectRef()
{
/*if (m_object)
infostream<<"ObjectRef destructing for id="
<<m_object->getId()<<std::endl;
else
infostream<<"ObjectRef destructing for id=unknown"<<std::endl;*/
}
// Creates an ObjectRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.
void ObjectRef::create(lua_State *L, ServerActiveObject *object)

View file

@ -35,7 +35,7 @@ class ObjectRef : public ModApiBase {
public:
ObjectRef(ServerActiveObject *object);
~ObjectRef();
~ObjectRef() = default;
// Creates an ObjectRef and leaves it on top of stack
// Not callable from Lua; all references are created on the C side.

View file

@ -53,8 +53,8 @@ int ModApiParticles::l_add_particle(lua_State *L)
struct TileAnimationParams animation;
animation.type = TAT_NONE;
std::string texture = "";
std::string playername = "";
std::string texture;
std::string playername;
u8 glow = 0;
@ -158,8 +158,8 @@ int ModApiParticles::l_add_particlespawner(lua_State *L)
struct TileAnimationParams animation;
animation.type = TAT_NONE;
ServerActiveObject *attached = NULL;
std::string texture = "";
std::string playername = "";
std::string texture;
std::string playername;
u8 glow = 0;
if (lua_gettop(L) > 1) //deprecated
@ -262,7 +262,7 @@ int ModApiParticles::l_delete_particlespawner(lua_State *L)
// Get parameters
u32 id = luaL_checknumber(L, 1);
std::string playername = "";
std::string playername;
if (lua_gettop(L) == 2) {
playername = luaL_checkstring(L, 2);
}

View file

@ -198,10 +198,9 @@ int LuaSettings::l_to_table(lua_State* L)
std::vector<std::string> keys = o->m_settings->getNames();
lua_newtable(L);
for (unsigned int i=0; i < keys.size(); i++)
{
lua_pushstring(L, o->m_settings->get(keys[i]).c_str());
lua_setfield(L, -2, keys[i].c_str());
for (const std::string &key : keys) {
lua_pushstring(L, o->m_settings->get(key).c_str());
lua_setfield(L, -2, key.c_str());
}
return 1;

View file

@ -50,7 +50,7 @@ private:
public:
StorageRef(ModMetadata *object);
~StorageRef() {}
~StorageRef() = default;
static void Register(lua_State *L);
static void create(lua_State *L, ModMetadata *object);

View file

@ -344,9 +344,9 @@ int ModApiUtil::l_get_dir_list(lua_State *L)
int index = 0;
lua_newtable(L);
for (size_t i = 0; i < list.size(); i++) {
if (list_all || list_dirs == list[i].dir) {
lua_pushstring(L, list[i].name.c_str());
for (const fs::DirListNode &dln : list) {
if (list_all || list_dirs == dln.dir) {
lua_pushstring(L, dln.name.c_str());
lua_rawseti(L, -2, ++index);
}
}
@ -414,7 +414,7 @@ int ModApiUtil::l_get_version(lua_State *L)
lua_pushstring(L, g_version_string);
lua_setfield(L, table, "string");
if (strcmp(g_version_string, g_version_hash)) {
if (strcmp(g_version_string, g_version_hash) != 0) {
lua_pushstring(L, g_version_hash);
lua_setfield(L, table, "hash");
}

View file

@ -111,7 +111,7 @@ int LuaVoxelManip::l_write_to_map(lua_State *L)
MAP_LOCK_REQUIRED;
LuaVoxelManip *o = checkobject(L, 1);
bool update_light = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : true;
bool update_light = !lua_isboolean(L, 2) || lua_toboolean(L, 2);
GET_ENV_PTR;
ServerMap *map = &(env->getServerMap());
if (o->is_mapgen_vm || !update_light) {
@ -123,9 +123,8 @@ int LuaVoxelManip::l_write_to_map(lua_State *L)
MapEditEvent event;
event.type = MEET_OTHER;
for (std::map<v3s16, MapBlock *>::iterator it = o->modified_blocks.begin();
it != o->modified_blocks.end(); ++it)
event.modified_blocks.insert(it->first);
for (const auto &modified_block : o->modified_blocks)
event.modified_blocks.insert(modified_block.first);
map->dispatchEvent(&event);
@ -198,7 +197,7 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L)
v3s16 fpmax = vm->m_area.MaxEdge;
v3s16 pmin = lua_istable(L, 2) ? check_v3s16(L, 2) : fpmin + yblock;
v3s16 pmax = lua_istable(L, 3) ? check_v3s16(L, 3) : fpmax - yblock;
bool propagate_shadow = lua_isboolean(L, 4) ? lua_toboolean(L, 4) : true;
bool propagate_shadow = !lua_isboolean(L, 4) || lua_toboolean(L, 4);
sortBoxVerticies(pmin, pmax);
if (!vm->m_area.contains(VoxelArea(pmin, pmax)))