File: JsonUpdater.cpp

package info (click to toggle)
vcmi 1.1.0%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: bookworm
  • size: 14,672 kB
  • sloc: cpp: 181,738; sh: 220; python: 178; ansic: 69; objc: 66; xml: 59; makefile: 34
file content (313 lines) | stat: -rw-r--r-- 7,915 bytes parent folder | download
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * JsonUpdater.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */
#include "StdInc.h"
#include "JsonUpdater.h"

#include "../JsonNode.h"
#include "../HeroBonus.h"

VCMI_LIB_NAMESPACE_BEGIN

JsonUpdater::JsonUpdater(const IInstanceResolver * instanceResolver_, const JsonNode & root_)
	: JsonTreeSerializer(instanceResolver_, &root_, false, true)
{

}

void JsonUpdater::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
{
	const JsonNode & data = currentObject->operator[](fieldName);
	if(data.getType() == JsonNode::JsonType::DATA_BOOL)
		value = data.Bool();
}

void JsonUpdater::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
{
//	std::string identifier;
//	serializeString(fieldName, identifier);
//
//	value = defaultValue ? defaultValue.get() : 0;
//
//	if(identifier != "")
//	{
//		si32 rawId = decoder(identifier);
//		if(rawId >= 0)
//			value = rawId;
//	}
}

void JsonUpdater::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
{
//	const JsonVector & data = currentObject->operator[](fieldName).Vector();
//
//	value.clear();
//	value.reserve(data.size());
//
//	for(const JsonNode elem : data)
//	{
//		si32 rawId = decoder(elem.String());
//
//		if(rawId >= 0)
//			value.push_back(rawId);
//	}
}

void JsonUpdater::serializeInternal(const std::string & fieldName, double & value, const boost::optional<double> & defaultValue)
{
	const JsonNode & data = currentObject->operator[](fieldName);

	if(data.isNumber())
		value = data.Float();
}

void JsonUpdater::serializeInternal(const std::string & fieldName, si64 & value, const boost::optional<si64> &)
{
	const JsonNode & data = currentObject->operator[](fieldName);

	if(data.isNumber())
		value = data.Integer();
}

void JsonUpdater::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
{
//	const std::string & valueName = currentObject->operator[](fieldName).String();
//
//	const si32 actualOptional = defaultValue ? defaultValue.get() : 0;
//
//	si32 rawValue = vstd::find_pos(enumMap, valueName);
//	if(rawValue < 0)
//		value = actualOptional;
//	else
//		value = rawValue;
}

void JsonUpdater::serializeInternal(std::string & value)
{
	value = currentObject->String();
}

void JsonUpdater::serializeInternal(int64_t & value)
{
	value = currentObject->Integer();
}

void JsonUpdater::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
{
	const JsonNode & field = currentObject->operator[](fieldName);

	if(field.isNull())
		return;

	const JsonNode & anyOf = field["anyOf"];
	const JsonNode & allOf = field["allOf"];
	const JsonNode & noneOf = field["noneOf"];

	if(anyOf.Vector().empty() && allOf.Vector().empty())
	{
		//permissive mode
		value = standard;
	}
	else
	{
		//restrictive mode
		value.clear();
		value.resize(standard.size(), false);

		readLICPart(anyOf, decoder, true, value);
		readLICPart(allOf, decoder, true, value);
	}

	readLICPart(noneOf, decoder, false, value);
}

void JsonUpdater::serializeLIC(const std::string & fieldName, LIC & value)
{
	const JsonNode & field = currentObject->operator[](fieldName);

	if(field.isNull())
		return;

	const JsonNode & anyOf = field["anyOf"];
	const JsonNode & allOf = field["allOf"];
	const JsonNode & noneOf = field["noneOf"];

	if(anyOf.Vector().empty())
	{
		//permissive mode
		value.any = value.standard;
	}
	else
	{
		//restrictive mode
		value.any.clear();
		value.any.resize(value.standard.size(), false);

		readLICPart(anyOf, value.decoder, true, value.any);
	}

	readLICPart(allOf, value.decoder, true, value.all);
	readLICPart(noneOf, value.decoder, true, value.none);

	//remove any banned from allowed and required
	for(si32 idx = 0; idx < value.none.size(); idx++)
	{
		if(value.none[idx])
		{
			value.all[idx] = false;
			value.any[idx] = false;
		}
	}

	//add all required to allowed
	for(si32 idx = 0; idx < value.all.size(); idx++)
	{
		if(value.all[idx])
		{
			value.any[idx] = true;
		}
	}
}

void JsonUpdater::serializeLIC(const std::string & fieldName, LICSet & value)
{
	const JsonNode & field = currentObject->operator[](fieldName);

	if(field.isNull())
		return;

	const JsonNode & anyOf = field["anyOf"];
	const JsonNode & allOf = field["allOf"];
	const JsonNode & noneOf = field["noneOf"];

	value.all.clear();
	value.none.clear();

	if(anyOf.Vector().empty())
	{
		//permissive mode
		value.any = value.standard;
	}
	else
	{
		//restrictive mode
		value.any.clear();
		readLICPart(anyOf, value.decoder, value.any);

		for(si32 item : value.standard)
			if(!vstd::contains(value.any, item))
				value.none.insert(item);
	}

	readLICPart(allOf, value.decoder, value.all);
	readLICPart(noneOf, value.decoder, value.none);

	//remove any banned from allowed and required
	auto isBanned = [&value](const si32 item)->bool
	{
		return vstd::contains(value.none, item);
	};
	vstd::erase_if(value.all, isBanned);
	vstd::erase_if(value.any, isBanned);

	//add all required to allowed
	for(si32 item : value.all)
	{
		value.any.insert(item);
	}
}

void JsonUpdater::serializeString(const std::string & fieldName, std::string & value)
{
	const JsonNode & data = currentObject->operator[](fieldName);
	if(data.getType() == JsonNode::JsonType::DATA_STRING)
		value = data.String();
}

void JsonUpdater::serializeRaw(const std::string & fieldName, JsonNode & value, const boost::optional<const JsonNode &> defaultValue)
{
	const JsonNode & data = currentObject->operator[](fieldName);
	if(data.getType() != JsonNode::JsonType::DATA_NULL)
		value = data;
}

void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNode * value)
{
	const JsonNode & data = currentObject->operator[](fieldName);

	const JsonNode & toAdd = data["toAdd"];

	if(toAdd.getType() == JsonNode::JsonType::DATA_VECTOR)
	{
		for(auto & item : toAdd.Vector())
		{
			auto b = JsonUtils::parseBonus(item);
			value->addNewBonus(b);
		}
	}

	const JsonNode & toRemove = data["toRemove"];

	if(toRemove.getType() == JsonNode::JsonType::DATA_VECTOR)
	{
		for(auto & item : toRemove.Vector())
		{
			auto mask = JsonUtils::parseBonus(item);

			auto selector = [mask](const Bonus * b)
			{
				//compare everything but turnsRemain, limiter and propagator
				return mask->duration == b->duration
				&& mask->type == b->type
				&& mask->subtype == b->subtype
				&& mask->source == b->source
				&& mask->val == b->val
				&& mask->sid == b->sid
				&& mask->valType == b->valType
				&& mask->additionalInfo == b->additionalInfo
				&& mask->effectRange == b->effectRange
				&& mask->description == b->description;
			};

			value->removeBonuses(selector);
		}
	}
}

void JsonUpdater::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
{
	for(size_t index = 0; index < part.Vector().size(); index++)
	{
		const std::string & identifier = part.Vector()[index].String();

		const si32 rawId = decoder(identifier);
		if(rawId >= 0)
		{
			if(rawId < value.size())
				value[rawId] = val;
			else
				logGlobal->error("JsonUpdater::serializeLIC: id out of bounds %d", rawId);
		}
	}
}

void JsonUpdater::readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value)
{
	for(size_t index = 0; index < part.Vector().size(); index++)
	{
		const std::string & identifier = part.Vector()[index].String();

		const si32 rawId = decoder(identifier);
		if(rawId != -1)
			value.insert(rawId);
	}
}


VCMI_LIB_NAMESPACE_END