CGeneralTextHandler.cpp 22 KB

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