2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// WorldStorage.cpp
|
|
|
|
|
|
|
|
// Implements the cWorldStorage class representing the chunk loading / saving thread
|
|
|
|
|
|
|
|
// To add a new storage schema, implement a cWSSchema descendant and add it to cWorldStorage::InitSchemas()
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "WorldStorage.h"
|
|
|
|
#include "WSSCompact.h"
|
|
|
|
#include "WSSAnvil.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "../World.h"
|
2012-09-23 16:22:46 -04:00
|
|
|
#include "../Generating/ChunkGenerator.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Entity.h"
|
2013-05-28 16:36:35 -04:00
|
|
|
#include "../BlockEntities/BlockEntity.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-15 07:54:13 -04:00
|
|
|
/// If a chunk with this Y coord is de-queued, it is a signal to emit the saved-all message (cWorldStorage::QueueSavedMessage())
|
|
|
|
#define CHUNK_Y_MESSAGE 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
/// Example storage schema - forgets all chunks ;)
|
|
|
|
class cWSSForgetful :
|
|
|
|
public cWSSchema
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cWSSForgetful(cWorld * a_World) : cWSSchema(a_World) {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// cWSSchema overrides:
|
|
|
|
virtual bool LoadChunk(const cChunkCoords & a_Chunk) override {return false; }
|
|
|
|
virtual bool SaveChunk(const cChunkCoords & a_Chunk) override {return true; }
|
|
|
|
virtual const AString GetName(void) const override {return "forgetful"; }
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// cWorldStorage:
|
|
|
|
|
|
|
|
cWorldStorage::cWorldStorage(void) :
|
|
|
|
super("cWorldStorage"),
|
|
|
|
m_World(NULL),
|
|
|
|
m_SaveSchema(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cWorldStorage::~cWorldStorage()
|
|
|
|
{
|
|
|
|
for (cWSSchemaList::iterator itr = m_Schemas.begin(); itr != m_Schemas.end(); ++itr)
|
|
|
|
{
|
|
|
|
delete *itr;
|
|
|
|
} // for itr - m_Schemas[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-17 14:01:14 -05:00
|
|
|
bool cWorldStorage::Start(cWorld * a_World, const AString & a_StorageSchemaName, int a_StorageCompressionFactor )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
m_World = a_World;
|
|
|
|
m_StorageSchemaName = a_StorageSchemaName;
|
2014-01-17 14:01:14 -05:00
|
|
|
InitSchemas(a_StorageCompressionFactor);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
return super::Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-11 14:16:41 -04:00
|
|
|
void cWorldStorage::Stop(void)
|
|
|
|
{
|
|
|
|
WaitForFinish();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cWorldStorage::WaitForFinish(void)
|
|
|
|
{
|
|
|
|
LOG("Waiting for the world storage to finish saving");
|
|
|
|
|
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
m_LoadQueue.Clear();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the saving to finish:
|
2014-01-02 07:32:55 -05:00
|
|
|
WaitForSaveQueueEmpty();
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// Wait for the thread to finish:
|
|
|
|
m_ShouldTerminate = true;
|
2014-01-02 12:37:34 -05:00
|
|
|
m_Event.Set(); // Wake up the thread if waiting
|
2012-06-14 09:06:06 -04:00
|
|
|
super::Wait();
|
|
|
|
LOG("World storage thread finished");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-02 07:32:55 -05:00
|
|
|
void cWorldStorage::WaitForLoadQueueEmpty(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-01-02 07:32:55 -05:00
|
|
|
m_LoadQueue.BlockTillEmpty();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
2014-01-06 04:09:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-02 07:32:55 -05:00
|
|
|
void cWorldStorage::WaitForSaveQueueEmpty(void)
|
|
|
|
{
|
|
|
|
m_SaveQueue.BlockTillEmpty();
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-06 04:09:00 -05:00
|
|
|
|
|
|
|
|
2013-12-31 10:48:57 -05:00
|
|
|
size_t cWorldStorage::GetLoadQueueLength(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
return m_LoadQueue.Size();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-12-31 10:48:57 -05:00
|
|
|
size_t cWorldStorage::GetSaveQueueLength(void)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
return m_SaveQueue.Size();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ, bool a_Generate)
|
|
|
|
{
|
2014-04-08 11:43:28 -04:00
|
|
|
m_LoadQueue.EnqueueItem(sChunkLoad(a_ChunkX, a_ChunkY, a_ChunkZ, a_Generate));
|
2014-01-06 04:09:00 -05:00
|
|
|
m_Event.Set();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWorldStorage::QueueSaveChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
m_SaveQueue.EnqueueItemIfNotPresent(cChunkCoords(a_ChunkX, a_ChunkY, a_ChunkZ));
|
2014-01-06 04:09:00 -05:00
|
|
|
m_Event.Set();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-08-15 07:54:13 -04:00
|
|
|
void cWorldStorage::QueueSavedMessage(void)
|
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
// Pushes a special coord pair into the queue, signalizing a message instead
|
|
|
|
m_SaveQueue.EnqueueItem(cChunkCoords(0, CHUNK_Y_MESSAGE, 0));
|
2014-01-06 04:09:00 -05:00
|
|
|
m_Event.Set();
|
2012-08-15 07:54:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
void cWorldStorage::UnqueueLoad(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
m_LoadQueue.Remove(sChunkLoad(a_ChunkX, a_ChunkY, a_ChunkZ,true));
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWorldStorage::UnqueueSave(const cChunkCoords & a_Chunk)
|
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
m_SaveQueue.Remove(a_Chunk);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-01-17 14:01:14 -05:00
|
|
|
void cWorldStorage::InitSchemas(int a_StorageCompressionFactor)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// The first schema added is considered the default
|
2014-01-17 14:01:14 -05:00
|
|
|
m_Schemas.push_back(new cWSSAnvil (m_World,a_StorageCompressionFactor));
|
|
|
|
m_Schemas.push_back(new cWSSCompact (m_World,a_StorageCompressionFactor));
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Schemas.push_back(new cWSSForgetful(m_World));
|
|
|
|
// Add new schemas here
|
|
|
|
|
|
|
|
if (NoCaseCompare(m_StorageSchemaName, "default") == 0)
|
|
|
|
{
|
|
|
|
m_SaveSchema = m_Schemas.front();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (cWSSchemaList::iterator itr = m_Schemas.begin(); itr != m_Schemas.end(); ++itr)
|
|
|
|
{
|
|
|
|
if (NoCaseCompare((*itr)->GetName(), m_StorageSchemaName) == 0)
|
|
|
|
{
|
|
|
|
m_SaveSchema = *itr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} // for itr - m_Schemas[]
|
|
|
|
|
|
|
|
// Unknown schema selected, let the admin know:
|
|
|
|
LOGWARNING("Unknown storage schema name \"%s\". Using default (\"%s\"). Available schemas:",
|
|
|
|
m_StorageSchemaName.c_str(), m_SaveSchema->GetName().c_str()
|
|
|
|
);
|
|
|
|
for (cWSSchemaList::iterator itr = m_Schemas.begin(); itr != m_Schemas.end(); ++itr)
|
|
|
|
{
|
|
|
|
LOGWARNING("\t\"%s\"", (*itr)->GetName().c_str());
|
|
|
|
}
|
|
|
|
m_SaveSchema = m_Schemas.front();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cWorldStorage::Execute(void)
|
|
|
|
{
|
|
|
|
while (!m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
m_Event.Wait();
|
|
|
|
// Process both queues until they are empty again:
|
2013-12-31 10:48:57 -05:00
|
|
|
bool Success;
|
2012-06-14 09:06:06 -04:00
|
|
|
do
|
|
|
|
{
|
|
|
|
if (m_ShouldTerminate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-31 10:48:57 -05:00
|
|
|
Success = LoadOneChunk();
|
|
|
|
Success |= SaveOneChunk();
|
|
|
|
} while (Success);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cWorldStorage::LoadOneChunk(void)
|
|
|
|
{
|
|
|
|
sChunkLoad ToLoad(0, 0, 0, false);
|
2013-12-31 10:48:57 -05:00
|
|
|
bool ShouldLoad = m_LoadQueue.TryDequeueItem(ToLoad);
|
2012-06-14 09:06:06 -04:00
|
|
|
if (ShouldLoad && !LoadChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkY, ToLoad.m_ChunkZ))
|
|
|
|
{
|
|
|
|
if (ToLoad.m_Generate)
|
|
|
|
{
|
|
|
|
// The chunk couldn't be loaded, generate it:
|
|
|
|
m_World->GetGenerator().QueueGenerateChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkY, ToLoad.m_ChunkZ);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: Notify the world that the load has failed:
|
|
|
|
// m_World->ChunkLoadFailed(ToLoad.m_ChunkX, ToLoad.m_ChunkY, ToLoad.m_ChunkZ);
|
|
|
|
}
|
|
|
|
}
|
2013-12-31 10:48:57 -05:00
|
|
|
return ShouldLoad;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cWorldStorage::SaveOneChunk(void)
|
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
cChunkCoords ToSave(0, 0, 0);
|
|
|
|
bool ShouldSave = m_SaveQueue.TryDequeueItem(ToSave);
|
|
|
|
if(ShouldSave) {
|
|
|
|
if (ToSave.m_ChunkY == CHUNK_Y_MESSAGE)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
LOGINFO("Saved all chunks in world %s", m_World->GetName().c_str());
|
|
|
|
return ShouldSave;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-12-31 10:48:57 -05:00
|
|
|
if (ShouldSave && m_World->IsChunkValid(ToSave.m_ChunkX, ToSave.m_ChunkZ))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
m_World->MarkChunkSaving(ToSave.m_ChunkX, ToSave.m_ChunkZ);
|
|
|
|
if (m_SaveSchema->SaveChunk(ToSave))
|
|
|
|
{
|
|
|
|
m_World->MarkChunkSaved(ToSave.m_ChunkX, ToSave.m_ChunkZ);
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2013-12-31 10:48:57 -05:00
|
|
|
return ShouldSave;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cWorldStorage::LoadChunk(int a_ChunkX, int a_ChunkY, int a_ChunkZ)
|
|
|
|
{
|
2013-04-13 17:02:10 -04:00
|
|
|
if (m_World->IsChunkValid(a_ChunkX, a_ChunkZ))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
// Already loaded (can happen, since the queue is async)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cChunkCoords Coords(a_ChunkX, a_ChunkY, a_ChunkZ);
|
|
|
|
|
|
|
|
// First try the schema that is used for saving
|
|
|
|
if (m_SaveSchema->LoadChunk(Coords))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it didn't have the chunk, try all the other schemas:
|
|
|
|
for (cWSSchemaList::iterator itr = m_Schemas.begin(); itr != m_Schemas.end(); ++itr)
|
|
|
|
{
|
|
|
|
if (((*itr) != m_SaveSchema) && (*itr)->LoadChunk(Coords))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify the chunk owner that the chunk failed to load (sets cChunk::m_HasLoadFailed to true):
|
|
|
|
m_World->ChunkLoadFailed(a_ChunkX, a_ChunkY, a_ChunkZ);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|