1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// ScoreboardSerializer.cpp
#include "Globals.h"
#include "ScoreboardSerializer.h"
#include "OSSupport/GZipFile.h"
#include "FastNBT.h"
#include "../Scoreboard.h"
cScoreboardSerializer::cScoreboardSerializer(const AString & a_WorldName, cScoreboard * a_ScoreBoard):
m_ScoreBoard(a_ScoreBoard)
{
auto DataPath = fmt::format(FMT_STRING("{}{}data"), a_WorldName, cFile::PathSeparator());
m_Path = DataPath + cFile::PathSeparator() + "scoreboard.dat";
cFile::CreateFolder(DataPath);
}
bool cScoreboardSerializer::Load()
{
try
{
const auto Data = GZipFile::ReadRestOfFile(m_Path);
const cParsedNBT NBT(Data.GetView());
if (!NBT.IsValid())
{
// NBT Parsing failed
return false;
}
return LoadScoreboardFromNBT(NBT);
}
catch (const std::exception & Oops)
{
LOGWARNING("Failed to load scoreboard from \"%s\": %s", m_Path.c_str(), Oops.what());
return false;
}
}
bool cScoreboardSerializer::Save(void)
{
cFastNBTWriter Writer;
SaveScoreboardToNBT(Writer);
Writer.Finish();
#ifndef NDEBUG
cParsedNBT TestParse(Writer.GetResult());
ASSERT(TestParse.IsValid());
#endif // !NDEBUG
GZipFile::Write(m_Path, Writer.GetResult());
return true;
}
void cScoreboardSerializer::SaveScoreboardToNBT(cFastNBTWriter & a_Writer)
{
a_Writer.BeginCompound("data");
a_Writer.BeginList("Objectives", TAG_Compound);
for (cScoreboard::cObjectiveMap::const_iterator it = m_ScoreBoard->m_Objectives.begin(); it != m_ScoreBoard->m_Objectives.end(); ++it)
{
const cObjective & Objective = it->second;
a_Writer.BeginCompound("");
a_Writer.AddString("CriteriaName", cObjective::TypeToString(Objective.GetType()));
a_Writer.AddString("DisplayName", Objective.GetDisplayName());
a_Writer.AddString("Name", it->first);
a_Writer.EndCompound();
}
a_Writer.EndList(); // Objectives
a_Writer.BeginList("PlayerScores", TAG_Compound);
for (cScoreboard::cObjectiveMap::const_iterator it = m_ScoreBoard->m_Objectives.begin(); it != m_ScoreBoard->m_Objectives.end(); ++it)
{
const cObjective & Objective = it->second;
for (cObjective::cScoreMap::const_iterator it2 = Objective.m_Scores.begin(); it2 != Objective.m_Scores.end(); ++it2)
{
a_Writer.BeginCompound("");
a_Writer.AddInt("Score", it2->second);
a_Writer.AddString("Name", it2->first);
a_Writer.AddString("Objective", it->first);
a_Writer.EndCompound();
}
}
a_Writer.EndList(); // PlayerScores
a_Writer.BeginList("Teams", TAG_Compound);
for (cScoreboard::cTeamMap::const_iterator it = m_ScoreBoard->m_Teams.begin(); it != m_ScoreBoard->m_Teams.end(); ++it)
{
const cTeam & Team = it->second;
a_Writer.BeginCompound("");
a_Writer.AddByte("AllowFriendlyFire", Team.AllowsFriendlyFire() ? 1 : 0);
a_Writer.AddByte("SeeFriendlyInvisibles", Team.CanSeeFriendlyInvisible() ? 1 : 0);
a_Writer.AddString("DisplayName", Team.GetDisplayName());
a_Writer.AddString("Name", it->first);
a_Writer.AddString("Prefix", Team.GetPrefix());
a_Writer.AddString("Suffix", Team.GetSuffix());
a_Writer.BeginList("Players", TAG_String);
for (cTeam::cPlayerNameSet::const_iterator it2 = Team.m_Players.begin(); it2 != Team.m_Players.end(); ++it2)
{
a_Writer.AddString("", *it2);
}
a_Writer.EndList();
a_Writer.EndCompound();
}
a_Writer.EndList(); // Teams
a_Writer.BeginCompound("DisplaySlots");
cObjective * Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::dsList);
a_Writer.AddString("slot_0", (Objective == nullptr) ? "" : Objective->GetName());
Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::dsSidebar);
a_Writer.AddString("slot_1", (Objective == nullptr) ? "" : Objective->GetName());
Objective = m_ScoreBoard->GetObjectiveIn(cScoreboard::dsName);
a_Writer.AddString("slot_2", (Objective == nullptr) ? "" : Objective->GetName());
a_Writer.EndCompound(); // DisplaySlots
a_Writer.EndCompound(); // Data
}
bool cScoreboardSerializer::LoadScoreboardFromNBT(const cParsedNBT & a_NBT)
{
int Data = a_NBT.FindChildByName(0, "data");
if (Data < 0)
{
return false;
}
int Objectives = a_NBT.FindChildByName(Data, "Objectives");
if (Objectives < 0)
{
return false;
}
for (int Child = a_NBT.GetFirstChild(Objectives); Child >= 0; Child = a_NBT.GetNextSibling(Child))
{
AString CriteriaName, DisplayName, Name;
int CurrLine = a_NBT.FindChildByName(Child, "CriteriaName");
if (CurrLine >= 0)
{
CriteriaName = a_NBT.GetString(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "DisplayName");
if (CurrLine >= 0)
{
DisplayName = a_NBT.GetString(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "Name");
if (CurrLine >= 0)
{
Name = a_NBT.GetString(CurrLine);
}
cObjective::eType Type = cObjective::StringToType(CriteriaName);
m_ScoreBoard->RegisterObjective(Name, DisplayName, Type);
}
int PlayerScores = a_NBT.FindChildByName(Data, "PlayerScores");
if (PlayerScores < 0)
{
return false;
}
for (int Child = a_NBT.GetFirstChild(PlayerScores); Child >= 0; Child = a_NBT.GetNextSibling(Child))
{
AString Name, ObjectiveName;
cObjective::Score Score = 0;
int CurrLine = a_NBT.FindChildByName(Child, "Score");
if (CurrLine >= 0)
{
Score = a_NBT.GetInt(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "Name");
if (CurrLine >= 0)
{
Name = a_NBT.GetString(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "Objective");
if (CurrLine >= 0)
{
ObjectiveName = a_NBT.GetString(CurrLine);
}
cObjective * Objective = m_ScoreBoard->GetObjective(ObjectiveName);
if (Objective)
{
Objective->SetScore(Name, Score);
}
}
int Teams = a_NBT.FindChildByName(Data, "Teams");
if (Teams < 0)
{
return false;
}
for (int Child = a_NBT.GetFirstChild(Teams); Child >= 0; Child = a_NBT.GetNextSibling(Child))
{
AString Name, DisplayName, Prefix, Suffix;
bool AllowsFriendlyFire = true, CanSeeFriendlyInvisible = false;
int CurrLine = a_NBT.FindChildByName(Child, "Name");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String))
{
Name = a_NBT.GetString(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "DisplayName");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String))
{
DisplayName = a_NBT.GetString(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "Prefix");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String))
{
Prefix = a_NBT.GetString(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "Suffix");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_String))
{
Suffix = a_NBT.GetString(CurrLine);
}
CurrLine = a_NBT.FindChildByName(Child, "AllowFriendlyFire");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Int))
{
AllowsFriendlyFire = (a_NBT.GetInt(CurrLine) != 0);
}
CurrLine = a_NBT.FindChildByName(Child, "SeeFriendlyInvisibles");
if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Int))
{
CanSeeFriendlyInvisible = (a_NBT.GetInt(CurrLine) != 0);
}
cTeam * Team = m_ScoreBoard->RegisterTeam(Name, DisplayName, Prefix, Suffix);
Team->SetFriendlyFire(AllowsFriendlyFire);
Team->SetCanSeeFriendlyInvisible(CanSeeFriendlyInvisible);
int Players = a_NBT.FindChildByName(Child, "Players");
if (Players < 0)
{
continue;
}
for (int ChildB = a_NBT.GetFirstChild(Players); ChildB >= 0; ChildB = a_NBT.GetNextSibling(ChildB))
{
Team->AddPlayer(a_NBT.GetString(ChildB));
}
}
int DisplaySlots = a_NBT.FindChildByName(Data, "DisplaySlots");
if (DisplaySlots < 0)
{
return false;
}
int CurrLine = a_NBT.FindChildByName(DisplaySlots, "slot_0");
if (CurrLine >= 0)
{
AString Name = a_NBT.GetString(CurrLine);
m_ScoreBoard->SetDisplay(Name, cScoreboard::dsList);
}
CurrLine = a_NBT.FindChildByName(DisplaySlots, "slot_1");
if (CurrLine >= 0)
{
AString Name = a_NBT.GetString(CurrLine);
m_ScoreBoard->SetDisplay(Name, cScoreboard::dsSidebar);
}
CurrLine = a_NBT.FindChildByName(DisplaySlots, "slot_2");
if (CurrLine >= 0)
{
AString Name = a_NBT.GetString(CurrLine);
m_ScoreBoard->SetDisplay(Name, cScoreboard::dsName);
}
return true;
}
|