Switch player names to std::string

This commit is contained in:
Lars Mueller
2023-11-12 15:25:46 +01:00
committed by sfan5
parent 603eb57943
commit cc8e7a569e
18 changed files with 39 additions and 36 deletions

View File

@ -1594,7 +1594,7 @@ Inventory* Client::getInventory(const InventoryLocation &loc)
{ {
// Check if we are working with local player inventory // Check if we are working with local player inventory
LocalPlayer *player = m_env.getLocalPlayer(); LocalPlayer *player = m_env.getLocalPlayer();
if (!player || strcmp(player->getName(), loc.name.c_str()) != 0) if (!player || player->getName() != loc.name)
return NULL; return NULL;
return &player->inventory; return &player->inventory;
} }

View File

@ -385,7 +385,7 @@ void GenericCAO::processInitData(const std::string &data)
if (m_is_player) { if (m_is_player) {
// Check if it's the current player // Check if it's the current player
LocalPlayer *player = m_env->getLocalPlayer(); 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_local_player = true;
m_is_visible = false; m_is_visible = false;
player->setCAO(this); player->setCAO(this);

View File

@ -19,6 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "localplayer.h" #include "localplayer.h"
#include <cmath> #include <cmath>
#include <string>
#include "mtevent.h" #include "mtevent.h"
#include "collision.h" #include "collision.h"
#include "nodedef.h" #include "nodedef.h"
@ -75,7 +76,7 @@ void PlayerSettings::settingsChangedCallback(const std::string &name, void *data
LocalPlayer LocalPlayer
*/ */
LocalPlayer::LocalPlayer(Client *client, const char *name): LocalPlayer::LocalPlayer(Client *client, const std::string &name):
Player(name, client->idef()), Player(name, client->idef()),
m_client(client) m_client(client)
{ {

View File

@ -24,6 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "constants.h" #include "constants.h"
#include "settings.h" #include "settings.h"
#include "lighting.h" #include "lighting.h"
#include <string>
class Client; class Client;
class Environment; class Environment;
@ -63,7 +64,8 @@ private:
class LocalPlayer : public Player class LocalPlayer : public Player
{ {
public: public:
LocalPlayer(Client *client, const char *name);
LocalPlayer(Client *client, const std::string &name);
virtual ~LocalPlayer(); virtual ~LocalPlayer();
// Initialize hp to 0, so that no hearts will be shown if server // Initialize hp to 0, so that no hearts will be shown if server

View File

@ -48,8 +48,7 @@ void PlayerDatabaseFiles::deSerialize(RemotePlayer *p, std::istream &is,
p->m_dirty = true; p->m_dirty = true;
//args.getS32("version"); // Version field value not used //args.getS32("version"); // Version field value not used
const std::string &name = args.get("name"); p->m_name = args.get("name");
strlcpy(p->m_name, name.c_str(), PLAYERNAME_SIZE);
if (sao) { if (sao) {
try { try {
@ -96,7 +95,7 @@ void PlayerDatabaseFiles::deSerialize(RemotePlayer *p, std::istream &is,
p->inventory.deSerialize(is); p->inventory.deSerialize(is);
} catch (SerializationError &e) { } catch (SerializationError &e) {
errorstream << "Failed to deserialize player inventory. player_name=" 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")) { if (!p->inventory.getList("craftpreview") && p->inventory.getList("craftresult")) {
@ -119,7 +118,7 @@ void PlayerDatabaseFiles::serialize(RemotePlayer *p, std::ostream &os)
// Utilize a Settings object for storing values // Utilize a Settings object for storing values
Settings args("PlayerArgsEnd"); Settings args("PlayerArgsEnd");
args.setS32("version", 1); args.setS32("version", 1);
args.set("name", p->m_name); args.set("name", p->getName());
PlayerSAO *sao = p->getPlayerSAO(); PlayerSAO *sao = p->getPlayerSAO();
// This should not happen // This should not happen
@ -171,7 +170,7 @@ void PlayerDatabaseFiles::savePlayer(RemotePlayer *player)
deSerialize(&testplayer, is, path, NULL); deSerialize(&testplayer, is, path, NULL);
is.close(); is.close();
if (strcmp(testplayer.getName(), player->getName()) == 0) { if (testplayer.getName() == player->getName()) {
path_found = true; path_found = true;
continue; continue;
} }

View File

@ -468,7 +468,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
std::string hp = itos(sao->getHP()); std::string hp = itos(sao->getHP());
std::string breath = itos(sao->getBreath()); std::string breath = itos(sao->getBreath());
const char *values[] = { const char *values[] = {
player->getName(), player->getName().c_str(),
pitch.c_str(), pitch.c_str(),
yaw.c_str(), yaw.c_str(),
posx.c_str(), posy.c_str(), posz.c_str(), posx.c_str(), posy.c_str(), posz.c_str(),
@ -476,7 +476,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
breath.c_str() breath.c_str()
}; };
const char* rmvalues[] = { player->getName() }; const char* rmvalues[] = { player->getName().c_str() };
beginSave(); beginSave();
if (getPGVersion() < 90500) { if (getPGVersion() < 90500) {
@ -501,7 +501,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
inv_id = itos(i), lsize = itos(list->getSize()); inv_id = itos(i), lsize = itos(list->getSize());
const char* inv_values[] = { const char* inv_values[] = {
player->getName(), player->getName().c_str(),
inv_id.c_str(), inv_id.c_str(),
width.c_str(), width.c_str(),
name.c_str(), name.c_str(),
@ -516,7 +516,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
std::string itemStr = oss.str(), slotId = itos(j); std::string itemStr = oss.str(), slotId = itos(j);
const char* invitem_values[] = { const char* invitem_values[] = {
player->getName(), player->getName().c_str(),
inv_id.c_str(), inv_id.c_str(),
slotId.c_str(), slotId.c_str(),
itemStr.c_str() itemStr.c_str()
@ -529,7 +529,7 @@ void PlayerDatabasePostgreSQL::savePlayer(RemotePlayer *player)
const StringMap &attrs = sao->getMeta().getStrings(); const StringMap &attrs = sao->getMeta().getStrings();
for (const auto &attr : attrs) { for (const auto &attr : attrs) {
const char *meta_values[] = { const char *meta_values[] = {
player->getName(), player->getName().c_str(),
attr.first.c_str(), attr.first.c_str(),
attr.second.c_str() attr.second.c_str()
}; };
@ -545,7 +545,7 @@ bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
sanity_check(sao); sanity_check(sao);
verifyDatabase(); verifyDatabase();
const char *values[] = { player->getName() }; const char *values[] = { player->getName().c_str() };
PGresult *results = execPrepared("load_player", 1, values, false, false); PGresult *results = execPrepared("load_player", 1, values, false, false);
// Player not found, return not found // Player not found, return not found
@ -580,7 +580,7 @@ bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
std::string invIdStr = itos(invId); std::string invIdStr = itos(invId);
const char* values2[] = { const char* values2[] = {
player->getName(), player->getName().c_str(),
invIdStr.c_str() invIdStr.c_str()
}; };
PGresult *results2 = execPrepared("load_player_inventory_items", 2, PGresult *results2 = execPrepared("load_player_inventory_items", 2,

View File

@ -615,7 +615,7 @@ void Server::handleCommand_InventoryAction(NetworkPacket* pkt)
// If something goes wrong, this player is to blame // If something goes wrong, this player is to blame
RollbackScopeActor rollback_scope(m_rollback, RollbackScopeActor rollback_scope(m_rollback,
std::string("player:")+player->getName()); "player:" + player->getName());
/* /*
Note: Always set inventory not sent, to repair cases Note: Always set inventory not sent, to repair cases
@ -1069,7 +1069,7 @@ void Server::handleCommand_Interact(NetworkPacket *pkt)
If something goes wrong, this player is to blame If something goes wrong, this player is to blame
*/ */
RollbackScopeActor rollback_scope(m_rollback, RollbackScopeActor rollback_scope(m_rollback,
std::string("player:")+player->getName()); "player:" + player->getName());
switch (action) { switch (action) {
// Start digging or punch object // Start digging or punch object
@ -1400,7 +1400,7 @@ void Server::handleCommand_NodeMetaFields(NetworkPacket* pkt)
// If something goes wrong, this player is to blame // If something goes wrong, this player is to blame
RollbackScopeActor rollback_scope(m_rollback, RollbackScopeActor rollback_scope(m_rollback,
std::string("player:")+player->getName()); "player:" + player->getName());
// Check the target node for rollback data; leave others unnoticed // Check the target node for rollback data; leave others unnoticed
RollbackNode rn_old(&m_env->getMap(), p, this); RollbackNode rn_old(&m_env->getMap(), p, this);

View File

@ -30,10 +30,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h" // strlcpy #include "porting.h" // strlcpy
Player::Player(const char *name, IItemDefManager *idef): Player::Player(const std::string name, IItemDefManager *idef):
inventory(idef) inventory(idef)
{ {
strlcpy(m_name, name, PLAYERNAME_SIZE); m_name = name;
inventory.clear(); inventory.clear();
inventory.addList("main", PLAYER_INVENTORY_SIZE); inventory.addList("main", PLAYER_INVENTORY_SIZE);

View File

@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <mutex> #include <mutex>
#include <functional> #include <functional>
#include <tuple> #include <tuple>
#include <string>
#define PLAYERNAME_SIZE 20 #define PLAYERNAME_SIZE 20
@ -158,7 +159,7 @@ class Player
{ {
public: public:
Player(const char *name, IItemDefManager *idef); Player(const std::string &name, IItemDefManager *idef);
virtual ~Player() = 0; virtual ~Player() = 0;
DISABLE_CLASS_COPY(Player); DISABLE_CLASS_COPY(Player);
@ -178,7 +179,7 @@ public:
// in BS-space // in BS-space
v3f getSpeed() const { return m_speed; } v3f getSpeed() const { return m_speed; }
const char *getName() const { return m_name; } const std::string& getName() const { return m_name; }
u32 getFreeHudID() u32 getFreeHudID()
{ {
@ -251,7 +252,7 @@ public:
u16 getMaxHotbarItemcount(); u16 getMaxHotbarItemcount();
protected: protected:
char m_name[PLAYERNAME_SIZE]; std::string m_name;
v3f m_speed; // velocity; in BS-space v3f m_speed; // velocity; in BS-space
u16 m_wield_index = 0; u16 m_wield_index = 0;
PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f }; PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };

View File

@ -37,7 +37,7 @@ bool RemotePlayer::m_setting_cache_loaded = false;
float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f; float RemotePlayer::m_setting_chat_message_limit_per_10sec = 0.0f;
u16 RemotePlayer::m_setting_chat_message_limit_trigger_kick = 0; 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) Player(name, idef)
{ {
if (!RemotePlayer::m_setting_cache_loaded) { if (!RemotePlayer::m_setting_cache_loaded) {

View File

@ -41,7 +41,7 @@ class RemotePlayer : public Player
friend class PlayerDatabaseFiles; friend class PlayerDatabaseFiles;
public: public:
RemotePlayer(const char *name, IItemDefManager *idef); RemotePlayer(const std::string &name, IItemDefManager *idef);
virtual ~RemotePlayer(); virtual ~RemotePlayer();
PlayerSAO *getPlayerSAO() { return m_sao; } PlayerSAO *getPlayerSAO() { return m_sao; }

View File

@ -246,7 +246,7 @@ void ScriptApiServer::freeDynamicMediaCallback(u32 token)
lua_pop(L, 2); 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 SCRIPTAPI_PRECHECKHEADER
@ -257,6 +257,6 @@ void ScriptApiServer::on_dynamic_media_added(u32 token, const char *playername)
lua_rawgeti(L, -1, token); lua_rawgeti(L, -1, token);
luaL_checktype(L, -1, LUA_TFUNCTION); 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)); PCALL_RES(lua_pcall(L, 1, 0, error_handler));
} }

View File

@ -53,7 +53,7 @@ public:
/* dynamic media handling */ /* dynamic media handling */
static u32 allocateDynamicMediaCallback(lua_State *L, int f_idx); static u32 allocateDynamicMediaCallback(lua_State *L, int f_idx);
void freeDynamicMediaCallback(u32 token); 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: private:
void getAuthHandler(); void getAuthHandler();

View File

@ -72,7 +72,7 @@ int LuaLocalPlayer::l_get_name(lua_State *L)
{ {
LocalPlayer *player = getobject(L, 1); LocalPlayer *player = getobject(L, 1);
lua_pushstring(L, player->getName()); lua_pushstring(L, player->getName().c_str());
return 1; return 1;
} }

View File

@ -1166,7 +1166,7 @@ int ObjectRef::l_get_player_name(lua_State *L)
return 1; return 1;
} }
lua_pushstring(L, player->getName()); lua_pushstring(L, player->getName().c_str());
return 1; return 1;
} }

View File

@ -1153,7 +1153,7 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
*/ */
{ {
NetworkPacket notice_pkt(TOCLIENT_UPDATE_PLAYER_LIST, 0, PEER_ID_INEXISTENT); 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(&notice_pkt); m_clients.sendToAll(&notice_pkt);
} }
{ {
@ -3176,7 +3176,7 @@ std::string Server::getStatusString()
RemotePlayer *player = m_env->getPlayer(client_id); RemotePlayer *player = m_env->getPlayer(client_id);
// Get name of player // Get name of player
const char *name = player ? player->getName() : "<unknown>"; const std::string name = player ? player->getName() : "<unknown>";
// Add name to information string // Add name to information string
if (!first) if (!first)

View File

@ -575,10 +575,10 @@ RemotePlayer *ServerEnvironment::getPlayer(const session_t peer_id)
return NULL; return NULL;
} }
RemotePlayer *ServerEnvironment::getPlayer(const char* name, bool match_invalid_peer) RemotePlayer *ServerEnvironment::getPlayer(const std::string &name, bool match_invalid_peer)
{ {
for (RemotePlayer *player : m_players) { for (RemotePlayer *player : m_players) {
if (strcmp(player->getName(), name) != 0) if (player->getName() != name)
continue; continue;
if (match_invalid_peer || player->getPeerId() != PEER_ID_INEXISTENT) if (match_invalid_peer || player->getPeerId() != PEER_ID_INEXISTENT)

View File

@ -379,7 +379,7 @@ public:
bool static_exists, v3s16 static_block=v3s16(0,0,0)); bool static_exists, v3s16 static_block=v3s16(0,0,0));
RemotePlayer *getPlayer(const session_t peer_id); RemotePlayer *getPlayer(const session_t peer_id);
RemotePlayer *getPlayer(const char* name, bool match_invalid_peer = false); RemotePlayer *getPlayer(const std::string &name, bool match_invalid_peer = false);
const std::vector<RemotePlayer *> getPlayers() const { return m_players; } const std::vector<RemotePlayer *> getPlayers() const { return m_players; }
u32 getPlayerCount() const { return m_players.size(); } u32 getPlayerCount() const { return m_players.size(); }