2012-02-13 16:47:03 -05:00
// WorldStorage.h
// Interfaces to the cWorldStorage class representing the chunk loading / saving thread
// This class decides which storage schema to use for saving; it queries all available schemas for loading
// Also declares the base class for all storage schemas, cWSSchema
2012-02-16 08:42:35 -05:00
// Helper serialization class cJsonChunkSerializer is declared as well
2012-02-13 16:47:03 -05:00
# pragma once
# ifndef WORLDSTORAGE_H_INCLUDED
# define WORLDSTORAGE_H_INCLUDED
# include "cChunk.h"
# include "cIsThread.h"
2012-02-16 08:42:35 -05:00
# include <json/json.h>
// fwd:
class cEntity ;
class cBlockEntity ;
2012-02-13 16:47:03 -05:00
/// Interface that all the world storage schemas need to implement
class cWSSchema ABSTRACT
{
public :
cWSSchema ( cWorld * a_World ) : m_World ( a_World ) { }
virtual ~ cWSSchema ( ) { } // Force the descendants' destructors to be virtual
2012-02-16 08:42:35 -05:00
virtual bool LoadChunk ( const cChunkCoords & a_Chunk ) = 0 ;
virtual bool SaveChunk ( const cChunkCoords & a_Chunk ) = 0 ;
2012-02-13 16:47:03 -05:00
virtual const AString GetName ( void ) const = 0 ;
protected :
cWorld * m_World ;
} ;
typedef std : : list < cWSSchema * > cWSSchemaList ;
2012-02-16 08:42:35 -05:00
/// Helper class for serializing a chunk into Json
class cJsonChunkSerializer :
public cChunkDataCallback
{
public :
cJsonChunkSerializer ( void ) ;
Json : : Value & GetRoot ( void ) { return m_Root ; }
AString & GetBlockData ( void ) { return m_BlockData ; }
bool HasJsonData ( void ) const { return m_HasJsonData ; }
protected :
// BlockData is serialized into string
AString m_BlockData ;
// Entities and BlockEntities are serialized to Json
Json : : Value m_Root ;
Json : : Value m_AllChests ;
Json : : Value m_AllFurnaces ;
Json : : Value m_AllSigns ;
bool m_HasJsonData ;
// cChunkDataCallback overrides:
virtual void BlockData ( const char * a_Data ) override ;
virtual void Entity ( cEntity * a_Entity ) override ;
virtual void BlockEntity ( cBlockEntity * a_Entity ) override ;
} ;
/// The actual world storage class
2012-02-13 16:47:03 -05:00
class cWorldStorage :
public cIsThread
{
typedef cIsThread super ;
public :
cWorldStorage ( void ) ;
~ cWorldStorage ( ) ;
2012-02-26 11:46:23 -05:00
void QueueLoadChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , bool a_Generate ) ; // Queues the chunk for loading; if not loaded, the chunk will be generated if a_Generate is true
2012-02-17 12:56:25 -05:00
void QueueSaveChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) ;
2012-02-13 16:47:03 -05:00
2012-02-28 05:45:53 -05:00
/// Loads the chunk specified; returns true on success, false on failure
bool LoadChunk ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) ;
2012-02-26 11:46:23 -05:00
void UnqueueLoad ( int a_ChunkX , int a_ChunkY , int a_ChunkZ ) ;
2012-02-16 08:42:35 -05:00
void UnqueueSave ( const cChunkCoords & a_Chunk ) ;
2012-02-13 16:47:03 -05:00
bool Start ( cWorld * a_World , const AString & a_StorageSchemaName ) ; // Hide the cIsThread's Start() method, we need to provide args
void WaitForFinish ( void ) ;
2012-02-18 12:53:22 -05:00
void WaitForQueuesEmpty ( void ) ;
int GetLoadQueueLength ( void ) ;
int GetSaveQueueLength ( void ) ;
2012-02-13 16:47:03 -05:00
protected :
2012-02-26 11:46:23 -05:00
struct sChunkLoad
{
int m_ChunkX ;
int m_ChunkY ;
int m_ChunkZ ;
bool m_Generate ; // If true, the chunk will be generated if it cannot be loaded
sChunkLoad ( int a_ChunkX , int a_ChunkY , int a_ChunkZ , bool a_Generate ) : m_ChunkX ( a_ChunkX ) , m_ChunkY ( a_ChunkY ) , m_ChunkZ ( a_ChunkZ ) , m_Generate ( a_Generate ) { }
} ;
typedef std : : list < sChunkLoad > sChunkLoadQueue ;
2012-02-13 16:47:03 -05:00
cWorld * m_World ;
AString m_StorageSchemaName ;
2012-02-18 12:53:22 -05:00
// Both queues are locked by the same CS
cCriticalSection m_CSQueues ;
2012-02-26 11:46:23 -05:00
sChunkLoadQueue m_LoadQueue ;
2012-02-18 12:53:22 -05:00
cChunkCoordsList m_SaveQueue ;
2012-02-13 16:47:03 -05:00
2012-02-18 12:53:22 -05:00
cEvent m_Event ; // Set when there's any addition to the queues
cEvent m_evtRemoved ; // Set when an item has been removed from the queue, either by the worker thread or the Unqueue methods
2012-02-13 16:47:03 -05:00
2012-02-16 08:42:35 -05:00
/// All the storage schemas (all used for loading)
2012-02-13 16:47:03 -05:00
cWSSchemaList m_Schemas ;
2012-02-16 08:42:35 -05:00
/// The one storage schema used for saving
cWSSchema * m_SaveSchema ;
2012-02-13 16:47:03 -05:00
void InitSchemas ( void ) ;
virtual void Execute ( void ) override ;
2012-02-16 08:42:35 -05:00
/// Loads one chunk from the queue (if any queued); returns true if there are more chunks in the load queue
bool LoadOneChunk ( void ) ;
/// Saves one chunk from the queue (if any queued); returns true if there are more chunks in the save queue
bool SaveOneChunk ( void ) ;
2012-02-13 16:47:03 -05:00
} ;
# endif // WORLDSTORAGE_H_INCLUDED