summaryrefslogtreecommitdiffstats
path: root/src/WorldStorage/WorldStorage.h
blob: dc4bb2e1793260ee5243b6baa7a6be81c6e1c530 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

// 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

#include "../OSSupport/IsThread.h"
#include "../OSSupport/Queue.h"





// fwd:
class cWorld;





/** 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

	virtual bool LoadChunk(const cChunkCoords & a_Chunk) = 0;
	virtual bool SaveChunk(const cChunkCoords & a_Chunk) = 0;
	virtual const AString GetName(void) const = 0;

protected:

	cWorld * m_World;
} ;

typedef std::list<cWSSchema *> cWSSchemaList;





/** The actual world storage class */
class cWorldStorage:
	public cIsThread
{
	using Super = cIsThread;

public:

	cWorldStorage();
	virtual ~cWorldStorage() override;

	/** Queues a chunk to be loaded, asynchronously. */
	void QueueLoadChunk(int a_ChunkX, int a_ChunkZ);

	/** Queues a chunk to be saved, asynchronously. */
	void QueueSaveChunk(int a_ChunkX, int a_ChunkZ);

	/** Initializes the storage schemas, ready to be started. */
	void Initialize(cWorld & a_World, const AString & a_StorageSchemaName, int a_StorageCompressionFactor);
	void Stop(void);  // Hide the cIsThread's Stop() method, we need to signal the event
	void WaitForFinish(void);
	void WaitForLoadQueueEmpty(void);
	void WaitForSaveQueueEmpty(void);

	size_t GetLoadQueueLength(void);
	size_t GetSaveQueueLength(void);

protected:

	cWorld * m_World;
	AString  m_StorageSchemaName;

	cQueue<cChunkCoords> m_LoadQueue;
	cQueue<cChunkCoords> m_SaveQueue;

	/** All the storage schemas (all used for loading) */
	cWSSchemaList m_Schemas;

	/** The one storage schema used for saving */
	cWSSchema * m_SaveSchema;

	/** Set when there's any addition to the queues */
	cEvent m_Event;


	/** Loads the chunk specified; returns true on success, false on failure */
	bool LoadChunk(int a_ChunkX, int a_ChunkZ);

	void InitSchemas(int a_StorageCompressionFactor);

	virtual void Execute(void) override;

	/** 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);
} ;