X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fnetwork%2Fclientpackethandler.cpp;h=19f8bbf58a83f99bf35194c44f1c6dac5199fb37;hb=563199698dedeec473108d1c333a2a0d88e541d1;hp=2106e4368b5ed6ca1b77774ba0f111e495a0f9d2;hpb=82e35edff52d88dcd64a9bfc9d2c4c93f1341b78;p=dragonfireclient.git diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 2106e4368..19f8bbf58 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -24,13 +24,16 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "log.h" #include "map.h" #include "mapsector.h" +#include "minimap.h" #include "nodedef.h" #include "serialization.h" #include "server.h" -#include "strfnd.h" +#include "util/strfnd.h" #include "network/clientopcodes.h" +#include "script/clientscripting.h" #include "util/serialize.h" #include "util/srp.h" +#include "tileanimation.h" void Client::handleCommand_Deprecated(NetworkPacket* pkt) { @@ -44,26 +47,32 @@ void Client::handleCommand_Hello(NetworkPacket* pkt) if (pkt->getSize() < 1) return; - u8 deployed; + u8 serialization_ver; + u16 proto_ver; + u16 compression_mode; u32 auth_mechs; std::string username_legacy; // for case insensitivity - *pkt >> deployed >> auth_mechs >> username_legacy; + *pkt >> serialization_ver >> compression_mode >> proto_ver + >> auth_mechs >> username_legacy; // Chose an auth method we support AuthMechanism chosen_auth_mechanism = choseAuthMech(auth_mechs); infostream << "Client: TOCLIENT_HELLO received with " - "deployed=" << ((int)deployed & 0xff) << ", auth_mechs=" - << auth_mechs << ", chosen=" << chosen_auth_mechanism << std::endl; + << "serialization_ver=" << (u32)serialization_ver + << ", auth_mechs=" << auth_mechs + << ", proto_ver=" << proto_ver + << ", compression_mode=" << compression_mode + << ". Doing auth with mech " << chosen_auth_mechanism << std::endl; - if (!ser_ver_supported(deployed)) { + if (!ser_ver_supported(serialization_ver)) { infostream << "Client: TOCLIENT_HELLO: Server sent " << "unsupported ser_fmt_ver"<< std::endl; return; } - m_server_ser_ver = deployed; - m_proto_ver = deployed; + m_server_ser_ver = serialization_ver; + m_proto_ver = proto_ver; //TODO verify that username_legacy matches sent username, only // differs in casing (make both uppercase and compare) @@ -94,7 +103,6 @@ void Client::handleCommand_Hello(NetworkPacket* pkt) void Client::handleCommand_AuthAccept(NetworkPacket* pkt) { - m_chosen_auth_mech = AUTH_MECHANISM_NONE; deleteAuthData(); v3f playerpos; @@ -104,7 +112,7 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt) playerpos -= v3f(0, BS / 2, 0); // Set player position - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->setPosition(playerpos); @@ -120,7 +128,6 @@ void Client::handleCommand_AuthAccept(NetworkPacket* pkt) } void Client::handleCommand_AcceptSudoMode(NetworkPacket* pkt) { - m_chosen_auth_mech = AUTH_MECHANISM_NONE; deleteAuthData(); m_password = m_new_password; @@ -135,30 +142,32 @@ void Client::handleCommand_AcceptSudoMode(NetworkPacket* pkt) } void Client::handleCommand_DenySudoMode(NetworkPacket* pkt) { - m_chat_queue.push(L"Password change denied. Password NOT changed."); + pushToChatQueue(L"Password change denied. Password NOT changed."); // reset everything and be sad deleteAuthData(); - m_chosen_auth_mech = AUTH_MECHANISM_NONE; } void Client::handleCommand_InitLegacy(NetworkPacket* pkt) { if (pkt->getSize() < 1) return; - u8 deployed; - *pkt >> deployed; + u8 server_ser_ver; + *pkt >> server_ser_ver; infostream << "Client: TOCLIENT_INIT_LEGACY received with " - "deployed=" << ((int)deployed & 0xff) << std::endl; + "server_ser_ver=" << ((int)server_ser_ver & 0xff) << std::endl; - if (!ser_ver_supported(deployed)) { + if (!ser_ver_supported(server_ser_ver)) { infostream << "Client: TOCLIENT_INIT_LEGACY: Server sent " << "unsupported ser_fmt_ver"<< std::endl; return; } - m_server_ser_ver = deployed; - m_proto_ver = deployed; + m_server_ser_ver = server_ser_ver; + + // We can be totally wrong with this guess + // but we only need some value < 25. + m_proto_ver = 24; // Get player position v3s16 playerpos_s16(0, BS * 2 + BS * 20, 0); @@ -169,7 +178,7 @@ void Client::handleCommand_InitLegacy(NetworkPacket* pkt) // Set player position - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->setPosition(playerpos_f); @@ -206,11 +215,28 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt) u8 denyCode = SERVER_ACCESSDENIED_UNEXPECTED_DATA; *pkt >> denyCode; - if (denyCode == SERVER_ACCESSDENIED_CUSTOM_STRING) { + if (denyCode == SERVER_ACCESSDENIED_SHUTDOWN || + denyCode == SERVER_ACCESSDENIED_CRASH) { *pkt >> m_access_denied_reason; - } - else if (denyCode < SERVER_ACCESSDENIED_MAX) { + if (m_access_denied_reason == "") { + m_access_denied_reason = accessDeniedStrings[denyCode]; + } + u8 reconnect; + *pkt >> reconnect; + m_access_denied_reconnect = reconnect & 1; + } else if (denyCode == SERVER_ACCESSDENIED_CUSTOM_STRING) { + *pkt >> m_access_denied_reason; + } else if (denyCode < SERVER_ACCESSDENIED_MAX) { m_access_denied_reason = accessDeniedStrings[denyCode]; + } else { + // Allow us to add new error messages to the + // protocol without raising the protocol version, if we want to. + // Until then (which may be never), this is outside + // of the defined protocol. + *pkt >> m_access_denied_reason; + if (m_access_denied_reason == "") { + m_access_denied_reason = "Unknown"; + } } } // 13/03/15 Legacy code from 0.4.12 and lesser. must stay 1 year @@ -219,7 +245,7 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt) if (pkt->getSize() >= 2) { std::wstring wide_reason; *pkt >> wide_reason; - m_access_denied_reason = wide_to_narrow(wide_reason); + m_access_denied_reason = wide_to_utf8(wide_reason); } } } @@ -309,7 +335,7 @@ void Client::handleCommand_Inventory(NetworkPacket* pkt) std::string datastring(pkt->getString(0), pkt->getSize()); std::istringstream is(datastring, std::ios_base::binary); - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); player->inventory.deSerialize(is); @@ -386,7 +412,10 @@ void Client::handleCommand_ChatMessage(NetworkPacket* pkt) message += (wchar_t)read_wchar; } - m_chat_queue.push(message); + // If chat message not consummed by client lua API + if (!moddingEnabled() || !m_script->on_receiving_message(wide_to_utf8(message))) { + pushToChatQueue(message); + } } void Client::handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt) @@ -440,39 +469,29 @@ void Client::handleCommand_ActiveObjectMessages(NetworkPacket* pkt) string message } */ - char buf[6]; - // Get all data except the command number std::string datastring(pkt->getString(0), pkt->getSize()); - // Throw them in an istringstream std::istringstream is(datastring, std::ios_base::binary); try { - while(is.eof() == false) { - is.read(buf, 2); - u16 id = readU16((u8*)buf); - if (is.eof()) + while (is.good()) { + u16 id = readU16(is); + if (!is.good()) break; - is.read(buf, 2); - size_t message_size = readU16((u8*)buf); - std::string message; - message.reserve(message_size); - for (u32 i = 0; i < message_size; i++) { - is.read(buf, 1); - message.append(buf, 1); - } + + std::string message = deSerializeString(is); + // Pass on to the environment m_env.processActiveObjectMessage(id, message); } - // Packet could be unreliable then ignore it - } catch (PacketError &e) { - infostream << "handleCommand_ActiveObjectMessages: " << e.what() - << ". The packet is unreliable, ignoring" << std::endl; + } catch (SerializationError &e) { + errorstream << "Client::handleCommand_ActiveObjectMessages: " + << "caught SerializationError: " << e.what() << std::endl; } } void Client::handleCommand_Movement(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); float mad, maa, maf, msw, mscr, msf, mscl, msj, lf, lfs, ls, g; @@ -497,7 +516,7 @@ void Client::handleCommand_Movement(NetworkPacket* pkt) void Client::handleCommand_HP(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u8 oldhp = player->hp; @@ -507,6 +526,10 @@ void Client::handleCommand_HP(NetworkPacket* pkt) player->hp = hp; + if (moddingEnabled()) { + m_script->on_hp_modification(hp); + } + if (hp < oldhp) { // Add to ClientEvent queue ClientEvent event; @@ -518,7 +541,7 @@ void Client::handleCommand_HP(NetworkPacket* pkt) void Client::handleCommand_Breath(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); u16 breath; @@ -530,7 +553,7 @@ void Client::handleCommand_Breath(NetworkPacket* pkt) void Client::handleCommand_MovePlayer(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); v3f pos; @@ -538,6 +561,7 @@ void Client::handleCommand_MovePlayer(NetworkPacket* pkt) *pkt >> pos >> pitch >> yaw; + player->got_teleported = true; player->setPosition(pos); infostream << "Client got TOCLIENT_MOVE_PLAYER" @@ -565,7 +589,7 @@ void Client::handleCommand_MovePlayer(NetworkPacket* pkt) void Client::handleCommand_PlayerItem(NetworkPacket* pkt) { - infostream << "Client: WARNING: Ignoring TOCLIENT_PLAYERITEM" << std::endl; + warningstream << "Client: Ignoring TOCLIENT_PLAYERITEM" << std::endl; } void Client::handleCommand_DeathScreen(NetworkPacket* pkt) @@ -608,7 +632,7 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt) // Mesh update thread must be stopped while // updating content definitions - sanity_check(!m_mesh_update_thread.IsRunning()); + sanity_check(!m_mesh_update_thread.isRunning()); for (u16 i = 0; i < num_files; i++) { std::string name, sha1_base64; @@ -619,14 +643,13 @@ void Client::handleCommand_AnnounceMedia(NetworkPacket* pkt) m_media_downloader->addFile(name, sha1_raw); } - std::vector remote_media; try { std::string str; *pkt >> str; Strfnd sf(str); - while(!sf.atend()) { + while(!sf.at_end()) { std::string baseurl = trim(sf.next(",")); if (baseurl != "") m_media_downloader->addRemoteServer(baseurl); @@ -681,7 +704,7 @@ void Client::handleCommand_Media(NetworkPacket* pkt) // Mesh update thread must be stopped while // updating content definitions - sanity_check(!m_mesh_update_thread.IsRunning()); + sanity_check(!m_mesh_update_thread.isRunning()); for (u32 i=0; i < num_files; i++) { std::string name; @@ -697,7 +720,7 @@ void Client::handleCommand_Media(NetworkPacket* pkt) void Client::handleCommand_ToolDef(NetworkPacket* pkt) { - infostream << "Client: WARNING: Ignoring TOCLIENT_TOOLDEF" << std::endl; + warningstream << "Client: Ignoring TOCLIENT_TOOLDEF" << std::endl; } void Client::handleCommand_NodeDef(NetworkPacket* pkt) @@ -707,12 +730,10 @@ void Client::handleCommand_NodeDef(NetworkPacket* pkt) // Mesh update thread must be stopped while // updating content definitions - sanity_check(!m_mesh_update_thread.IsRunning()); + sanity_check(!m_mesh_update_thread.isRunning()); // Decompress node definitions - std::string datastring(pkt->getString(0), pkt->getSize()); - std::istringstream is(datastring, std::ios_base::binary); - std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary); + std::istringstream tmp_is(pkt->readLongString(), std::ios::binary); std::ostringstream tmp_os; decompressZlib(tmp_is, tmp_os); @@ -724,7 +745,7 @@ void Client::handleCommand_NodeDef(NetworkPacket* pkt) void Client::handleCommand_CraftItemDef(NetworkPacket* pkt) { - infostream << "Client: WARNING: Ignoring TOCLIENT_CRAFTITEMDEF" << std::endl; + warningstream << "Client: Ignoring TOCLIENT_CRAFTITEMDEF" << std::endl; } void Client::handleCommand_ItemDef(NetworkPacket* pkt) @@ -734,12 +755,10 @@ void Client::handleCommand_ItemDef(NetworkPacket* pkt) // Mesh update thread must be stopped while // updating content definitions - sanity_check(!m_mesh_update_thread.IsRunning()); + sanity_check(!m_mesh_update_thread.isRunning()); // Decompress item definitions - std::string datastring(pkt->getString(0), pkt->getSize()); - std::istringstream is(datastring, std::ios_base::binary); - std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary); + std::istringstream tmp_is(pkt->readLongString(), std::ios::binary); std::ostringstream tmp_os; decompressZlib(tmp_is, tmp_os); @@ -797,9 +816,7 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt) *pkt >> server_id; - std::map::iterator i = - m_sounds_server_to_client.find(server_id); - + UNORDERED_MAP::iterator 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); @@ -827,7 +844,7 @@ void Client::handleCommand_Privileges(NetworkPacket* pkt) void Client::handleCommand_InventoryFormSpec(NetworkPacket* pkt) { - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); // Store formspec in LocalPlayer @@ -883,8 +900,15 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) bool collisiondetection = readU8(is); std::string texture = deSerializeLongString(is); bool vertical = false; + bool collision_removal = false; + struct TileAnimationParams animation; + animation.type = TAT_NONE; + u8 glow = 0; try { vertical = readU8(is); + collision_removal = readU8(is); + animation.deSerialize(is, m_proto_ver); + glow = readU8(is); } catch (...) {} ClientEvent event; @@ -895,8 +919,11 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) event.spawn_particle.expirationtime = expirationtime; event.spawn_particle.size = size; event.spawn_particle.collisiondetection = collisiondetection; + event.spawn_particle.collision_removal = collision_removal; event.spawn_particle.vertical = vertical; event.spawn_particle.texture = new std::string(texture); + event.spawn_particle.animation = animation; + event.spawn_particle.glow = glow; m_client_event_queue.push(event); } @@ -927,8 +954,21 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) *pkt >> id; bool vertical = false; + bool collision_removal = false; + struct TileAnimationParams animation; + animation.type = TAT_NONE; + u8 glow = 0; + u16 attached_id = 0; try { *pkt >> vertical; + *pkt >> collision_removal; + *pkt >> attached_id; + + // This is horrible but required (why are there two ways to deserialize pkts?) + std::string datastring(pkt->getRemainingString(), pkt->getRemainingBytes()); + std::istringstream is(datastring, std::ios_base::binary); + animation.deSerialize(is, m_proto_ver); + glow = readU8(is); } catch (...) {} ClientEvent event; @@ -946,9 +986,13 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt) event.add_particlespawner.minsize = minsize; event.add_particlespawner.maxsize = maxsize; event.add_particlespawner.collisiondetection = collisiondetection; + event.add_particlespawner.collision_removal = collision_removal; + event.add_particlespawner.attached_id = attached_id; event.add_particlespawner.vertical = vertical; event.add_particlespawner.texture = new std::string(texture); event.add_particlespawner.id = id; + event.add_particlespawner.animation = animation; + event.add_particlespawner.glow = glow; m_client_event_queue.push(event); } @@ -1078,11 +1122,22 @@ void Client::handleCommand_HudSetFlags(NetworkPacket* pkt) *pkt >> flags >> mask; - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); + bool was_minimap_visible = player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE; + player->hud_flags &= ~mask; player->hud_flags |= flags; + + m_minimap_disabled_by_server = !(player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE); + + // Hide minimap if it has been disabled by the server + if (m_minimap_disabled_by_server && was_minimap_visible) { + // defers a minimap update, therefore only call it if really + // needed, by checking that minimap was visible before + m_minimap->setMinimapMode(MINIMAP_MODE_OFF); + } } void Client::handleCommand_HudSetParam(NetworkPacket* pkt) @@ -1091,7 +1146,7 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt) *pkt >> param >> value; - Player *player = m_env.getLocalPlayer(); + LocalPlayer *player = m_env.getLocalPlayer(); assert(player != NULL); if (param == HUD_PARAM_HOTBAR_ITEMCOUNT && value.size() == 4) { @@ -1100,10 +1155,10 @@ void Client::handleCommand_HudSetParam(NetworkPacket* pkt) player->hud_hotbar_itemcount = hotbar_itemcount; } else if (param == HUD_PARAM_HOTBAR_IMAGE) { - ((LocalPlayer *) player)->hotbar_image = value; + player->hotbar_image = value; } else if (param == HUD_PARAM_HOTBAR_SELECTED_IMAGE) { - ((LocalPlayer *) player)->hotbar_selected_image = value; + player->hotbar_selected_image = value; } }