1
0

Check to see if header write out is required

This commit is contained in:
planetx 2014-12-08 11:23:19 -08:00
parent 1674f505f8
commit ce3adf89fa

View File

@ -2923,6 +2923,8 @@ cWSSAnvil::cMCAFile::cMCAFile(const AString & a_FileName, int a_RegionX, int a_R
bool cWSSAnvil::cMCAFile::OpenFile(bool a_IsForReading)
{
bool writeOutNeeded = false;
if (m_File.IsOpen())
{
// Already open
@ -2948,28 +2950,25 @@ bool cWSSAnvil::cMCAFile::OpenFile(bool a_IsForReading)
if (m_File.Read(m_Header, sizeof(m_Header)) != sizeof(m_Header))
{
// Cannot read the header - perhaps the file has just been created?
// Try writing a nullptr header (both chunk offsets and timestamps):
// Try writing a nullptr header for chunk offsets:
memset(m_Header, 0, sizeof(m_Header));
if (
(m_File.Write(m_Header, sizeof(m_Header)) != sizeof(m_Header)) || // Null header - chunk offsets
(m_File.Write(m_Header, sizeof(m_Header)) != sizeof(m_Header)) // Bogus data for the chunk timestamps
)
{
LOGWARNING("Cannot process MCA header in file \"%s\", chunks in that file will be lost", m_FileName.c_str());
m_File.Close();
return false;
}
writeOutNeeded = true;
}
// Load the TimeStamps:
if (m_File.Read(m_TimeStamps, sizeof(m_TimeStamps)) != sizeof(m_TimeStamps))
{
// Cannot read the time stamps - perhaps the file has just been created?
// Try writing a nullptr header (both chunk offsets and timestamps):
// Try writing a nullptr header for timestamps:
memset(m_TimeStamps, 0, sizeof(m_TimeStamps));
writeOutNeeded = true;
}
if (writeOutNeeded)
{
if (
(m_File.Write(m_Header, sizeof(m_Header)) != sizeof(m_Header)) || // Real header - chunk offsets
(m_File.Write(m_TimeStamps, sizeof(m_TimeStamps)) != sizeof(m_TimeStamps)) // Bogus data for the chunk timestamps
(m_File.Write(m_Header, sizeof(m_Header)) != sizeof(m_Header)) || // Write chunk offsets
(m_File.Write(m_TimeStamps, sizeof(m_TimeStamps)) != sizeof(m_TimeStamps)) // Write chunk timestamps
)
{
LOGWARNING("Cannot process MCA header in file \"%s\", chunks in that file will be lost", m_FileName.c_str());
@ -2977,7 +2976,6 @@ bool cWSSAnvil::cMCAFile::OpenFile(bool a_IsForReading)
return false;
}
}
return true;
}
@ -3106,7 +3104,7 @@ bool cWSSAnvil::cMCAFile::SetChunkData(const cChunkCoords & a_Chunk, const AStri
m_Header[LocalX + 32 * LocalZ] = htonl((ChunkSector << 8) | ChunkSize);
// Set the modification time
m_TimeStamps[LocalX + 32 * LocalZ] = time(nullptr);
m_TimeStamps[LocalX + 32 * LocalZ] = htonl(time(nullptr));
if (m_File.Seek(0) < 0)
{