1
0
cuberite-2a/src/WorldStorage/WorldStorage.cpp

305 lines
5.5 KiB
C++
Raw Normal View History

// 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"
#include "../World.h"
#include "../Generating/ChunkGenerator.h"
#include "../Entities/Entity.h"
#include "../BlockEntities/BlockEntity.h"
2015-07-31 10:49:10 -04:00
/** Example storage schema - forgets all chunks */
class cWSSForgetful :
public cWSSchema
{
public:
cWSSForgetful(cWorld * a_World) : cWSSchema(a_World) {}
2016-02-05 16:45:45 -05: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"; }
} ;
////////////////////////////////////////////////////////////////////////////////
// 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)
{
}
cWorldStorage::~cWorldStorage()
{
for (cWSSchemaList::iterator itr = m_Schemas.begin(); itr != m_Schemas.end(); ++itr)
{
delete *itr;
} // for itr - m_Schemas[]
}
void cWorldStorage::Initialize(cWorld & a_World, const AString & a_StorageSchemaName, int a_StorageCompressionFactor)
{
m_World = &a_World;
m_StorageSchemaName = a_StorageSchemaName;
2014-01-17 14:01:14 -05:00
InitSchemas(a_StorageCompressionFactor);
}
void cWorldStorage::Stop(void)
{
WaitForFinish();
}
void cWorldStorage::WaitForFinish(void)
{
LOGD("Waiting for the world storage to finish saving");
2016-02-05 16:45:45 -05:00
{
m_LoadQueue.Clear();
}
2016-02-05 16:45:45 -05:00
// Wait for the saving to finish:
WaitForSaveQueueEmpty();
2016-02-05 16:45:45 -05:00
// Wait for the thread to finish:
m_ShouldTerminate = true;
m_Event.Set(); // Wake up the thread if waiting
2020-04-13 12:38:06 -04:00
Super::Stop();
LOGD("World storage thread finished");
}
void cWorldStorage::WaitForLoadQueueEmpty(void)
{
m_LoadQueue.BlockTillEmpty();
}
2014-01-06 04:09:00 -05:00
void cWorldStorage::WaitForSaveQueueEmpty(void)
{
m_SaveQueue.BlockTillEmpty();
}
2014-01-06 04:09:00 -05:00
size_t cWorldStorage::GetLoadQueueLength(void)
{
return m_LoadQueue.Size();
}
size_t cWorldStorage::GetSaveQueueLength(void)
{
return m_SaveQueue.Size();
}
void cWorldStorage::QueueLoadChunk(int a_ChunkX, int a_ChunkZ)
{
ASSERT((a_ChunkX > -0x08000000) && (a_ChunkX < 0x08000000));
ASSERT((a_ChunkZ > -0x08000000) && (a_ChunkZ < 0x08000000));
ASSERT(m_World->IsChunkQueued(a_ChunkX, a_ChunkZ));
m_LoadQueue.EnqueueItem({ a_ChunkX, a_ChunkZ });
2014-01-06 04:09:00 -05:00
m_Event.Set();
}
void cWorldStorage::QueueSaveChunk(int a_ChunkX, int a_ChunkZ)
{
ASSERT(m_World->IsChunkValid(a_ChunkX, a_ChunkZ));
m_SaveQueue.EnqueueItem({ a_ChunkX, a_ChunkZ });
2014-01-06 04:09:00 -05:00
m_Event.Set();
}
2014-01-17 14:01:14 -05:00
void cWorldStorage::InitSchemas(int a_StorageCompressionFactor)
{
// 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));
m_Schemas.push_back(new cWSSForgetful(m_World));
// Add new schemas here
2016-02-05 16:45:45 -05: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
// 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:",
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:
bool Success;
do
{
if (m_ShouldTerminate)
{
return;
}
2016-02-05 16:45:45 -05:00
Success = LoadOneChunk();
Success |= SaveOneChunk();
} while (Success);
}
}
bool cWorldStorage::LoadOneChunk(void)
{
// Dequeue an item, bail out if there's none left:
cChunkCoords ToLoad(0, 0);
bool ShouldLoad = m_LoadQueue.TryDequeueItem(ToLoad);
if (!ShouldLoad)
{
return false;
}
// Load the chunk:
return LoadChunk(ToLoad.m_ChunkX, ToLoad.m_ChunkZ);
}
bool cWorldStorage::SaveOneChunk(void)
{
// Dequeue one chunk to save:
cChunkCoords ToSave(0, 0);
bool ShouldSave = m_SaveQueue.TryDequeueItem(ToSave);
if (!ShouldSave)
{
return false;
}
2016-02-05 16:45:45 -05:00
// Save the chunk, if it's valid:
bool Status = false;
if (m_World->IsChunkValid(ToSave.m_ChunkX, ToSave.m_ChunkZ))
{
m_World->MarkChunkSaving(ToSave.m_ChunkX, ToSave.m_ChunkZ);
if (m_SaveSchema->SaveChunk(cChunkCoords(ToSave.m_ChunkX, ToSave.m_ChunkZ)))
{
m_World->MarkChunkSaved(ToSave.m_ChunkX, ToSave.m_ChunkZ);
Status = true;
}
}
return true;
}
2014-08-28 05:36:35 -04:00
bool cWorldStorage::LoadChunk(int a_ChunkX, int a_ChunkZ)
{
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);
// First try the schema that is used for saving
if (m_SaveSchema->LoadChunk(Coords))
{
return true;
}
2016-02-05 16:45:45 -05: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
// 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
return false;
}