JsonDetail.cpp 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /*
  2. * JsonDetail.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 "JsonDetail.h"
  12. #include "VCMI_Lib.h"
  13. #include "CGeneralTextHandler.h"
  14. #include "CModHandler.h"
  15. #include "filesystem/Filesystem.h"
  16. #include "ScopeGuard.h"
  17. static const JsonNode nullNode;
  18. template<typename Iterator>
  19. void JsonWriter::writeContainer(Iterator begin, Iterator end)
  20. {
  21. if (begin == end)
  22. return;
  23. prefix += '\t';
  24. writeEntry(begin++);
  25. while (begin != end)
  26. {
  27. out<<",\n";
  28. writeEntry(begin++);
  29. }
  30. out<<"\n";
  31. prefix.resize(prefix.size()-1);
  32. }
  33. void JsonWriter::writeEntry(JsonMap::const_iterator entry)
  34. {
  35. if (!entry->second.meta.empty())
  36. out << prefix << " // " << entry->second.meta << "\n";
  37. out << prefix;
  38. writeString(entry->first);
  39. out << " : ";
  40. writeNode(entry->second);
  41. }
  42. void JsonWriter::writeEntry(JsonVector::const_iterator entry)
  43. {
  44. if (!entry->meta.empty())
  45. out << prefix << " // " << entry->meta << "\n";
  46. out << prefix;
  47. writeNode(*entry);
  48. }
  49. void JsonWriter::writeString(const std::string &string)
  50. {
  51. static const std::string escaped = "\"\\\b\f\n\r\t";
  52. static const std::string escaped_code = "\"\\bfnrt";
  53. out <<'\"';
  54. size_t pos=0, start=0;
  55. for (; pos<string.size(); pos++)
  56. {
  57. size_t escapedPos = escaped.find(string[pos]);
  58. if (escapedPos != std::string::npos)
  59. {
  60. out.write(string.data()+start, pos - start);
  61. out << '\\' << escaped_code[escapedPos];
  62. start = pos;
  63. }
  64. }
  65. out.write(string.data()+start, pos - start);
  66. out <<'\"';
  67. }
  68. void JsonWriter::writeNode(const JsonNode &node)
  69. {
  70. switch(node.getType())
  71. {
  72. break; case JsonNode::DATA_NULL:
  73. out << "null";
  74. break; case JsonNode::DATA_BOOL:
  75. if (node.Bool())
  76. out << "true";
  77. else
  78. out << "false";
  79. break; case JsonNode::DATA_FLOAT:
  80. out << node.Float();
  81. break; case JsonNode::DATA_STRING:
  82. writeString(node.String());
  83. break; case JsonNode::DATA_VECTOR:
  84. out << "[" << "\n";
  85. writeContainer(node.Vector().begin(), node.Vector().end());
  86. out << prefix << "]";
  87. break; case JsonNode::DATA_STRUCT:
  88. out << "{" << "\n";
  89. writeContainer(node.Struct().begin(), node.Struct().end());
  90. out << prefix << "}";
  91. }
  92. }
  93. JsonWriter::JsonWriter(std::ostream &output, const JsonNode &node):
  94. out(output)
  95. {
  96. writeNode(node);
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////
  99. JsonParser::JsonParser(const char * inputString, size_t stringSize):
  100. input(inputString, stringSize),
  101. lineCount(1),
  102. lineStart(0),
  103. pos(0)
  104. {
  105. }
  106. JsonNode JsonParser::parse(std::string fileName)
  107. {
  108. JsonNode root;
  109. if (input.size() == 0)
  110. {
  111. error("File is empty", false);
  112. }
  113. else
  114. {
  115. if (!Unicode::isValidString(&input[0], input.size()))
  116. error("Not a valid UTF-8 file", false);
  117. extractValue(root);
  118. extractWhitespace(false);
  119. //Warn if there are any non-whitespace symbols left
  120. if (pos < input.size())
  121. error("Not all file was parsed!", true);
  122. }
  123. if (!errors.empty())
  124. {
  125. logGlobal->warnStream()<<"File " << fileName << " is not a valid JSON file!";
  126. logGlobal->warnStream()<<errors;
  127. }
  128. return root;
  129. }
  130. bool JsonParser::isValid()
  131. {
  132. return errors.empty();
  133. }
  134. bool JsonParser::extractSeparator()
  135. {
  136. if (!extractWhitespace())
  137. return false;
  138. if ( input[pos] !=':')
  139. return error("Separator expected");
  140. pos++;
  141. return true;
  142. }
  143. bool JsonParser::extractValue(JsonNode &node)
  144. {
  145. if (!extractWhitespace())
  146. return false;
  147. switch (input[pos])
  148. {
  149. case '\"': return extractString(node);
  150. case 'n' : return extractNull(node);
  151. case 't' : return extractTrue(node);
  152. case 'f' : return extractFalse(node);
  153. case '{' : return extractStruct(node);
  154. case '[' : return extractArray(node);
  155. case '-' : return extractFloat(node);
  156. default:
  157. {
  158. if (input[pos] >= '0' && input[pos] <= '9')
  159. return extractFloat(node);
  160. return error("Value expected!");
  161. }
  162. }
  163. }
  164. bool JsonParser::extractWhitespace(bool verbose)
  165. {
  166. while (true)
  167. {
  168. while (pos < input.size() && (ui8)input[pos] <= ' ')
  169. {
  170. if (input[pos] == '\n')
  171. {
  172. lineCount++;
  173. lineStart = pos+1;
  174. }
  175. pos++;
  176. }
  177. if (pos >= input.size() || input[pos] != '/')
  178. break;
  179. pos++;
  180. if (pos == input.size())
  181. break;
  182. if (input[pos] == '/')
  183. pos++;
  184. else
  185. error("Comments must consist from two slashes!", true);
  186. while (pos < input.size() && input[pos] != '\n')
  187. pos++;
  188. }
  189. if (pos >= input.size() && verbose)
  190. return error("Unexpected end of file!");
  191. return true;
  192. }
  193. bool JsonParser::extractEscaping(std::string &str)
  194. {
  195. switch(input[pos])
  196. {
  197. break; case '\"': str += '\"';
  198. break; case '\\': str += '\\';
  199. break; case 'b': str += '\b';
  200. break; case 'f': str += '\f';
  201. break; case 'n': str += '\n';
  202. break; case 'r': str += '\r';
  203. break; case 't': str += '\t';
  204. break; default: return error("Unknown escape sequence!", true);
  205. };
  206. return true;
  207. }
  208. bool JsonParser::extractString(std::string &str)
  209. {
  210. if (input[pos] != '\"')
  211. return error("String expected!");
  212. pos++;
  213. size_t first = pos;
  214. while (pos != input.size())
  215. {
  216. if (input[pos] == '\"') // Correct end of string
  217. {
  218. str.append( &input[first], pos-first);
  219. pos++;
  220. return true;
  221. }
  222. if (input[pos] == '\\') // Escaping
  223. {
  224. str.append( &input[first], pos-first);
  225. pos++;
  226. if (pos == input.size())
  227. break;
  228. extractEscaping(str);
  229. first = pos + 1;
  230. }
  231. if (input[pos] == '\n') // end-of-line
  232. {
  233. str.append( &input[first], pos-first);
  234. return error("Closing quote not found!", true);
  235. }
  236. if ((unsigned char)(input[pos]) < ' ') // control character
  237. {
  238. str.append( &input[first], pos-first);
  239. first = pos+1;
  240. error("Illegal character in the string!", true);
  241. }
  242. pos++;
  243. }
  244. return error("Unterminated string!");
  245. }
  246. bool JsonParser::extractString(JsonNode &node)
  247. {
  248. std::string str;
  249. if (!extractString(str))
  250. return false;
  251. node.setType(JsonNode::DATA_STRING);
  252. node.String() = str;
  253. return true;
  254. }
  255. bool JsonParser::extractLiteral(const std::string &literal)
  256. {
  257. if (literal.compare(0, literal.size(), &input[pos], literal.size()) != 0)
  258. {
  259. while (pos < input.size() && ((input[pos]>'a' && input[pos]<'z')
  260. || (input[pos]>'A' && input[pos]<'Z')))
  261. pos++;
  262. return error("Unknown literal found", true);
  263. }
  264. pos += literal.size();
  265. return true;
  266. }
  267. bool JsonParser::extractNull(JsonNode &node)
  268. {
  269. if (!extractLiteral("null"))
  270. return false;
  271. node.clear();
  272. return true;
  273. }
  274. bool JsonParser::extractTrue(JsonNode &node)
  275. {
  276. if (!extractLiteral("true"))
  277. return false;
  278. node.Bool() = true;
  279. return true;
  280. }
  281. bool JsonParser::extractFalse(JsonNode &node)
  282. {
  283. if (!extractLiteral("false"))
  284. return false;
  285. node.Bool() = false;
  286. return true;
  287. }
  288. bool JsonParser::extractStruct(JsonNode &node)
  289. {
  290. node.setType(JsonNode::DATA_STRUCT);
  291. pos++;
  292. if (!extractWhitespace())
  293. return false;
  294. //Empty struct found
  295. if (input[pos] == '}')
  296. {
  297. pos++;
  298. return true;
  299. }
  300. while (true)
  301. {
  302. if (!extractWhitespace())
  303. return false;
  304. std::string key;
  305. if (!extractString(key))
  306. return false;
  307. if (node.Struct().find(key) != node.Struct().end())
  308. error("Dublicated element encountered!", true);
  309. if (!extractSeparator())
  310. return false;
  311. if (!extractElement(node.Struct()[key], '}'))
  312. return false;
  313. if (input[pos] == '}')
  314. {
  315. pos++;
  316. return true;
  317. }
  318. }
  319. }
  320. bool JsonParser::extractArray(JsonNode &node)
  321. {
  322. pos++;
  323. node.setType(JsonNode::DATA_VECTOR);
  324. if (!extractWhitespace())
  325. return false;
  326. //Empty array found
  327. if (input[pos] == ']')
  328. {
  329. pos++;
  330. return true;
  331. }
  332. while (true)
  333. {
  334. //NOTE: currently 50% of time is this vector resizing.
  335. //May be useful to use list during parsing and then swap() all items to vector
  336. node.Vector().resize(node.Vector().size()+1);
  337. if (!extractElement(node.Vector().back(), ']'))
  338. return false;
  339. if (input[pos] == ']')
  340. {
  341. pos++;
  342. return true;
  343. }
  344. }
  345. }
  346. bool JsonParser::extractElement(JsonNode &node, char terminator)
  347. {
  348. if (!extractValue(node))
  349. return false;
  350. if (!extractWhitespace())
  351. return false;
  352. bool comma = (input[pos] == ',');
  353. if (comma )
  354. {
  355. pos++;
  356. if (!extractWhitespace())
  357. return false;
  358. }
  359. if (input[pos] == terminator)
  360. {
  361. //FIXME: MOD COMPATIBILITY: Too many of these right now, re-enable later
  362. //if (comma)
  363. //error("Extra comma found!", true);
  364. return true;
  365. }
  366. if (!comma)
  367. error("Comma expected!", true);
  368. return true;
  369. }
  370. bool JsonParser::extractFloat(JsonNode &node)
  371. {
  372. assert(input[pos] == '-' || (input[pos] >= '0' && input[pos] <= '9'));
  373. bool negative=false;
  374. double result=0;
  375. if (input[pos] == '-')
  376. {
  377. pos++;
  378. negative = true;
  379. }
  380. if (input[pos] < '0' || input[pos] > '9')
  381. return error("Number expected!");
  382. //Extract integer part
  383. while (input[pos] >= '0' && input[pos] <= '9')
  384. {
  385. result = result*10+(input[pos]-'0');
  386. pos++;
  387. }
  388. if (input[pos] == '.')
  389. {
  390. //extract fractional part
  391. pos++;
  392. double fractMult = 0.1;
  393. if (input[pos] < '0' || input[pos] > '9')
  394. return error("Decimal part expected!");
  395. while (input[pos] >= '0' && input[pos] <= '9')
  396. {
  397. result = result + fractMult*(input[pos]-'0');
  398. fractMult /= 10;
  399. pos++;
  400. }
  401. }
  402. //TODO: exponential part
  403. if (negative)
  404. result = -result;
  405. node.setType(JsonNode::DATA_FLOAT);
  406. node.Float() = result;
  407. return true;
  408. }
  409. bool JsonParser::error(const std::string &message, bool warning)
  410. {
  411. std::ostringstream stream;
  412. std::string type(warning?" warning: ":" error: ");
  413. stream << "At line " << lineCount << ", position "<<pos-lineStart
  414. << type << message <<"\n";
  415. errors += stream.str();
  416. return warning;
  417. }
  418. ///////////////////////////////////////////////////////////////////////////////
  419. static const std::unordered_map<std::string, JsonNode::JsonType> stringToType =
  420. {
  421. {"null", JsonNode::DATA_NULL},
  422. {"boolean", JsonNode::DATA_BOOL},
  423. {"number", JsonNode::DATA_FLOAT},
  424. {"string", JsonNode::DATA_STRING},
  425. {"array", JsonNode::DATA_VECTOR},
  426. {"object", JsonNode::DATA_STRUCT}
  427. };
  428. namespace
  429. {
  430. namespace Common
  431. {
  432. std::string emptyCheck(Validation::ValidationData &, const JsonNode &, const JsonNode &, const JsonNode &)
  433. {
  434. // check is not needed - e.g. incorporated into another check
  435. return "";
  436. }
  437. std::string notImplementedCheck(Validation::ValidationData &, const JsonNode &, const JsonNode &, const JsonNode &)
  438. {
  439. return "Not implemented entry in schema";
  440. }
  441. std::string schemaListCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data,
  442. std::string errorMsg, std::function<bool(size_t)> isValid)
  443. {
  444. std::string errors = "<tested schemas>\n";
  445. size_t result = 0;
  446. for(auto & schemaEntry : schema.Vector())
  447. {
  448. std::string error = check(schemaEntry, data, validator);
  449. if (error.empty())
  450. {
  451. result++;
  452. }
  453. else
  454. {
  455. errors += error;
  456. errors += "<end of schema>\n";
  457. }
  458. }
  459. if (isValid(result))
  460. return "";
  461. else
  462. return validator.makeErrorMessage(errorMsg) + errors;
  463. }
  464. std::string allOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  465. {
  466. return schemaListCheck(validator, baseSchema, schema, data, "Failed to pass all schemas", [&](size_t count)
  467. {
  468. return count == schema.Vector().size();
  469. });
  470. }
  471. std::string anyOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  472. {
  473. return schemaListCheck(validator, baseSchema, schema, data, "Failed to pass any schema", [&](size_t count)
  474. {
  475. return count > 0;
  476. });
  477. }
  478. std::string oneOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  479. {
  480. return schemaListCheck(validator, baseSchema, schema, data, "Failed to pass exactly one schema", [&](size_t count)
  481. {
  482. return count == 1;
  483. });
  484. }
  485. std::string notCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  486. {
  487. if (check(schema, data, validator).empty())
  488. return validator.makeErrorMessage("Successful validation against negative check");
  489. return "";
  490. }
  491. std::string enumCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  492. {
  493. for(auto & enumEntry : schema.Vector())
  494. {
  495. if (data == enumEntry)
  496. return "";
  497. }
  498. return validator.makeErrorMessage("Key must have one of predefined values");
  499. }
  500. std::string typeCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  501. {
  502. const auto typeName = schema.String();
  503. auto it = stringToType.find(typeName);
  504. if(it == stringToType.end())
  505. {
  506. return validator.makeErrorMessage("Unknown type in schema:" + typeName);
  507. }
  508. JsonNode::JsonType type = it->second;
  509. if(type != data.getType() && data.getType() != JsonNode::DATA_NULL)
  510. return validator.makeErrorMessage("Type mismatch! Expected " + schema.String());
  511. return "";
  512. }
  513. std::string refCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  514. {
  515. std::string URI = schema.String();
  516. //node must be validated using schema pointed by this reference and not by data here
  517. //Local reference. Turn it into more easy to handle remote ref
  518. if (boost::algorithm::starts_with(URI, "#"))
  519. URI = validator.usedSchemas.back() + URI;
  520. return check(URI, data, validator);
  521. }
  522. std::string formatCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  523. {
  524. auto formats = Validation::getKnownFormats();
  525. std::string errors;
  526. auto checker = formats.find(schema.String());
  527. if (checker != formats.end())
  528. {
  529. std::string result = checker->second(data);
  530. if (!result.empty())
  531. errors += validator.makeErrorMessage(result);
  532. }
  533. else
  534. errors += validator.makeErrorMessage("Unsupported format type: " + schema.String());
  535. return errors;
  536. }
  537. }
  538. namespace String
  539. {
  540. std::string maxLengthCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  541. {
  542. if (data.String().size() > schema.Float())
  543. return validator.makeErrorMessage((boost::format("String is longer than %d symbols") % schema.Float()).str());
  544. return "";
  545. }
  546. std::string minLengthCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  547. {
  548. if (data.String().size() < schema.Float())
  549. return validator.makeErrorMessage((boost::format("String is shorter than %d symbols") % schema.Float()).str());
  550. return "";
  551. }
  552. }
  553. namespace Number
  554. {
  555. std::string maximumCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  556. {
  557. if (baseSchema["exclusiveMaximum"].Bool())
  558. {
  559. if (data.Float() >= schema.Float())
  560. return validator.makeErrorMessage((boost::format("Value is bigger than %d") % schema.Float()).str());
  561. }
  562. else
  563. {
  564. if (data.Float() > schema.Float())
  565. return validator.makeErrorMessage((boost::format("Value is bigger than %d") % schema.Float()).str());
  566. }
  567. return "";
  568. }
  569. std::string minimumCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  570. {
  571. if (baseSchema["exclusiveMinimum"].Bool())
  572. {
  573. if (data.Float() <= schema.Float())
  574. return validator.makeErrorMessage((boost::format("Value is smaller than %d") % schema.Float()).str());
  575. }
  576. else
  577. {
  578. if (data.Float() < schema.Float())
  579. return validator.makeErrorMessage((boost::format("Value is smaller than %d") % schema.Float()).str());
  580. }
  581. return "";
  582. }
  583. std::string multipleOfCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  584. {
  585. double result = data.Float() / schema.Float();
  586. if (floor(result) != result)
  587. return validator.makeErrorMessage((boost::format("Value is not divisible by %d") % schema.Float()).str());
  588. return "";
  589. }
  590. }
  591. namespace Vector
  592. {
  593. std::string itemEntryCheck(Validation::ValidationData & validator, const JsonVector items, const JsonNode & schema, size_t index)
  594. {
  595. validator.currentPath.push_back(JsonNode());
  596. validator.currentPath.back().Float() = index;
  597. auto onExit = vstd::makeScopeGuard([&]
  598. {
  599. validator.currentPath.pop_back();
  600. });
  601. if (!schema.isNull())
  602. return check(schema, items[index], validator);
  603. return "";
  604. }
  605. std::string itemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  606. {
  607. std::string errors;
  608. for (size_t i=0; i<data.Vector().size(); i++)
  609. {
  610. if (schema.getType() == JsonNode::DATA_VECTOR)
  611. {
  612. if (schema.Vector().size() > i)
  613. errors += itemEntryCheck(validator, data.Vector(), schema.Vector()[i], i);
  614. }
  615. else
  616. {
  617. errors += itemEntryCheck(validator, data.Vector(), schema, i);
  618. }
  619. }
  620. return errors;
  621. }
  622. std::string additionalItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  623. {
  624. std::string errors;
  625. // "items" is struct or empty (defaults to empty struct) - validation always successful
  626. const JsonNode & items = baseSchema["items"];
  627. if (items.getType() != JsonNode::DATA_VECTOR)
  628. return "";
  629. for (size_t i=items.Vector().size(); i<data.Vector().size(); i++)
  630. {
  631. if (schema.getType() == JsonNode::DATA_STRUCT)
  632. errors += itemEntryCheck(validator, data.Vector(), schema, i);
  633. else if (!schema.isNull() && schema.Bool() == false)
  634. errors += validator.makeErrorMessage("Unknown entry found");
  635. }
  636. return errors;
  637. }
  638. std::string minItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  639. {
  640. if (data.Vector().size() < schema.Float())
  641. return validator.makeErrorMessage((boost::format("Length is smaller than %d") % schema.Float()).str());
  642. return "";
  643. }
  644. std::string maxItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  645. {
  646. if (data.Vector().size() > schema.Float())
  647. return validator.makeErrorMessage((boost::format("Length is bigger than %d") % schema.Float()).str());
  648. return "";
  649. }
  650. std::string uniqueItemsCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  651. {
  652. if (schema.Bool())
  653. {
  654. for (auto itA = schema.Vector().begin(); itA != schema.Vector().end(); itA++)
  655. {
  656. auto itB = itA;
  657. while (++itB != schema.Vector().end())
  658. {
  659. if (*itA == *itB)
  660. return validator.makeErrorMessage("List must consist from unique items");
  661. }
  662. }
  663. }
  664. return "";
  665. }
  666. }
  667. namespace Struct
  668. {
  669. std::string maxPropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  670. {
  671. if (data.Struct().size() > schema.Float())
  672. return validator.makeErrorMessage((boost::format("Number of entries is bigger than %d") % schema.Float()).str());
  673. return "";
  674. }
  675. std::string minPropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  676. {
  677. if (data.Struct().size() < schema.Float())
  678. return validator.makeErrorMessage((boost::format("Number of entries is less than %d") % schema.Float()).str());
  679. return "";
  680. }
  681. std::string uniquePropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  682. {
  683. for (auto itA = data.Struct().begin(); itA != data.Struct().end(); itA++)
  684. {
  685. auto itB = itA;
  686. while (++itB != data.Struct().end())
  687. {
  688. if (itA->second == itB->second)
  689. return validator.makeErrorMessage("List must consist from unique items");
  690. }
  691. }
  692. return "";
  693. }
  694. std::string requiredCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  695. {
  696. std::string errors;
  697. for(auto & required : schema.Vector())
  698. {
  699. if (data[required.String()].isNull())
  700. errors += validator.makeErrorMessage("Required entry " + required.String() + " is missing");
  701. }
  702. return errors;
  703. }
  704. std::string dependenciesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  705. {
  706. std::string errors;
  707. for(auto & deps : schema.Struct())
  708. {
  709. if (!data[deps.first].isNull())
  710. {
  711. if (deps.second.getType() == JsonNode::DATA_VECTOR)
  712. {
  713. JsonVector depList = deps.second.Vector();
  714. for(auto & depEntry : depList)
  715. {
  716. if (data[depEntry.String()].isNull())
  717. errors += validator.makeErrorMessage("Property " + depEntry.String() + " required for " + deps.first + " is missing");
  718. }
  719. }
  720. else
  721. {
  722. if (!check(deps.second, data, validator).empty())
  723. errors += validator.makeErrorMessage("Requirements for " + deps.first + " are not fulfilled");
  724. }
  725. }
  726. }
  727. return errors;
  728. }
  729. std::string propertyEntryCheck(Validation::ValidationData & validator, const JsonNode &node, const JsonNode & schema, std::string nodeName)
  730. {
  731. validator.currentPath.push_back(JsonNode());
  732. validator.currentPath.back().String() = nodeName;
  733. auto onExit = vstd::makeScopeGuard([&]
  734. {
  735. validator.currentPath.pop_back();
  736. });
  737. // there is schema specifically for this item
  738. if (!schema.isNull())
  739. return check(schema, node, validator);
  740. return "";
  741. }
  742. std::string propertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  743. {
  744. std::string errors;
  745. for(auto & entry : data.Struct())
  746. errors += propertyEntryCheck(validator, entry.second, schema[entry.first], entry.first);
  747. return errors;
  748. }
  749. std::string additionalPropertiesCheck(Validation::ValidationData & validator, const JsonNode & baseSchema, const JsonNode & schema, const JsonNode & data)
  750. {
  751. std::string errors;
  752. for(auto & entry : data.Struct())
  753. {
  754. if (baseSchema["properties"].Struct().count(entry.first) == 0)
  755. {
  756. // try generic additionalItems schema
  757. if (schema.getType() == JsonNode::DATA_STRUCT)
  758. errors += propertyEntryCheck(validator, entry.second, schema, entry.first);
  759. // or, additionalItems field can be bool which indicates if such items are allowed
  760. else if (!schema.isNull() && schema.Bool() == false) // present and set to false - error
  761. errors += validator.makeErrorMessage("Unknown entry found: " + entry.first);
  762. }
  763. }
  764. return errors;
  765. }
  766. }
  767. namespace Formats
  768. {
  769. bool testFilePresence(std::string scope, ResourceID resource)
  770. {
  771. std::set<std::string> allowedScopes;
  772. if (scope != "core" && scope != "") // all real mods may have dependencies
  773. {
  774. //NOTE: recursive dependencies are not allowed at the moment - update code if this changes
  775. allowedScopes = VLC->modh->getModData(scope).dependencies;
  776. allowedScopes.insert("core"); // all mods can use H3 files
  777. }
  778. allowedScopes.insert(scope); // mods can use their own files
  779. for (auto & entry : allowedScopes)
  780. {
  781. if (CResourceHandler::get(entry)->existsResource(resource))
  782. return true;
  783. }
  784. return false;
  785. }
  786. #define TEST_FILE(scope, prefix, file, type) \
  787. if (testFilePresence(scope, ResourceID(prefix + file, type))) \
  788. return ""
  789. std::string testAnimation(std::string path, std::string scope)
  790. {
  791. TEST_FILE(scope, "Sprites/", path, EResType::ANIMATION);
  792. TEST_FILE(scope, "Sprites/", path, EResType::TEXT);
  793. return "Animation file \"" + path + "\" was not found";
  794. }
  795. std::string textFile(const JsonNode & node)
  796. {
  797. TEST_FILE(node.meta, "", node.String(), EResType::TEXT);
  798. return "Text file \"" + node.String() + "\" was not found";
  799. }
  800. std::string musicFile(const JsonNode & node)
  801. {
  802. TEST_FILE(node.meta, "", node.String(), EResType::MUSIC);
  803. return "Music file \"" + node.String() + "\" was not found";
  804. }
  805. std::string soundFile(const JsonNode & node)
  806. {
  807. TEST_FILE(node.meta, "Sounds/", node.String(), EResType::SOUND);
  808. return "Sound file \"" + node.String() + "\" was not found";
  809. }
  810. std::string defFile(const JsonNode & node)
  811. {
  812. TEST_FILE(node.meta, "Sprites/", node.String(), EResType::ANIMATION);
  813. return "Def file \"" + node.String() + "\" was not found";
  814. }
  815. std::string animationFile(const JsonNode & node)
  816. {
  817. return testAnimation(node.String(), node.meta);
  818. }
  819. std::string imageFile(const JsonNode & node)
  820. {
  821. TEST_FILE(node.meta, "Data/", node.String(), EResType::IMAGE);
  822. TEST_FILE(node.meta, "Sprites/", node.String(), EResType::IMAGE);
  823. if (node.String().find(':') != std::string::npos)
  824. return testAnimation(node.String().substr(0, node.String().find(':')), node.meta);
  825. return "Image file \"" + node.String() + "\" was not found";
  826. }
  827. std::string videoFile(const JsonNode & node)
  828. {
  829. TEST_FILE(node.meta, "Video/", node.String(), EResType::VIDEO);
  830. return "Video file \"" + node.String() + "\" was not found";
  831. }
  832. #undef TEST_FILE
  833. }
  834. Validation::TValidatorMap createCommonFields()
  835. {
  836. Validation::TValidatorMap ret;
  837. ret["format"] = Common::formatCheck;
  838. ret["allOf"] = Common::allOfCheck;
  839. ret["anyOf"] = Common::anyOfCheck;
  840. ret["oneOf"] = Common::oneOfCheck;
  841. ret["enum"] = Common::enumCheck;
  842. ret["type"] = Common::typeCheck;
  843. ret["not"] = Common::notCheck;
  844. ret["$ref"] = Common::refCheck;
  845. // fields that don't need implementation
  846. ret["title"] = Common::emptyCheck;
  847. ret["$schema"] = Common::emptyCheck;
  848. ret["default"] = Common::emptyCheck;
  849. ret["description"] = Common::emptyCheck;
  850. ret["definitions"] = Common::emptyCheck;
  851. return ret;
  852. }
  853. Validation::TValidatorMap createStringFields()
  854. {
  855. Validation::TValidatorMap ret = createCommonFields();
  856. ret["maxLength"] = String::maxLengthCheck;
  857. ret["minLength"] = String::minLengthCheck;
  858. ret["pattern"] = Common::notImplementedCheck;
  859. return ret;
  860. }
  861. Validation::TValidatorMap createNumberFields()
  862. {
  863. Validation::TValidatorMap ret = createCommonFields();
  864. ret["maximum"] = Number::maximumCheck;
  865. ret["minimum"] = Number::minimumCheck;
  866. ret["multipleOf"] = Number::multipleOfCheck;
  867. ret["exclusiveMaximum"] = Common::emptyCheck;
  868. ret["exclusiveMinimum"] = Common::emptyCheck;
  869. return ret;
  870. }
  871. Validation::TValidatorMap createVectorFields()
  872. {
  873. Validation::TValidatorMap ret = createCommonFields();
  874. ret["items"] = Vector::itemsCheck;
  875. ret["minItems"] = Vector::minItemsCheck;
  876. ret["maxItems"] = Vector::maxItemsCheck;
  877. ret["uniqueItems"] = Vector::uniqueItemsCheck;
  878. ret["additionalItems"] = Vector::additionalItemsCheck;
  879. return ret;
  880. }
  881. Validation::TValidatorMap createStructFields()
  882. {
  883. Validation::TValidatorMap ret = createCommonFields();
  884. ret["additionalProperties"] = Struct::additionalPropertiesCheck;
  885. ret["uniqueProperties"] = Struct::uniquePropertiesCheck;
  886. ret["maxProperties"] = Struct::maxPropertiesCheck;
  887. ret["minProperties"] = Struct::minPropertiesCheck;
  888. ret["dependencies"] = Struct::dependenciesCheck;
  889. ret["properties"] = Struct::propertiesCheck;
  890. ret["required"] = Struct::requiredCheck;
  891. ret["patternProperties"] = Common::notImplementedCheck;
  892. return ret;
  893. }
  894. Validation::TFormatMap createFormatMap()
  895. {
  896. Validation::TFormatMap ret;
  897. ret["textFile"] = Formats::textFile;
  898. ret["musicFile"] = Formats::musicFile;
  899. ret["soundFile"] = Formats::soundFile;
  900. ret["defFile"] = Formats::defFile;
  901. ret["animationFile"] = Formats::animationFile;
  902. ret["imageFile"] = Formats::imageFile;
  903. ret["videoFile"] = Formats::videoFile;
  904. return ret;
  905. }
  906. }
  907. namespace Validation
  908. {
  909. std::string ValidationData::makeErrorMessage(const std::string &message)
  910. {
  911. std::string errors;
  912. errors += "At ";
  913. if (!currentPath.empty())
  914. {
  915. for(const JsonNode &path : currentPath)
  916. {
  917. errors += "/";
  918. if (path.getType() == JsonNode::DATA_STRING)
  919. errors += path.String();
  920. else
  921. errors += boost::lexical_cast<std::string>(static_cast<unsigned>(path.Float()));
  922. }
  923. }
  924. else
  925. errors += "<root>";
  926. errors += "\n\t Error: " + message + "\n";
  927. return errors;
  928. }
  929. std::string check(std::string schemaName, const JsonNode & data)
  930. {
  931. ValidationData validator;
  932. return check(schemaName, data, validator);
  933. }
  934. std::string check(std::string schemaName, const JsonNode & data, ValidationData & validator)
  935. {
  936. validator.usedSchemas.push_back(schemaName);
  937. auto onscopeExit = vstd::makeScopeGuard([&]()
  938. {
  939. validator.usedSchemas.pop_back();
  940. });
  941. return check(JsonUtils::getSchema(schemaName), data, validator);
  942. }
  943. std::string check(const JsonNode & schema, const JsonNode & data, ValidationData & validator)
  944. {
  945. const TValidatorMap & knownFields = getKnownFieldsFor(data.getType());
  946. std::string errors;
  947. for(auto & entry : schema.Struct())
  948. {
  949. auto checker = knownFields.find(entry.first);
  950. if (checker != knownFields.end())
  951. errors += checker->second(validator, schema, entry.second, data);
  952. //else
  953. // errors += validator.makeErrorMessage("Unknown entry in schema " + entry.first);
  954. }
  955. return errors;
  956. }
  957. const TValidatorMap & getKnownFieldsFor(JsonNode::JsonType type)
  958. {
  959. static const TValidatorMap commonFields = createCommonFields();
  960. static const TValidatorMap numberFields = createNumberFields();
  961. static const TValidatorMap stringFields = createStringFields();
  962. static const TValidatorMap vectorFields = createVectorFields();
  963. static const TValidatorMap structFields = createStructFields();
  964. switch (type)
  965. {
  966. case JsonNode::DATA_FLOAT: return numberFields;
  967. case JsonNode::DATA_STRING: return stringFields;
  968. case JsonNode::DATA_VECTOR: return vectorFields;
  969. case JsonNode::DATA_STRUCT: return structFields;
  970. default: return commonFields;
  971. }
  972. }
  973. const TFormatMap & getKnownFormats()
  974. {
  975. static TFormatMap knownFormats = createFormatMap();
  976. return knownFormats;
  977. }
  978. } // Validation namespace