2
0

JsonNode.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /*
  2. * JsonNode.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 "JsonNode.h"
  12. #include "ScopeGuard.h"
  13. #include "HeroBonus.h"
  14. #include "filesystem/Filesystem.h"
  15. #include "VCMI_Lib.h" //for identifier resolution
  16. #include "CModHandler.h"
  17. #include "CGeneralTextHandler.h"
  18. #include "JsonDetail.h"
  19. using namespace JsonDetail;
  20. class LibClasses;
  21. class CModHandler;
  22. static const JsonNode nullNode;
  23. JsonNode::JsonNode(JsonType Type):
  24. type(DATA_NULL)
  25. {
  26. setType(Type);
  27. }
  28. JsonNode::JsonNode(const char *data, size_t datasize):
  29. type(DATA_NULL)
  30. {
  31. JsonParser parser(data, datasize);
  32. *this = parser.parse("<unknown>");
  33. }
  34. JsonNode::JsonNode(ResourceID && fileURI):
  35. type(DATA_NULL)
  36. {
  37. auto file = CResourceHandler::get()->load(fileURI)->readAll();
  38. JsonParser parser(reinterpret_cast<char*>(file.first.get()), file.second);
  39. *this = parser.parse(fileURI.getName());
  40. }
  41. JsonNode::JsonNode(const JsonNode &copy):
  42. type(DATA_NULL)
  43. {
  44. meta = copy.meta;
  45. setType(copy.getType());
  46. switch(type)
  47. {
  48. break; case DATA_NULL:
  49. break; case DATA_BOOL: Bool() = copy.Bool();
  50. break; case DATA_FLOAT: Float() = copy.Float();
  51. break; case DATA_STRING: String() = copy.String();
  52. break; case DATA_VECTOR: Vector() = copy.Vector();
  53. break; case DATA_STRUCT: Struct() = copy.Struct();
  54. }
  55. }
  56. JsonNode::~JsonNode()
  57. {
  58. setType(DATA_NULL);
  59. }
  60. void JsonNode::swap(JsonNode &b)
  61. {
  62. using std::swap;
  63. swap(meta, b.meta);
  64. swap(data, b.data);
  65. swap(type, b.type);
  66. }
  67. JsonNode & JsonNode::operator =(JsonNode node)
  68. {
  69. swap(node);
  70. return *this;
  71. }
  72. bool JsonNode::operator == (const JsonNode &other) const
  73. {
  74. if (getType() == other.getType())
  75. {
  76. switch(type)
  77. {
  78. break; case DATA_NULL: return true;
  79. break; case DATA_BOOL: return Bool() == other.Bool();
  80. break; case DATA_FLOAT: return Float() == other.Float();
  81. break; case DATA_STRING: return String() == other.String();
  82. break; case DATA_VECTOR: return Vector() == other.Vector();
  83. break; case DATA_STRUCT: return Struct() == other.Struct();
  84. }
  85. }
  86. return false;
  87. }
  88. bool JsonNode::operator != (const JsonNode &other) const
  89. {
  90. return !(*this == other);
  91. }
  92. JsonNode::JsonType JsonNode::getType() const
  93. {
  94. return type;
  95. }
  96. void JsonNode::setMeta(std::string metadata, bool recursive)
  97. {
  98. meta = metadata;
  99. if (recursive)
  100. {
  101. switch (type)
  102. {
  103. break; case DATA_VECTOR:
  104. {
  105. for(auto & node : Vector())
  106. {
  107. node.setMeta(metadata);
  108. }
  109. }
  110. break; case DATA_STRUCT:
  111. {
  112. for(auto & node : Struct())
  113. {
  114. node.second.setMeta(metadata);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. void JsonNode::setType(JsonType Type)
  121. {
  122. if (type == Type)
  123. return;
  124. //Reset node to nullptr
  125. if (Type != DATA_NULL)
  126. setType(DATA_NULL);
  127. switch (type)
  128. {
  129. break; case DATA_STRING: delete data.String;
  130. break; case DATA_VECTOR: delete data.Vector;
  131. break; case DATA_STRUCT: delete data.Struct;
  132. break; default:
  133. break;
  134. }
  135. //Set new node type
  136. type = Type;
  137. switch(type)
  138. {
  139. break; case DATA_NULL:
  140. break; case DATA_BOOL: data.Bool = false;
  141. break; case DATA_FLOAT: data.Float = 0;
  142. break; case DATA_STRING: data.String = new std::string();
  143. break; case DATA_VECTOR: data.Vector = new JsonVector();
  144. break; case DATA_STRUCT: data.Struct = new JsonMap();
  145. }
  146. }
  147. bool JsonNode::isNull() const
  148. {
  149. return type == DATA_NULL;
  150. }
  151. void JsonNode::clear()
  152. {
  153. setType(DATA_NULL);
  154. }
  155. bool & JsonNode::Bool()
  156. {
  157. setType(DATA_BOOL);
  158. return data.Bool;
  159. }
  160. double & JsonNode::Float()
  161. {
  162. setType(DATA_FLOAT);
  163. return data.Float;
  164. }
  165. std::string & JsonNode::String()
  166. {
  167. setType(DATA_STRING);
  168. return *data.String;
  169. }
  170. JsonVector & JsonNode::Vector()
  171. {
  172. setType(DATA_VECTOR);
  173. return *data.Vector;
  174. }
  175. JsonMap & JsonNode::Struct()
  176. {
  177. setType(DATA_STRUCT);
  178. return *data.Struct;
  179. }
  180. const bool boolDefault = false;
  181. const bool & JsonNode::Bool() const
  182. {
  183. if (type == DATA_NULL)
  184. return boolDefault;
  185. assert(type == DATA_BOOL);
  186. return data.Bool;
  187. }
  188. const double floatDefault = 0;
  189. const double & JsonNode::Float() const
  190. {
  191. if (type == DATA_NULL)
  192. return floatDefault;
  193. assert(type == DATA_FLOAT);
  194. return data.Float;
  195. }
  196. const std::string stringDefault = std::string();
  197. const std::string & JsonNode::String() const
  198. {
  199. if (type == DATA_NULL)
  200. return stringDefault;
  201. assert(type == DATA_STRING);
  202. return *data.String;
  203. }
  204. const JsonVector vectorDefault = JsonVector();
  205. const JsonVector & JsonNode::Vector() const
  206. {
  207. if (type == DATA_NULL)
  208. return vectorDefault;
  209. assert(type == DATA_VECTOR);
  210. return *data.Vector;
  211. }
  212. const JsonMap mapDefault = JsonMap();
  213. const JsonMap & JsonNode::Struct() const
  214. {
  215. if (type == DATA_NULL)
  216. return mapDefault;
  217. assert(type == DATA_STRUCT);
  218. return *data.Struct;
  219. }
  220. JsonNode & JsonNode::operator[](std::string child)
  221. {
  222. return Struct()[child];
  223. }
  224. const JsonNode & JsonNode::operator[](std::string child) const
  225. {
  226. auto it = Struct().find(child);
  227. if (it != Struct().end())
  228. return it->second;
  229. return nullNode;
  230. }
  231. // to avoid duplicating const and non-const code
  232. template<typename Node>
  233. Node & resolvePointer(Node & in, const std::string & pointer)
  234. {
  235. if (pointer.empty())
  236. return in;
  237. assert(pointer[0] == '/');
  238. size_t splitPos = pointer.find('/', 1);
  239. std::string entry = pointer.substr(1, splitPos -1);
  240. std::string remainer = splitPos == std::string::npos ? "" : pointer.substr(splitPos);
  241. if (in.getType() == JsonNode::DATA_VECTOR)
  242. {
  243. if (entry.find_first_not_of("0123456789") != std::string::npos) // non-numbers in string
  244. throw std::runtime_error("Invalid Json pointer");
  245. if (entry.size() > 1 && entry[0] == '0') // leading zeros are not allowed
  246. throw std::runtime_error("Invalid Json pointer");
  247. size_t index = boost::lexical_cast<size_t>(entry);
  248. if (in.Vector().size() > index)
  249. return in.Vector()[index].resolvePointer(remainer);
  250. }
  251. return in[entry].resolvePointer(remainer);
  252. }
  253. const JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer) const
  254. {
  255. return ::resolvePointer(*this, jsonPointer);
  256. }
  257. JsonNode & JsonNode::resolvePointer(const std::string &jsonPointer)
  258. {
  259. return ::resolvePointer(*this, jsonPointer);
  260. }
  261. ///JsonUtils
  262. void JsonUtils::parseTypedBonusShort(const JsonVector& source, Bonus *dest)
  263. {
  264. dest->val = source[1].Float();
  265. resolveIdentifier(source[2],dest->subtype);
  266. dest->additionalInfo = source[3].Float();
  267. dest->duration = Bonus::PERMANENT; //TODO: handle flags (as integer)
  268. dest->turnsRemain = 0;
  269. }
  270. Bonus * JsonUtils::parseBonus (const JsonVector &ability_vec) //TODO: merge with AddAbility, create universal parser for all bonus properties
  271. {
  272. auto b = new Bonus();
  273. std::string type = ability_vec[0].String();
  274. auto it = bonusNameMap.find(type);
  275. if (it == bonusNameMap.end())
  276. {
  277. logGlobal->errorStream() << "Error: invalid ability type " << type;
  278. return b;
  279. }
  280. b->type = it->second;
  281. parseTypedBonusShort(ability_vec, b);
  282. return b;
  283. }
  284. template <typename T>
  285. const T & parseByMap(const std::map<std::string, T> & map, const JsonNode * val, std::string err)
  286. {
  287. static T defaultValue = T();
  288. if (!val->isNull())
  289. {
  290. std::string type = val->String();
  291. auto it = map.find(type);
  292. if (it == map.end())
  293. {
  294. logGlobal->errorStream() << "Error: invalid " << err << type;
  295. return defaultValue;
  296. }
  297. else
  298. {
  299. return it->second;
  300. }
  301. }
  302. else
  303. return defaultValue;
  304. }
  305. void JsonUtils::resolveIdentifier (si32 &var, const JsonNode &node, std::string name)
  306. {
  307. const JsonNode &value = node[name];
  308. if (!value.isNull())
  309. {
  310. switch (value.getType())
  311. {
  312. case JsonNode::DATA_FLOAT:
  313. var = value.Float();
  314. break;
  315. case JsonNode::DATA_STRING:
  316. VLC->modh->identifiers.requestIdentifier(value, [&](si32 identifier)
  317. {
  318. var = identifier;
  319. });
  320. break;
  321. default:
  322. logGlobal->errorStream() << "Error! Wrong indentifier used for value of " << name;
  323. }
  324. }
  325. }
  326. void JsonUtils::resolveIdentifier (const JsonNode &node, si32 &var)
  327. {
  328. switch (node.getType())
  329. {
  330. case JsonNode::DATA_FLOAT:
  331. var = node.Float();
  332. break;
  333. case JsonNode::DATA_STRING:
  334. VLC->modh->identifiers.requestIdentifier (node, [&](si32 identifier)
  335. {
  336. var = identifier;
  337. });
  338. break;
  339. default:
  340. logGlobal->errorStream() << "Error! Wrong indentifier used for identifier!";
  341. }
  342. }
  343. Bonus * JsonUtils::parseBonus (const JsonNode &ability)
  344. {
  345. auto b = new Bonus();
  346. const JsonNode *value;
  347. std::string type = ability["type"].String();
  348. auto it = bonusNameMap.find(type);
  349. if (it == bonusNameMap.end())
  350. {
  351. logGlobal->errorStream() << "Error: invalid ability type " << type;
  352. return b;
  353. }
  354. b->type = it->second;
  355. resolveIdentifier (b->subtype, ability, "subtype");
  356. b->val = ability["val"].Float();
  357. value = &ability["valueType"];
  358. if (!value->isNull())
  359. b->valType = static_cast<Bonus::ValueType>(parseByMap(bonusValueMap, value, "value type "));
  360. resolveIdentifier (b->additionalInfo, ability, "addInfo");
  361. b->turnsRemain = ability["turns"].Float();
  362. b->sid = ability["sourceID"].Float();
  363. b->description = ability["description"].String();
  364. value = &ability["effectRange"];
  365. if (!value->isNull())
  366. b->effectRange = static_cast<Bonus::LimitEffect>(parseByMap(bonusLimitEffect, value, "effect range "));
  367. value = &ability["duration"];
  368. if (!value->isNull())
  369. {
  370. switch (value->getType())
  371. {
  372. case JsonNode::DATA_STRING:
  373. b->duration = parseByMap(bonusDurationMap, value, "duration type ");
  374. break;
  375. case JsonNode::DATA_VECTOR:
  376. {
  377. ui16 dur = 0;
  378. for (const JsonNode & d : value->Vector())
  379. {
  380. dur |= parseByMap(bonusDurationMap, &d, "duration type ");
  381. }
  382. b->duration = dur;
  383. }
  384. break;
  385. default:
  386. logGlobal->errorStream() << "Error! Wrong bonus duration format.";
  387. }
  388. }
  389. value = &ability["source"];
  390. if (!value->isNull())
  391. b->source = static_cast<Bonus::BonusSource>(parseByMap(bonusSourceMap, value, "source type "));
  392. value = &ability["limiters"];
  393. if (!value->isNull())
  394. {
  395. for (const JsonNode & limiter : value->Vector())
  396. {
  397. switch (limiter.getType())
  398. {
  399. case JsonNode::DATA_STRING: //pre-defined limiters
  400. b->limiter = parseByMap(bonusLimiterMap, &limiter, "limiter type ");
  401. break;
  402. case JsonNode::DATA_STRUCT: //customizable limiters
  403. {
  404. shared_ptr<ILimiter> l;
  405. if (limiter["type"].String() == "CREATURE_TYPE_LIMITER")
  406. {
  407. shared_ptr<CCreatureTypeLimiter> l2 = make_shared<CCreatureTypeLimiter>(); //TODO: How the hell resolve pointer to creature?
  408. const JsonVector vec = limiter["parameters"].Vector();
  409. VLC->modh->identifiers.requestIdentifier("creature", vec[0], [=](si32 creature)
  410. {
  411. l2->setCreature (CreatureID(creature));
  412. });
  413. if (vec.size() > 1)
  414. {
  415. l2->includeUpgrades = vec[1].Bool();
  416. }
  417. else
  418. l2->includeUpgrades = false;
  419. l = l2;
  420. }
  421. if (limiter["type"].String() == "HAS_ANOTHER_BONUS_LIMITER")
  422. {
  423. shared_ptr<HasAnotherBonusLimiter> l2 = make_shared<HasAnotherBonusLimiter>();
  424. const JsonVector vec = limiter["parameters"].Vector();
  425. std::string anotherBonusType = vec[0].String();
  426. auto it = bonusNameMap.find (anotherBonusType);
  427. if (it == bonusNameMap.end())
  428. {
  429. logGlobal->errorStream() << "Error: invalid ability type " << anotherBonusType;
  430. continue;
  431. }
  432. l2->type = it->second;
  433. if (vec.size() > 1 )
  434. {
  435. resolveIdentifier (vec[1], l2->subtype);
  436. l2->isSubtypeRelevant = true;
  437. }
  438. l = l2;
  439. }
  440. b->addLimiter(l);
  441. }
  442. break;
  443. }
  444. }
  445. }
  446. value = &ability["propagator"];
  447. if (!value->isNull())
  448. b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type ");
  449. return b;
  450. }
  451. //returns first Key with value equal to given one
  452. template<class Key, class Val>
  453. Key reverseMapFirst(const Val & val, const std::map<Key, Val> map)
  454. {
  455. for(auto it : map)
  456. {
  457. if(it.second == val)
  458. {
  459. return it.first;
  460. }
  461. }
  462. assert(0);
  463. return "";
  464. }
  465. void JsonUtils::unparseBonus( JsonNode &node, const Bonus * bonus )
  466. {
  467. node["type"].String() = reverseMapFirst<std::string, Bonus::BonusType>(bonus->type, bonusNameMap);
  468. node["subtype"].Float() = bonus->subtype;
  469. node["val"].Float() = bonus->val;
  470. node["valueType"].String() = reverseMapFirst<std::string, Bonus::ValueType>(bonus->valType, bonusValueMap);
  471. node["additionalInfo"].Float() = bonus->additionalInfo;
  472. node["turns"].Float() = bonus->turnsRemain;
  473. node["sourceID"].Float() = bonus->source;
  474. node["description"].String() = bonus->description;
  475. node["effectRange"].String() = reverseMapFirst<std::string, Bonus::LimitEffect>(bonus->effectRange, bonusLimitEffect);
  476. node["duration"].String() = reverseMapFirst<std::string, ui16>(bonus->duration, bonusDurationMap);
  477. node["source"].String() = reverseMapFirst<std::string, Bonus::BonusSource>(bonus->source, bonusSourceMap);
  478. if(bonus->limiter)
  479. {
  480. node["limiter"].String() = reverseMapFirst<std::string, TLimiterPtr>(bonus->limiter, bonusLimiterMap);
  481. }
  482. if(bonus->propagator)
  483. {
  484. node["propagator"].String() = reverseMapFirst<std::string, TPropagatorPtr>(bonus->propagator, bonusPropagatorMap);
  485. }
  486. }
  487. void minimizeNode(JsonNode & node, const JsonNode & schema)
  488. {
  489. if (schema["type"].String() == "object")
  490. {
  491. std::set<std::string> foundEntries;
  492. for(auto & entry : schema["required"].Vector())
  493. {
  494. std::string name = entry.String();
  495. foundEntries.insert(name);
  496. minimizeNode(node[name], schema["properties"][name]);
  497. if (vstd::contains(node.Struct(), name) &&
  498. node[name] == schema["properties"][name]["default"])
  499. {
  500. node.Struct().erase(name);
  501. }
  502. }
  503. // erase all unhandled entries
  504. for (auto it = node.Struct().begin(); it != node.Struct().end();)
  505. {
  506. if (!vstd::contains(foundEntries, it->first))
  507. it = node.Struct().erase(it);
  508. else
  509. it++;
  510. }
  511. }
  512. }
  513. void JsonUtils::minimize(JsonNode & node, std::string schemaName)
  514. {
  515. minimizeNode(node, getSchema(schemaName));
  516. }
  517. // FIXME: except for several lines function is identical to minimizeNode. Some way to reduce duplication?
  518. void maximizeNode(JsonNode & node, const JsonNode & schema)
  519. {
  520. // "required" entry can only be found in object/struct
  521. if (schema["type"].String() == "object")
  522. {
  523. std::set<std::string> foundEntries;
  524. // check all required entries that have default version
  525. for(auto & entry : schema["required"].Vector())
  526. {
  527. std::string name = entry.String();
  528. foundEntries.insert(name);
  529. if (node[name].isNull() &&
  530. !schema["properties"][name]["default"].isNull())
  531. {
  532. node[name] = schema["properties"][name]["default"];
  533. }
  534. maximizeNode(node[name], schema["properties"][name]);
  535. }
  536. // erase all unhandled entries
  537. for (auto it = node.Struct().begin(); it != node.Struct().end();)
  538. {
  539. if (!vstd::contains(foundEntries, it->first))
  540. it = node.Struct().erase(it);
  541. else
  542. it++;
  543. }
  544. }
  545. }
  546. void JsonUtils::maximize(JsonNode & node, std::string schemaName)
  547. {
  548. maximizeNode(node, getSchema(schemaName));
  549. }
  550. bool JsonUtils::validate(const JsonNode &node, std::string schemaName, std::string dataName)
  551. {
  552. std::string log = Validation::check(schemaName, node);
  553. if (!log.empty())
  554. {
  555. logGlobal->errorStream() << "Data in " << dataName << " is invalid!";
  556. logGlobal->errorStream() << log;
  557. }
  558. return log.empty();
  559. }
  560. const JsonNode & getSchemaByName(std::string name)
  561. {
  562. // cached schemas to avoid loading json data multiple times
  563. static std::map<std::string, JsonNode> loadedSchemas;
  564. if (vstd::contains(loadedSchemas, name))
  565. return loadedSchemas[name];
  566. std::string filename = "config/schemas/" + name + ".json";
  567. if (CResourceHandler::get()->existsResource(ResourceID(filename)))
  568. {
  569. loadedSchemas[name] = JsonNode(ResourceID(filename));
  570. return loadedSchemas[name];
  571. }
  572. logGlobal->errorStream() << "Error: missing schema with name " << name << "!";
  573. assert(0);
  574. return nullNode;
  575. }
  576. const JsonNode & JsonUtils::getSchema(std::string URI)
  577. {
  578. std::vector<std::string> segments;
  579. size_t posColon = URI.find(':');
  580. size_t posHash = URI.find('#');
  581. assert(posColon != std::string::npos);
  582. std::string protocolName = URI.substr(0, posColon);
  583. std::string filename = URI.substr(posColon + 1, posHash - posColon - 1);
  584. if (protocolName != "vcmi")
  585. {
  586. logGlobal->errorStream() << "Error: unsupported URI protocol for schema: " << segments[0];
  587. return nullNode;
  588. }
  589. // check if json pointer if present (section after hash in string)
  590. if (posHash == std::string::npos || posHash == URI.size() - 1)
  591. return getSchemaByName(filename);
  592. else
  593. return getSchemaByName(filename).resolvePointer(URI.substr(posHash + 1));
  594. }
  595. void JsonUtils::merge(JsonNode & dest, JsonNode & source)
  596. {
  597. if (dest.getType() == JsonNode::DATA_NULL)
  598. {
  599. std::swap(dest, source);
  600. return;
  601. }
  602. switch (source.getType())
  603. {
  604. case JsonNode::DATA_NULL:
  605. {
  606. dest.clear();
  607. break;
  608. }
  609. case JsonNode::DATA_BOOL:
  610. case JsonNode::DATA_FLOAT:
  611. case JsonNode::DATA_STRING:
  612. {
  613. std::swap(dest, source);
  614. break;
  615. }
  616. case JsonNode::DATA_VECTOR:
  617. {
  618. size_t total = std::min(source.Vector().size(), dest.Vector().size());
  619. for (size_t i=0; i< total; i++)
  620. merge(dest.Vector()[i], source.Vector()[i]);
  621. if (dest.Vector().size() < source.Vector().size())
  622. {
  623. //reserve place and *move* remaining data from source to dest
  624. source.Vector().reserve(source.Vector().size() + dest.Vector().size());
  625. std::move(source.Vector().begin() + total, source.Vector().end(),
  626. std::back_inserter(dest.Vector()));
  627. }
  628. break;
  629. }
  630. case JsonNode::DATA_STRUCT:
  631. {
  632. //recursively merge all entries from struct
  633. for(auto & node : source.Struct())
  634. merge(dest[node.first], node.second);
  635. }
  636. }
  637. }
  638. void JsonUtils::mergeCopy(JsonNode & dest, JsonNode source)
  639. {
  640. // uses copy created in stack to safely merge two nodes
  641. merge(dest, source);
  642. }
  643. JsonNode JsonUtils::assembleFromFiles(std::vector<std::string> files)
  644. {
  645. JsonNode result;
  646. for(std::string file : files)
  647. {
  648. JsonNode section(ResourceID(file, EResType::TEXT));
  649. merge(result, section);
  650. }
  651. return result;
  652. }
  653. JsonNode JsonUtils::assembleFromFiles(std::string filename)
  654. {
  655. JsonNode result;
  656. ResourceID resID(filename, EResType::TEXT);
  657. for(auto & loader : CResourceHandler::get()->getResourcesWithName(resID))
  658. {
  659. // FIXME: some way to make this code more readable
  660. auto stream = loader->load(resID);
  661. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  662. stream->read(textData.get(), stream->getSize());
  663. JsonNode section((char*)textData.get(), stream->getSize());
  664. merge(result, section);
  665. }
  666. return result;
  667. }