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

Clean up some packet-related code

This commit is contained in:
sfan5 2025-02-22 16:37:45 +01:00
parent 5e89371ecd
commit ee9258cefd
11 changed files with 94 additions and 239 deletions

View file

@ -132,17 +132,10 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt)
{
deleteAuthData();
v3f playerpos;
*pkt >> playerpos >> m_map_seed >> m_recommended_send_interval
v3f unused;
*pkt >> unused >> m_map_seed >> m_recommended_send_interval
>> m_sudo_auth_methods;
playerpos -= v3f(0, BS / 2, 0);
// Set player position
LocalPlayer *player = m_env.getLocalPlayer();
assert(player != NULL);
player->setPosition(playerpos);
infostream << "Client: received map seed: " << m_map_seed << std::endl;
infostream << "Client: received recommended send interval "
<< m_recommended_send_interval<<std::endl;
@ -176,6 +169,7 @@ void Client::handleCommand_AcceptSudoMode(NetworkPacket* pkt)
// reset again
m_chosen_auth_mech = AUTH_MECHANISM_NONE;
}
void Client::handleCommand_DenySudoMode(NetworkPacket* pkt)
{
ChatMessage *chatMessage = new ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
@ -193,8 +187,8 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt)
m_access_denied = true;
if (pkt->getCommand() != TOCLIENT_ACCESS_DENIED) {
// Legacy code from 0.4.12 and older but is still used
// in some places of the server code
// Servers older than 5.6 still send TOCLIENT_ACCESS_DENIED_LEGACY sometimes.
// see commit a65f6f07f3a5601207b790edcc8cc945133112f7
if (pkt->getSize() >= 2) {
std::wstring wide_reason;
*pkt >> wide_reason;
@ -231,9 +225,6 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt)
void Client::handleCommand_RemoveNode(NetworkPacket* pkt)
{
if (pkt->getSize() < 6)
return;
v3s16 p;
*pkt >> p;
removeNode(p);
@ -241,20 +232,17 @@ void Client::handleCommand_RemoveNode(NetworkPacket* pkt)
void Client::handleCommand_AddNode(NetworkPacket* pkt)
{
if (pkt->getSize() < 6 + MapNode::serializedLength(m_server_ser_ver))
return;
v3s16 p;
*pkt >> p;
MapNode n;
n.deSerialize(pkt->getU8Ptr(6), m_server_ser_ver);
auto *ptr = reinterpret_cast<const u8*>(pkt->getRemainingString());
pkt->skip(MapNode::serializedLength(m_server_ser_ver)); // performs length check
bool remove_metadata = true;
u32 index = 6 + MapNode::serializedLength(m_server_ser_ver);
if ((pkt->getSize() >= index + 1) && pkt->getU8(index)) {
remove_metadata = false;
}
MapNode n;
n.deSerialize(ptr, m_server_ser_ver);
bool remove_metadata;
*pkt >> remove_metadata;
addNode(p, n, remove_metadata);
}
@ -272,7 +260,7 @@ void Client::handleCommand_NodemetaChanged(NetworkPacket *pkt)
meta_updates_list.deSerialize(sstr, m_itemdef, true);
Map &map = m_env.getMap();
for (NodeMetadataMap::const_iterator i = meta_updates_list.begin();
for (auto i = meta_updates_list.begin();
i != meta_updates_list.end(); ++i) {
v3s16 pos = i->first;
@ -294,7 +282,7 @@ void Client::handleCommand_BlockData(NetworkPacket* pkt)
v3s16 p;
*pkt >> p;
std::string datastring(pkt->getString(6), pkt->getSize() - 6);
std::string datastring(pkt->getRemainingString(), pkt->getRemainingBytes());
std::istringstream istr(datastring, std::ios_base::binary);
MapSector *sector;
@ -358,46 +346,15 @@ void Client::handleCommand_TimeOfDay(NetworkPacket* pkt)
return;
u16 time_of_day;
*pkt >> time_of_day;
time_of_day = time_of_day % 24000;
float time_speed = 0;
if (pkt->getSize() >= 2 + 4) {
*pkt >> time_speed;
}
else {
// Old message; try to approximate speed of time by ourselves
float time_of_day_f = (float)time_of_day / 24000.0f;
float tod_diff_f = 0;
if (time_of_day_f < 0.2 && m_last_time_of_day_f > 0.8)
tod_diff_f = time_of_day_f - m_last_time_of_day_f + 1.0f;
else
tod_diff_f = time_of_day_f - m_last_time_of_day_f;
m_last_time_of_day_f = time_of_day_f;
float time_diff = m_time_of_day_update_timer;
m_time_of_day_update_timer = 0;
if (m_time_of_day_set) {
time_speed = (3600.0f * 24.0f) * tod_diff_f / time_diff;
infostream << "Client: Measured time_of_day speed (old format): "
<< time_speed << " tod_diff_f=" << tod_diff_f
<< " time_diff=" << time_diff << std::endl;
}
}
float time_speed;
*pkt >> time_speed;
// Update environment
m_env.setTimeOfDay(time_of_day);
m_env.setTimeOfDaySpeed(time_speed);
m_time_of_day_set = true;
//u32 dr = m_env.getDayNightRatio();
//infostream << "Client: time_of_day=" << time_of_day
// << " time_speed=" << time_speed
// << " dr=" << dr << std::endl;
}
void Client::handleCommand_ChatMessage(NetworkPacket *pkt)
@ -851,6 +808,8 @@ void Client::handleCommand_PlaySound(NetworkPacket* pkt)
pos = cao->getPosition() * (1.0f/BS);
vel = cao->getVelocity() * (1.0f/BS);
}
// Note that the server sends 'pos' correctly even for attached sounds,
// so this fallback path is not a mistake.
m_sound->playSoundAt(client_id, spec, pos, vel);
break;
}
@ -883,7 +842,7 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt)
*pkt >> server_id;
std::unordered_map<s32, int>::iterator i = m_sounds_server_to_client.find(server_id);
auto i = m_sounds_server_to_client.find(server_id);
if (i != m_sounds_server_to_client.end()) {
int client_id = i->second;
m_sound->stopSound(client_id);
@ -898,9 +857,7 @@ void Client::handleCommand_FadeSound(NetworkPacket *pkt)
*pkt >> sound_id >> step >> gain;
std::unordered_map<s32, int>::const_iterator i =
m_sounds_server_to_client.find(sound_id);
auto i = m_sounds_server_to_client.find(sound_id);
if (i != m_sounds_server_to_client.end())
m_sound->fadeSound(i->second, step, gain);
}
@ -958,8 +915,8 @@ void Client::handleCommand_DetachedInventory(NetworkPacket* pkt)
inv = inv_it->second;
}
u16 ignore;
*pkt >> ignore; // this used to be the length of the following string, ignore it
// this used to be the length of the following string, ignore it
pkt->skip(2);
std::string contents(pkt->getRemainingString(), pkt->getRemainingBytes());
std::istringstream is(contents, std::ios::binary);
@ -1009,7 +966,7 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt)
p.amount = readU16(is);
p.time = readF32(is);
if (p.time < 0)
throw SerializationError("particle spawner time < 0");
throw PacketError("particle spawner time < 0");
bool missing_end_values = false;
if (m_proto_ver >= 42) {
@ -1324,10 +1281,7 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)
for (size_t i = 0; i < count; i++)
skybox.textures.emplace_back(deSerializeString16(is));
skybox.clouds = true;
try {
skybox.clouds = readU8(is);
} catch (...) {}
skybox.clouds = readU8(is) != 0;
// Use default skybox settings:
SunParams sun = SkyboxDefaults::getSunDefaults();