6 #include "../ByteBuffer.h" 19 if (Section !=
nullptr)
35 const unsigned char * a_BiomeData,
39 m_BiomeData(a_BiomeData),
40 m_Dimension(a_Dimension)
66 LOGERROR(
"cChunkDataSerializer::Serialize(): Unknown version: %d", a_Version);
67 ASSERT(!
"Unknown chunk data serialization version");
112 Packet.
WriteBEUInt8(static_cast<unsigned char>(BlockType << 4) | BlockMeta);
113 Packet.
WriteBEUInt8(static_cast<unsigned char>(BlockType >> 4));
140 if (PacketData.size() >= 256)
144 ASSERT(!
"Packet compression failed.");
158 a_Data.reserve(PostData.size() + PacketData.size());
159 a_Data.append(PostData.data(), PostData.size());
160 a_Data.append(PacketData.data(), PacketData.size());
180 const size_t BitsPerEntry = 13;
181 const size_t Mask = (1 << BitsPerEntry) - 1;
183 size_t ChunkSectionSize = (
187 ChunkSectionDataArraySize * 8 +
209 Packet.
WriteVarInt32(static_cast<UInt32>(ChunkSectionDataArraySize));
212 UInt64 CurrentlyWrittenIndex = 0;
228 size_t BitPosition = Index * BitsPerEntry;
229 size_t FirstIndex = BitPosition / 64;
230 size_t SecondIndex = ((Index + 1) * BitsPerEntry - 1) / 64;
231 size_t BitOffset = BitPosition % 64;
233 if (FirstIndex != CurrentlyWrittenIndex)
238 CurrentlyWrittenIndex = FirstIndex;
241 TempLong |= (Value << BitOffset);
243 if (FirstIndex != SecondIndex)
247 CurrentlyWrittenIndex = SecondIndex;
249 TempLong = (Value >> (64 - BitOffset));
273 if (PacketData.size() >= 256)
277 ASSERT(!
"Packet compression failed.");
291 a_Data.reserve(PostData.size() + PacketData.size());
292 a_Data.append(PostData.data(), PostData.size());
293 a_Data.append(PacketData.data(), PacketData.size());
313 const size_t BitsPerEntry = 13;
314 const size_t Mask = (1 << BitsPerEntry) - 1;
316 size_t ChunkSectionSize = (
320 ChunkSectionDataArraySize * 8 +
342 Packet.
WriteVarInt32(static_cast<UInt32>(ChunkSectionDataArraySize));
345 UInt64 CurrentlyWrittenIndex = 0;
361 size_t BitPosition = Index * BitsPerEntry;
362 size_t FirstIndex = BitPosition / 64;
363 size_t SecondIndex = ((Index + 1) * BitsPerEntry - 1) / 64;
364 size_t BitOffset = BitPosition % 64;
366 if (FirstIndex != CurrentlyWrittenIndex)
371 CurrentlyWrittenIndex = FirstIndex;
374 TempLong |= (Value << BitOffset);
376 if (FirstIndex != SecondIndex)
380 CurrentlyWrittenIndex = SecondIndex;
382 TempLong = (Value >> (64 - BitOffset));
409 if (PacketData.size() >= 256)
413 ASSERT(!
"Packet compression failed.");
427 a_Data.reserve(PostData.size() + PacketData.size());
428 a_Data.append(PostData.data(), PostData.size());
429 a_Data.append(PacketData.data(), PacketData.size());
441 ASSERT(!a_BlockTypeMap.empty());
445 Packet.WriteVarInt32(0x22);
446 Packet.WriteBEInt32(a_ChunkX);
447 Packet.WriteBEInt32(a_ChunkZ);
448 Packet.WriteBool(
true);
452 const size_t BitsPerEntry = 14;
453 const size_t Mask = (1 << BitsPerEntry) - 1;
455 size_t ChunkSectionSize = (
457 Packet.GetVarIntSize(static_cast<UInt32>(ChunkSectionDataArraySize)) +
458 ChunkSectionDataArraySize * 8 +
473 Packet.WriteVarInt32(static_cast<UInt32>(ChunkSize));
478 Packet.WriteBEUInt8(static_cast<UInt8>(BitsPerEntry));
479 Packet.WriteVarInt32(static_cast<UInt32>(ChunkSectionDataArraySize));
482 UInt64 CurrentlyWrittenIndex = 0;
488 auto itr = a_BlockTypeMap.find(blockType * 16 | blockMeta);
489 UInt64 Value = (itr == a_BlockTypeMap.end()) ? 0 :itr->second;
493 size_t BitPosition = Index * BitsPerEntry;
494 size_t FirstIndex = BitPosition / 64;
495 size_t SecondIndex = ((Index + 1) * BitsPerEntry - 1) / 64;
496 size_t BitOffset = BitPosition % 64;
498 if (FirstIndex != CurrentlyWrittenIndex)
501 Packet.WriteBEUInt64(TempLong);
503 CurrentlyWrittenIndex = FirstIndex;
506 TempLong |= (Value << BitOffset);
508 if (FirstIndex != SecondIndex)
511 Packet.WriteBEUInt64(TempLong);
512 CurrentlyWrittenIndex = SecondIndex;
514 TempLong = (Value >> (64 - BitOffset));
518 Packet.WriteBEUInt64(TempLong);
531 for (
size_t i = 0; i != BiomeDataSize; i++)
533 Packet.WriteBEUInt32(static_cast<UInt32>(
m_BiomeData[i]) & 0xff);
537 Packet.WriteVarInt32(0);
540 Packet.ReadAll(PacketData);
543 if (PacketData.size() >= 256)
547 ASSERT(!
"Packet compression failed.");
557 Buffer.
WriteVarInt32(static_cast<UInt32>(Packet.GetUsedSpace() + 1));
563 a_Data.reserve(PostData.size() + PacketData.size());
564 a_Data.append(PostData.data(), PostData.size());
565 a_Data.append(PacketData.data(), PacketData.size());
bool WriteVarInt32(UInt32 a_Value)
void ReadAll(AString &a_Data)
Reads all available data into a_Data.
eDimension
Dimension of a world.
void ForEachSection(const cChunkData &a_Data, Func a_Func)
Calls the given function with every present chunk section.
BLOCKTYPE m_BlockTypes[SectionBlockCount]
unsigned char BLOCKTYPE
The datatype used by blockdata.
const sChunkSection * GetSection(size_t a_SectionNum) const
Return a pointer to the chunk section or nullptr if all air.
void Serialize393(AString &a_Data, int a_ChunkX, int a_ChunkZ, const std::map< UInt32, UInt32 > &a_BlockTypeMap)
void Serialize107(AString &a_Data, int a_ChunkX, int a_ChunkZ)
unsigned char NIBBLETYPE
The datatype used by nibbledata (meta, light, skylight)
void LOGERROR(const char *a_Format, fmt::ArgList a_ArgList)
void Serialize47(AString &a_Data, int a_ChunkX, int a_ChunkZ)
void CommitRead(void)
Removes the bytes that have been read from the ringbuffer.
bool WriteBEUInt64(UInt64 a_Value)
static const size_t SectionBlockCount
unsigned long long UInt64
NIBBLETYPE m_BlockSkyLight[SectionBlockCount/2]
cChunkDataSerializer(const cChunkData &a_Data, const unsigned char *a_BiomeData, const eDimension a_Dimension)
An object that can store incoming bytes and lets its clients read the bytes sequentially The bytes ar...
#define KiB
Allows arithmetic expressions like "32 KiB" (but consider using parenthesis around it...
bool WriteBEUInt8(UInt8 a_Value)
const AString & Serialize(int a_Version, int a_ChunkX, int a_ChunkZ, const std::map< UInt32, UInt32 > &a_BlockTypeMap)
Serializes the contained chunk data into the specified protocol version.
NIBBLETYPE m_BlockMetas[SectionBlockCount/2]
size_t GetUsedSpace(void) const
Returns the number of bytes that are currently in the ringbuffer.
UInt16 GetSectionBitmask() const
Returns a bitmask of chunk sections which are currently stored.
bool WriteBEInt32(Int32 a_Value)
bool WriteBuf(const void *a_Buffer, size_t a_Count)
Writes a_Count bytes into a_Buffer; returns true if successful.
static bool CompressPacket(const AString &a_Packet, AString &a_Compressed)
Compress the packet.
static const size_t NumSections
const unsigned char * m_BiomeData
The biomes in the chunk, to be serialized.
const cChunkData & m_Data
The data read from the chunk, to be serialized.
bool WriteBool(bool a_Value)
void Serialize110(AString &a_Data, int a_ChunkX, int a_ChunkZ)
UInt32 NumPresentSections() const
Returns the number of sections present (i.e.
NIBBLETYPE m_BlockLight[SectionBlockCount/2]
Serializations m_Serializations
The per-protocol serialized data, cached for reuse for other clients.
static bool CompressPacket(const AString &a_Packet, AString &a_Compressed)
Compress the packet.
bool WriteBEUInt16(UInt16 a_Value)
const eDimension m_Dimension
The dimension where the chunk resides.