2
0

JsonNode.cpp 19 KB

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