From 71c3369e084d0b9e3b91f9517069e58aaecc15e3 Mon Sep 17 00:00:00 2001 From: Howaner Date: Tue, 9 Sep 2014 18:27:31 +0200 Subject: 1.8: Added MultiBlockChange packet. --- src/ClientHandle.cpp | 7 +-- src/Protocol/ChunkDataSerializer.cpp | 45 +++++++------------ src/Protocol/Protocol18x.cpp | 83 ++++++++++++++++++++++++++---------- src/Protocol/Protocol18x.h | 5 +++ 4 files changed, 86 insertions(+), 54 deletions(-) diff --git a/src/ClientHandle.cpp b/src/ClientHandle.cpp index f97ce26c9..fa2c2124b 100644 --- a/src/ClientHandle.cpp +++ b/src/ClientHandle.cpp @@ -362,13 +362,14 @@ void cClientHandle::Authenticate(const AString & a_Name, const AString & a_UUID, // Send experience m_Player->SendExperience(); - - m_Player->Initialize(*World); - m_State = csAuthenticated; // Send player list items SendPlayerListItem(*m_Player, 0); + World->BroadcastPlayerListItem(*m_Player, 0); World->SendPlayerList(m_Player); + + m_Player->Initialize(*World); + m_State = csAuthenticated; // Query player team m_Player->UpdateTeam(); diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index d4574954e..268cf4a9f 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -9,6 +9,7 @@ #include "ChunkDataSerializer.h" #include "zlib/zlib.h" #include "ByteBuffer.h" +#include "Protocol18x.h" @@ -54,7 +55,10 @@ const AString & cChunkDataSerializer::Serialize(int a_Version, int a_ChunkX, int break; } } - m_Serializations[a_Version] = data; + if (!data.empty()) + { + m_Serializations[a_Version] = data; + } return m_Serializations[a_Version]; } @@ -219,40 +223,23 @@ void cChunkDataSerializer::Serialize80(AString & a_Data, int a_ChunkX, int a_Chu Packet.ReadAll(PacketData); Packet.CommitRead(); - cByteBuffer NumberBuffer(20); + cByteBuffer Buffer(20); if (PacketData.size() >= 256) { - AString PostData; - NumberBuffer.WriteVarInt(PacketData.size()); - NumberBuffer.ReadAll(PostData); - NumberBuffer.CommitRead(); - - // Compress the data: - const uLongf CompressedMaxSize = 200000; - char CompressedData[CompressedMaxSize]; - - uLongf CompressedSize = compressBound(PacketData.size()); - // Run-time check that our compile-time guess about CompressedMaxSize was enough: - ASSERT(CompressedSize <= CompressedMaxSize); - compress2((Bytef*)CompressedData, &CompressedSize, (const Bytef*)PacketData.data(), PacketData.size(), Z_DEFAULT_COMPRESSION); - - NumberBuffer.WriteVarInt(CompressedSize + PostData.size()); - NumberBuffer.WriteVarInt(PacketData.size()); - NumberBuffer.ReadAll(PostData); - NumberBuffer.CommitRead(); - - a_Data.clear(); - a_Data.resize(PostData.size() + CompressedSize); - a_Data.append(PostData.data(), PostData.size()); - a_Data.append(CompressedData, CompressedSize); + if (!cProtocol180::CompressPacket(PacketData, a_Data)) + { + ASSERT(!"Packet compression failed."); + a_Data.clear(); + return; + } } else { AString PostData; - NumberBuffer.WriteVarInt(Packet.GetUsedSpace() + 1); - NumberBuffer.WriteVarInt(0); - NumberBuffer.ReadAll(PostData); - NumberBuffer.CommitRead(); + Buffer.WriteVarInt(Packet.GetUsedSpace() + 1); + Buffer.WriteVarInt(0); + Buffer.ReadAll(PostData); + Buffer.CommitRead(); a_Data.clear(); a_Data.resize(PostData.size() + PacketData.size()); diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index 5f1f97a4a..c7c6402d9 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -200,18 +200,16 @@ void cProtocol180::SendBlockChanges(int a_ChunkX, int a_ChunkZ, const sSetBlockV { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x22); // Multi Block Change packet + cPacketizer Pkt(*this, 0x22); // Multi Block Change packet Pkt.WriteInt(a_ChunkX); Pkt.WriteInt(a_ChunkZ); Pkt.WriteVarInt((UInt32)a_Changes.size()); for (sSetBlockVector::const_iterator itr = a_Changes.begin(), end = a_Changes.end(); itr != end; ++itr) { - short Coords = itr->y | (itr->z << 8) | (itr->x << 12); + short Coords = (short) (itr->y | (itr->z << 8) | (itr->x << 12)); Pkt.WriteShort(Coords); - - UInt32 Block = ((UInt32)itr->BlockType << 4) | ((UInt32)itr->BlockMeta & 15); - Pkt.WriteVarInt(Block); - } // for itr - a_Changes[]*/ + Pkt.WriteVarInt((itr->BlockType & 0xFFF) << 4 | (itr->BlockMeta & 0xF)); + } // for itr - a_Changes[] } @@ -369,9 +367,9 @@ void cProtocol180::SendDestroyEntity(const cEntity & a_Entity) { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x13); // Destroy Entities packet + cPacketizer Pkt(*this, 0x13); // Destroy Entities packet Pkt.WriteVarInt(1); - Pkt.WriteVarInt(a_Entity.GetUniqueID());*/ + Pkt.WriteVarInt(a_Entity.GetUniqueID()); } @@ -437,10 +435,10 @@ void cProtocol180::SendEntityEquipment(const cEntity & a_Entity, short a_SlotNum { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x04); // Entity Equipment packet + cPacketizer Pkt(*this, 0x04); // Entity Equipment packet Pkt.WriteVarInt((UInt32)a_Entity.GetUniqueID()); Pkt.WriteShort(a_SlotNum); - Pkt.WriteItem(a_Item);*/ + Pkt.WriteItem(a_Item); } @@ -451,9 +449,9 @@ void cProtocol180::SendEntityHeadLook(const cEntity & a_Entity) { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x19); // Entity Head Look packet + cPacketizer Pkt(*this, 0x19); // Entity Head Look packet Pkt.WriteVarInt((UInt32)a_Entity.GetUniqueID()); - Pkt.WriteByteAngle(a_Entity.GetHeadYaw());*/ + Pkt.WriteByteAngle(a_Entity.GetHeadYaw()); } @@ -464,11 +462,11 @@ void cProtocol180::SendEntityLook(const cEntity & a_Entity) { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x16); // Entity Look packet + cPacketizer Pkt(*this, 0x16); // Entity Look packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteBool(true); // TODO: IsOnGround() on entities*/ + Pkt.WriteBool(true); // TODO: IsOnGround() on entities } @@ -479,10 +477,10 @@ void cProtocol180::SendEntityMetadata(const cEntity & a_Entity) { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet + cPacketizer Pkt(*this, 0x1c); // Entity Metadata packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteEntityMetadata(a_Entity); - Pkt.WriteByte(0x7f); // The termination byte*/ + Pkt.WriteByte(0x7f); // The termination byte } @@ -506,12 +504,12 @@ void cProtocol180::SendEntityRelMove(const cEntity & a_Entity, char a_RelX, char { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet + cPacketizer Pkt(*this, 0x15); // Entity Relative Move packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteByte(a_RelX); Pkt.WriteByte(a_RelY); Pkt.WriteByte(a_RelZ); - Pkt.WriteBool(true); // TODO: IsOnGround() on entities*/ + Pkt.WriteBool(true); // TODO: IsOnGround() on entities } @@ -522,14 +520,14 @@ void cProtocol180::SendEntityRelMoveLook(const cEntity & a_Entity, char a_RelX, { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet + cPacketizer Pkt(*this, 0x17); // Entity Look And Relative Move packet Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteByte(a_RelX); Pkt.WriteByte(a_RelY); Pkt.WriteByte(a_RelZ); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteBool(true); // TODO: IsOnGround() on entities*/ + Pkt.WriteBool(true); // TODO: IsOnGround() on entities } @@ -1320,14 +1318,14 @@ void cProtocol180::SendTeleportEntity(const cEntity & a_Entity) { ASSERT(m_State == 3); // In game mode? - /*cPacketizer Pkt(*this, 0x18); + cPacketizer Pkt(*this, 0x18); Pkt.WriteVarInt(a_Entity.GetUniqueID()); Pkt.WriteFPInt(a_Entity.GetPosX()); Pkt.WriteFPInt(a_Entity.GetPosY()); Pkt.WriteFPInt(a_Entity.GetPosZ()); Pkt.WriteByteAngle(a_Entity.GetYaw()); Pkt.WriteByteAngle(a_Entity.GetPitch()); - Pkt.WriteBool(true); // TODO: IsOnGrond() on entities*/ + Pkt.WriteBool(true); // TODO: IsOnGrond() on entities } @@ -1531,6 +1529,47 @@ void cProtocol180::SendWindowProperty(const cWindow & a_Window, int a_Property, +bool cProtocol180::CompressPacket(const AString & a_Packet, AString & a_CompressedData) +{ + // Compress the data: + const uLongf CompressedMaxSize = 200000; + char CompressedData[CompressedMaxSize]; + + uLongf CompressedSize = compressBound(a_Packet.size()); + if (CompressedSize >= CompressedMaxSize) + { + ASSERT(!"Too high packet size."); + return false; + } + + int Status = compress2((Bytef*)CompressedData, &CompressedSize, (const Bytef*)a_Packet.data(), a_Packet.size(), Z_DEFAULT_COMPRESSION); + if (Status != Z_OK) + { + return false; + } + + AString LengthData; + cByteBuffer Buffer(20); + Buffer.WriteVarInt((UInt32)a_Packet.size()); + Buffer.ReadAll(LengthData); + Buffer.CommitRead(); + + Buffer.WriteVarInt(CompressedSize + LengthData.size()); + Buffer.WriteVarInt(a_Packet.size()); + Buffer.ReadAll(LengthData); + Buffer.CommitRead(); + + a_CompressedData.clear(); + a_CompressedData.resize(LengthData.size() + CompressedSize); + a_CompressedData.append(LengthData.data(), LengthData.size()); + a_CompressedData.append(CompressedData, CompressedSize); + return true; +} + + + + + void cProtocol180::AddReceivedData(const char * a_Data, size_t a_Size) { // Write the incoming data into the comm log file: diff --git a/src/Protocol/Protocol18x.h b/src/Protocol/Protocol18x.h index 8e8570b68..33e298e48 100644 --- a/src/Protocol/Protocol18x.h +++ b/src/Protocol/Protocol18x.h @@ -133,6 +133,11 @@ public: virtual AString GetAuthServerID(void) override { return m_AuthServerID; } + /** Compress the packet. a_Packet must be without packet length. + a_Compressed will be set to the compressed packet includes packet length and data length. + If compression fails, the function returns false. */ + static bool CompressPacket(const AString & a_Packet, AString & a_Compressed); + protected: /** Composes individual packets in the protocol's m_OutPacketBuffer; sends them upon being destructed */ -- cgit v1.2.3