CGeneralTextHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. tcommands (*this, "core.tcommand" ),
  101. hcommands (*this, "core.hallinfo" ),
  102. fcommands (*this, "core.castinfo" ),
  103. advobtxt (*this, "core.advevent" ),
  104. restypes (*this, "core.restypes" ),
  105. overview (*this, "core.overview" ),
  106. arraytxt (*this, "core.arraytxt" ),
  107. primarySkillNames(*this, "core.priskill" ),
  108. jktexts (*this, "core.jktext" ),
  109. tavernInfo (*this, "core.tvrninfo" ),
  110. turnDurations (*this, "core.turndur" ),
  111. heroscrn (*this, "core.heroscrn" ),
  112. tentColors (*this, "core.tentcolr" ),
  113. levels (*this, "core.skilllev" ),
  114. zelp (*this, "core.help" ),
  115. allTexts (*this, "core.genrltxt" ),
  116. // pseudo-array, that don't have H3 file with same name
  117. seerEmpty (*this, "core.seerhut.empty" ),
  118. seerNames (*this, "core.seerhut.names" ),
  119. capColors (*this, "vcmi.capitalColors" )
  120. {
  121. readToVector("core.vcdesc", "DATA/VCDESC.TXT" );
  122. readToVector("core.lcdesc", "DATA/LCDESC.TXT" );
  123. readToVector("core.tcommand", "DATA/TCOMMAND.TXT" );
  124. readToVector("core.hallinfo", "DATA/HALLINFO.TXT" );
  125. readToVector("core.castinfo", "DATA/CASTINFO.TXT" );
  126. readToVector("core.advevent", "DATA/ADVEVENT.TXT" );
  127. readToVector("core.restypes", "DATA/RESTYPES.TXT" );
  128. readToVector("core.randsign", "DATA/RANDSIGN.TXT" );
  129. readToVector("core.overview", "DATA/OVERVIEW.TXT" );
  130. readToVector("core.arraytxt", "DATA/ARRAYTXT.TXT" );
  131. readToVector("core.priskill", "DATA/PRISKILL.TXT" );
  132. readToVector("core.plcolors", "DATA/PLCOLORS.TXT" );
  133. readToVector("core.jktext", "DATA/JKTEXT.TXT" );
  134. readToVector("core.tvrninfo", "DATA/TVRNINFO.TXT" );
  135. readToVector("core.turndur", "DATA/TURNDUR.TXT" );
  136. readToVector("core.heroscrn", "DATA/HEROSCRN.TXT" );
  137. readToVector("core.tentcolr", "DATA/TENTCOLR.TXT" );
  138. readToVector("core.skilllev", "DATA/SKILLLEV.TXT" );
  139. readToVector("core.minename", "DATA/MINENAME.TXT" );
  140. readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
  141. readToVector("core.xtrainfo", "DATA/XTRAINFO.TXT" );
  142. {
  143. CLegacyConfigParser parser(TextPath::builtin("DATA/RANDTVRN.TXT"));
  144. parser.endLine();
  145. size_t index = 0;
  146. do
  147. {
  148. std::string line = parser.readString();
  149. if(!line.empty())
  150. {
  151. registerString("core", {"core.randtvrn", index}, line);
  152. index += 1;
  153. }
  154. }
  155. while (parser.endLine());
  156. }
  157. {
  158. CLegacyConfigParser parser(TextPath::builtin("DATA/GENRLTXT.TXT"));
  159. parser.endLine();
  160. size_t index = 0;
  161. do
  162. {
  163. registerString("core", {"core.genrltxt", index}, parser.readString());
  164. index += 1;
  165. }
  166. while (parser.endLine());
  167. }
  168. {
  169. CLegacyConfigParser parser(TextPath::builtin("DATA/HELP.TXT"));
  170. size_t index = 0;
  171. do
  172. {
  173. std::string first = parser.readString();
  174. std::string second = parser.readString();
  175. registerString("core", "core.help." + std::to_string(index) + ".hover", first);
  176. registerString("core", "core.help." + std::to_string(index) + ".help", second);
  177. index += 1;
  178. }
  179. while (parser.endLine());
  180. }
  181. {
  182. CLegacyConfigParser parser(TextPath::builtin("DATA/SEERHUT.TXT"));
  183. //skip header
  184. parser.endLine();
  185. for (size_t i = 0; i < 6; ++i)
  186. {
  187. registerString("core", {"core.seerhut.empty", i}, parser.readString());
  188. }
  189. parser.endLine();
  190. for (size_t i = 0; i < 9; ++i) //9 types of quests
  191. {
  192. EQuestMission missionID = static_cast<EQuestMission>(i+1);
  193. std::string questName = CQuest::missionName(missionID);
  194. for (size_t j = 0; j < 5; ++j)
  195. {
  196. std::string questState = CQuest::missionState(j);
  197. parser.readString(); //front description
  198. for (size_t k = 0; k < 6; ++k)
  199. {
  200. registerString("core", {"core.seerhut.quest", questName, questState, k}, parser.readString());
  201. }
  202. parser.endLine();
  203. }
  204. }
  205. for (size_t k = 0; k < 6; ++k) //Time limit
  206. {
  207. registerString("core", {"core.seerhut.time", k}, parser.readString());
  208. }
  209. parser.endLine();
  210. parser.endLine(); // empty line
  211. parser.endLine(); // header
  212. for (size_t i = 0; i < 48; ++i)
  213. {
  214. registerString("core", {"core.seerhut.names", i}, parser.readString());
  215. parser.endLine();
  216. }
  217. }
  218. {
  219. CLegacyConfigParser parser(TextPath::builtin("DATA/CAMPTEXT.TXT"));
  220. //skip header
  221. parser.endLine();
  222. std::string text;
  223. size_t campaignsCount = 0;
  224. do
  225. {
  226. text = parser.readString();
  227. if (!text.empty())
  228. {
  229. registerString("core", {"core.camptext.names", campaignsCount}, text);
  230. campaignsCount += 1;
  231. }
  232. }
  233. while (parser.endLine() && !text.empty());
  234. for (size_t campaign=0; campaign<campaignsCount; campaign++)
  235. {
  236. size_t region = 0;
  237. do // skip empty space and header
  238. {
  239. text = parser.readString();
  240. }
  241. while (parser.endLine() && text.empty());
  242. do
  243. {
  244. text = parser.readString();
  245. if (!text.empty())
  246. {
  247. registerString("core", {"core.camptext.regions", std::to_string(campaign), region}, text);
  248. region += 1;
  249. }
  250. }
  251. while (parser.endLine() && !text.empty());
  252. }
  253. }
  254. }
  255. int32_t CGeneralTextHandler::pluralText(const int32_t textIndex, const int32_t count) const
  256. {
  257. if(textIndex == 0)
  258. return 0;
  259. if(textIndex < 0)
  260. return -textIndex;
  261. if(count == 1)
  262. return textIndex;
  263. return textIndex + 1;
  264. }
  265. std::string CGeneralTextHandler::getPreferredLanguage()
  266. {
  267. assert(!settings["general"]["language"].String().empty());
  268. return settings["general"]["language"].String();
  269. }
  270. std::string CGeneralTextHandler::getInstalledLanguage()
  271. {
  272. assert(!settings["session"]["language"].String().empty());
  273. return settings["session"]["language"].String();
  274. }
  275. std::string CGeneralTextHandler::getInstalledEncoding()
  276. {
  277. assert(!settings["session"]["encoding"].String().empty());
  278. return settings["session"]["encoding"].String();
  279. }
  280. std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(const std::string & prefix)
  281. {
  282. std::lock_guard globalLock(globalTextMutex);
  283. std::vector<std::string> result;
  284. for(const auto & entry : stringsLocalizations)
  285. {
  286. if(boost::algorithm::starts_with(entry.first, prefix))
  287. result.push_back(entry.first);
  288. }
  289. return result;
  290. }
  291. LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::string basePath):
  292. owner(owner),
  293. basePath(std::move(basePath))
  294. {}
  295. std::string LegacyTextContainer::operator[](size_t index) const
  296. {
  297. return owner.translate(basePath, index);
  298. }
  299. LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string basePath):
  300. owner(owner),
  301. basePath(std::move(basePath))
  302. {}
  303. std::pair<std::string, std::string> LegacyHelpContainer::operator[](size_t index) const
  304. {
  305. return {
  306. owner.translate(basePath + "." + std::to_string(index) + ".hover"),
  307. owner.translate(basePath + "." + std::to_string(index) + ".help")
  308. };
  309. }
  310. VCMI_LIB_NAMESPACE_END