2020-01-03 11:31:13 -05:00
|
|
|
#pragma once
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2020-08-21 13:22:04 -04:00
|
|
|
#include "../ByteBuffer.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../ChunkData.h"
|
2020-04-03 02:57:01 -04:00
|
|
|
#include "../Defines.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-03 11:31:13 -05:00
|
|
|
|
2020-07-19 11:29:49 -04:00
|
|
|
class cByteBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-01-03 11:31:13 -05:00
|
|
|
/** Serializes one chunk's data to (possibly multiple) protocol versions.
|
|
|
|
Caches the serialized data for as long as this object lives, so that the same data can be sent to
|
|
|
|
other clients using the same protocol. */
|
2013-07-29 07:13:03 -04:00
|
|
|
class cChunkDataSerializer
|
|
|
|
{
|
2020-08-21 13:22:04 -04:00
|
|
|
using ClientHandles = std::unordered_set<cClientHandle *>;
|
|
|
|
|
|
|
|
/** Enum to collapse protocol versions into a contiguous index. */
|
|
|
|
enum class CacheVersion
|
|
|
|
{
|
|
|
|
v47,
|
|
|
|
v107,
|
|
|
|
v110,
|
|
|
|
v393,
|
|
|
|
v401,
|
|
|
|
v477,
|
|
|
|
|
|
|
|
Count
|
|
|
|
};
|
|
|
|
|
|
|
|
/** A single cache entry containing the raw data, compressed data, and a validity flag. */
|
|
|
|
struct ChunkDataCache
|
|
|
|
{
|
|
|
|
std::string PacketData;
|
|
|
|
std::string ToSend;
|
|
|
|
bool Engaged = false;
|
|
|
|
};
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
public:
|
2020-07-18 09:22:14 -04:00
|
|
|
|
2020-08-21 13:22:04 -04:00
|
|
|
cChunkDataSerializer(eDimension a_Dimension);
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2020-08-21 13:22:04 -04:00
|
|
|
/** For each client, serializes the chunk into their protocol version and sends it.
|
|
|
|
Parameters are the coordinates of the chunk to serialise, and the data and biome data read from the chunk. */
|
|
|
|
void SendToClients(int a_ChunkX, int a_ChunkZ, const cChunkData & a_Data, const unsigned char * a_BiomeData, const ClientHandles & a_SendTo);
|
2020-01-03 11:31:13 -05:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2020-08-21 13:22:04 -04:00
|
|
|
/** Serialises the given chunk, storing the result into the given cache entry, and sends the data.
|
|
|
|
If the cache entry is already present, simply re-uses it. */
|
|
|
|
inline void Serialize(cClientHandle * a_Client, int a_ChunkX, int a_ChunkZ, const cChunkData & a_Data, const unsigned char * a_BiomeData, CacheVersion a_CacheVersion);
|
|
|
|
|
|
|
|
inline void Serialize47 (int a_ChunkX, int a_ChunkZ, const cChunkData & a_Data, const unsigned char * a_BiomeData); // Release 1.8
|
|
|
|
inline void Serialize107(int a_ChunkX, int a_ChunkZ, const cChunkData & a_Data, const unsigned char * a_BiomeData); // Release 1.9
|
|
|
|
inline void Serialize110(int a_ChunkX, int a_ChunkZ, const cChunkData & a_Data, const unsigned char * a_BiomeData); // Release 1.9.4
|
2020-07-27 19:08:57 -04:00
|
|
|
template <auto Palette>
|
2020-08-21 13:22:04 -04:00
|
|
|
inline void Serialize393(int a_ChunkX, int a_ChunkZ, const cChunkData & a_Data, const unsigned char * a_BiomeData); // Release 1.13 - 1.13.2
|
|
|
|
inline void Serialize477(int a_ChunkX, int a_ChunkZ, const cChunkData & a_Data, const unsigned char * a_BiomeData); // Release 1.14 - 1.14.4
|
2020-07-19 11:29:49 -04:00
|
|
|
|
2020-07-20 04:56:27 -04:00
|
|
|
/** Writes all blocks in a chunk section into a series of Int64.
|
|
|
|
Writes start from the bit directly subsequent to the previous write's end, possibly crossing over to the next Int64. */
|
2020-08-20 18:07:46 -04:00
|
|
|
template <auto Palette>
|
2020-08-21 13:22:04 -04:00
|
|
|
inline void WriteSectionDataSeamless(const cChunkData::sChunkSection & a_Section, const UInt8 a_BitsPerEntry);
|
2020-07-19 11:29:49 -04:00
|
|
|
|
2020-08-21 13:22:04 -04:00
|
|
|
/** Finalises the data, compresses it if required, and stores it into cache. */
|
|
|
|
inline void CompressPacketInto(ChunkDataCache & a_Cache);
|
2020-01-03 11:31:13 -05:00
|
|
|
|
2020-08-21 13:22:04 -04:00
|
|
|
/** A staging area used to construct the chunk packet, persistent to avoid reallocating. */
|
|
|
|
cByteBuffer m_Packet;
|
2020-01-03 11:31:13 -05:00
|
|
|
|
2020-08-21 13:22:04 -04:00
|
|
|
/** The dimension for the World this Serializer is tied to. */
|
2020-01-03 11:31:13 -05:00
|
|
|
const eDimension m_Dimension;
|
2020-08-21 13:22:04 -04:00
|
|
|
|
|
|
|
/** A cache, mapping protocol version to a fully serialised chunk.
|
|
|
|
It is used during a single invocation of SendToClients with more than one client. */
|
|
|
|
std::array<ChunkDataCache, static_cast<size_t>(CacheVersion::Count)> m_Cache;
|
2013-07-29 07:13:03 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|