From 07fba5c98ec94f98b742e8aa76dcec5572bfcecb Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 4 Sep 2014 03:22:35 +0200 Subject: Added more 1.8 protocol things. --- src/Protocol/ChunkDataSerializer.cpp | 64 ++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'src/Protocol/ChunkDataSerializer.cpp') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index ebe61631b..9eadc4f0b 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -43,6 +43,7 @@ const AString & cChunkDataSerializer::Serialize(int a_Version) { case RELEASE_1_2_5: Serialize29(data); break; case RELEASE_1_3_2: Serialize39(data); break; + case RELEASE_1_8_0: Serialize80(data); break; // TODO: Other protocol versions may serialize the data differently; implement here default: @@ -174,3 +175,66 @@ void cChunkDataSerializer::Serialize39(AString & a_Data) + +void cChunkDataSerializer::Serialize80(AString & a_Data) +{ + // TODO: Do not copy data and then compress it; rather, compress partial blocks of data (zlib *can* stream) + + // Blocktypes converter (1.8 included the meta into the blocktype): + unsigned short Blocks[ARRAYCOUNT(m_BlockTypes)]; + for (int RelX = 0; RelX < cChunkDef::Width; RelX++) + { + for (int RelZ = 0; RelZ < cChunkDef::Width; RelZ++) + { + for (int RelY = 0; RelY < cChunkDef::Height; RelY++) + { + int Index = cChunkDef::MakeIndexNoCheck(RelX, RelY, RelZ); + BLOCKTYPE BlockType = m_BlockTypes[Index]; + NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; + + Blocks[Index] = ((unsigned short)BlockType << 4) | ((unsigned short)BlockMeta & 15); + } + } + } + + const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; + const int BlockLightOffset = sizeof(Blocks); + const int SkyLightOffset = BlockLightOffset + sizeof(m_BlockLight); + const int BiomeOffset = SkyLightOffset + sizeof(m_BlockSkyLight); + const int DataSize = BiomeOffset + BiomeDataSize; + + // Temporary buffer for the composed data: + char AllData [DataSize]; + memcpy(AllData, Blocks, sizeof(Blocks)); + memcpy(AllData + BlockLightOffset, m_BlockLight, sizeof(m_BlockLight)); + memcpy(AllData + SkyLightOffset, m_BlockSkyLight, sizeof(m_BlockSkyLight)); + memcpy(AllData + BiomeOffset, m_BiomeData, BiomeDataSize); + + // Put all those data into a_Data: + a_Data.push_back('\x01'); // "Ground-up continuous", or rather, "biome data present" flag + + // Two bitmaps; we're aways sending the full chunk with no additional data, so the bitmaps are 0xffff and 0, respectively + // Also, no endian flipping is needed because of the const values + unsigned short BitMap = 0xffff; + a_Data.append((const char *)&BitMap, sizeof(short)); + + // Write chunk size: + UInt32 ChunkSize = htonl((UInt32)DataSize); + + unsigned char b[5]; // // A 32-bit integer can be encoded by at most 5 bytes + size_t idx = 0; + UInt32 Value = ChunkSize; + do + { + b[idx] = (Value & 0x7f) | ((Value > 0x7f) ? 0x80 : 0x00); + Value = Value >> 7; + idx++; + } while (Value > 0); + a_Data.append((const char *)b, idx); + + a_Data.append(AllData, ChunkSize); // Chunk data +} + + + + -- cgit v1.2.3 From da28c70def4be849925709b2c4d4857b6db1abb8 Mon Sep 17 00:00:00 2001 From: Howaner Date: Thu, 4 Sep 2014 19:03:21 +0200 Subject: Fixed client errors. --- src/Protocol/ChunkDataSerializer.cpp | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) (limited to 'src/Protocol/ChunkDataSerializer.cpp') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index 9eadc4f0b..29e32ce32 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -182,19 +182,11 @@ void cChunkDataSerializer::Serialize80(AString & a_Data) // Blocktypes converter (1.8 included the meta into the blocktype): unsigned short Blocks[ARRAYCOUNT(m_BlockTypes)]; - for (int RelX = 0; RelX < cChunkDef::Width; RelX++) + for (size_t Index = 0; Index < cChunkDef::NumBlocks; Index++) { - for (int RelZ = 0; RelZ < cChunkDef::Width; RelZ++) - { - for (int RelY = 0; RelY < cChunkDef::Height; RelY++) - { - int Index = cChunkDef::MakeIndexNoCheck(RelX, RelY, RelZ); - BLOCKTYPE BlockType = m_BlockTypes[Index]; - NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; - - Blocks[Index] = ((unsigned short)BlockType << 4) | ((unsigned short)BlockMeta & 15); - } - } + BLOCKTYPE BlockType = m_BlockTypes[Index]; + NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; + Blocks[Index] = ((unsigned short)BlockType << 4) | ((unsigned short)BlockMeta); } const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; @@ -216,23 +208,13 @@ void cChunkDataSerializer::Serialize80(AString & a_Data) // Two bitmaps; we're aways sending the full chunk with no additional data, so the bitmaps are 0xffff and 0, respectively // Also, no endian flipping is needed because of the const values unsigned short BitMap = 0xffff; - a_Data.append((const char *)&BitMap, sizeof(short)); + a_Data.append((const char *)&BitMap, sizeof(unsigned short)); // Write chunk size: UInt32 ChunkSize = htonl((UInt32)DataSize); + a_Data.append((const char *)&ChunkSize, 4); - unsigned char b[5]; // // A 32-bit integer can be encoded by at most 5 bytes - size_t idx = 0; - UInt32 Value = ChunkSize; - do - { - b[idx] = (Value & 0x7f) | ((Value > 0x7f) ? 0x80 : 0x00); - Value = Value >> 7; - idx++; - } while (Value > 0); - a_Data.append((const char *)b, idx); - - a_Data.append(AllData, ChunkSize); // Chunk data + a_Data.append(AllData, DataSize); // Chunk data } -- cgit v1.2.3 From 09ff17b71ea5659dc628d9d6e3f1fd308d10037a Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 8 Sep 2014 00:36:30 +0200 Subject: Implemented packet compression. ChunkData packet needs this. --- src/Protocol/ChunkDataSerializer.cpp | 83 +++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 20 deletions(-) (limited to 'src/Protocol/ChunkDataSerializer.cpp') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index 29e32ce32..a2e0c9ef9 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -8,6 +8,7 @@ #include "Globals.h" #include "ChunkDataSerializer.h" #include "zlib/zlib.h" +#include "ByteBuffer.h" @@ -30,7 +31,7 @@ cChunkDataSerializer::cChunkDataSerializer( -const AString & cChunkDataSerializer::Serialize(int a_Version) +const AString & cChunkDataSerializer::Serialize(int a_Version, int a_ChunkX, int a_ChunkZ) { Serializations::const_iterator itr = m_Serializations.find(a_Version); if (itr != m_Serializations.end()) @@ -43,7 +44,7 @@ const AString & cChunkDataSerializer::Serialize(int a_Version) { case RELEASE_1_2_5: Serialize29(data); break; case RELEASE_1_3_2: Serialize39(data); break; - case RELEASE_1_8_0: Serialize80(data); break; + case RELEASE_1_8_0: Serialize80(data, a_ChunkX, a_ChunkZ); break; // TODO: Other protocol versions may serialize the data differently; implement here default: @@ -176,45 +177,87 @@ void cChunkDataSerializer::Serialize39(AString & a_Data) -void cChunkDataSerializer::Serialize80(AString & a_Data) +void cChunkDataSerializer::Serialize80(AString & a_Data, int a_ChunkX, int a_ChunkZ) { // TODO: Do not copy data and then compress it; rather, compress partial blocks of data (zlib *can* stream) // Blocktypes converter (1.8 included the meta into the blocktype): - unsigned short Blocks[ARRAYCOUNT(m_BlockTypes)]; + /*unsigned short Blocks[ARRAYCOUNT(m_BlockTypes)]; for (size_t Index = 0; Index < cChunkDef::NumBlocks; Index++) { BLOCKTYPE BlockType = m_BlockTypes[Index]; NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; Blocks[Index] = ((unsigned short)BlockType << 4) | ((unsigned short)BlockMeta); - } + }*/ const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; - const int BlockLightOffset = sizeof(Blocks); + const int MetadataOffset = sizeof(m_BlockTypes); + const int BlockLightOffset = MetadataOffset + sizeof(m_BlockMetas); const int SkyLightOffset = BlockLightOffset + sizeof(m_BlockLight); const int BiomeOffset = SkyLightOffset + sizeof(m_BlockSkyLight); const int DataSize = BiomeOffset + BiomeDataSize; // Temporary buffer for the composed data: char AllData [DataSize]; - memcpy(AllData, Blocks, sizeof(Blocks)); + memcpy(AllData, m_BlockTypes, sizeof(m_BlockTypes)); + memcpy(AllData + MetadataOffset, m_BlockMetas, sizeof(m_BlockMetas)); memcpy(AllData + BlockLightOffset, m_BlockLight, sizeof(m_BlockLight)); memcpy(AllData + SkyLightOffset, m_BlockSkyLight, sizeof(m_BlockSkyLight)); memcpy(AllData + BiomeOffset, m_BiomeData, BiomeDataSize); - // Put all those data into a_Data: - a_Data.push_back('\x01'); // "Ground-up continuous", or rather, "biome data present" flag - - // Two bitmaps; we're aways sending the full chunk with no additional data, so the bitmaps are 0xffff and 0, respectively - // Also, no endian flipping is needed because of the const values - unsigned short BitMap = 0xffff; - a_Data.append((const char *)&BitMap, sizeof(unsigned short)); - - // Write chunk size: - UInt32 ChunkSize = htonl((UInt32)DataSize); - a_Data.append((const char *)&ChunkSize, 4); - - a_Data.append(AllData, DataSize); // Chunk data + cByteBuffer Packet(512 KiB); + Packet.WriteVarInt(0x21); // Packet id (Chunk Data packet) + Packet.WriteBEInt(a_ChunkX); + Packet.WriteBEInt(a_ChunkZ); + Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag + Packet.WriteBEShort(0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff + Packet.WriteVarInt(DataSize); // Chunk size + Packet.WriteBuf(AllData, DataSize); // Chunk data + + AString PacketData; + Packet.ReadAll(PacketData); + Packet.CommitRead(); + + cByteBuffer NumberBuffer(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); + } + else + { + AString PostData; + NumberBuffer.WriteVarInt(Packet.GetUsedSpace() + 1); + NumberBuffer.WriteVarInt(0); + NumberBuffer.ReadAll(PostData); + NumberBuffer.CommitRead(); + + a_Data.clear(); + a_Data.resize(PostData.size() + PacketData.size()); + a_Data.append(PostData.data(), PostData.size()); + a_Data.append(PacketData.data(), PacketData.size()); + } } -- cgit v1.2.3 From 38124bcce3c5e8ed8e4dba904dc4f15f89a82c14 Mon Sep 17 00:00:00 2001 From: Howaner Date: Mon, 8 Sep 2014 12:24:06 +0200 Subject: Updated chunk sending to 1.8 --- src/Protocol/ChunkDataSerializer.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/Protocol/ChunkDataSerializer.cpp') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index a2e0c9ef9..d4574954e 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -182,25 +182,26 @@ void cChunkDataSerializer::Serialize80(AString & a_Data, int a_ChunkX, int a_Chu // TODO: Do not copy data and then compress it; rather, compress partial blocks of data (zlib *can* stream) // Blocktypes converter (1.8 included the meta into the blocktype): - /*unsigned short Blocks[ARRAYCOUNT(m_BlockTypes)]; + unsigned char Blocks[cChunkDef::NumBlocks * 2]; + size_t LastOffset = 0; for (size_t Index = 0; Index < cChunkDef::NumBlocks; Index++) { - BLOCKTYPE BlockType = m_BlockTypes[Index]; + BLOCKTYPE BlockType = m_BlockTypes[Index] & 0xFF; NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; - Blocks[Index] = ((unsigned short)BlockType << 4) | ((unsigned short)BlockMeta); - }*/ + Blocks[LastOffset] = (BlockType << 4) | ((unsigned char)BlockMeta); + Blocks[LastOffset + 1] = (unsigned char)BlockType >> 4; + LastOffset += 2; + } const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; - const int MetadataOffset = sizeof(m_BlockTypes); - const int BlockLightOffset = MetadataOffset + sizeof(m_BlockMetas); + const int BlockLightOffset = sizeof(Blocks); const int SkyLightOffset = BlockLightOffset + sizeof(m_BlockLight); const int BiomeOffset = SkyLightOffset + sizeof(m_BlockSkyLight); const int DataSize = BiomeOffset + BiomeDataSize; // Temporary buffer for the composed data: char AllData [DataSize]; - memcpy(AllData, m_BlockTypes, sizeof(m_BlockTypes)); - memcpy(AllData + MetadataOffset, m_BlockMetas, sizeof(m_BlockMetas)); + memcpy(AllData, Blocks, sizeof(Blocks)); memcpy(AllData + BlockLightOffset, m_BlockLight, sizeof(m_BlockLight)); memcpy(AllData + SkyLightOffset, m_BlockSkyLight, sizeof(m_BlockSkyLight)); memcpy(AllData + BiomeOffset, m_BiomeData, BiomeDataSize); -- cgit v1.2.3 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/Protocol/ChunkDataSerializer.cpp | 45 +++++++++++++----------------------- 1 file changed, 16 insertions(+), 29 deletions(-) (limited to 'src/Protocol/ChunkDataSerializer.cpp') 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()); -- cgit v1.2.3 From 382a42b3d6a5b1808ad9c8bfe1494ad44676c14b Mon Sep 17 00:00:00 2001 From: Howaner Date: Sun, 14 Sep 2014 14:24:28 +0200 Subject: Fixed warnings. --- src/Protocol/ChunkDataSerializer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Protocol/ChunkDataSerializer.cpp') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index 268cf4a9f..61df24c31 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -192,8 +192,8 @@ void cChunkDataSerializer::Serialize80(AString & a_Data, int a_ChunkX, int a_Chu { BLOCKTYPE BlockType = m_BlockTypes[Index] & 0xFF; NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; - Blocks[LastOffset] = (BlockType << 4) | ((unsigned char)BlockMeta); - Blocks[LastOffset + 1] = (unsigned char)BlockType >> 4; + Blocks[LastOffset] = (unsigned char)(BlockType << 4) | BlockMeta; + Blocks[LastOffset + 1] = (unsigned char)(BlockType >> 4); LastOffset += 2; } @@ -215,7 +215,7 @@ void cChunkDataSerializer::Serialize80(AString & a_Data, int a_ChunkX, int a_Chu Packet.WriteBEInt(a_ChunkX); Packet.WriteBEInt(a_ChunkZ); Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag - Packet.WriteBEShort(0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff + Packet.WriteBEShort((short) 0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff Packet.WriteVarInt(DataSize); // Chunk size Packet.WriteBuf(AllData, DataSize); // Chunk data -- cgit v1.2.3 From 6aa331a4fa314daebfb76161b7e26685d7786e2e Mon Sep 17 00:00:00 2001 From: Howaner Date: Fri, 19 Sep 2014 15:07:01 +0200 Subject: Code improvements. --- src/Protocol/ChunkDataSerializer.cpp | 62 +++++++++++++++++------------------- 1 file changed, 30 insertions(+), 32 deletions(-) (limited to 'src/Protocol/ChunkDataSerializer.cpp') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index 61df24c31..a133cc0e3 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -45,7 +45,7 @@ const AString & cChunkDataSerializer::Serialize(int a_Version, int a_ChunkX, int { case RELEASE_1_2_5: Serialize29(data); break; case RELEASE_1_3_2: Serialize39(data); break; - case RELEASE_1_8_0: Serialize80(data, a_ChunkX, a_ChunkZ); break; + case RELEASE_1_8_0: Serialize47(data, a_ChunkX, a_ChunkZ); break; // TODO: Other protocol versions may serialize the data differently; implement here default: @@ -181,43 +181,41 @@ void cChunkDataSerializer::Serialize39(AString & a_Data) -void cChunkDataSerializer::Serialize80(AString & a_Data, int a_ChunkX, int a_ChunkZ) +void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_ChunkZ) { - // TODO: Do not copy data and then compress it; rather, compress partial blocks of data (zlib *can* stream) - - // Blocktypes converter (1.8 included the meta into the blocktype): - unsigned char Blocks[cChunkDef::NumBlocks * 2]; - size_t LastOffset = 0; - for (size_t Index = 0; Index < cChunkDef::NumBlocks; Index++) - { - BLOCKTYPE BlockType = m_BlockTypes[Index] & 0xFF; - NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; - Blocks[LastOffset] = (unsigned char)(BlockType << 4) | BlockMeta; - Blocks[LastOffset + 1] = (unsigned char)(BlockType >> 4); - LastOffset += 2; - } - - const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; - const int BlockLightOffset = sizeof(Blocks); - const int SkyLightOffset = BlockLightOffset + sizeof(m_BlockLight); - const int BiomeOffset = SkyLightOffset + sizeof(m_BlockSkyLight); - const int DataSize = BiomeOffset + BiomeDataSize; - - // Temporary buffer for the composed data: - char AllData [DataSize]; - memcpy(AllData, Blocks, sizeof(Blocks)); - memcpy(AllData + BlockLightOffset, m_BlockLight, sizeof(m_BlockLight)); - memcpy(AllData + SkyLightOffset, m_BlockSkyLight, sizeof(m_BlockSkyLight)); - memcpy(AllData + BiomeOffset, m_BiomeData, BiomeDataSize); + // This function returns the fully compressed packet (including packet size), not the raw packet! + // Create the packet: cByteBuffer Packet(512 KiB); Packet.WriteVarInt(0x21); // Packet id (Chunk Data packet) Packet.WriteBEInt(a_ChunkX); Packet.WriteBEInt(a_ChunkZ); Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag Packet.WriteBEShort((short) 0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff - Packet.WriteVarInt(DataSize); // Chunk size - Packet.WriteBuf(AllData, DataSize); // Chunk data + + // Write the chunk size: + const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; + UInt32 ChunkSize = ( + (cChunkDef::NumBlocks * 2) + // Block meta + type + sizeof(m_BlockLight) + // Block light + sizeof(m_BlockSkyLight) + // Block sky light + BiomeDataSize // Biome data + ); + Packet.WriteVarInt(ChunkSize); + + // Write the block types to the packet: + for (size_t Index = 0; Index < cChunkDef::NumBlocks; Index++) + { + BLOCKTYPE BlockType = m_BlockTypes[Index] & 0xFF; + NIBBLETYPE BlockMeta = m_BlockMetas[Index / 2] >> ((Index & 1) * 4) & 0x0f; + Packet.WriteByte((unsigned char)(BlockType << 4) | BlockMeta); + Packet.WriteByte((unsigned char)(BlockType >> 4)); + } + + // Write the rest: + Packet.WriteBuf(m_BlockLight, sizeof(m_BlockLight)); + Packet.WriteBuf(m_BlockSkyLight, sizeof(m_BlockSkyLight)); + Packet.WriteBuf(m_BiomeData, BiomeDataSize); AString PacketData; Packet.ReadAll(PacketData); @@ -236,13 +234,13 @@ void cChunkDataSerializer::Serialize80(AString & a_Data, int a_ChunkX, int a_Chu else { AString PostData; - Buffer.WriteVarInt(Packet.GetUsedSpace() + 1); + Buffer.WriteVarInt((UInt32)Packet.GetUsedSpace() + 1); Buffer.WriteVarInt(0); Buffer.ReadAll(PostData); Buffer.CommitRead(); a_Data.clear(); - a_Data.resize(PostData.size() + PacketData.size()); + a_Data.reserve(PostData.size() + PacketData.size()); a_Data.append(PostData.data(), PostData.size()); a_Data.append(PacketData.data(), PacketData.size()); } -- cgit v1.2.3 From 8f4cc27e39188d302f6575e7324002e2515fd789 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 25 Sep 2014 20:46:50 +0200 Subject: Added cByteBuffer::WriteBEUShort(). --- src/Protocol/ChunkDataSerializer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Protocol/ChunkDataSerializer.cpp') diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index a133cc0e3..5d080656d 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -191,7 +191,7 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu Packet.WriteBEInt(a_ChunkX); Packet.WriteBEInt(a_ChunkZ); Packet.WriteBool(true); // "Ground-up continuous", or rather, "biome data present" flag - Packet.WriteBEShort((short) 0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff + Packet.WriteBEUShort(0xffff); // We're aways sending the full chunk with no additional data, so the bitmap is 0xffff // Write the chunk size: const int BiomeDataSize = cChunkDef::Width * cChunkDef::Width; -- cgit v1.2.3