CGeneralTextHandler.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * CGeneralTextHandler.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 "CGeneralTextHandler.h"
  12. #include <boost/locale.hpp>
  13. #include "filesystem/Filesystem.h"
  14. #include "CConfigHandler.h"
  15. #include "CModHandler.h"
  16. #include "GameConstants.h"
  17. #include "mapObjects/CQuest.h"
  18. #include "VCMI_Lib.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. size_t Unicode::getCharacterSize(char firstByte)
  21. {
  22. // length of utf-8 character can be determined from 1st byte by counting number of highest bits set to 1:
  23. // 0xxxxxxx -> 1 - ASCII chars
  24. // 110xxxxx -> 2
  25. // 11110xxx -> 4 - last allowed in current standard
  26. // 1111110x -> 6 - last allowed in original standard
  27. if ((ui8)firstByte < 0x80)
  28. return 1; // ASCII
  29. size_t ret = 0;
  30. for (size_t i=0; i<8; i++)
  31. {
  32. if (((ui8)firstByte & (0x80 >> i)) != 0)
  33. ret++;
  34. else
  35. break;
  36. }
  37. return ret;
  38. }
  39. bool Unicode::isValidCharacter(const char * character, size_t maxSize)
  40. {
  41. // can't be first byte in UTF8
  42. if ((ui8)character[0] >= 0x80 && (ui8)character[0] < 0xC0)
  43. return false;
  44. // first character must follow rules checked in getCharacterSize
  45. size_t size = getCharacterSize((ui8)character[0]);
  46. if ((ui8)character[0] > 0xF4)
  47. return false; // above maximum allowed in standard (UTF codepoints are capped at 0x0010FFFF)
  48. if (size > maxSize)
  49. return false;
  50. // remaining characters must have highest bit set to 1
  51. for (size_t i = 1; i < size; i++)
  52. {
  53. if (((ui8)character[i] & 0x80) == 0)
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool Unicode::isValidASCII(const std::string & text)
  59. {
  60. for (const char & ch : text)
  61. if (ui8(ch) >= 0x80 )
  62. return false;
  63. return true;
  64. }
  65. bool Unicode::isValidASCII(const char * data, size_t size)
  66. {
  67. for (size_t i=0; i<size; i++)
  68. if (ui8(data[i]) >= 0x80 )
  69. return false;
  70. return true;
  71. }
  72. bool Unicode::isValidString(const std::string & text)
  73. {
  74. for (size_t i=0; i<text.size(); i += getCharacterSize(text[i]))
  75. {
  76. if (!isValidCharacter(text.data() + i, text.size() - i))
  77. return false;
  78. }
  79. return true;
  80. }
  81. bool Unicode::isValidString(const char * data, size_t size)
  82. {
  83. for (size_t i=0; i<size; i += getCharacterSize(data[i]))
  84. {
  85. if (!isValidCharacter(data + i, size - i))
  86. return false;
  87. }
  88. return true;
  89. }
  90. /// Detects language and encoding of H3 text files based on matching against pregenerated footprints of H3 file
  91. void CGeneralTextHandler::detectInstallParameters() const
  92. {
  93. struct LanguageFootprint
  94. {
  95. std::string language;
  96. std::string encoding;
  97. std::array<double, 16> footprint;
  98. };
  99. static const std::vector<LanguageFootprint> knownFootprints =
  100. {
  101. { "English", "CP1252", { { 0.0559, 0.0000, 0.1983, 0.0051, 0.0222, 0.0183, 0.4596, 0.2405, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000 } } },
  102. { "French", "CP1252", { { 0.0493, 0.0000, 0.1926, 0.0047, 0.0230, 0.0121, 0.4133, 0.2780, 0.0002, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0259, 0.0008 } } },
  103. { "German", "CP1252", { { 0.0534, 0.0000, 0.1705, 0.0047, 0.0418, 0.0208, 0.4775, 0.2191, 0.0001, 0.0000, 0.0000, 0.0000, 0.0000, 0.0005, 0.0036, 0.0080 } } },
  104. { "Polish", "CP1250", { { 0.0534, 0.0000, 0.1701, 0.0067, 0.0157, 0.0133, 0.4328, 0.2540, 0.0001, 0.0043, 0.0000, 0.0244, 0.0000, 0.0000, 0.0181, 0.0071 } } },
  105. { "Russian", "CP1251", { { 0.0548, 0.0000, 0.1744, 0.0061, 0.0031, 0.0009, 0.0046, 0.0136, 0.0000, 0.0004, 0.0000, 0.0000, 0.0227, 0.0061, 0.4882, 0.2252 } } },
  106. { "Ukrainian", "CP1251", { { 0.0559, 0.0000, 0.1807, 0.0059, 0.0036, 0.0013, 0.0046, 0.0134, 0.0000, 0.0004, 0.0000, 0.0487, 0.0209, 0.0060, 0.4615, 0.1972 } } },
  107. };
  108. // load file that will be used for footprint generation
  109. // this is one of the most text-heavy files in game and consists solely from translated texts
  110. auto resource = CResourceHandler::get()->load(ResourceID("DATA/GENRLTXT.TXT", EResType::TEXT));
  111. std::array<size_t, 256> charCount;
  112. std::array<double, 16> footprint;
  113. std::vector<double> deviations;
  114. boost::range::fill(charCount, 0);
  115. boost::range::fill(footprint, 0.0);
  116. deviations.resize(knownFootprints.size(), 0.0);
  117. auto data = resource->readAll();
  118. // compute how often each character occurs in input file
  119. for (si64 i = 0; i < data.second; ++i)
  120. charCount[data.first[i]] += 1;
  121. // and convert computed data into weights
  122. // to reduce amount of data, group footprint data into 16-char blocks.
  123. // While this will reduce precision, it should not affect output
  124. // since we expect only tiny differences compared to reference footprints
  125. for (size_t i = 0; i < 256; ++i)
  126. footprint[i/16] += double(charCount[i]) / data.second;
  127. logGlobal->debug("Language footprint: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
  128. footprint[0], footprint[1], footprint[2], footprint[3], footprint[4], footprint[5], footprint[6], footprint[7],
  129. footprint[8], footprint[9], footprint[10], footprint[11], footprint[12], footprint[13], footprint[14], footprint[15]
  130. );
  131. for (size_t i = 0; i < deviations.size(); ++i)
  132. {
  133. for (size_t j = 0; j < footprint.size(); ++j)
  134. deviations[i] += std::abs((footprint[j] - knownFootprints[i].footprint[j]));
  135. }
  136. size_t bestIndex = boost::range::min_element(deviations) - deviations.begin();
  137. for (size_t i = 0; i < deviations.size(); ++i)
  138. logGlobal->debug("Comparing to %s: %f", knownFootprints[i].language, deviations[i]);
  139. Settings language = settings.write["session"]["language"];
  140. language->String() = knownFootprints[bestIndex].language;
  141. Settings encoding = settings.write["session"]["encoding"];
  142. encoding->String() = knownFootprints[bestIndex].encoding;
  143. }
  144. std::string Unicode::toUnicode(const std::string &text)
  145. {
  146. return toUnicode(text, CGeneralTextHandler::getInstalledEncoding());
  147. }
  148. std::string Unicode::toUnicode(const std::string &text, const std::string &encoding)
  149. {
  150. return boost::locale::conv::to_utf<char>(text, encoding);
  151. }
  152. std::string Unicode::fromUnicode(const std::string & text)
  153. {
  154. return fromUnicode(text, CGeneralTextHandler::getInstalledEncoding());
  155. }
  156. std::string Unicode::fromUnicode(const std::string &text, const std::string &encoding)
  157. {
  158. return boost::locale::conv::from_utf<char>(text, encoding);
  159. }
  160. void Unicode::trimRight(std::string & text, const size_t amount)
  161. {
  162. if(text.empty())
  163. return;
  164. //todo: more efficient algorithm
  165. for(int i = 0; i< amount; i++){
  166. auto b = text.begin();
  167. auto e = text.end();
  168. size_t lastLen = 0;
  169. size_t len = 0;
  170. while (b != e) {
  171. lastLen = len;
  172. size_t n = getCharacterSize(*b);
  173. if(!isValidCharacter(&(*b),e-b))
  174. {
  175. logGlobal->error("Invalid UTF8 sequence");
  176. break;//invalid sequence will be trimmed
  177. }
  178. len += n;
  179. b += n;
  180. }
  181. text.resize(lastLen);
  182. }
  183. }
  184. //Helper for string -> float conversion
  185. class LocaleWithComma: public std::numpunct<char>
  186. {
  187. protected:
  188. char do_decimal_point() const override
  189. {
  190. return ',';
  191. }
  192. };
  193. CLegacyConfigParser::CLegacyConfigParser(std::string URI)
  194. {
  195. init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT)));
  196. }
  197. CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input)
  198. {
  199. init(input);
  200. }
  201. void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input)
  202. {
  203. data.reset(new char[input->getSize()]);
  204. input->read((ui8*)data.get(), input->getSize());
  205. curr = data.get();
  206. end = curr + input->getSize();
  207. }
  208. std::string CLegacyConfigParser::extractQuotedPart()
  209. {
  210. assert(*curr == '\"');
  211. curr++; // skip quote
  212. char * begin = curr;
  213. while (curr != end && *curr != '\"' && *curr != '\t')
  214. curr++;
  215. return std::string(begin, curr++); //increment curr to close quote
  216. }
  217. std::string CLegacyConfigParser::extractQuotedString()
  218. {
  219. assert(*curr == '\"');
  220. std::string ret;
  221. while (true)
  222. {
  223. ret += extractQuotedPart();
  224. // double quote - add it to string and continue quoted part
  225. if (curr < end && *curr == '\"')
  226. {
  227. ret += '\"';
  228. }
  229. //extract normal part
  230. else if(curr < end && *curr != '\t' && *curr != '\r')
  231. {
  232. char * begin = curr;
  233. while (curr < end && *curr != '\t' && *curr != '\r' && *curr != '\"')//find end of string or next quoted part start
  234. curr++;
  235. ret += std::string(begin, curr);
  236. if(curr>=end || *curr != '\"')
  237. return ret;
  238. }
  239. else // end of string
  240. return ret;
  241. }
  242. }
  243. std::string CLegacyConfigParser::extractNormalString()
  244. {
  245. char * begin = curr;
  246. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  247. curr++;
  248. return std::string(begin, curr);
  249. }
  250. std::string CLegacyConfigParser::readRawString()
  251. {
  252. if (curr >= end || *curr == '\n')
  253. return "";
  254. std::string ret;
  255. if (*curr == '\"')
  256. ret = extractQuotedString();// quoted text - find closing quote
  257. else
  258. ret = extractNormalString();//string without quotes - copy till \t or \r
  259. curr++;
  260. return ret;
  261. }
  262. std::string CLegacyConfigParser::readString()
  263. {
  264. // do not convert strings that are already in ASCII - this will only slow down loading process
  265. std::string str = readRawString();
  266. if (Unicode::isValidASCII(str))
  267. return str;
  268. return Unicode::toUnicode(str);
  269. }
  270. float CLegacyConfigParser::readNumber()
  271. {
  272. std::string input = readRawString();
  273. std::istringstream stream(input);
  274. if(input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  275. stream.imbue(std::locale(std::locale(), new LocaleWithComma()));
  276. float result;
  277. if ( !(stream >> result) )
  278. return 0;
  279. return result;
  280. }
  281. bool CLegacyConfigParser::isNextEntryEmpty() const
  282. {
  283. char * nextSymbol = curr;
  284. while (nextSymbol < end && *nextSymbol == ' ')
  285. nextSymbol++; //find next meaningfull symbol
  286. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  287. }
  288. bool CLegacyConfigParser::endLine()
  289. {
  290. while (curr < end && *curr != '\n')
  291. readString();
  292. curr++;
  293. return curr < end;
  294. }
  295. void CGeneralTextHandler::readToVector(std::string const & sourceID, std::string const & sourceName)
  296. {
  297. CLegacyConfigParser parser(sourceName);
  298. size_t index = 0;
  299. do
  300. {
  301. registerString({sourceID, index}, parser.readString());
  302. index += 1;
  303. }
  304. while (parser.endLine());
  305. }
  306. const std::string & CGeneralTextHandler::deserialize(const TextIdentifier & identifier) const
  307. {
  308. if(stringsOverrides.count(identifier.get()))
  309. return stringsOverrides.at(identifier.get());
  310. if(stringsLocalizations.count(identifier.get()))
  311. return stringsLocalizations.at(identifier.get());
  312. logGlobal->error("Unable to find localization for string '%s'", identifier.get());
  313. return identifier.get();
  314. }
  315. void CGeneralTextHandler::registerString(const TextIdentifier & UID, const std::string & localized)
  316. {
  317. assert(UID.get().find("..") == std::string::npos);
  318. stringsLocalizations[UID.get()] = localized;
  319. }
  320. void CGeneralTextHandler::registerStringOverride(const TextIdentifier & UID, const std::string & localized)
  321. {
  322. stringsOverrides[UID.get()] = localized;
  323. }
  324. void CGeneralTextHandler::loadTranslationOverrides(const JsonNode & config)
  325. {
  326. for ( auto const & node : config.Struct())
  327. registerStringOverride(node.first, node.second.String());
  328. }
  329. CGeneralTextHandler::CGeneralTextHandler():
  330. victoryConditions(*this, "core.vcdesc" ),
  331. lossCondtions (*this, "core.lcdesc" ),
  332. colors (*this, "core.plcolors" ),
  333. tcommands (*this, "core.tcommand" ),
  334. hcommands (*this, "core.hallinfo" ),
  335. fcommands (*this, "core.castinfo" ),
  336. advobtxt (*this, "core.advevent" ),
  337. xtrainfo (*this, "core.xtrainfo" ),
  338. restypes (*this, "core.restypes" ),
  339. randsign (*this, "core.randsign" ),
  340. overview (*this, "core.overview" ),
  341. arraytxt (*this, "core.arraytxt" ),
  342. primarySkillNames(*this, "core.priskill" ),
  343. jktexts (*this, "core.jktext" ),
  344. tavernInfo (*this, "core.tvrninfo" ),
  345. tavernRumors (*this, "core.randtvrn" ),
  346. turnDurations (*this, "core.turndur" ),
  347. heroscrn (*this, "core.heroscrn" ),
  348. tentColors (*this, "core.tentcolr" ),
  349. levels (*this, "core.skilllev" ),
  350. zelp (*this, "core.help" ),
  351. allTexts (*this, "core.genrltxt" ),
  352. // pseudo-array, that don't have H3 file with same name
  353. seerEmpty (*this, "core.seerhut.empty" ),
  354. seerNames (*this, "core.seerhut.names" ),
  355. capColors (*this, "vcmi.capitalColors" ),
  356. znpc00 (*this, "vcmi.znpc00" ), // technically - wog
  357. qeModCommands (*this, "vcmi.quickExchange" )
  358. {
  359. detectInstallParameters();
  360. readToVector("core.vcdesc", "DATA/VCDESC.TXT" );
  361. readToVector("core.lcdesc", "DATA/LCDESC.TXT" );
  362. readToVector("core.tcommand", "DATA/TCOMMAND.TXT" );
  363. readToVector("core.hallinfo", "DATA/HALLINFO.TXT" );
  364. readToVector("core.castinfo", "DATA/CASTINFO.TXT" );
  365. readToVector("core.advevent", "DATA/ADVEVENT.TXT" );
  366. readToVector("core.xtrainfo", "DATA/XTRAINFO.TXT" );
  367. readToVector("core.restypes", "DATA/RESTYPES.TXT" );
  368. readToVector("core.randsign", "DATA/RANDSIGN.TXT" );
  369. readToVector("core.crgen1", "DATA/CRGEN1.TXT" );
  370. readToVector("core.crgen4", "DATA/CRGEN4.TXT" );
  371. readToVector("core.overview", "DATA/OVERVIEW.TXT" );
  372. readToVector("core.arraytxt", "DATA/ARRAYTXT.TXT" );
  373. readToVector("core.priskill", "DATA/PRISKILL.TXT" );
  374. readToVector("core.jktext", "DATA/JKTEXT.TXT" );
  375. readToVector("core.tvrninfo", "DATA/TVRNINFO.TXT" );
  376. readToVector("core.turndur", "DATA/TURNDUR.TXT" );
  377. readToVector("core.heroscrn", "DATA/HEROSCRN.TXT" );
  378. readToVector("core.tentcolr", "DATA/TENTCOLR.TXT" );
  379. readToVector("core.skilllev", "DATA/SKILLLEV.TXT" );
  380. readToVector("core.cmpmusic", "DATA/CMPMUSIC.TXT" );
  381. readToVector("core.minename", "DATA/MINENAME.TXT" );
  382. readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
  383. static const char * QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
  384. if (CResourceHandler::get()->existsResource(ResourceID(QE_MOD_COMMANDS, EResType::TEXT)))
  385. readToVector("vcmi.quickExchange", QE_MOD_COMMANDS);
  386. {
  387. CLegacyConfigParser parser("DATA/RANDTVRN.TXT");
  388. parser.endLine();
  389. size_t index = 0;
  390. do
  391. {
  392. std::string line = parser.readString();
  393. if(!line.empty())
  394. {
  395. registerString({"core.randtvrn", index}, line);
  396. index += 1;
  397. }
  398. }
  399. while (parser.endLine());
  400. }
  401. {
  402. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  403. parser.endLine();
  404. size_t index = 0;
  405. do
  406. {
  407. registerString({"core.genrltxt", index}, parser.readString());
  408. index += 1;
  409. }
  410. while (parser.endLine());
  411. }
  412. {
  413. CLegacyConfigParser parser("DATA/HELP.TXT");
  414. size_t index = 0;
  415. do
  416. {
  417. std::string first = parser.readString();
  418. std::string second = parser.readString();
  419. registerString("core.help." + std::to_string(index) + ".hover", first);
  420. registerString("core.help." + std::to_string(index) + ".help", second);
  421. index += 1;
  422. }
  423. while (parser.endLine());
  424. }
  425. {
  426. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  427. size_t index = 0;
  428. do
  429. {
  430. std::string color = parser.readString();
  431. registerString({"core.plcolors", index}, color);
  432. color[0] = toupper(color[0]);
  433. registerString({"vcmi.capitalColors", index}, color);
  434. index += 1;
  435. }
  436. while (parser.endLine());
  437. }
  438. {
  439. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  440. //skip header
  441. parser.endLine();
  442. for (size_t i = 0; i < 6; ++i)
  443. {
  444. registerString({"core.seerhut.empty", i}, parser.readString());
  445. }
  446. parser.endLine();
  447. for (size_t i = 0; i < 9; ++i) //9 types of quests
  448. {
  449. std::string questName = CQuest::missionName(CQuest::Emission(1+i));
  450. for (size_t j = 0; j < 5; ++j)
  451. {
  452. std::string questState = CQuest::missionState(j);
  453. parser.readString(); //front description
  454. for (size_t k = 0; k < 6; ++k)
  455. {
  456. registerString({"core.seerhut.quest", questName, questState, k}, parser.readString());
  457. }
  458. parser.endLine();
  459. }
  460. }
  461. for (size_t k = 0; k < 6; ++k) //Time limit
  462. {
  463. registerString({"core.seerhut.time", k}, parser.readString());
  464. }
  465. parser.endLine();
  466. parser.endLine(); // empty line
  467. parser.endLine(); // header
  468. for (size_t i = 0; i < 48; ++i)
  469. {
  470. registerString({"core.seerhut.names", i}, parser.readString());
  471. parser.endLine();
  472. }
  473. }
  474. {
  475. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  476. //skip header
  477. parser.endLine();
  478. std::string text;
  479. size_t campaignsCount = 0;
  480. do
  481. {
  482. text = parser.readString();
  483. if (!text.empty())
  484. {
  485. registerString({"core.camptext.names", campaignsCount}, text);
  486. campaignsCount += 1;
  487. }
  488. }
  489. while (parser.endLine() && !text.empty());
  490. for (size_t campaign=0; campaign<campaignsCount; campaign++)
  491. {
  492. size_t region = 0;
  493. do // skip empty space and header
  494. {
  495. text = parser.readString();
  496. }
  497. while (parser.endLine() && text.empty());
  498. do
  499. {
  500. text = parser.readString();
  501. if (!text.empty())
  502. {
  503. registerString({"core.camptext.regions", std::to_string(campaign), region}, text);
  504. region += 1;
  505. }
  506. }
  507. while (parser.endLine() && !text.empty());
  508. scenariosCountPerCampaign.push_back(region);
  509. }
  510. }
  511. if (VLC->modh->modules.COMMANDERS)
  512. {
  513. if(CResourceHandler::get()->existsResource(ResourceID("DATA/ZNPC00.TXT", EResType::TEXT)))
  514. readToVector("vcmi.znpc00", "DATA/ZNPC00.TXT" );
  515. }
  516. }
  517. int32_t CGeneralTextHandler::pluralText(const int32_t textIndex, const int32_t count) const
  518. {
  519. if(textIndex == 0)
  520. return 0;
  521. else if(textIndex < 0)
  522. return -textIndex;
  523. else if(count == 1)
  524. return textIndex;
  525. else
  526. return textIndex + 1;
  527. }
  528. void CGeneralTextHandler::dumpAllTexts()
  529. {
  530. auto escapeString = [](std::string input)
  531. {
  532. boost::replace_all(input, "\\", "\\\\");
  533. boost::replace_all(input, "\n", "\\n");
  534. boost::replace_all(input, "\r", "\\r");
  535. boost::replace_all(input, "\t", "\\t");
  536. boost::replace_all(input, "\"", "\\\"");
  537. return input;
  538. };
  539. logGlobal->info("BEGIN TEXT EXPORT");
  540. for ( auto const & entry : stringsLocalizations)
  541. if (stringsOverrides.count(entry.first) == 0)
  542. logGlobal->info("\"%s\" : \"%s\",", entry.first, escapeString(entry.second));
  543. for ( auto const & entry : stringsOverrides)
  544. logGlobal->info("\"%s\" : \"%s\",", entry.first, escapeString(entry.second));
  545. logGlobal->info("END TEXT EXPORT");
  546. }
  547. size_t CGeneralTextHandler::getCampaignLength(size_t campaignID) const
  548. {
  549. assert(campaignID < scenariosCountPerCampaign.size());
  550. if(campaignID < scenariosCountPerCampaign.size())
  551. return scenariosCountPerCampaign[campaignID];
  552. return 0;
  553. }
  554. std::string CGeneralTextHandler::getInstalledLanguage()
  555. {
  556. auto explicitSetting = settings["general"]["language"].String();
  557. if (explicitSetting != "auto")
  558. return explicitSetting;
  559. return settings["session"]["language"].String();
  560. }
  561. std::string CGeneralTextHandler::getInstalledEncoding()
  562. {
  563. auto explicitSetting = settings["general"]["encoding"].String();
  564. if (explicitSetting != "auto")
  565. return explicitSetting;
  566. return settings["session"]["encoding"].String();
  567. }
  568. std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(std::string const & prefix)
  569. {
  570. std::vector<std::string> result;
  571. for (auto const & entry : stringsLocalizations)
  572. {
  573. if(boost::algorithm::starts_with(entry.first, prefix))
  574. result.push_back(entry.first);
  575. }
  576. return result;
  577. }
  578. LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::string const & basePath):
  579. owner(owner),
  580. basePath(basePath)
  581. {}
  582. std::string LegacyTextContainer::operator[](size_t index) const
  583. {
  584. return owner.translate(basePath, index);
  585. }
  586. LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string const & basePath):
  587. owner(owner),
  588. basePath(basePath)
  589. {}
  590. std::pair<std::string, std::string> LegacyHelpContainer::operator[](size_t index) const
  591. {
  592. return {
  593. owner.translate(basePath + "." + std::to_string(index) + ".hover"),
  594. owner.translate(basePath + "." + std::to_string(index) + ".help")
  595. };
  596. }
  597. VCMI_LIB_NAMESPACE_END