Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorder client initialization #12189

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 19 additions & 15 deletions src/server.cpp
Expand Up @@ -1079,7 +1079,7 @@ void Server::Receive()
}
}

PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
PlayerSAO *Server::StageTwoClientInit(session_t peer_id)
{
std::string playername;
PlayerSAO *playersao = NULL;
Expand All @@ -1092,11 +1092,12 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
}
}

RemotePlayer *player = m_env->getPlayer(playername.c_str());
RemotePlayer *player = playersao ? playersao->getPlayer() : nullptr;

// If failed, cancel
if (!playersao || !player) {
if (player && player->getPeerId() != PEER_ID_INEXISTENT) {
if (!player) {
RemotePlayer *joined = m_env->getPlayer(playername.c_str());
if (joined && joined->getPeerId() != PEER_ID_INEXISTENT) {
actionstream << "Server: Failed to emerge player \"" << playername
<< "\" (player allocated to another client)" << std::endl;
DenyAccess(peer_id, SERVER_ACCESSDENIED_ALREADY_CONNECTED);
Expand All @@ -1108,6 +1109,18 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
return nullptr;
}

// Add player to environment
m_env->addPlayer(player);

/* Clean up old HUD elements from previous sessions */
player->clearHud();

/* Add object to environment */
m_env->addActiveObject(playersao);

if (playersao->isNewPlayer())
m_script->on_newplayer(playersao);

/*
Send complete position information
*/
Expand Down Expand Up @@ -3207,9 +3220,7 @@ void Server::reportPrivsModified(const std::string &name)
PlayerSAO *sao = player->getPlayerSAO();
if(!sao)
return;
sao->updatePrivileges(
getPlayerEffectivePrivs(name),
isSingleplayer());
sao->updatePrivileges(getPlayerEffectivePrivs(name));
}
}

Expand Down Expand Up @@ -3864,20 +3875,13 @@ PlayerSAO* Server::emergePlayer(const char *name, session_t peer_id, u16 proto_v
player = new RemotePlayer(name, idef());
}

bool newplayer = false;

// Load player
PlayerSAO *playersao = m_env->loadPlayer(player, &newplayer, peer_id, isSingleplayer());
PlayerSAO *playersao = m_env->loadPlayer(player, peer_id);

// Complete init with server parts
playersao->finalize(player, getPlayerEffectivePrivs(player->getName()));
player->protocol_version = proto_version;

/* Run scripts */
if (newplayer) {
m_script->on_newplayer(playersao);
}

return playersao;
}

Expand Down
5 changes: 4 additions & 1 deletion src/server.h
Expand Up @@ -162,7 +162,10 @@ class Server : public con::PeerHandler, public MapEventReceiver,
// This is run by ServerThread and does the actual processing
void AsyncRunStep(bool initial_step=false);
void Receive();
PlayerSAO* StageTwoClientInit(session_t peer_id);

// Full player initialization after they processed all static media
// This is a helper function for TOSERVER_CLIENT_READY
PlayerSAO *StageTwoClientInit(session_t peer_id);

/*
* Command Handlers
Expand Down
9 changes: 6 additions & 3 deletions src/server/player_sao.h
Expand Up @@ -165,12 +165,14 @@ class PlayerSAO : public UnitSAO

// Other

void updatePrivileges(const std::set<std::string> &privs, bool is_singleplayer)
void updatePrivileges(const std::set<std::string> &privs)
{
m_privs = privs;
m_is_singleplayer = is_singleplayer;
}

inline void setNewPlayer() { m_is_new_player = true; }
inline bool isNewPlayer() { return m_is_new_player; }

bool getCollisionBox(aabb3f *toset) const override;
bool getSelectionBox(aabb3f *toset) const override;
bool collideWithObjects() const override { return true; }
Expand Down Expand Up @@ -211,7 +213,8 @@ class PlayerSAO : public UnitSAO

// Cached privileges for enforcement
std::set<std::string> m_privs;
bool m_is_singleplayer;
const bool m_is_singleplayer;
bool m_is_new_player = false;

u16 m_breath = PLAYER_MAX_BREATH_DEFAULT;
f32 m_pitch = 0.0f;
Expand Down
17 changes: 4 additions & 13 deletions src/serverenvironment.cpp
Expand Up @@ -617,13 +617,13 @@ void ServerEnvironment::savePlayer(RemotePlayer *player)
}
}

PlayerSAO *ServerEnvironment::loadPlayer(RemotePlayer *player, bool *new_player,
session_t peer_id, bool is_singleplayer)
PlayerSAO *ServerEnvironment::loadPlayer(RemotePlayer *player, session_t peer_id)
{
PlayerSAO *playersao = new PlayerSAO(this, player, peer_id, is_singleplayer);
PlayerSAO *playersao = new PlayerSAO(this, player, peer_id, m_server->isSingleplayer());
// Create player if it doesn't exist
if (!m_player_database->loadPlayer(player, playersao)) {
*new_player = true;
playersao->setNewPlayer();

// Set player position
infostream << "Server: Finding spawn place for player \""
<< player->getName() << "\"" << std::endl;
Expand All @@ -642,15 +642,6 @@ PlayerSAO *ServerEnvironment::loadPlayer(RemotePlayer *player, bool *new_player,
}
}

// Add player to environment
addPlayer(player);

/* Clean up old HUD elements from previous sessions */
player->clearHud();

/* Add object to environment */
addActiveObject(playersao);

// Update active blocks quickly for a bit so objects in those blocks appear on the client
m_fast_active_block_divider = 10;

Expand Down
3 changes: 1 addition & 2 deletions src/serverenvironment.h
Expand Up @@ -241,8 +241,7 @@ class ServerEnvironment final : public Environment
// Save players
void saveLoadedPlayers(bool force = false);
void savePlayer(RemotePlayer *player);
PlayerSAO *loadPlayer(RemotePlayer *player, bool *new_player, session_t peer_id,
bool is_singleplayer);
PlayerSAO *loadPlayer(RemotePlayer *player, session_t peer_id);
void addPlayer(RemotePlayer *player);
void removePlayer(RemotePlayer *player);
bool removePlayerFromDatabase(const std::string &name);
Expand Down