CGeneralTextHandler.cpp 22 KB

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