JsonUpdater.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "../bonuses/CBonusSystemNode.h"
  13. #include "../bonuses/Bonus.h"
  14. #include "../json/JsonBonus.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, std::map<std::string, uint16_t> & value)
  60. {
  61. // TODO
  62. }
  63. void JsonUpdater::serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue)
  64. {
  65. const JsonNode & data = currentObject->operator[](fieldName);
  66. if(data.isNumber())
  67. value = data.Float();
  68. }
  69. void JsonUpdater::serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> &)
  70. {
  71. const JsonNode & data = currentObject->operator[](fieldName);
  72. if(data.isNumber())
  73. value = data.Integer();
  74. }
  75. void JsonUpdater::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
  76. {
  77. // const std::string & valueName = currentObject->operator[](fieldName).String();
  78. //
  79. // const si32 actualOptional = defaultValue.value_or(0);
  80. //
  81. // si32 rawValue = vstd::find_pos(enumMap, valueName);
  82. // if(rawValue < 0)
  83. // value = actualOptional;
  84. // else
  85. // value = rawValue;
  86. }
  87. void JsonUpdater::serializeInternal(std::string & value)
  88. {
  89. value = currentObject->String();
  90. }
  91. void JsonUpdater::serializeInternal(int64_t & value)
  92. {
  93. value = currentObject->Integer();
  94. }
  95. void JsonUpdater::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<int32_t> & standard, std::set<int32_t> & value)
  96. {
  97. LICSet lic(standard, decoder, encoder);
  98. serializeLIC(fieldName, lic);
  99. value = lic.any;
  100. }
  101. void JsonUpdater::serializeLIC(const std::string & fieldName, LICSet & value)
  102. {
  103. const JsonNode & field = currentObject->operator[](fieldName);
  104. if(field.isNull())
  105. return;
  106. const JsonNode & anyOf = field["anyOf"];
  107. const JsonNode & allOf = field["allOf"];
  108. const JsonNode & noneOf = field["noneOf"];
  109. value.all.clear();
  110. value.none.clear();
  111. if(anyOf.Vector().empty())
  112. {
  113. //permissive mode
  114. value.any = value.standard;
  115. }
  116. else
  117. {
  118. //restrictive mode
  119. value.any.clear();
  120. readLICPart(anyOf, value.decoder, value.any);
  121. for(si32 item : value.standard)
  122. if(!vstd::contains(value.any, item))
  123. value.none.insert(item);
  124. }
  125. readLICPart(allOf, value.decoder, value.all);
  126. readLICPart(noneOf, value.decoder, value.none);
  127. //remove any banned from allowed and required
  128. auto isBanned = [&value](const si32 item)->bool
  129. {
  130. return vstd::contains(value.none, item);
  131. };
  132. vstd::erase_if(value.all, isBanned);
  133. vstd::erase_if(value.any, isBanned);
  134. //add all required to allowed
  135. for(si32 item : value.all)
  136. {
  137. value.any.insert(item);
  138. }
  139. }
  140. void JsonUpdater::serializeString(const std::string & fieldName, std::string & value)
  141. {
  142. const JsonNode & data = currentObject->operator[](fieldName);
  143. if(data.getType() == JsonNode::JsonType::DATA_STRING)
  144. value = data.String();
  145. }
  146. void JsonUpdater::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
  147. {
  148. const JsonNode & data = currentObject->operator[](fieldName);
  149. if(data.getType() != JsonNode::JsonType::DATA_NULL)
  150. value = data;
  151. }
  152. void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNode * value)
  153. {
  154. const JsonNode & data = currentObject->operator[](fieldName);
  155. const JsonNode & toAdd = data["toAdd"];
  156. if(toAdd.getType() == JsonNode::JsonType::DATA_VECTOR)
  157. {
  158. for(const auto & item : toAdd.Vector())
  159. {
  160. auto b = JsonUtils::parseBonus(item);
  161. value->addNewBonus(b);
  162. }
  163. }
  164. const JsonNode & toRemove = data["toRemove"];
  165. if(toRemove.getType() == JsonNode::JsonType::DATA_VECTOR)
  166. {
  167. for(const auto & item : toRemove.Vector())
  168. {
  169. auto mask = JsonUtils::parseBonus(item);
  170. auto selector = [mask](const Bonus * b)
  171. {
  172. //compare everything but turnsRemain, limiter and propagator
  173. return mask->duration == b->duration
  174. && mask->type == b->type
  175. && mask->subtype == b->subtype
  176. && mask->source == b->source
  177. && mask->val == b->val
  178. && mask->sid == b->sid
  179. && mask->valType == b->valType
  180. && mask->additionalInfo == b->additionalInfo
  181. && mask->effectRange == b->effectRange;
  182. };
  183. value->removeBonuses(selector);
  184. }
  185. }
  186. }
  187. VCMI_LIB_NAMESPACE_END