1
0
Fork 0
cuberite-2a/src/WorldStorage/StatSerializer.cpp

151 lines
2.5 KiB
C++
Raw Normal View History

2014-05-11 11:57:06 +00:00
// StatSerializer.cpp
#include "Globals.h"
#include "StatSerializer.h"
#include "../Statistics.h"
#include "../JsonUtils.h"
2014-05-11 11:57:06 +00:00
cStatSerializer::cStatSerializer(const AString & a_WorldName, const AString & a_PlayerName, const AString & a_FileName, cStatManager * a_Manager)
2014-05-11 11:57:06 +00:00
: m_Manager(a_Manager)
{
2014-05-13 11:53:15 +00:00
// Even though stats are shared between worlds, they are (usually) saved
// inside the folder of the default world.
2014-05-11 11:57:06 +00:00
AString StatsPath;
Printf(StatsPath, "%s%cstats", a_WorldName.c_str(), cFile::PathSeparator());
2014-05-11 11:57:06 +00:00
m_LegacyPath = StatsPath + "/" + a_PlayerName + ".json";
m_Path = StatsPath + "/" + a_FileName + ".json";
2014-05-11 11:57:06 +00:00
2014-05-13 11:53:15 +00:00
// Ensure that the directory exists.
2020-05-07 19:14:00 +00:00
cFile::CreateFolder(StatsPath);
2014-05-11 11:57:06 +00:00
}
bool cStatSerializer::Load(void)
{
2020-05-07 19:14:00 +00:00
AString Data = cFile::ReadWholeFile(m_Path);
2014-05-11 11:57:06 +00:00
if (Data.empty())
{
2020-05-07 19:14:00 +00:00
Data = cFile::ReadWholeFile(m_LegacyPath);
if (Data.empty())
{
return false;
}
2014-05-11 11:57:06 +00:00
}
Json::Value Root;
if (JsonUtils::ParseString(Data, Root))
2014-05-11 11:57:06 +00:00
{
return LoadStatFromJSON(Root);
}
return false;
}
bool cStatSerializer::Save(void)
{
Json::Value Root;
SaveStatToJSON(Root);
cFile File;
2020-05-07 19:14:00 +00:00
if (!File.Open(m_Path, cFile::fmWrite))
2014-05-11 11:57:06 +00:00
{
return false;
}
AString JsonData = JsonUtils::WriteStyledString(Root);
2014-05-11 11:57:06 +00:00
File.Write(JsonData.data(), JsonData.size());
File.Close();
return true;
}
void cStatSerializer::SaveStatToJSON(Json::Value & a_Out)
{
2015-05-24 11:56:56 +00:00
for (unsigned int i = 0; i < static_cast<unsigned int>(statCount); ++i)
2014-05-11 11:57:06 +00:00
{
2015-05-24 11:56:56 +00:00
StatValue Value = m_Manager->GetValue(static_cast<eStatistic>(i));
2014-05-11 11:57:06 +00:00
if (Value != 0)
{
2015-05-24 11:56:56 +00:00
const AString & StatName = cStatInfo::GetName(static_cast<eStatistic>(i));
2014-05-11 11:57:06 +00:00
a_Out[StatName] = Value;
}
2014-05-11 17:30:54 +00:00
// TODO 2014-05-11 xdot: Save "progress"
2014-05-11 11:57:06 +00:00
}
}
bool cStatSerializer::LoadStatFromJSON(const Json::Value & a_In)
{
m_Manager->Reset();
2015-10-19 14:03:55 +00:00
for (Json::Value::const_iterator it = a_In.begin() ; it != a_In.end() ; ++it)
2014-05-11 11:57:06 +00:00
{
AString StatName = it.key().asString();
eStatistic StatType = cStatInfo::GetType(StatName);
if (StatType == statInvalid)
{
2014-05-11 17:30:54 +00:00
LOGWARNING("Invalid statistic type \"%s\"", StatName.c_str());
2014-05-11 11:57:06 +00:00
continue;
}
2015-10-19 14:03:55 +00:00
const Json::Value & Node = *it;
2014-05-11 17:30:54 +00:00
if (Node.isInt())
{
m_Manager->SetValue(StatType, Node.asInt());
}
else if (Node.isObject())
{
StatValue Value = Node.get("value", 0).asInt();
// TODO 2014-05-11 xdot: Load "progress"
m_Manager->SetValue(StatType, Value);
}
else
{
LOGWARNING("Invalid statistic value for type \"%s\"", StatName.c_str());
}
2014-05-11 11:57:06 +00:00
}
return true;
}