2012-06-14 09:06:06 -04: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
// Helper serialization class cJsonChunkSerializer is declared as well
# pragma once
2012-09-23 17:23:33 -04:00
# include "../OSSupport/IsThread.h"
2013-12-31 10:48:57 -05:00
# include "../OSSupport/Queue.h"
2012-06-14 09:06:06 -04:00
// fwd:
class cWorld ;
2014-12-10 16:35:16 -05:00
typedef cQueue < cChunkCoordsWithCallback > cChunkCoordsQueue ;
2013-12-31 10:48:57 -05:00
2012-06-14 09:06:06 -04:00
2015-07-31 10:49:10 -04:00
/** Interface that all the world storage schemas need to implement */
2012-06-14 09:06:06 -04:00
class cWSSchema abstract
{
public :
cWSSchema ( cWorld * a_World ) : m_World ( a_World ) { }
virtual ~ cWSSchema ( ) { } // Force the descendants' destructors to be virtual
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
virtual bool LoadChunk ( const cChunkCoords & a_Chunk ) = 0 ;
virtual bool SaveChunk ( const cChunkCoords & a_Chunk ) = 0 ;
virtual const AString GetName ( void ) const = 0 ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
protected :
cWorld * m_World ;
} ;
typedef std : : list < cWSSchema * > cWSSchemaList ;
2015-07-31 10:49:10 -04:00
/** The actual world storage class */
2012-06-14 09:06:06 -04:00
class cWorldStorage :
public cIsThread
{
typedef cIsThread super ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
public :
cWorldStorage ( void ) ;
2017-05-20 02:16:28 -04:00
virtual ~ cWorldStorage ( ) override ;
2015-10-04 08:06:37 -04:00
/** Queues a chunk to be loaded, asynchronously.
The callback , if specified , will be called with the result of the load operation . */
2014-12-10 16:35:16 -05:00
void QueueLoadChunk ( int a_ChunkX , int a_ChunkZ , cChunkCoordCallback * a_Callback = nullptr ) ;
2015-10-04 08:06:37 -04:00
/** Queues a chunk to be saved, asynchronously.
The callback , if specified , will be called with the result of the save operation . */
2014-12-10 16:35:16 -05:00
void QueueSaveChunk ( int a_ChunkX , int a_ChunkZ , cChunkCoordCallback * a_Callback = nullptr ) ;
2016-02-05 16:45:45 -05:00
2014-01-17 14:01:14 -05:00
bool Start ( cWorld * a_World , const AString & a_StorageSchemaName , int a_StorageCompressionFactor ) ; // Hide the cIsThread's Start() method, we need to provide args
2013-08-11 14:16:41 -04:00
void Stop ( void ) ; // Hide the cIsThread's Stop() method, we need to signal the event
2012-06-14 09:06:06 -04:00
void WaitForFinish ( void ) ;
2014-01-02 07:32:55 -05:00
void WaitForLoadQueueEmpty ( void ) ;
void WaitForSaveQueueEmpty ( void ) ;
2016-02-05 16:45:45 -05:00
2013-12-31 10:48:57 -05:00
size_t GetLoadQueueLength ( void ) ;
size_t GetSaveQueueLength ( void ) ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
protected :
cWorld * m_World ;
AString m_StorageSchemaName ;
2014-01-02 12:37:34 -05:00
2014-09-05 17:26:00 -04:00
cChunkCoordsQueue m_LoadQueue ;
2013-12-31 10:48:57 -05:00
cChunkCoordsQueue m_SaveQueue ;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** All the storage schemas (all used for loading) */
2012-06-14 09:06:06 -04:00
cWSSchemaList m_Schemas ;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** The one storage schema used for saving */
cWSSchema * m_SaveSchema ;
/** Set when there's any addition to the queues */
cEvent m_Event ;
2014-09-05 17:26:00 -04:00
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Loads the chunk specified; returns true on success, false on failure */
2014-09-05 17:26:00 -04:00
bool LoadChunk ( int a_ChunkX , int a_ChunkZ ) ;
2014-01-17 14:01:14 -05:00
void InitSchemas ( int a_StorageCompressionFactor ) ;
2016-02-05 16:45:45 -05:00
2012-06-14 09:06:06 -04:00
virtual void Execute ( void ) override ;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Loads one chunk from the queue (if any queued); returns true if there are more chunks in the load queue */
2012-06-14 09:06:06 -04:00
bool LoadOneChunk ( void ) ;
2016-02-05 16:45:45 -05:00
2015-07-31 10:49:10 -04:00
/** Saves one chunk from the queue (if any queued); returns true if there are more chunks in the save queue */
2012-06-14 09:06:06 -04:00
bool SaveOneChunk ( void ) ;
} ;