From bafa0347a349af663a6181173a2c81a73d6b29de Mon Sep 17 00:00:00 2001 From: andrew Date: Thu, 23 Jan 2014 16:27:23 +0200 Subject: [PATCH] Fixed scoreboard serialization --- src/WorldStorage/ScoreboardSerializer.cpp | 61 ++++++++++------------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/src/WorldStorage/ScoreboardSerializer.cpp b/src/WorldStorage/ScoreboardSerializer.cpp index dabc5e2e1..d41456d15 100644 --- a/src/WorldStorage/ScoreboardSerializer.cpp +++ b/src/WorldStorage/ScoreboardSerializer.cpp @@ -13,11 +13,6 @@ -#define SCOREBOARD_INFLATE_MAX 16 KiB - - - - cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScoreboard* a_ScoreBoard) : m_ScoreBoard(a_ScoreBoard) @@ -37,37 +32,25 @@ cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScore bool cScoreboardSerializer::Load(void) { cFile File; - - if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmReadWrite)) + if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmRead)) { return false; } AString Data; - File.ReadRestOfFile(Data); - File.Close(); - char Uncompressed[SCOREBOARD_INFLATE_MAX]; - z_stream strm; - strm.zalloc = (alloc_func)NULL; - strm.zfree = (free_func)NULL; - strm.opaque = NULL; - inflateInit(&strm); - strm.next_out = (Bytef *)Uncompressed; - strm.avail_out = sizeof(Uncompressed); - strm.next_in = (Bytef *)Data.data(); - strm.avail_in = Data.size(); - int res = inflate(&strm, Z_FINISH); - inflateEnd(&strm); - if (res != Z_STREAM_END) + AString Uncompressed; + int res = UncompressStringGZIP(Data.data(), Data.size(), Uncompressed); + + if (res != Z_OK) { return false; } // Parse the NBT data: - cParsedNBT NBT(Uncompressed, strm.total_out); + cParsedNBT NBT(Uncompressed.data(), Uncompressed.size()); if (!NBT.IsValid()) { // NBT Parsing failed @@ -85,11 +68,8 @@ bool cScoreboardSerializer::Save(void) { cFastNBTWriter Writer; - Writer.BeginCompound(""); - m_ScoreBoard->RegisterObjective("test","test",cObjective::E_TYPE_DUMMY)->AddScore("dot", 2); SaveScoreboardToNBT(Writer); - Writer.EndCompound(); Writer.Finish(); #ifdef _DEBUG @@ -97,12 +77,22 @@ bool cScoreboardSerializer::Save(void) ASSERT(TestParse.IsValid()); #endif // _DEBUG - gzFile gz = gzopen((FILE_IO_PREFIX + m_Path).c_str(), "wb"); - if (gz != NULL) + cFile File; + if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmWrite)) { - gzwrite(gz, Writer.GetResult().data(), Writer.GetResult().size()); + return false; } - gzclose(gz); + + AString Compressed; + int res = CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); + + if (res != Z_OK) + { + return false; + } + + File.Write(Compressed.data(), Compressed.size()); + File.Close(); return true; } @@ -130,7 +120,7 @@ void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer) a_Writer.EndCompound(); } - a_Writer.EndList(); + a_Writer.EndList(); // Objectives a_Writer.BeginList("PlayerScores", TAG_Compound); @@ -151,7 +141,7 @@ void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer) } } - a_Writer.EndList(); + a_Writer.EndList(); // PlayerScores a_Writer.BeginList("Teams", TAG_Compound); @@ -182,8 +172,9 @@ void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer) a_Writer.EndCompound(); } - a_Writer.EndList(); - a_Writer.EndCompound(); + a_Writer.EndList(); // Teams + + a_Writer.EndCompound(); // Data a_Writer.BeginCompound("DisplaySlots"); @@ -196,7 +187,7 @@ void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer) Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::E_DISPLAY_SLOT_NAME); a_Writer.AddString("slot_2", (Objective == NULL) ? "" : Objective->GetName()); - a_Writer.EndCompound(); + a_Writer.EndCompound(); // DisplaySlots }