Make StatSerializer a namespace
This commit is contained in:
parent
8cca3db61b
commit
e23dcffaff
@ -2327,8 +2327,7 @@ bool cPlayer::LoadFromFile(const AString & a_FileName, cWorldPtr & a_World)
|
||||
{
|
||||
// Load the player stats.
|
||||
// We use the default world name (like bukkit) because stats are shared between dimensions / worlds.
|
||||
cStatSerializer StatSerializer(m_Stats, cRoot::Get()->GetDefaultWorld()->GetDataPath(), GetUUID().ToLongString());
|
||||
StatSerializer.Load();
|
||||
StatSerializer::Load(m_Stats, cRoot::Get()->GetDefaultWorld()->GetDataPath(), GetUUID().ToLongString());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
@ -2476,8 +2475,7 @@ bool cPlayer::SaveToDisk()
|
||||
{
|
||||
// Save the player stats.
|
||||
// We use the default world name (like bukkit) because stats are shared between dimensions / worlds.
|
||||
cStatSerializer StatSerializer(m_Stats, cRoot::Get()->GetDefaultWorld()->GetDataPath(), GetUUID().ToLongString());
|
||||
StatSerializer.Save();
|
||||
StatSerializer::Save(m_Stats, cRoot::Get()->GetDefaultWorld()->GetDataPath(), GetUUID().ToLongString());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
@ -104,53 +104,29 @@ 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
|
||||
{
|
||||
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.
|
||||
|
||||
// Path to the world's statistics folder.
|
||||
const auto Path = WorldPath + cFile::GetPathSeparator() + "stats";
|
||||
|
||||
// Ensure that the directory exists.
|
||||
cFile::CreateFolder(m_Path);
|
||||
cFile::CreateFolder(Path);
|
||||
|
||||
m_Path += cFile::GetPathSeparator() + std::move(FileName) + ".json";
|
||||
}
|
||||
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 cStatSerializer::Save(void)
|
||||
{
|
||||
Json::Value Root;
|
||||
|
||||
SaveStatToJSON(Root["stats"]);
|
||||
Root["DataVersion"] = NamespaceSerializer::DataVersion();
|
||||
|
||||
OutputFileStream(m_Path) << Root;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cStatSerializer::SaveStatToJSON(Json::Value & a_Out)
|
||||
{
|
||||
m_Manager.ForEachStatisticType([&a_Out](const cStatManager::CustomStore & Store)
|
||||
void SaveStatToJSON(const cStatManager & Manager, Json::Value & a_Out)
|
||||
{
|
||||
Manager.ForEachStatisticType([&a_Out](const cStatManager::CustomStore & Store)
|
||||
{
|
||||
if (Store.empty())
|
||||
{
|
||||
@ -164,14 +140,14 @@ void cStatSerializer::SaveStatToJSON(Json::Value & a_Out)
|
||||
Custom[NamespaceSerializer::From(Item.first)] = Item.second;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cStatSerializer::LoadLegacyFromJSON(const Json::Value & In)
|
||||
{
|
||||
void LoadLegacyFromJSON(cStatManager & Manager, const Json::Value & In)
|
||||
{
|
||||
for (auto Entry = In.begin(); Entry != In.end(); ++Entry)
|
||||
{
|
||||
const auto & Key = Entry.key().asString();
|
||||
@ -179,18 +155,18 @@ void cStatSerializer::LoadLegacyFromJSON(const Json::Value & In)
|
||||
|
||||
if ((FindResult != LegacyMapping.end()) && Entry->isInt())
|
||||
{
|
||||
m_Manager.SetValue(FindResult->second, Entry->asInt());
|
||||
Manager.SetValue(FindResult->second, Entry->asInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void cStatSerializer::LoadCustomStatFromJSON(const Json::Value & a_In)
|
||||
{
|
||||
for (auto it = a_In.begin() ; it != a_In.end() ; ++it)
|
||||
void LoadCustomStatFromJSON(cStatManager & Manager, const Json::Value & a_In)
|
||||
{
|
||||
for (auto it = a_In.begin(); it != a_In.end(); ++it)
|
||||
{
|
||||
const auto & Key = it.key().asString();
|
||||
const auto StatInfo = NamespaceSerializer::SplitNamespacedID(Key);
|
||||
@ -203,7 +179,7 @@ void cStatSerializer::LoadCustomStatFromJSON(const Json::Value & a_In)
|
||||
const auto & StatName = StatInfo.second;
|
||||
try
|
||||
{
|
||||
m_Manager.SetValue(NamespaceSerializer::ToCustomStatistic(StatName), it->asInt());
|
||||
Manager.SetValue(NamespaceSerializer::ToCustomStatistic(StatName), it->asInt());
|
||||
}
|
||||
catch (const std::out_of_range & Oops)
|
||||
{
|
||||
@ -214,4 +190,32 @@ void cStatSerializer::LoadCustomStatFromJSON(const Json::Value & a_In)
|
||||
FLOGWARNING("Invalid statistic value for type \"{}\"", StatName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Load(cStatManager & Manager, const std::string & WorldPath, std::string FileName)
|
||||
{
|
||||
Json::Value Root;
|
||||
InputFileStream(MakeStatisticsDirectory(WorldPath, FileName)) >> Root;
|
||||
|
||||
LoadLegacyFromJSON(Manager, Root);
|
||||
LoadCustomStatFromJSON(Manager, Root["stats"]["custom"]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Save(const cStatManager & Manager, const std::string & WorldPath, std::string FileName)
|
||||
{
|
||||
Json::Value Root;
|
||||
|
||||
SaveStatToJSON(Manager, Root["stats"]);
|
||||
Root["DataVersion"] = NamespaceSerializer::DataVersion();
|
||||
|
||||
OutputFileStream(MakeStatisticsDirectory(WorldPath, FileName)) << Root;
|
||||
}
|
||||
}
|
||||
|
@ -21,27 +21,11 @@ namespace Json { class Value; }
|
||||
|
||||
|
||||
|
||||
class cStatSerializer
|
||||
namespace StatSerializer
|
||||
{
|
||||
public:
|
||||
|
||||
cStatSerializer(cStatManager & Manager, const std::string & WorldPath, std::string FileName);
|
||||
|
||||
/* Try to load the player statistics. */
|
||||
void Load(void);
|
||||
void Load(cStatManager & Manager, const std::string & WorldPath, std::string FileName);
|
||||
|
||||
/* Try to save the player statistics. */
|
||||
void Save(void);
|
||||
|
||||
private:
|
||||
|
||||
void SaveStatToJSON(Json::Value & a_Out);
|
||||
|
||||
void LoadLegacyFromJSON(const Json::Value & In);
|
||||
|
||||
void LoadCustomStatFromJSON(const Json::Value & a_In);
|
||||
|
||||
cStatManager & m_Manager;
|
||||
|
||||
std::string m_Path;
|
||||
} ;
|
||||
void Save(const cStatManager & Manager, const std::string & WorldPath, std::string FileName);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user