diff --git a/src/client/client.cpp b/src/client/client.cpp index 8d204931cbf12..0c9b9bdd603db 100644 --- a/src/client/client.cpp +++ b/src/client/client.cpp @@ -1579,7 +1579,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc) { // Check if we are working with local player inventory LocalPlayer *player = m_env.getLocalPlayer(); - if (!player || strcmp(player->getName(), loc.name.c_str()) != 0) + if (!player || player->getName() != loc.name) return NULL; return &player->inventory; } diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index d3d2285dd0ddf..5310cc64451c5 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -385,7 +385,7 @@ void GenericCAO::processInitData(const std::string &data) if (m_is_player) { // Check if it's the current player LocalPlayer *player = m_env->getLocalPlayer(); - if (player && strcmp(player->getName(), m_name.c_str()) == 0) { + if (player && player->getName() == m_name) { m_is_local_player = true; m_is_visible = false; player->setCAO(this); diff --git a/src/client/localplayer.cpp b/src/client/localplayer.cpp index 6e5458c8a52d9..5c2407e87b1bf 100644 --- a/src/client/localplayer.cpp +++ b/src/client/localplayer.cpp @@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "localplayer.h" #include +#include #include "mtevent.h" #include "collision.h" #include "nodedef.h" @@ -32,7 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc., LocalPlayer */ -LocalPlayer::LocalPlayer(Client *client, const char *name): +LocalPlayer::LocalPlayer(Client *client, const std::string name): Player(name, client->idef()), m_client(client) { diff --git a/src/client/localplayer.h b/src/client/localplayer.h index f33aab4b31933..d6e2d8d6d6c74 100644 --- a/src/client/localplayer.h +++ b/src/client/localplayer.h @@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "settings.h" #include "lighting.h" #include +#include class Client; class Environment; @@ -45,7 +46,7 @@ enum class LocalPlayerAnimation class LocalPlayer : public Player { public: - LocalPlayer(Client *client, const char *name); + LocalPlayer(Client *client, const std::string name); virtual ~LocalPlayer() = default; // Initialize hp to 0, so that no hearts will be shown if server diff --git a/src/database/database-files.cpp b/src/database/database-files.cpp index 4357b5ea494bd..337e85893ee4e 100644 --- a/src/database/database-files.cpp +++ b/src/database/database-files.cpp @@ -47,8 +47,7 @@ void PlayerDatabaseFiles::deSerialize(RemotePlayer *p, std::istream &is, p->m_dirty = true; //args.getS32("version"); // Version field value not used - const std::string &name = args.get("name"); - strlcpy(p->m_name, name.c_str(), PLAYERNAME_SIZE); + p->m_name = args.get("name"); if (sao) { try { @@ -95,7 +94,7 @@ void PlayerDatabaseFiles::deSerialize(RemotePlayer *p, std::istream &is, p->inventory.deSerialize(is); } catch (SerializationError &e) { errorstream << "Failed to deserialize player inventory. player_name=" - << name << " " << e.what() << std::endl; + << p->getName() << " " << e.what() << std::endl; } if (!p->inventory.getList("craftpreview") && p->inventory.getList("craftresult")) { @@ -118,7 +117,7 @@ void PlayerDatabaseFiles::serialize(RemotePlayer *p, std::ostream &os) // Utilize a Settings object for storing values Settings args("PlayerArgsEnd"); args.setS32("version", 1); - args.set("name", p->m_name); + args.set("name", p->getName()); PlayerSAO *sao = p->getPlayerSAO(); // This should not happen @@ -172,7 +171,7 @@ void PlayerDatabaseFiles::savePlayer(RemotePlayer *player) deSerialize(&testplayer, is, path, NULL); is.close(); - if (strcmp(testplayer.getName(), player->getName()) == 0) { + if (testplayer.getName() == player->getName()) { path_found = true; continue; } diff --git a/src/database/database-postgresql.cpp b/src/database/database-postgresql.cpp index 7b6ecc9344fe9..a50f270d0dc04 100644 --- a/src/database/database-postgresql.cpp +++ b/src/database/database-postgresql.cpp @@ -468,7 +468,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player) std::string hp = itos(sao->getHP()); std::string breath = itos(sao->getBreath()); const char *values[] = { - player->getName(), + player->getName().c_str(), pitch.c_str(), yaw.c_str(), posx.c_str(), posy.c_str(), posz.c_str(), @@ -476,7 +476,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player) breath.c_str() }; - const char* rmvalues[] = { player->getName() }; + const char* rmvalues[] = { player->getName().c_str() }; beginSave(); if (getPGVersion() < 90500) { @@ -501,7 +501,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player) inv_id = itos(i), lsize = itos(list->getSize()); const char* inv_values[] = { - player->getName(), + player->getName().c_str(), inv_id.c_str(), width.c_str(), name.c_str(), @@ -516,7 +516,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player) std::string itemStr = oss.str(), slotId = itos(j); const char* invitem_values[] = { - player->getName(), + player->getName().c_str(), inv_id.c_str(), slotId.c_str(), itemStr.c_str() @@ -529,7 +529,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player) const StringMap &attrs = sao->getMeta().getStrings(); for (const auto &attr : attrs) { const char *meta_values[] = { - player->getName(), + player->getName().c_str(), attr.first.c_str(), attr.second.c_str() }; @@ -545,7 +545,7 @@ bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao) sanity_check(sao); verifyDatabase(); - const char *values[] = { player->getName() }; + const char *values[] = { player->getName().c_str() }; PGresult *results = execPrepared("load_player", 1, values, false, false); // Player not found, return not found @@ -580,7 +580,7 @@ bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao) std::string invIdStr = itos(invId); const char* values2[] = { - player->getName(), + player->getName().c_str(), invIdStr.c_str() }; PGresult *results2 = execPrepared("load_player_inventory_items", 2, diff --git a/src/database/database-sqlite3.h b/src/database/database-sqlite3.h index a400537ff7df8..6efe1971df674 100644 --- a/src/database/database-sqlite3.h +++ b/src/database/database-sqlite3.h @@ -49,11 +49,6 @@ class Database_SQLite3 : public Database sqlite3_vrfy(sqlite3_bind_text(s, iCol, str.c_str(), str.size(), NULL)); } - inline void str_to_sqlite(sqlite3_stmt *s, int iCol, const char *str) const - { - sqlite3_vrfy(sqlite3_bind_text(s, iCol, str, strlen(str), NULL)); - } - inline void int_to_sqlite(sqlite3_stmt *s, int iCol, int val) const { sqlite3_vrfy(sqlite3_bind_int(s, iCol, val)); diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index 92ff4d031caaf..985e8b2ea0629 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -612,7 +612,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt) // If something goes wrong, this player is to blame RollbackScopeActor rollback_scope(m_rollback, - std::string("player:")+player->getName()); + "player:" + player->getName()); /* Note: Always set inventory not sent, to repair cases @@ -1067,7 +1067,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt) If something goes wrong, this player is to blame */ RollbackScopeActor rollback_scope(m_rollback, - std::string("player:")+player->getName()); + "player:" + player->getName()); switch (action) { // Start digging or punch object @@ -1399,7 +1399,7 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt) // If something goes wrong, this player is to blame RollbackScopeActor rollback_scope(m_rollback, - std::string("player:")+player->getName()); + "player:" + player->getName()); // Check the target node for rollback data; leave others unnoticed RollbackNode rn_old(&m_env->getMap(), p, this); diff --git a/src/player.cpp b/src/player.cpp index 8742454d2962d..201639afbf442 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -30,10 +30,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "porting.h" // strlcpy -Player::Player(const char *name, IItemDefManager *idef): +Player::Player(const std::string name, IItemDefManager *idef): inventory(idef) { - strlcpy(m_name, name, PLAYERNAME_SIZE); + m_name = name; inventory.clear(); inventory.addList("main", PLAYER_INVENTORY_SIZE); diff --git a/src/player.h b/src/player.h index af1d73f4a7491..9ff99c91e5f6a 100644 --- a/src/player.h +++ b/src/player.h @@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/basic_macros.h" #include #include +#include #define PLAYERNAME_SIZE 20 @@ -143,7 +144,7 @@ class Player { public: - Player(const char *name, IItemDefManager *idef); + Player(const std::string name, IItemDefManager *idef); virtual ~Player() = 0; DISABLE_CLASS_COPY(Player); @@ -166,7 +167,7 @@ class Player m_speed = speed; } - const char *getName() const { return m_name; } + const std::string& getName() const { return m_name; } u32 getFreeHudID() { @@ -233,7 +234,7 @@ class Player s32 hud_hotbar_itemcount; protected: - char m_name[PLAYERNAME_SIZE]; + std::string m_name; v3f m_speed; // velocity; in BS-space u16 m_wield_index = 0; PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f }; diff --git a/src/remoteplayer.cpp b/src/remoteplayer.cpp index 20be7a8c81f39..79d58ee7f8132 100644 --- a/src/remoteplayer.cpp +++ b/src/remoteplayer.cpp @@ -37,7 +37,7 @@ bool RemotePlayer::m_setting_cache_loaded = false; float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; -RemotePlayer::RemotePlayer(const char *name, IItemDefManager *idef): +RemotePlayer::RemotePlayer(const std::string name, IItemDefManager *idef): Player(name, idef) { if (!RemotePlayer::m_setting_cache_loaded) { diff --git a/src/remoteplayer.h b/src/remoteplayer.h index 0ab33adfe18e3..db87b0fe5a16f 100644 --- a/src/remoteplayer.h +++ b/src/remoteplayer.h @@ -41,7 +41,7 @@ class RemotePlayer : public Player friend class PlayerDatabaseFiles; public: - RemotePlayer(const char *name, IItemDefManager *idef); + RemotePlayer(const std::string name, IItemDefManager *idef); virtual ~RemotePlayer() = default; PlayerSAO *getPlayerSAO() { return m_sao; } diff --git a/src/script/cpp_api/s_server.cpp b/src/script/cpp_api/s_server.cpp index c255b0c71cfb6..d8282998d166a 100644 --- a/src/script/cpp_api/s_server.cpp +++ b/src/script/cpp_api/s_server.cpp @@ -246,7 +246,7 @@ void ScriptApiServer::freeDynamicMediaCallback(u32 token) lua_pop(L, 2); } -void ScriptApiServer::on_dynamic_media_added(u32 token, const char *playername) +void ScriptApiServer::on_dynamic_media_added(u32 token, const std::string &playername) { SCRIPTAPI_PRECHECKHEADER @@ -257,6 +257,6 @@ void ScriptApiServer::on_dynamic_media_added(u32 token, const char *playername) lua_rawgeti(L, -1, token); luaL_checktype(L, -1, LUA_TFUNCTION); - lua_pushstring(L, playername); + lua_pushstring(L, playername.c_str()); PCALL_RES(lua_pcall(L, 1, 0, error_handler)); } diff --git a/src/script/cpp_api/s_server.h b/src/script/cpp_api/s_server.h index 58c8c0e481cec..bb1289dd97af7 100644 --- a/src/script/cpp_api/s_server.h +++ b/src/script/cpp_api/s_server.h @@ -53,7 +53,7 @@ class ScriptApiServer /* dynamic media handling */ static u32 allocateDynamicMediaCallback(lua_State *L, int f_idx); void freeDynamicMediaCallback(u32 token); - void on_dynamic_media_added(u32 token, const char *playername); + void on_dynamic_media_added(u32 token, const std::string &playername); private: void getAuthHandler(); diff --git a/src/script/lua_api/l_localplayer.cpp b/src/script/lua_api/l_localplayer.cpp index 42ca2e0c213f9..92689475420dc 100644 --- a/src/script/lua_api/l_localplayer.cpp +++ b/src/script/lua_api/l_localplayer.cpp @@ -72,7 +72,7 @@ int LuaLocalPlayer::l_get_name(lua_State *L) { LocalPlayer *player = getobject(L, 1); - lua_pushstring(L, player->getName()); + lua_pushstring(L, player->getName().c_str()); return 1; } diff --git a/src/script/lua_api/l_object.cpp b/src/script/lua_api/l_object.cpp index c9dae18855949..be091a6e0ef81 100644 --- a/src/script/lua_api/l_object.cpp +++ b/src/script/lua_api/l_object.cpp @@ -1145,7 +1145,7 @@ int ObjectRef::l_get_player_name(lua_State *L) return 1; } - lua_pushstring(L, player->getName()); + lua_pushstring(L, player->getName().c_str()); return 1; } diff --git a/src/server.cpp b/src/server.cpp index 1949de2889b75..577318703cf88 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -1139,7 +1139,7 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id) */ { NetworkPacket notice_pkt(TOCLIENT_UPDATE_PLAYER_LIST, 0, PEER_ID_INEXISTENT); - notice_pkt << (u8) PLAYER_LIST_ADD << (u16) 1 << std::string(player->getName()); + notice_pkt << (u8) PLAYER_LIST_ADD << (u16) 1 << player->getName(); m_clients.sendToAll(¬ice_pkt); } { @@ -3196,7 +3196,7 @@ std::string Server::getStatusString() RemotePlayer *player = m_env->getPlayer(client_id); // Get name of player - const char *name = player ? player->getName() : ""; + const std::string name = player ? player->getName() : ""; // Add name to information string if (!first) diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 986ef72d919f7..a715ec8015910 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -554,10 +554,10 @@ RemotePlayer *ServerEnvironment::getPlayer(const session_t peer_id) return NULL; } -RemotePlayer *ServerEnvironment::getPlayer(const char* name) +RemotePlayer *ServerEnvironment::getPlayer(const std::string &name) { for (RemotePlayer *player : m_players) { - if (strcmp(player->getName(), name) == 0) + if (player->getName() == name) return player; } return NULL; diff --git a/src/serverenvironment.h b/src/serverenvironment.h index 215fd37ee16e1..0d4977af9e748 100644 --- a/src/serverenvironment.h +++ b/src/serverenvironment.h @@ -386,7 +386,7 @@ class ServerEnvironment final : public Environment bool static_exists, v3s16 static_block=v3s16(0,0,0)); RemotePlayer *getPlayer(const session_t peer_id); - RemotePlayer *getPlayer(const char* name); + RemotePlayer *getPlayer(const std::string &name); const std::vector getPlayers() const { return m_players; } u32 getPlayerCount() const { return m_players.size(); }