1
0

Fixed scoreboard serialization

This commit is contained in:
andrew 2014-01-23 16:27:23 +02:00
parent 9ae31d913c
commit bafa0347a3

View File

@ -13,11 +13,6 @@
#define SCOREBOARD_INFLATE_MAX 16 KiB
cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScoreboard* a_ScoreBoard) cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScoreboard* a_ScoreBoard)
: m_ScoreBoard(a_ScoreBoard) : m_ScoreBoard(a_ScoreBoard)
@ -37,37 +32,25 @@ cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScore
bool cScoreboardSerializer::Load(void) bool cScoreboardSerializer::Load(void)
{ {
cFile File; cFile File;
if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmRead))
if (!File.Open(FILE_IO_PREFIX + m_Path, cFile::fmReadWrite))
{ {
return false; return false;
} }
AString Data; AString Data;
File.ReadRestOfFile(Data); File.ReadRestOfFile(Data);
File.Close(); File.Close();
char Uncompressed[SCOREBOARD_INFLATE_MAX]; AString Uncompressed;
z_stream strm; int res = UncompressStringGZIP(Data.data(), Data.size(), Uncompressed);
strm.zalloc = (alloc_func)NULL;
strm.zfree = (free_func)NULL; if (res != Z_OK)
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)
{ {
return false; return false;
} }
// Parse the NBT data: // Parse the NBT data:
cParsedNBT NBT(Uncompressed, strm.total_out); cParsedNBT NBT(Uncompressed.data(), Uncompressed.size());
if (!NBT.IsValid()) if (!NBT.IsValid())
{ {
// NBT Parsing failed // NBT Parsing failed
@ -85,11 +68,8 @@ bool cScoreboardSerializer::Save(void)
{ {
cFastNBTWriter Writer; cFastNBTWriter Writer;
Writer.BeginCompound("");
m_ScoreBoard->RegisterObjective("test","test",cObjective::E_TYPE_DUMMY)->AddScore("dot", 2);
SaveScoreboardToNBT(Writer); SaveScoreboardToNBT(Writer);
Writer.EndCompound();
Writer.Finish(); Writer.Finish();
#ifdef _DEBUG #ifdef _DEBUG
@ -97,12 +77,22 @@ bool cScoreboardSerializer::Save(void)
ASSERT(TestParse.IsValid()); ASSERT(TestParse.IsValid());
#endif // _DEBUG #endif // _DEBUG
gzFile gz = gzopen((FILE_IO_PREFIX + m_Path).c_str(), "wb"); cFile File;
if (gz != NULL) 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; return true;
} }
@ -130,7 +120,7 @@ void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer)
a_Writer.EndCompound(); a_Writer.EndCompound();
} }
a_Writer.EndList(); a_Writer.EndList(); // Objectives
a_Writer.BeginList("PlayerScores", TAG_Compound); 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); a_Writer.BeginList("Teams", TAG_Compound);
@ -182,8 +172,9 @@ void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer)
a_Writer.EndCompound(); a_Writer.EndCompound();
} }
a_Writer.EndList(); a_Writer.EndList(); // Teams
a_Writer.EndCompound();
a_Writer.EndCompound(); // Data
a_Writer.BeginCompound("DisplaySlots"); a_Writer.BeginCompound("DisplaySlots");
@ -196,7 +187,7 @@ void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer)
Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::E_DISPLAY_SLOT_NAME); Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::E_DISPLAY_SLOT_NAME);
a_Writer.AddString("slot_2", (Objective == NULL) ? "" : Objective->GetName()); a_Writer.AddString("slot_2", (Objective == NULL) ? "" : Objective->GetName());
a_Writer.EndCompound(); a_Writer.EndCompound(); // DisplaySlots
} }