JsonDetail.cpp 31 KB

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