2
0

JsonUpdater.cpp 7.1 KB

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