2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// WSSCompact.h
|
|
|
|
|
|
|
|
// Interfaces to the cWSSCompact class representing the "Compact" storage schema (PAK-files)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#ifndef WSSCOMPACT_H_INCLUDED
|
|
|
|
#define WSSCOMPACT_H_INCLUDED
|
|
|
|
|
|
|
|
#include "WorldStorage.h"
|
2014-03-11 10:01:17 -04:00
|
|
|
#include "../Vector3.h"
|
2013-11-28 14:14:34 -05:00
|
|
|
#include "json/json.h"
|
2014-05-18 10:10:12 -04:00
|
|
|
#include "ChunkDataCallback.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Helper class for serializing a chunk into Json
|
|
|
|
class cJsonChunkSerializer :
|
2014-05-21 14:58:48 -04:00
|
|
|
public cChunkDataArrayCollector
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
cJsonChunkSerializer(void);
|
|
|
|
|
|
|
|
Json::Value & GetRoot (void) {return m_Root; }
|
2012-08-03 07:53:11 -04:00
|
|
|
BLOCKTYPE * GetBlockData(void) {return (BLOCKTYPE *)m_BlockData; }
|
2012-06-14 09:06:06 -04:00
|
|
|
bool HasJsonData (void) const {return m_HasJsonData; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
// NOTE: block data is serialized into inherited cChunkDataCollector's m_BlockData[] array
|
|
|
|
|
|
|
|
// Entities and BlockEntities are serialized to Json
|
|
|
|
Json::Value m_Root;
|
|
|
|
bool m_HasJsonData;
|
|
|
|
|
|
|
|
// cChunkDataCollector overrides:
|
|
|
|
virtual void Entity (cEntity * a_Entity) override;
|
|
|
|
virtual void BlockEntity (cBlockEntity * a_Entity) override;
|
2014-04-26 13:50:23 -04:00
|
|
|
virtual void LightIsValid (bool a_IsLightValid) override;
|
2012-06-14 09:06:06 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cWSSCompact :
|
|
|
|
public cWSSchema
|
|
|
|
{
|
|
|
|
public:
|
2014-01-17 14:01:14 -05:00
|
|
|
cWSSCompact(cWorld * a_World, int a_CompressionFactor) : cWSSchema(a_World), m_CompressionFactor(a_CompressionFactor) {}
|
2012-06-14 09:06:06 -04:00
|
|
|
virtual ~cWSSCompact();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2013-12-20 10:15:39 -05:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
// Offsets to individual components in the joined blockdata array
|
|
|
|
MetaOffset = cChunkDef::NumBlocks,
|
|
|
|
LightOffset = MetaOffset + cChunkDef::NumBlocks / 2,
|
|
|
|
SkyLightOffset = LightOffset + cChunkDef::NumBlocks / 2,
|
|
|
|
} ;
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
struct sChunkHeader;
|
|
|
|
typedef std::vector<sChunkHeader *> sChunkHeaders;
|
|
|
|
|
|
|
|
/// Implements a cache for a single PAK file; implements lazy-write in order to be able to write multiple chunks fast
|
|
|
|
class cPAKFile
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-01-17 14:01:14 -05:00
|
|
|
cPAKFile(const AString & a_FileName, int a_LayerX, int a_LayerZ, int a_CompressionFactor);
|
2012-06-14 09:06:06 -04:00
|
|
|
~cPAKFile();
|
|
|
|
|
|
|
|
bool GetChunkData(const cChunkCoords & a_Chunk, int & a_UncompressedSize, AString & a_Data);
|
|
|
|
bool SetChunkData(const cChunkCoords & a_Chunk, int a_UncompressedSize, const AString & a_Data);
|
|
|
|
bool EraseChunkData(const cChunkCoords & a_Chunk);
|
|
|
|
|
|
|
|
bool SaveChunk(const cChunkCoords & a_Chunk, cWorld * a_World);
|
|
|
|
|
|
|
|
int GetLayerX(void) const {return m_LayerX; }
|
|
|
|
int GetLayerZ(void) const {return m_LayerZ; }
|
|
|
|
|
|
|
|
static const int PAK_VERSION = 1;
|
|
|
|
#if AXIS_ORDER == AXIS_ORDER_XZY
|
|
|
|
static const int CHUNK_VERSION = 3;
|
|
|
|
#elif AXIS_ORDER == AXIS_ORDER_YZX
|
|
|
|
static const int CHUNK_VERSION = 2;
|
|
|
|
#endif
|
|
|
|
protected:
|
|
|
|
|
|
|
|
AString m_FileName;
|
2014-01-17 14:01:14 -05:00
|
|
|
int m_CompressionFactor;
|
2012-06-14 09:06:06 -04:00
|
|
|
int m_LayerX;
|
|
|
|
int m_LayerZ;
|
|
|
|
|
|
|
|
sChunkHeaders m_ChunkHeaders;
|
|
|
|
AString m_DataContents; // Data contents of the file, cached
|
|
|
|
|
|
|
|
int m_NumDirty; // Number of chunks that were written into m_DataContents but not into the file
|
|
|
|
|
|
|
|
Vector3i m_ChunkSize; // Is related to m_ChunkVersion
|
|
|
|
char m_ChunkVersion;
|
|
|
|
char m_PakVersion;
|
|
|
|
|
|
|
|
bool SaveChunkToData(const cChunkCoords & a_Chunk, cWorld * a_World); // Saves the chunk to m_DataContents, updates headers and m_NumDirty
|
|
|
|
void SynchronizeFile(void); // Writes m_DataContents along with the headers to file, resets m_NumDirty
|
|
|
|
|
|
|
|
void UpdateChunk1To2(void); // Height from 128 to 256
|
|
|
|
void UpdateChunk2To3(void); // Axis order from YZX to XZY
|
|
|
|
} ;
|
|
|
|
|
|
|
|
typedef std::list<cPAKFile *> cPAKFiles;
|
|
|
|
|
|
|
|
cCriticalSection m_CS;
|
|
|
|
cPAKFiles m_PAKFiles; // A MRU cache of PAK files
|
|
|
|
|
2014-01-17 14:01:14 -05:00
|
|
|
int m_CompressionFactor;
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
/// Loads the correct PAK file either from cache or from disk, manages the m_PAKFiles cache
|
|
|
|
cPAKFile * LoadPAKFile(const cChunkCoords & a_Chunk);
|
|
|
|
|
|
|
|
/// Gets chunk data from the correct file; locks CS as needed
|
|
|
|
bool GetChunkData(const cChunkCoords & a_Chunk, int & a_UncompressedSize, AString & a_Data);
|
|
|
|
|
|
|
|
/// Sets chunk data to the correct file; locks CS as needed
|
|
|
|
bool SetChunkData(const cChunkCoords & a_Chunk, int a_UncompressedSize, const AString & a_Data);
|
|
|
|
|
|
|
|
/// Erases chunk data from the correct file; locks CS as needed
|
|
|
|
bool EraseChunkData(const cChunkCoords & a_Chunk);
|
|
|
|
|
|
|
|
/// Loads the chunk from the data (no locking needed)
|
2014-04-18 15:09:44 -04:00
|
|
|
bool LoadChunkFromData(const cChunkCoords & a_Chunk, int a_UncompressedSize, const AString & a_Data, cWorld * a_World);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
void LoadEntitiesFromJson(Json::Value & a_Value, cEntityList & a_Entities, cBlockEntityList & a_BlockEntities, cWorld * a_World);
|
|
|
|
|
|
|
|
// cWSSchema overrides:
|
|
|
|
virtual bool LoadChunk(const cChunkCoords & a_Chunk) override;
|
|
|
|
virtual bool SaveChunk(const cChunkCoords & a_Chunk) override;
|
|
|
|
virtual const AString GetName(void) const override {return "compact"; }
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // WSSCOMPACT_H_INCLUDED
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|