summaryrefslogtreecommitdiffstats
path: root/src/Protocol/ProtocolBlockTypePalette.cpp
blob: 86d05623b780daf7b531839f7471d7223154b14e (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
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
#include "Globals.h"
#include "ProtocolBlockTypePalette.h"
#include <cstdint>
#include <sstream>
#include "json/value.h"
#include "json/reader.h"





ProtocolBlockTypePalette::ProtocolBlockTypePalette()
{
	// empty
}





bool ProtocolBlockTypePalette::loadFromString(const AString & aMapping)
{
	std::stringstream stream;
	stream << aMapping;

	return loadFromStream(stream);
}





bool ProtocolBlockTypePalette::loadFromStream(std::istream & aInputStream)
{
	Json::Value root;

	try
	{
		aInputStream >> root;
	}
	#if defined _DEBUG
	catch (const std::exception & e)
	{
		LOGD(e.what());
		return false;
	}
	#else
	catch (const std::exception &)
	{
		return false;
	}
	#endif

	if (!root.isObject() ||
		!root.isMember("Metadata") ||
		!root["Metadata"].isMember("ProtocolBlockTypePaletteVersion") ||
		!root.isMember("Palette") ||
		!root["Palette"].isArray())
	{
		LOGD("Incorrect palette format.");
		return false;
	}

	if (root["Metadata"]["ProtocolBlockTypePaletteVersion"].asUInt() != 1)
	{
		LOGD("Palette format version not supported.");
		return false;
	}

	auto len = root["Palette"].size();
	for (decltype(len) i = 0; i < len; ++i)
	{
		const auto & record = root["Palette"][i];
		if (!record.isObject())
		{
			LOGD("Record #%u must be a JSON object.", i);
			return false;
		}

		auto blocktype = record["name"].asString();
		auto id = std::stoul(record["id"].asString());
		std::map<AString, AString> state;

		if (id >= NOT_FOUND)
		{
			LOGD("`id` must be less than ProtocolBlockTypePalette::NOT_FOUND, but is %lu", id);
			return false;
		}

		if (record.isMember("props"))
		{
			const auto & props = record["props"];
			if (!props.isObject())
			{
				LOGD("`props` key must be a JSON object.");
				return false;
			}
			for (const auto & key: props.getMemberNames())
			{
				state[key] = props[key].asString();
			}
		}

		// Block type map entry already exists?
		if (mIndex.count(blocktype) == 0)
		{
			mIndex.insert({blocktype, std::map<BlockState, UInt32>()});
		}

		const auto & result = mIndex[blocktype].insert({BlockState(state), id});
		if (result.second == false)
		{
			LOGINFO("Duplicate block state encountered (Current ID: %lu, other: %lu)", result.first->second, id);
		}
	}
	return true;
}





UInt32 ProtocolBlockTypePalette::index(const AString & aBlockTypeName, const BlockState & aBlockState) const
{
	auto a = mIndex.find(aBlockTypeName);
	if (a != mIndex.end())
	{
		auto b = a->second.find(aBlockState);
		if (b != a->second.end())
		{
			return b->second;
		}
	}
	return NOT_FOUND;
}





void ProtocolBlockTypePalette::clear()
{
	return mIndex.clear();
}