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 "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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-07-31 10:49:10 -04:00
|
|
|
/** Example storage schema - forgets all chunks */
|
2012-06-14 09:06:06 -04:00
|
|
|
class cWSSForgetful :
|
|
|
|
public cWSSchema
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cWSSForgetful(cWorld * a_World) : cWSSchema(a_World) {}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
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"; }
|
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2012-06-14 09:06:06 -04:00
|
|
|
// cWorldStorage:
|
|
|
|
|
|
|
|
cWorldStorage::cWorldStorage(void) :
|
2020-04-13 12:38:06 -04:00
|
|
|
Super("cWorldStorage"),
|
2014-10-20 16:55:07 -04:00
|
|
|
m_World(nullptr),
|
|
|
|
m_SaveSchema(nullptr)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cWorldStorage::~cWorldStorage()
|
|
|
|
{
|
|
|
|
for (cWSSchemaList::iterator itr = m_Schemas.begin(); itr != m_Schemas.end(); ++itr)
|
|
|
|
{
|
|
|
|
delete *itr;
|
|
|
|
} // for itr - m_Schemas[]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-09-22 12:16:47 -04:00
|
|
|
void cWorldStorage::Initialize(cWorld & a_World, const AString & a_StorageSchemaName, int a_StorageCompressionFactor)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2017-09-22 12:16:47 -04:00
|
|
|
m_World = &a_World;
|
2012-06-14 09:06:06 -04:00
|
|
|
m_StorageSchemaName = a_StorageSchemaName;
|
2014-01-17 14:01:14 -05:00
|
|
|
InitSchemas(a_StorageCompressionFactor);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2015-06-14 06:23:14 -04:00
|
|
|
LOGD("Waiting for the world storage to finish saving");
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-12-31 10:48:57 -05:00
|
|
|
m_LoadQueue.Clear();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Wait for the saving to finish:
|
2014-01-02 07:32:55 -05:00
|
|
|
WaitForSaveQueueEmpty();
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Wait for the thread to finish:
|
|
|
|
m_ShouldTerminate = true;
|
2014-07-17 16:15:34 -04:00
|
|
|
m_Event.Set(); // Wake up the thread if waiting
|
2020-04-13 12:38:06 -04:00
|
|
|
Super::Stop();
|
2015-06-14 06:23:14 -04:00
|
|
|
LOGD("World storage thread finished");
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-28 16:36:46 -04:00
|
|
|
void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2016-12-15 08:57:48 -05:00
|
|
|
ASSERT((a_ChunkX > -0x08000000) && (a_ChunkX < 0x08000000));
|
|
|
|
ASSERT((a_ChunkZ > -0x08000000) && (a_ChunkZ < 0x08000000));
|
2014-09-05 16:16:48 -04:00
|
|
|
ASSERT(m_World->IsChunkQueued(a_ChunkX, a_ChunkZ));
|
|
|
|
|
2020-08-28 16:36:46 -04:00
|
|
|
m_LoadQueue.EnqueueItem({ a_ChunkX, a_ChunkZ });
|
2014-01-06 04:09:00 -05:00
|
|
|
m_Event.Set();
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-28 16:36:46 -04:00
|
|
|
void cWorldStorage::QueueSaveChunk(int a_ChunkX, int a_ChunkZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-09-05 16:16:48 -04:00
|
|
|
ASSERT(m_World->IsChunkValid(a_ChunkX, a_ChunkZ));
|
|
|
|
|
2020-08-28 16:36:46 -04:00
|
|
|
m_SaveQueue.EnqueueItem({ a_ChunkX, a_ChunkZ });
|
2014-01-06 04:09:00 -05:00
|
|
|
m_Event.Set();
|
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-07-19 08:53:41 -04:00
|
|
|
m_Schemas.push_back(new cWSSAnvil (m_World, a_StorageCompressionFactor));
|
2012-06-14 09:06:06 -04:00
|
|
|
m_Schemas.push_back(new cWSSForgetful(m_World));
|
|
|
|
// Add new schemas here
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
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[]
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Unknown schema selected, let the admin know:
|
2014-07-17 16:50:58 -04:00
|
|
|
LOGWARNING("Unknown storage schema name \"%s\". Using default (\"%s\"). Available schemas:",
|
2012-06-14 09:06:06 -04:00
|
|
|
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;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
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)
|
|
|
|
{
|
2014-12-10 16:35:16 -05:00
|
|
|
// Dequeue an item, bail out if there's none left:
|
2020-08-28 16:36:46 -04:00
|
|
|
cChunkCoords ToLoad(0, 0);
|
2013-12-31 10:48:57 -05:00
|
|
|
bool ShouldLoad = m_LoadQueue.TryDequeueItem(ToLoad);
|
2014-12-10 16:35:16 -05:00
|
|
|
if (!ShouldLoad)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-05 16:16:48 -04:00
|
|
|
|
2014-12-10 16:35:16 -05:00
|
|
|
// Load the chunk:
|
2020-08-28 16:36:46 -04:00
|
|
|
return LoadChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cWorldStorage::SaveOneChunk(void)
|
|
|
|
{
|
2014-12-10 16:35:16 -05:00
|
|
|
// Dequeue one chunk to save:
|
2020-08-28 16:36:46 -04:00
|
|
|
cChunkCoords ToSave(0, 0);
|
2013-12-31 10:48:57 -05:00
|
|
|
bool ShouldSave = m_SaveQueue.TryDequeueItem(ToSave);
|
2014-12-10 16:35:16 -05:00
|
|
|
if (!ShouldSave)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-12-10 16:35:16 -05:00
|
|
|
// Save the chunk, if it's valid:
|
2015-10-04 08:06:37 -04:00
|
|
|
bool Status = false;
|
2014-12-10 16:35:16 -05:00
|
|
|
if (m_World->IsChunkValid(ToSave.m_ChunkX, ToSave.m_ChunkZ))
|
2014-07-04 05:50:50 -04:00
|
|
|
{
|
|
|
|
m_World->MarkChunkSaving(ToSave.m_ChunkX, ToSave.m_ChunkZ);
|
2014-12-10 16:35:16 -05:00
|
|
|
if (m_SaveSchema->SaveChunk(cChunkCoords(ToSave.m_ChunkX, ToSave.m_ChunkZ)))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-07-04 05:50:50 -04:00
|
|
|
m_World->MarkChunkSaved(ToSave.m_ChunkX, ToSave.m_ChunkZ);
|
2015-10-04 08:06:37 -04:00
|
|
|
Status = true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
2014-12-10 16:35:16 -05:00
|
|
|
|
|
|
|
return true;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-28 05:36:35 -04:00
|
|
|
bool cWorldStorage::LoadChunk(int a_ChunkX, int a_ChunkZ)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2014-09-05 16:16:48 -04:00
|
|
|
ASSERT(m_World->IsChunkQueued(a_ChunkX, a_ChunkZ));
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2014-08-28 05:36:35 -04:00
|
|
|
cChunkCoords Coords(a_ChunkX, a_ChunkZ);
|
2012-06-14 09:06:06 -04:00
|
|
|
|
|
|
|
// First try the schema that is used for saving
|
|
|
|
if (m_SaveSchema->LoadChunk(Coords))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
// Notify the chunk owner that the chunk failed to load (sets cChunk::m_HasLoadFailed to true):
|
2014-08-28 05:36:35 -04:00
|
|
|
m_World->ChunkLoadFailed(a_ChunkX, a_ChunkZ);
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|