Skip to content

Commit

Permalink
Reorder client initialization
Browse files Browse the repository at this point in the history
Previously, ServerEnv created a player instance before they're fully initialized.
This commit moves all initialization steps and callbacks into TOSERVER_CLIENT_READY
^ which includes StageTwoClientInit for player loading or creation
  • Loading branch information
SmallJoker committed Apr 9, 2022
1 parent 2d8eac4 commit 7186307
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
34 changes: 19 additions & 15 deletions src/server.cpp
Expand Up @@ -1048,7 +1048,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 @@ -1061,11 +1061,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 an another client)" << std::endl;
DenyAccess_Legacy(peer_id, L"Another client is connected with this "
Expand All @@ -1079,6 +1080,18 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
return NULL;
}

// 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 @@ -3168,9 +3181,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 @@ -3830,20 +3841,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 @@ -169,7 +169,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 @@ -585,13 +585,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 @@ -610,15 +610,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);

return playersao;
}

Expand Down
3 changes: 1 addition & 2 deletions src/serverenvironment.h
Expand Up @@ -220,8 +220,7 @@ class ServerEnvironment : 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

0 comments on commit 7186307

Please sign in to comment.