JsonUpdater.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * JsonUpdater.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "JsonUpdater.h"
  12. #include "../JsonNode.h"
  13. #include "../HeroBonus.h"
  14. JsonUpdater::JsonUpdater(const IInstanceResolver * instanceResolver_, const JsonNode & root_)
  15. : JsonTreeSerializer(instanceResolver_, &root_, false, true)
  16. {
  17. }
  18. void JsonUpdater::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
  19. {
  20. const JsonNode & data = currentObject->operator[](fieldName);
  21. if(data.getType() == JsonNode::JsonType::DATA_BOOL)
  22. value = data.Bool();
  23. }
  24. void JsonUpdater::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
  25. {
  26. // std::string identifier;
  27. // serializeString(fieldName, identifier);
  28. //
  29. // value = defaultValue ? defaultValue.get() : 0;
  30. //
  31. // if(identifier != "")
  32. // {
  33. // si32 rawId = decoder(identifier);
  34. // if(rawId >= 0)
  35. // value = rawId;
  36. // }
  37. }
  38. void JsonUpdater::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
  39. {
  40. // const JsonVector & data = currentObject->operator[](fieldName).Vector();
  41. //
  42. // value.clear();
  43. // value.reserve(data.size());
  44. //
  45. // for(const JsonNode elem : data)
  46. // {
  47. // si32 rawId = decoder(elem.String());
  48. //
  49. // if(rawId >= 0)
  50. // value.push_back(rawId);
  51. // }
  52. }
  53. void JsonUpdater::serializeInternal(const std::string & fieldName, double & value, const boost::optional<double> & defaultValue)
  54. {
  55. const JsonNode & data = currentObject->operator[](fieldName);
  56. if(data.isNumber())
  57. value = data.Float();
  58. }
  59. void JsonUpdater::serializeInternal(const std::string & fieldName, si64 & value, const boost::optional<si64> &)
  60. {
  61. const JsonNode & data = currentObject->operator[](fieldName);
  62. if(data.isNumber())
  63. value = data.Integer();
  64. }
  65. void JsonUpdater::serializeInternal(const std::string & fieldName, si32 & value, const boost::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
  66. {
  67. // const std::string & valueName = currentObject->operator[](fieldName).String();
  68. //
  69. // const si32 actualOptional = defaultValue ? defaultValue.get() : 0;
  70. //
  71. // si32 rawValue = vstd::find_pos(enumMap, valueName);
  72. // if(rawValue < 0)
  73. // value = actualOptional;
  74. // else
  75. // value = rawValue;
  76. }
  77. void JsonUpdater::serializeInternal(std::string & value)
  78. {
  79. value = currentObject->String();
  80. }
  81. void JsonUpdater::serializeInternal(int64_t & value)
  82. {
  83. value = currentObject->Integer();
  84. }
  85. void JsonUpdater::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::vector<bool> & standard, std::vector<bool> & value)
  86. {
  87. const JsonNode & field = currentObject->operator[](fieldName);
  88. if(field.isNull())
  89. return;
  90. const JsonNode & anyOf = field["anyOf"];
  91. const JsonNode & allOf = field["allOf"];
  92. const JsonNode & noneOf = field["noneOf"];
  93. if(anyOf.Vector().empty() && allOf.Vector().empty())
  94. {
  95. //permissive mode
  96. value = standard;
  97. }
  98. else
  99. {
  100. //restrictive mode
  101. value.clear();
  102. value.resize(standard.size(), false);
  103. readLICPart(anyOf, decoder, true, value);
  104. readLICPart(allOf, decoder, true, value);
  105. }
  106. readLICPart(noneOf, decoder, false, value);
  107. }
  108. void JsonUpdater::serializeLIC(const std::string & fieldName, LIC & value)
  109. {
  110. const JsonNode & field = currentObject->operator[](fieldName);
  111. if(field.isNull())
  112. return;
  113. const JsonNode & anyOf = field["anyOf"];
  114. const JsonNode & allOf = field["allOf"];
  115. const JsonNode & noneOf = field["noneOf"];
  116. if(anyOf.Vector().empty())
  117. {
  118. //permissive mode
  119. value.any = value.standard;
  120. }
  121. else
  122. {
  123. //restrictive mode
  124. value.any.clear();
  125. value.any.resize(value.standard.size(), false);
  126. readLICPart(anyOf, value.decoder, true, value.any);
  127. }
  128. readLICPart(allOf, value.decoder, true, value.all);
  129. readLICPart(noneOf, value.decoder, true, value.none);
  130. //remove any banned from allowed and required
  131. for(si32 idx = 0; idx < value.none.size(); idx++)
  132. {
  133. if(value.none[idx])
  134. {
  135. value.all[idx] = false;
  136. value.any[idx] = false;
  137. }
  138. }
  139. //add all required to allowed
  140. for(si32 idx = 0; idx < value.all.size(); idx++)
  141. {
  142. if(value.all[idx])
  143. {
  144. value.any[idx] = true;
  145. }
  146. }
  147. }
  148. void JsonUpdater::serializeLIC(const std::string & fieldName, LICSet & value)
  149. {
  150. const JsonNode & field = currentObject->operator[](fieldName);
  151. if(field.isNull())
  152. return;
  153. const JsonNode & anyOf = field["anyOf"];
  154. const JsonNode & allOf = field["allOf"];
  155. const JsonNode & noneOf = field["noneOf"];
  156. value.all.clear();
  157. value.none.clear();
  158. if(anyOf.Vector().empty())
  159. {
  160. //permissive mode
  161. value.any = value.standard;
  162. }
  163. else
  164. {
  165. //restrictive mode
  166. value.any.clear();
  167. readLICPart(anyOf, value.decoder, value.any);
  168. for(si32 item : value.standard)
  169. if(!vstd::contains(value.any, item))
  170. value.none.insert(item);
  171. }
  172. readLICPart(allOf, value.decoder, value.all);
  173. readLICPart(noneOf, value.decoder, value.none);
  174. //remove any banned from allowed and required
  175. auto isBanned = [&value](const si32 item)->bool
  176. {
  177. return vstd::contains(value.none, item);
  178. };
  179. vstd::erase_if(value.all, isBanned);
  180. vstd::erase_if(value.any, isBanned);
  181. //add all required to allowed
  182. for(si32 item : value.all)
  183. {
  184. value.any.insert(item);
  185. }
  186. }
  187. void JsonUpdater::serializeString(const std::string & fieldName, std::string & value)
  188. {
  189. const JsonNode & data = currentObject->operator[](fieldName);
  190. if(data.getType() == JsonNode::JsonType::DATA_STRING)
  191. value = data.String();
  192. }
  193. void JsonUpdater::serializeRaw(const std::string & fieldName, JsonNode & value, const boost::optional<const JsonNode &> defaultValue)
  194. {
  195. const JsonNode & data = currentObject->operator[](fieldName);
  196. if(data.getType() != JsonNode::JsonType::DATA_NULL)
  197. value = data;
  198. }
  199. void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNode * value)
  200. {
  201. const JsonNode & data = currentObject->operator[](fieldName);
  202. const JsonNode & toAdd = data["toAdd"];
  203. if(toAdd.getType() == JsonNode::JsonType::DATA_VECTOR)
  204. {
  205. for(auto & item : toAdd.Vector())
  206. {
  207. auto b = JsonUtils::parseBonus(item);
  208. value->addNewBonus(b);
  209. }
  210. }
  211. const JsonNode & toRemove = data["toRemove"];
  212. if(toRemove.getType() == JsonNode::JsonType::DATA_VECTOR)
  213. {
  214. for(auto & item : toRemove.Vector())
  215. {
  216. auto mask = JsonUtils::parseBonus(item);
  217. auto selector = [mask](const Bonus * b)
  218. {
  219. //compare everything but turnsRemain, limiter and propagator
  220. return mask->duration == b->duration
  221. && mask->type == b->type
  222. && mask->subtype == b->subtype
  223. && mask->source == b->source
  224. && mask->val == b->val
  225. && mask->sid == b->sid
  226. && mask->valType == b->valType
  227. && mask->additionalInfo == b->additionalInfo
  228. && mask->effectRange == b->effectRange
  229. && mask->description == b->description;
  230. };
  231. value->removeBonuses(selector);
  232. }
  233. }
  234. }
  235. void JsonUpdater::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
  236. {
  237. for(size_t index = 0; index < part.Vector().size(); index++)
  238. {
  239. const std::string & identifier = part.Vector()[index].String();
  240. const si32 rawId = decoder(identifier);
  241. if(rawId >= 0)
  242. {
  243. if(rawId < value.size())
  244. value[rawId] = val;
  245. else
  246. logGlobal->error("JsonUpdater::serializeLIC: id out of bounds %d", rawId);
  247. }
  248. }
  249. }
  250. void JsonUpdater::readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value)
  251. {
  252. for(size_t index = 0; index < part.Vector().size(); index++)
  253. {
  254. const std::string & identifier = part.Vector()[index].String();
  255. const si32 rawId = decoder(identifier);
  256. if(rawId != -1)
  257. value.insert(rawId);
  258. }
  259. }