1
0
Fork 0

Moved AString reading hack to cFile.

This commit is contained in:
Mattes D 2015-06-24 16:38:40 +02:00
parent 735d590abf
commit 7379848ae5
3 changed files with 49 additions and 24 deletions

View File

@ -147,7 +147,7 @@ bool cFile::IsEOF(void) const
int cFile::Read (void * iBuffer, size_t iNumBytes)
int cFile::Read (void * a_Buffer, size_t a_NumBytes)
{
ASSERT(IsOpen());
@ -156,14 +156,35 @@ int cFile::Read (void * iBuffer, size_t iNumBytes)
return -1;
}
return static_cast<int>(fread(iBuffer, 1, static_cast<size_t>(iNumBytes), m_File)); // fread() returns the portion of Count parameter actually read, so we need to send iNumBytes as Count
return static_cast<int>(fread(a_Buffer, 1, a_NumBytes, m_File)); // fread() returns the portion of Count parameter actually read, so we need to send a_a_NumBytes as Count
}
int cFile::Write(const void * iBuffer, size_t iNumBytes)
AString cFile::Read(size_t a_NumBytes)
{
ASSERT(IsOpen());
if (!IsOpen())
{
return AString();
}
// HACK: This depends on the knowledge that AString::data() returns the internal buffer, rather than a copy of it.
AString res;
res.resize(a_NumBytes);
auto newSize = fread(const_cast<char *>(res.data()), 1, a_NumBytes, m_File);
res.resize(newSize);
return res;
}
int cFile::Write(const void * a_Buffer, size_t a_NumBytes)
{
ASSERT(IsOpen());
@ -172,7 +193,7 @@ int cFile::Write(const void * iBuffer, size_t iNumBytes)
return -1;
}
int res = static_cast<int>(fwrite(iBuffer, 1, static_cast<size_t>(iNumBytes), m_File)); // fwrite() returns the portion of Count parameter actually written, so we need to send iNumBytes as Count
int res = static_cast<int>(fwrite(a_Buffer, 1, a_NumBytes, m_File)); // fwrite() returns the portion of Count parameter actually written, so we need to send a_NumBytes as Count
return res;
}

View File

@ -80,11 +80,14 @@ public:
bool IsOpen(void) const;
bool IsEOF(void) const;
/** Reads up to iNumBytes bytes into iBuffer, returns the number of bytes actually read, or -1 on failure; asserts if not open */
int Read (void * iBuffer, size_t iNumBytes);
/** Reads up to a_NumBytes bytes into a_Buffer, returns the number of bytes actually read, or -1 on failure; asserts if not open */
int Read(void * a_Buffer, size_t a_NumBytes);
/** Writes up to iNumBytes bytes from iBuffer, returns the number of bytes actually written, or -1 on failure; asserts if not open */
int Write(const void * iBuffer, size_t iNumBytes);
/** Reads up to a_NumBytes bytes, returns the bytes actually read, or empty string on failure; asserts if not open */
AString Read(size_t a_NumBytes);
/** Writes up to a_NumBytes bytes from a_Buffer, returns the number of bytes actually written, or -1 on failure; asserts if not open */
int Write(const void * a_Buffer, size_t a_NumBytes);
/** Seeks to iPosition bytes from file start, returns old position or -1 for failure; asserts if not open */
long Seek (int iPosition);

View File

@ -109,11 +109,11 @@ cWSSAnvil::cWSSAnvil(cWorld * a_World, int a_CompressionFactor) :
Writer.AddByte("thundering", a_World->IsWeatherStorm() ? 1 : 0);
Writer.AddInt("GameType", static_cast<int>(a_World->GetGameMode()));
Writer.AddInt("generatorVersion", 1);
Writer.AddInt("SpawnX", static_cast<int>(a_World->GetSpawnX()));
Writer.AddInt("SpawnY", static_cast<int>(a_World->GetSpawnY()));
Writer.AddInt("SpawnZ", static_cast<int>(a_World->GetSpawnZ()));
Writer.AddInt("SpawnX", FloorC(a_World->GetSpawnX()));
Writer.AddInt("SpawnY", FloorC(a_World->GetSpawnY()));
Writer.AddInt("SpawnZ", FloorC(a_World->GetSpawnZ()));
Writer.AddInt("version", 19133);
Writer.AddLong("DayTime", static_cast<Int64>(a_World->GetTimeOfDay()));
Writer.AddLong("DayTime", a_World->GetTimeOfDay());
Writer.AddLong("Time", a_World->GetWorldAge());
Writer.AddLong("SizeOnDisk", 0);
Writer.AddString("generatorName", "default");
@ -122,11 +122,6 @@ cWSSAnvil::cWSSAnvil(cWorld * a_World, int a_CompressionFactor) :
Writer.EndCompound();
Writer.Finish();
#ifdef _DEBUG
cParsedNBT TestParse(Writer.GetResult().data(), Writer.GetResult().size());
ASSERT(TestParse.IsValid());
#endif // _DEBUG
gzFile gz = gzopen((FILE_IO_PREFIX + fnam).c_str(), "wb");
if (gz != nullptr)
{
@ -1888,7 +1883,7 @@ void cWSSAnvil::LoadArrowFromNBT(cEntityList & a_Entities, const cParsedNBT & a_
case TAG_Short:
{
// Vanilla uses this
Arrow->SetBlockHit(Vector3i(static_cast<int>(a_NBT.GetShort(InBlockXIdx)), static_cast<int>(a_NBT.GetShort(InBlockYIdx)), static_cast<int>(a_NBT.GetShort(InBlockZIdx))));
Arrow->SetBlockHit(Vector3i(a_NBT.GetShort(InBlockXIdx), a_NBT.GetShort(InBlockYIdx), a_NBT.GetShort(InBlockZIdx)));
break;
}
default:
@ -3138,15 +3133,22 @@ bool cWSSAnvil::cMCAFile::GetChunkData(const cChunkCoords & a_Chunk, AString & a
return false;
}
m_File.Seek(static_cast<int>(ChunkOffset) * 4096);
m_File.Seek(static_cast<int>(ChunkOffset * 4096));
int ChunkSize = 0;
UInt32 ChunkSize = 0;
if (m_File.Read(&ChunkSize, 4) != 4)
{
LOAD_FAILED(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ);
return false;
}
ChunkSize = ntohl(static_cast<u_long>(ChunkSize));
ChunkSize = ntohl(ChunkSize);
if (ChunkSize < 1)
{
// Chunk size too small
LOAD_FAILED(a_Chunk.m_ChunkX, a_Chunk.m_ChunkZ);
return false;
}
char CompressionType = 0;
if (m_File.Read(&CompressionType, 1) != 1)
{
@ -3161,9 +3163,8 @@ bool cWSSAnvil::cMCAFile::GetChunkData(const cChunkCoords & a_Chunk, AString & a
}
ChunkSize--;
// HACK: This depends on the internal knowledge that AString's data() function returns the internal buffer directly
a_Data.assign(static_cast<size_t>(ChunkSize), '\0');
if (static_cast<size_t>(m_File.Read(static_cast<void *>(const_cast<char*>(a_Data.data())), static_cast<size_t>(ChunkSize))) == static_cast<size_t>(ChunkSize))
a_Data = m_File.Read(ChunkSize);
if (a_Data.size() == ChunkSize)
{
return true;
}