CGeneralTextHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "texts/CGeneralTextHandler.h"
  12. #include "CLegacyConfigParser.h"
  13. #include "CConfigHandler.h"
  14. #include "IGameSettings.h"
  15. #include "Languages.h"
  16. #include "../filesystem/Filesystem.h"
  17. #include "../mapObjects/CQuest.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. /// Detects language and encoding of H3 text files based on matching against pregenerated footprints of H3 file
  20. void CGeneralTextHandler::detectInstallParameters()
  21. {
  22. using LanguageFootprint = std::array<double, 16>;
  23. static const std::array<LanguageFootprint, 7> knownFootprints =
  24. { {
  25. { { 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 } },
  26. { { 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 } },
  27. { { 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 } },
  28. { { 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 } },
  29. { { 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 } },
  30. { { 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 } },
  31. { { 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 } },
  32. } };
  33. static const std::array<std::string, 7> knownLanguages =
  34. { {
  35. "chinese",
  36. "english",
  37. "french",
  38. "german",
  39. "polish",
  40. "russian",
  41. "ukrainian"
  42. } };
  43. if(!CResourceHandler::get("core")->existsResource(TextPath::builtin("DATA/GENRLTXT.TXT")))
  44. {
  45. Settings language = settings.write["session"]["language"];
  46. language->String() = "english";
  47. Settings confidence = settings.write["session"]["languageDeviation"];
  48. confidence->Float() = 1.0;
  49. Settings encoding = settings.write["session"]["encoding"];
  50. encoding->String() = Languages::getLanguageOptions("english").encoding;
  51. return;
  52. }
  53. // load file that will be used for footprint generation
  54. // this is one of the most text-heavy files in game and consists solely from translated texts
  55. auto resource = CResourceHandler::get("core")->load(TextPath::builtin("DATA/GENRLTXT.TXT"));
  56. std::array<size_t, 256> charCount{};
  57. std::array<double, 16> footprint{};
  58. std::array<double, 6> deviations{};
  59. auto data = resource->readAll();
  60. // compute how often each character occurs in input file
  61. for (si64 i = 0; i < data.second; ++i)
  62. charCount[data.first[i]] += 1;
  63. // and convert computed data into weights
  64. // to reduce amount of data, group footprint data into 16-char blocks.
  65. // While this will reduce precision, it should not affect output
  66. // since we expect only tiny differences compared to reference footprints
  67. for (size_t i = 0; i < 256; ++i)
  68. footprint[i/16] += static_cast<double>(charCount[i]) / data.second;
  69. logGlobal->debug("Language footprint: %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f",
  70. footprint[0], footprint[1], footprint[2], footprint[3], footprint[4], footprint[5], footprint[6], footprint[7],
  71. footprint[8], footprint[9], footprint[10], footprint[11], footprint[12], footprint[13], footprint[14], footprint[15]
  72. );
  73. for (size_t i = 0; i < deviations.size(); ++i)
  74. {
  75. for (size_t j = 0; j < footprint.size(); ++j)
  76. deviations[i] += std::abs((footprint[j] - knownFootprints[i][j]));
  77. }
  78. size_t bestIndex = boost::range::min_element(deviations) - deviations.begin();
  79. for (size_t i = 0; i < deviations.size(); ++i)
  80. logGlobal->debug("Comparing to %s: %f", knownLanguages[i], deviations[i]);
  81. Settings language = settings.write["session"]["language"];
  82. language->String() = knownLanguages[bestIndex];
  83. Settings confidence = settings.write["session"]["languageDeviation"];
  84. confidence->Float() = deviations[bestIndex];
  85. Settings encoding = settings.write["session"]["encoding"];
  86. encoding->String() = Languages::getLanguageOptions(knownLanguages[bestIndex]).encoding;
  87. }
  88. void CGeneralTextHandler::readToVector(const std::string & sourceID, const std::string & sourceName)
  89. {
  90. CLegacyConfigParser parser(TextPath::builtin(sourceName));
  91. size_t index = 0;
  92. do
  93. {
  94. registerString( "core", {sourceID, index}, parser.readString());
  95. index += 1;
  96. }
  97. while (parser.endLine());
  98. }
  99. CGeneralTextHandler::CGeneralTextHandler():
  100. victoryConditions(*this, "core.vcdesc" ),
  101. lossConditions (*this, "core.lcdesc" ),
  102. colors (*this, "core.plcolors" ),
  103. tcommands (*this, "core.tcommand" ),
  104. hcommands (*this, "core.hallinfo" ),
  105. fcommands (*this, "core.castinfo" ),
  106. advobtxt (*this, "core.advevent" ),
  107. restypes (*this, "core.restypes" ),
  108. randsign (*this, "core.randsign" ),
  109. overview (*this, "core.overview" ),
  110. arraytxt (*this, "core.arraytxt" ),
  111. primarySkillNames(*this, "core.priskill" ),
  112. jktexts (*this, "core.jktext" ),
  113. tavernInfo (*this, "core.tvrninfo" ),
  114. tavernRumors (*this, "core.randtvrn" ),
  115. turnDurations (*this, "core.turndur" ),
  116. heroscrn (*this, "core.heroscrn" ),
  117. tentColors (*this, "core.tentcolr" ),
  118. levels (*this, "core.skilllev" ),
  119. zelp (*this, "core.help" ),
  120. allTexts (*this, "core.genrltxt" ),
  121. // pseudo-array, that don't have H3 file with same name
  122. seerEmpty (*this, "core.seerhut.empty" ),
  123. seerNames (*this, "core.seerhut.names" ),
  124. capColors (*this, "vcmi.capitalColors" ),
  125. znpc00 (*this, "vcmi.znpc00" ), // technically - wog
  126. qeModCommands (*this, "vcmi.quickExchange" )
  127. {
  128. readToVector("core.vcdesc", "DATA/VCDESC.TXT" );
  129. readToVector("core.lcdesc", "DATA/LCDESC.TXT" );
  130. readToVector("core.tcommand", "DATA/TCOMMAND.TXT" );
  131. readToVector("core.hallinfo", "DATA/HALLINFO.TXT" );
  132. readToVector("core.castinfo", "DATA/CASTINFO.TXT" );
  133. readToVector("core.advevent", "DATA/ADVEVENT.TXT" );
  134. readToVector("core.restypes", "DATA/RESTYPES.TXT" );
  135. readToVector("core.randsign", "DATA/RANDSIGN.TXT" );
  136. readToVector("core.overview", "DATA/OVERVIEW.TXT" );
  137. readToVector("core.arraytxt", "DATA/ARRAYTXT.TXT" );
  138. readToVector("core.priskill", "DATA/PRISKILL.TXT" );
  139. readToVector("core.plcolors", "DATA/PLCOLORS.TXT" );
  140. readToVector("core.jktext", "DATA/JKTEXT.TXT" );
  141. readToVector("core.tvrninfo", "DATA/TVRNINFO.TXT" );
  142. readToVector("core.turndur", "DATA/TURNDUR.TXT" );
  143. readToVector("core.heroscrn", "DATA/HEROSCRN.TXT" );
  144. readToVector("core.tentcolr", "DATA/TENTCOLR.TXT" );
  145. readToVector("core.skilllev", "DATA/SKILLLEV.TXT" );
  146. readToVector("core.cmpmusic", "DATA/CMPMUSIC.TXT" );
  147. readToVector("core.minename", "DATA/MINENAME.TXT" );
  148. readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
  149. readToVector("core.xtrainfo", "DATA/XTRAINFO.TXT" );
  150. static const std::string QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
  151. if (CResourceHandler::get()->existsResource(TextPath::builtin(QE_MOD_COMMANDS)))
  152. readToVector("vcmi.quickExchange", QE_MOD_COMMANDS);
  153. {
  154. CLegacyConfigParser parser(TextPath::builtin("DATA/RANDTVRN.TXT"));
  155. parser.endLine();
  156. size_t index = 0;
  157. do
  158. {
  159. std::string line = parser.readString();
  160. if(!line.empty())
  161. {
  162. registerString("core", {"core.randtvrn", index}, line);
  163. index += 1;
  164. }
  165. }
  166. while (parser.endLine());
  167. }
  168. {
  169. CLegacyConfigParser parser(TextPath::builtin("DATA/GENRLTXT.TXT"));
  170. parser.endLine();
  171. size_t index = 0;
  172. do
  173. {
  174. registerString("core", {"core.genrltxt", index}, parser.readString());
  175. index += 1;
  176. }
  177. while (parser.endLine());
  178. }
  179. {
  180. CLegacyConfigParser parser(TextPath::builtin("DATA/HELP.TXT"));
  181. size_t index = 0;
  182. do
  183. {
  184. std::string first = parser.readString();
  185. std::string second = parser.readString();
  186. registerString("core", "core.help." + std::to_string(index) + ".hover", first);
  187. registerString("core", "core.help." + std::to_string(index) + ".help", second);
  188. index += 1;
  189. }
  190. while (parser.endLine());
  191. }
  192. {
  193. CLegacyConfigParser parser(TextPath::builtin("DATA/SEERHUT.TXT"));
  194. //skip header
  195. parser.endLine();
  196. for (size_t i = 0; i < 6; ++i)
  197. {
  198. registerString("core", {"core.seerhut.empty", i}, parser.readString());
  199. }
  200. parser.endLine();
  201. for (size_t i = 0; i < 9; ++i) //9 types of quests
  202. {
  203. EQuestMission missionID = static_cast<EQuestMission>(i+1);
  204. std::string questName = CQuest::missionName(missionID);
  205. for (size_t j = 0; j < 5; ++j)
  206. {
  207. std::string questState = CQuest::missionState(j);
  208. parser.readString(); //front description
  209. for (size_t k = 0; k < 6; ++k)
  210. {
  211. registerString("core", {"core.seerhut.quest", questName, questState, k}, parser.readString());
  212. }
  213. parser.endLine();
  214. }
  215. }
  216. for (size_t k = 0; k < 6; ++k) //Time limit
  217. {
  218. registerString("core", {"core.seerhut.time", k}, parser.readString());
  219. }
  220. parser.endLine();
  221. parser.endLine(); // empty line
  222. parser.endLine(); // header
  223. for (size_t i = 0; i < 48; ++i)
  224. {
  225. registerString("core", {"core.seerhut.names", i}, parser.readString());
  226. parser.endLine();
  227. }
  228. }
  229. {
  230. CLegacyConfigParser parser(TextPath::builtin("DATA/CAMPTEXT.TXT"));
  231. //skip header
  232. parser.endLine();
  233. std::string text;
  234. size_t campaignsCount = 0;
  235. do
  236. {
  237. text = parser.readString();
  238. if (!text.empty())
  239. {
  240. registerString("core", {"core.camptext.names", campaignsCount}, text);
  241. campaignsCount += 1;
  242. }
  243. }
  244. while (parser.endLine() && !text.empty());
  245. for (size_t campaign=0; campaign<campaignsCount; campaign++)
  246. {
  247. size_t region = 0;
  248. do // skip empty space and header
  249. {
  250. text = parser.readString();
  251. }
  252. while (parser.endLine() && text.empty());
  253. do
  254. {
  255. text = parser.readString();
  256. if (!text.empty())
  257. {
  258. registerString("core", {"core.camptext.regions", std::to_string(campaign), region}, text);
  259. region += 1;
  260. }
  261. }
  262. while (parser.endLine() && !text.empty());
  263. scenariosCountPerCampaign.push_back(region);
  264. }
  265. }
  266. if (VLC->engineSettings()->getBoolean(EGameSettings::MODULE_COMMANDERS))
  267. {
  268. if(CResourceHandler::get()->existsResource(TextPath::builtin("DATA/ZNPC00.TXT")))
  269. readToVector("vcmi.znpc00", "DATA/ZNPC00.TXT" );
  270. }
  271. }
  272. int32_t CGeneralTextHandler::pluralText(const int32_t textIndex, const int32_t count) const
  273. {
  274. if(textIndex == 0)
  275. return 0;
  276. if(textIndex < 0)
  277. return -textIndex;
  278. if(count == 1)
  279. return textIndex;
  280. return textIndex + 1;
  281. }
  282. size_t CGeneralTextHandler::getCampaignLength(size_t campaignID) const
  283. {
  284. assert(campaignID < scenariosCountPerCampaign.size());
  285. if(campaignID < scenariosCountPerCampaign.size())
  286. return scenariosCountPerCampaign[campaignID];
  287. return 0;
  288. }
  289. std::string CGeneralTextHandler::getPreferredLanguage()
  290. {
  291. assert(!settings["general"]["language"].String().empty());
  292. return settings["general"]["language"].String();
  293. }
  294. std::string CGeneralTextHandler::getInstalledLanguage()
  295. {
  296. assert(!settings["session"]["language"].String().empty());
  297. return settings["session"]["language"].String();
  298. }
  299. std::string CGeneralTextHandler::getInstalledEncoding()
  300. {
  301. assert(!settings["session"]["encoding"].String().empty());
  302. return settings["session"]["encoding"].String();
  303. }
  304. std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(const std::string & prefix)
  305. {
  306. std::lock_guard globalLock(globalTextMutex);
  307. std::vector<std::string> result;
  308. for(const auto & entry : stringsLocalizations)
  309. {
  310. if(boost::algorithm::starts_with(entry.first, prefix))
  311. result.push_back(entry.first);
  312. }
  313. return result;
  314. }
  315. LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::string basePath):
  316. owner(owner),
  317. basePath(std::move(basePath))
  318. {}
  319. std::string LegacyTextContainer::operator[](size_t index) const
  320. {
  321. return owner.translate(basePath, index);
  322. }
  323. LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string basePath):
  324. owner(owner),
  325. basePath(std::move(basePath))
  326. {}
  327. std::pair<std::string, std::string> LegacyHelpContainer::operator[](size_t index) const
  328. {
  329. return {
  330. owner.translate(basePath + "." + std::to_string(index) + ".hover"),
  331. owner.translate(basePath + "." + std::to_string(index) + ".help")
  332. };
  333. }
  334. VCMI_LIB_NAMESPACE_END