CGeneralTextHandler.cpp 22 KB

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