JsonUpdater.cpp 7.0 KB

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