JsonUpdater.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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::set<int32_t> & standard, std::set<int32_t> & value)
  92. {
  93. LICSet lic(standard, decoder, encoder);
  94. serializeLIC(fieldName, lic);
  95. value = lic.any;
  96. }
  97. void JsonUpdater::serializeLIC(const std::string & fieldName, LICSet & value)
  98. {
  99. const JsonNode & field = currentObject->operator[](fieldName);
  100. if(field.isNull())
  101. return;
  102. const JsonNode & anyOf = field["anyOf"];
  103. const JsonNode & allOf = field["allOf"];
  104. const JsonNode & noneOf = field["noneOf"];
  105. value.all.clear();
  106. value.none.clear();
  107. if(anyOf.Vector().empty())
  108. {
  109. //permissive mode
  110. value.any = value.standard;
  111. }
  112. else
  113. {
  114. //restrictive mode
  115. value.any.clear();
  116. readLICPart(anyOf, value.decoder, value.any);
  117. for(si32 item : value.standard)
  118. if(!vstd::contains(value.any, item))
  119. value.none.insert(item);
  120. }
  121. readLICPart(allOf, value.decoder, value.all);
  122. readLICPart(noneOf, value.decoder, value.none);
  123. //remove any banned from allowed and required
  124. auto isBanned = [&value](const si32 item)->bool
  125. {
  126. return vstd::contains(value.none, item);
  127. };
  128. vstd::erase_if(value.all, isBanned);
  129. vstd::erase_if(value.any, isBanned);
  130. //add all required to allowed
  131. for(si32 item : value.all)
  132. {
  133. value.any.insert(item);
  134. }
  135. }
  136. void JsonUpdater::serializeString(const std::string & fieldName, std::string & value)
  137. {
  138. const JsonNode & data = currentObject->operator[](fieldName);
  139. if(data.getType() == JsonNode::JsonType::DATA_STRING)
  140. value = data.String();
  141. }
  142. void JsonUpdater::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
  143. {
  144. const JsonNode & data = currentObject->operator[](fieldName);
  145. if(data.getType() != JsonNode::JsonType::DATA_NULL)
  146. value = data;
  147. }
  148. void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNode * value)
  149. {
  150. const JsonNode & data = currentObject->operator[](fieldName);
  151. const JsonNode & toAdd = data["toAdd"];
  152. if(toAdd.getType() == JsonNode::JsonType::DATA_VECTOR)
  153. {
  154. for(const auto & item : toAdd.Vector())
  155. {
  156. auto b = JsonUtils::parseBonus(item);
  157. value->addNewBonus(b);
  158. }
  159. }
  160. const JsonNode & toRemove = data["toRemove"];
  161. if(toRemove.getType() == JsonNode::JsonType::DATA_VECTOR)
  162. {
  163. for(const auto & item : toRemove.Vector())
  164. {
  165. auto mask = JsonUtils::parseBonus(item);
  166. auto selector = [mask](const Bonus * b)
  167. {
  168. //compare everything but turnsRemain, limiter and propagator
  169. return mask->duration == b->duration
  170. && mask->type == b->type
  171. && mask->subtype == b->subtype
  172. && mask->source == b->source
  173. && mask->val == b->val
  174. && mask->sid == b->sid
  175. && mask->valType == b->valType
  176. && mask->additionalInfo == b->additionalInfo
  177. && mask->effectRange == b->effectRange
  178. && mask->description == b->description;
  179. };
  180. value->removeBonuses(selector);
  181. }
  182. }
  183. }
  184. VCMI_LIB_NAMESPACE_END