JsonUpdater.cpp 7.0 KB

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