summaryrefslogtreecommitdiffstats
path: root/src/WorldStorage/StatSerializer.cpp
blob: de8a1d2774ceea37c6a23bbcd107ba3232dbc084 (plain) (blame)
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

// StatSerializer.cpp


#include "Globals.h"
#include "StatSerializer.h"
#include "../Statistics.h"
#include "NamespaceSerializer.h"

#include <fstream>
#include <json/json.h>





cStatSerializer::cStatSerializer(cStatManager & a_Manager, const AString & a_WorldName, const AString & a_FileName)
	: m_Manager(a_Manager)
{
	// Even though stats are shared between worlds, they are (usually) saved
	// inside the folder of the default world.

	AString StatsPath;
	Printf(StatsPath, "%s%cstats", a_WorldName.c_str(), cFile::PathSeparator());

	m_Path = StatsPath + cFile::PathSeparator() + a_FileName + ".json";

	// Ensure that the directory exists.
	cFile::CreateFolder(StatsPath);
}





void cStatSerializer::Load(void)
{
	Json::Value Root;
	std::ifstream(m_Path) >> Root;

	LoadCustomStatFromJSON(Root["stats"]["custom"]);
}





void cStatSerializer::Save(void)
{
	Json::Value Root;

	SaveStatToJSON(Root["stats"]);
	Root["DataVersion"] = NamespaceSerializer::DataVersion();

	std::ofstream(m_Path) << Root;
}





void cStatSerializer::SaveStatToJSON(Json::Value & a_Out)
{
	m_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::LoadCustomStatFromJSON(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);
		if (StatInfo.first == NamespaceSerializer::Namespace::Unknown)
		{
			// Ignore non-Vanilla, non-Cuberite namespaces for now:
			continue;
		}

		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);
		}
	}
}