summaryrefslogtreecommitdiffstats
path: root/src/WorldStorage/StatSerializer.cpp
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@outlook.com>2020-08-21 00:27:08 +0200
committerTiger Wang <ziwei.tiger@outlook.com>2020-08-21 01:53:10 +0200
commite23dcffaff5ce3b3da1e44fba21c03e66f48c1ce (patch)
treea53e6c1eca0d223ebc016a541e9ec1d70273faee /src/WorldStorage/StatSerializer.cpp
parentConsolidate various Chunk serialisers under one roof (diff)
downloadcuberite-e23dcffaff5ce3b3da1e44fba21c03e66f48c1ce.tar
cuberite-e23dcffaff5ce3b3da1e44fba21c03e66f48c1ce.tar.gz
cuberite-e23dcffaff5ce3b3da1e44fba21c03e66f48c1ce.tar.bz2
cuberite-e23dcffaff5ce3b3da1e44fba21c03e66f48c1ce.tar.lz
cuberite-e23dcffaff5ce3b3da1e44fba21c03e66f48c1ce.tar.xz
cuberite-e23dcffaff5ce3b3da1e44fba21c03e66f48c1ce.tar.zst
cuberite-e23dcffaff5ce3b3da1e44fba21c03e66f48c1ce.zip
Diffstat (limited to 'src/WorldStorage/StatSerializer.cpp')
-rw-r--r--src/WorldStorage/StatSerializer.cpp146
1 files changed, 75 insertions, 71 deletions
diff --git a/src/WorldStorage/StatSerializer.cpp b/src/WorldStorage/StatSerializer.cpp
index 50767ae94..eff80149e 100644
--- a/src/WorldStorage/StatSerializer.cpp
+++ b/src/WorldStorage/StatSerializer.cpp
@@ -104,114 +104,118 @@ static const std::unordered_map<std::string_view, Statistic> LegacyMapping
-cStatSerializer::cStatSerializer(cStatManager & Manager, const std::string & WorldPath, std::string FileName) :
- m_Manager(Manager),
- m_Path(WorldPath + cFile::GetPathSeparator() + "stats")
+namespace StatSerializer
{
- // Even though stats are shared between worlds, they are (usually) saved
- // inside the folder of the default world.
+ auto MakeStatisticsDirectory(const std::string & WorldPath, std::string FileName)
+ {
+ // Even though stats are shared between worlds, they are (usually) saved
+ // inside the folder of the default world.
- // Ensure that the directory exists.
- cFile::CreateFolder(m_Path);
+ // Path to the world's statistics folder.
+ const auto Path = WorldPath + cFile::GetPathSeparator() + "stats";
- m_Path += cFile::GetPathSeparator() + std::move(FileName) + ".json";
-}
+ // Ensure that the directory exists.
+ cFile::CreateFolder(Path);
+ return Path + cFile::GetPathSeparator() + std::move(FileName) + ".json";
+ }
-void cStatSerializer::Load(void)
-{
- Json::Value Root;
- InputFileStream(m_Path) >> Root;
-
- LoadLegacyFromJSON(Root);
- LoadCustomStatFromJSON(Root["stats"]["custom"]);
-}
+ void SaveStatToJSON(const cStatManager & Manager, Json::Value & a_Out)
+ {
+ Manager.ForEachStatisticType([&a_Out](const cStatManager::CustomStore & Store)
+ {
+ if (Store.empty())
+ {
+ // Avoid saving "custom": null to disk:
+ return;
+ }
+
+ auto & Custom = a_Out["custom"];
+ for (const auto & Item : Store)
+ {
+ Custom[NamespaceSerializer::From(Item.first)] = Item.second;
+ }
+ });
+ }
-void cStatSerializer::Save(void)
-{
- Json::Value Root;
- SaveStatToJSON(Root["stats"]);
- Root["DataVersion"] = NamespaceSerializer::DataVersion();
+ void LoadLegacyFromJSON(cStatManager & Manager, const Json::Value & In)
+ {
+ for (auto Entry = In.begin(); Entry != In.end(); ++Entry)
+ {
+ const auto & Key = Entry.key().asString();
+ const auto FindResult = LegacyMapping.find(Key);
- OutputFileStream(m_Path) << Root;
-}
+ if ((FindResult != LegacyMapping.end()) && Entry->isInt())
+ {
+ Manager.SetValue(FindResult->second, Entry->asInt());
+ }
+ }
+ }
-void cStatSerializer::SaveStatToJSON(Json::Value & a_Out)
-{
- m_Manager.ForEachStatisticType([&a_Out](const cStatManager::CustomStore & Store)
+ void LoadCustomStatFromJSON(cStatManager & Manager, const Json::Value & a_In)
{
- if (Store.empty())
+ for (auto it = a_In.begin(); it != a_In.end(); ++it)
{
- // Avoid saving "custom": null to disk:
- return;
+ const auto & Key = it.key().asString();
+ const auto StatInfo = NamespaceSerializer::SplitNamespacedID(Key);
+ if (StatInfo.first == NamespaceSerializer::Namespace::Unknown)
+ {
+ // Ignore non-Vanilla, non-Cuberite namespaces for now:
+ continue;
+ }
+
+ const auto & StatName = StatInfo.second;
+ try
+ {
+ Manager.SetValue(NamespaceSerializer::ToCustomStatistic(StatName), it->asInt());
+ }
+ catch (const std::out_of_range & Oops)
+ {
+ FLOGWARNING("Invalid statistic type \"{}\"", StatName);
+ }
+ catch (const Json::LogicError & Oops)
+ {
+ FLOGWARNING("Invalid statistic value for type \"{}\"", StatName);
+ }
}
-
- auto & Custom = a_Out["custom"];
- for (const auto & Item : Store)
- {
- Custom[NamespaceSerializer::From(Item.first)] = Item.second;
- }
- });
-}
+ }
-void cStatSerializer::LoadLegacyFromJSON(const Json::Value & In)
-{
- for (auto Entry = In.begin(); Entry != In.end(); ++Entry)
+ void Load(cStatManager & Manager, const std::string & WorldPath, std::string FileName)
{
- const auto & Key = Entry.key().asString();
- const auto FindResult = LegacyMapping.find(Key);
+ Json::Value Root;
+ InputFileStream(MakeStatisticsDirectory(WorldPath, FileName)) >> Root;
- if ((FindResult != LegacyMapping.end()) && Entry->isInt())
- {
- m_Manager.SetValue(FindResult->second, Entry->asInt());
- }
+ LoadLegacyFromJSON(Manager, Root);
+ LoadCustomStatFromJSON(Manager, Root["stats"]["custom"]);
}
-}
-void cStatSerializer::LoadCustomStatFromJSON(const Json::Value & a_In)
-{
- for (auto it = a_In.begin() ; it != a_In.end() ; ++it)
+ void Save(const cStatManager & Manager, const std::string & WorldPath, std::string FileName)
{
- const auto & Key = it.key().asString();
- const auto StatInfo = NamespaceSerializer::SplitNamespacedID(Key);
- if (StatInfo.first == NamespaceSerializer::Namespace::Unknown)
- {
- // Ignore non-Vanilla, non-Cuberite namespaces for now:
- continue;
- }
+ Json::Value Root;
- const auto & StatName = StatInfo.second;
- try
- {
- m_Manager.SetValue(NamespaceSerializer::ToCustomStatistic(StatName), it->asInt());
- }
- catch (const std::out_of_range & Oops)
- {
- FLOGWARNING("Invalid statistic type \"{}\"", StatName);
- }
- catch (const Json::LogicError & Oops)
- {
- FLOGWARNING("Invalid statistic value for type \"{}\"", StatName);
- }
+ SaveStatToJSON(Manager, Root["stats"]);
+ Root["DataVersion"] = NamespaceSerializer::DataVersion();
+
+ OutputFileStream(MakeStatisticsDirectory(WorldPath, FileName)) << Root;
}
}