Skip to content

Commit

Permalink
NBTChunkSerializer: Cleaned up interface.
Browse files Browse the repository at this point in the history
Removed dependency on cChunkDataCallback.
Moved all the serializing code into a worker class.
Changed the serialization into a single-call action.
  • Loading branch information
madmaxoft committed Sep 24, 2019
1 parent d1c9574 commit 66e73a2
Show file tree
Hide file tree
Showing 9 changed files with 909 additions and 960 deletions.
10 changes: 8 additions & 2 deletions src/ChunkMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,12 +444,18 @@ void cChunkMap::ChunkLighted(



bool cChunkMap::GetChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataCallback & a_Callback)
bool cChunkMap::GetChunkData(cChunkCoords a_Coords, cChunkDataCallback & a_Callback)
{
if (!a_Callback.Coords(a_Coords.m_ChunkX, a_Coords.m_ChunkZ))
{
// The callback doesn't want the data
return false;
}
cCSLock Lock(m_CSChunks);
cChunkPtr Chunk = GetChunkNoGen(a_ChunkX, a_ChunkZ);
cChunkPtr Chunk = GetChunkNoGen(a_Coords);
if ((Chunk == nullptr) || !Chunk->IsValid())
{
// The chunk is not present
return false;
}
Chunk->GetAllData(a_Callback);
Expand Down
8 changes: 5 additions & 3 deletions src/ChunkMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ class cChunkMap
const cChunkDef::BlockNibbles & a_SkyLight
);

bool GetChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataCallback & a_Callback);
/** Calls the callback with the chunk's data, if available (with ChunkCS locked).
Returns true if the chunk was reported successfully, false if not (chunk not present or callback failed). */
bool GetChunkData(cChunkCoords a_Coords, cChunkDataCallback & a_Callback);

/** Copies the chunk's blocktypes into a_Blocks; returns true if successful */
bool GetChunkBlockTypes (int a_ChunkX, int a_ChunkZ, BLOCKTYPE * a_Blocks);
Expand Down Expand Up @@ -439,7 +441,7 @@ class cChunkMap

typedef std::list<cChunkStay *> cChunkStays;

cCriticalSection m_CSChunks;
mutable cCriticalSection m_CSChunks;

/** A map of chunk coordinates to chunk pointers
Uses a map (as opposed to unordered_map) because sorted maps are apparently faster */
Expand Down Expand Up @@ -468,7 +470,7 @@ class cChunkMap
// Deprecated in favor of the vector version
cChunkPtr GetChunkNoGen(int a_ChunkX, int a_ChunkZ)
{
return GetChunkNoGen(cChunkCoords(a_ChunkX, a_ChunkZ));
return GetChunkNoGen({a_ChunkX, a_ChunkZ});
}

/** Constructs a chunk, returning it. Doesn't load, doesn't generate */
Expand Down
2 changes: 1 addition & 1 deletion src/ChunkSender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void cChunkSender::SendChunk(int a_ChunkX, int a_ChunkZ, std::unordered_set<cCli
}

// Query and prepare chunk data:
if (!m_World.GetChunkData(a_ChunkX, a_ChunkZ, *this))
if (!m_World.GetChunkData({a_ChunkX, a_ChunkZ}, *this))
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/LightingThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ void cLightingThread::ReadChunks(int a_ChunkX, int a_ChunkZ)
for (int x = 0; x < 3; x++)
{
Reader.m_ReadingChunkX = x;
VERIFY(m_World.GetChunkData(a_ChunkX + x - 1, a_ChunkZ + z - 1, Reader));
VERIFY(m_World.GetChunkData({a_ChunkX + x - 1, a_ChunkZ + z - 1}, Reader));
} // for z
} // for x

Expand Down
4 changes: 2 additions & 2 deletions src/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2636,9 +2636,9 @@ void cWorld::ChunkLighted(



bool cWorld::GetChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataCallback & a_Callback)
bool cWorld::GetChunkData(cChunkCoords a_Coords, cChunkDataCallback & a_Callback) const
{
return m_ChunkMap->GetChunkData(a_ChunkX, a_ChunkZ, a_Callback);
return m_ChunkMap->GetChunkData(a_Coords, a_Callback);
}


Expand Down
4 changes: 3 additions & 1 deletion src/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ class cWorld // tolua_export
const cChunkDef::BlockNibbles & a_SkyLight
);

bool GetChunkData (int a_ChunkX, int a_ChunkZ, cChunkDataCallback & a_Callback);
/** Calls the callback with the chunk's data, if available (with ChunkCS locked).
Returns true if the chunk was reported successfully, false if not (chunk not present or callback failed). */
bool GetChunkData(cChunkCoords a_Coords, cChunkDataCallback & a_Callback) const;

/** Gets the chunk's blocks, only the block types */
bool GetChunkBlockTypes(int a_ChunkX, int a_ChunkZ, BLOCKTYPE * a_BlockTypes);
Expand Down
Loading

0 comments on commit 66e73a2

Please sign in to comment.