summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorandrew <xdotftw@gmail.com>2014-01-23 15:27:23 +0100
committerandrew <xdotftw@gmail.com>2014-01-23 15:27:23 +0100
commitbafa0347a349af663a6181173a2c81a73d6b29de (patch)
tree8d5b83ecf75cb7f1220fe3721300eb3221bd8577
parentImproved code safety for the Compact world storage. (diff)
downloadcuberite-bafa0347a349af663a6181173a2c81a73d6b29de.tar
cuberite-bafa0347a349af663a6181173a2c81a73d6b29de.tar.gz
cuberite-bafa0347a349af663a6181173a2c81a73d6b29de.tar.bz2
cuberite-bafa0347a349af663a6181173a2c81a73d6b29de.tar.lz
cuberite-bafa0347a349af663a6181173a2c81a73d6b29de.tar.xz
cuberite-bafa0347a349af663a6181173a2c81a73d6b29de.tar.zst
cuberite-bafa0347a349af663a6181173a2c81a73d6b29de.zip
-rw-r--r--src/WorldStorage/ScoreboardSerializer.cpp61
1 files 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))
+ {
+ return false;
+ }
+
+ AString Compressed;
+ int res = CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed);
+
+ if (res != Z_OK)
{
- gzwrite(gz, Writer.GetResult().data(), Writer.GetResult().size());
+ return false;
}
- gzclose(gz);
+
+ 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
}