CGeneralTextHandler.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 "Terrain.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. static std::string getSelectedEncoding()
  92. {
  93. return settings["general"]["encoding"].String();
  94. }
  95. std::string Unicode::toUnicode(const std::string &text)
  96. {
  97. return toUnicode(text, getSelectedEncoding());
  98. }
  99. std::string Unicode::toUnicode(const std::string &text, const std::string &encoding)
  100. {
  101. return boost::locale::conv::to_utf<char>(text, encoding);
  102. }
  103. std::string Unicode::fromUnicode(const std::string & text)
  104. {
  105. return fromUnicode(text, getSelectedEncoding());
  106. }
  107. std::string Unicode::fromUnicode(const std::string &text, const std::string &encoding)
  108. {
  109. return boost::locale::conv::from_utf<char>(text, encoding);
  110. }
  111. void Unicode::trimRight(std::string & text, const size_t amount)
  112. {
  113. if(text.empty())
  114. return;
  115. //todo: more efficient algorithm
  116. for(int i = 0; i< amount; i++){
  117. auto b = text.begin();
  118. auto e = text.end();
  119. size_t lastLen = 0;
  120. size_t len = 0;
  121. while (b != e) {
  122. lastLen = len;
  123. size_t n = getCharacterSize(*b);
  124. if(!isValidCharacter(&(*b),e-b))
  125. {
  126. logGlobal->error("Invalid UTF8 sequence");
  127. break;//invalid sequence will be trimmed
  128. }
  129. len += n;
  130. b += n;
  131. }
  132. text.resize(lastLen);
  133. }
  134. }
  135. //Helper for string -> float conversion
  136. class LocaleWithComma: public std::numpunct<char>
  137. {
  138. protected:
  139. char do_decimal_point() const override
  140. {
  141. return ',';
  142. }
  143. };
  144. CLegacyConfigParser::CLegacyConfigParser(std::string URI)
  145. {
  146. init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT)));
  147. }
  148. CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input)
  149. {
  150. init(input);
  151. }
  152. void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input)
  153. {
  154. data.reset(new char[input->getSize()]);
  155. input->read((ui8*)data.get(), input->getSize());
  156. curr = data.get();
  157. end = curr + input->getSize();
  158. }
  159. std::string CLegacyConfigParser::extractQuotedPart()
  160. {
  161. assert(*curr == '\"');
  162. curr++; // skip quote
  163. char * begin = curr;
  164. while (curr != end && *curr != '\"' && *curr != '\t')
  165. curr++;
  166. return std::string(begin, curr++); //increment curr to close quote
  167. }
  168. std::string CLegacyConfigParser::extractQuotedString()
  169. {
  170. assert(*curr == '\"');
  171. std::string ret;
  172. while (true)
  173. {
  174. ret += extractQuotedPart();
  175. // double quote - add it to string and continue quoted part
  176. if (curr < end && *curr == '\"')
  177. {
  178. ret += '\"';
  179. }
  180. //extract normal part
  181. else if(curr < end && *curr != '\t' && *curr != '\r')
  182. {
  183. char * begin = curr;
  184. while (curr < end && *curr != '\t' && *curr != '\r' && *curr != '\"')//find end of string or next quoted part start
  185. curr++;
  186. ret += std::string(begin, curr);
  187. if(curr>=end || *curr != '\"')
  188. return ret;
  189. }
  190. else // end of string
  191. return ret;
  192. }
  193. }
  194. std::string CLegacyConfigParser::extractNormalString()
  195. {
  196. char * begin = curr;
  197. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  198. curr++;
  199. return std::string(begin, curr);
  200. }
  201. std::string CLegacyConfigParser::readRawString()
  202. {
  203. if (curr >= end || *curr == '\n')
  204. return "";
  205. std::string ret;
  206. if (*curr == '\"')
  207. ret = extractQuotedString();// quoted text - find closing quote
  208. else
  209. ret = extractNormalString();//string without quotes - copy till \t or \r
  210. curr++;
  211. return ret;
  212. }
  213. std::string CLegacyConfigParser::readString()
  214. {
  215. // do not convert strings that are already in ASCII - this will only slow down loading process
  216. std::string str = readRawString();
  217. if (Unicode::isValidASCII(str))
  218. return str;
  219. return Unicode::toUnicode(str);
  220. }
  221. float CLegacyConfigParser::readNumber()
  222. {
  223. std::string input = readRawString();
  224. std::istringstream stream(input);
  225. if(input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  226. stream.imbue(std::locale(std::locale(), new LocaleWithComma()));
  227. float result;
  228. if ( !(stream >> result) )
  229. return 0;
  230. return result;
  231. }
  232. bool CLegacyConfigParser::isNextEntryEmpty() const
  233. {
  234. char * nextSymbol = curr;
  235. while (nextSymbol < end && *nextSymbol == ' ')
  236. nextSymbol++; //find next meaningfull symbol
  237. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  238. }
  239. bool CLegacyConfigParser::endLine()
  240. {
  241. while (curr < end && *curr != '\n')
  242. readString();
  243. curr++;
  244. return curr < end;
  245. }
  246. void CGeneralTextHandler::readToVector(std::string sourceID, std::string sourceName)
  247. {
  248. CLegacyConfigParser parser(sourceName);
  249. size_t index = 0;
  250. do
  251. {
  252. registerH3String(sourceID, index, parser.readString());
  253. index += 1;
  254. }
  255. while (parser.endLine());
  256. }
  257. const std::string & CGeneralTextHandler::translate(const std::string & identifier, size_t index) const
  258. {
  259. return translate(identifier + std::to_string(index));
  260. }
  261. const std::string & CGeneralTextHandler::translate(const std::string & identifier) const
  262. {
  263. return deserialize(identifier);
  264. }
  265. const std::string & CGeneralTextHandler::serialize(const std::string & identifier) const
  266. {
  267. assert(stringsIdentifiers.count(identifier));
  268. return stringsIdentifiers.at(identifier);
  269. }
  270. const std::string & CGeneralTextHandler::deserialize(const std::string & identifier) const
  271. {
  272. if (stringsLocalizations.count(identifier))
  273. return stringsLocalizations.at(identifier);
  274. logGlobal->error("Unable to find localization for string '%s'", identifier);
  275. return identifier;
  276. }
  277. void CGeneralTextHandler::registerH3String(const std::string & file, size_t index, const std::string & localized)
  278. {
  279. registerString(file + '.' + std::to_string(index), localized);
  280. }
  281. void CGeneralTextHandler::registerString(const std::string & UID, const std::string & localized)
  282. {
  283. stringsIdentifiers[localized] = UID;
  284. stringsLocalizations[UID] = localized;
  285. }
  286. CGeneralTextHandler::CGeneralTextHandler():
  287. victoryConditions(*this, "core.vcdesc" ),
  288. lossCondtions (*this, "core.lcdesc" ),
  289. colors (*this, "core.plcolors" ),
  290. tcommands (*this, "core.tcommand" ),
  291. hcommands (*this, "core.hallinfo" ),
  292. fcommands (*this, "core.castinfo" ),
  293. advobtxt (*this, "core.advevent" ),
  294. xtrainfo (*this, "core.xtrainfo" ),
  295. restypes (*this, "core.restypes" ),
  296. terrainNames (*this, "core.terrname" ),
  297. randsign (*this, "core.randsign" ),
  298. creGens (*this, "core.crgen1" ),
  299. creGens4 (*this, "core.crgen4" ),
  300. overview (*this, "core.overview" ),
  301. arraytxt (*this, "core.arraytxt" ),
  302. primarySkillNames(*this, "core.priskill" ),
  303. jktexts (*this, "core.jktext" ),
  304. tavernInfo (*this, "core.tvrninfo" ),
  305. tavernRumors (*this, "core.randtvrn" ),
  306. turnDurations (*this, "core.turndur" ),
  307. heroscrn (*this, "core.heroscrn" ),
  308. tentColors (*this, "core.tentcolr" ),
  309. levels (*this, "core.skilllev" ),
  310. zelp (*this, "core.help" ),
  311. allTexts (*this, "core.genrltxt" ),
  312. // pseudo-array, that don't have H3 file with same name
  313. seerEmpty (*this, "core.seerhut.empty" ),
  314. seerNames (*this, "core.seerhut.names" ),
  315. capColors (*this, "vcmi.capitalColors" ),
  316. znpc00 (*this, "vcmi.znpc00" ), // technically - wog
  317. qeModCommands (*this, "vcmi.quickExchange" )
  318. {
  319. readToVector("core.vcdesc", "DATA/VCDESC.TXT" );
  320. readToVector("core.lcdesc", "DATA/LCDESC.TXT" );
  321. readToVector("core.tcommand", "DATA/TCOMMAND.TXT" );
  322. readToVector("core.hallinfo", "DATA/HALLINFO.TXT" );
  323. readToVector("core.castinfo", "DATA/CASTINFO.TXT" );
  324. readToVector("core.advevent", "DATA/ADVEVENT.TXT" );
  325. readToVector("core.xtrainfo", "DATA/XTRAINFO.TXT" );
  326. readToVector("core.restypes", "DATA/RESTYPES.TXT" );
  327. readToVector("core.terrname", "DATA/TERRNAME.TXT" );
  328. readToVector("core.randsign", "DATA/RANDSIGN.TXT" );
  329. readToVector("core.crgen1", "DATA/CRGEN1.TXT" );
  330. readToVector("core.crgen4", "DATA/CRGEN4.TXT" );
  331. readToVector("core.overview", "DATA/OVERVIEW.TXT" );
  332. readToVector("core.arraytxt", "DATA/ARRAYTXT.TXT" );
  333. readToVector("core.priskill", "DATA/PRISKILL.TXT" );
  334. readToVector("core.jktext", "DATA/JKTEXT.TXT" );
  335. readToVector("core.tvrninfo", "DATA/TVRNINFO.TXT" );
  336. readToVector("core.turndur", "DATA/TURNDUR.TXT" );
  337. readToVector("core.heroscrn", "DATA/HEROSCRN.TXT" );
  338. readToVector("core.tentcolr", "DATA/TENTCOLR.TXT" );
  339. readToVector("core.skilllev", "DATA/SKILLLEV.TXT" );
  340. readToVector("core.cmpmusic", "DATA/CMPMUSIC.TXT" );
  341. readToVector("core.minename", "DATA/MINENAME.TXT" );
  342. readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
  343. static const char * QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
  344. if (CResourceHandler::get()->existsResource(ResourceID(QE_MOD_COMMANDS, EResType::TEXT)))
  345. readToVector("vcmi.quickExchange", QE_MOD_COMMANDS);
  346. auto vcmiTexts = JsonNode(ResourceID("config/translate.json", EResType::TEXT));
  347. for ( auto const & node : vcmiTexts.Struct())
  348. registerString(node.first, node.second.String());
  349. {
  350. CLegacyConfigParser parser("DATA/RANDTVRN.TXT");
  351. parser.endLine();
  352. size_t index = 0;
  353. do
  354. {
  355. std::string line = parser.readString();
  356. if (!line.empty())
  357. {
  358. registerH3String("core.randtvrn", index, line);
  359. index += 1;
  360. }
  361. }
  362. while (parser.endLine());
  363. }
  364. {
  365. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  366. parser.endLine();
  367. size_t index = 0;
  368. do
  369. {
  370. registerH3String("core.genrltxt", index, parser.readString());
  371. index += 1;
  372. }
  373. while (parser.endLine());
  374. }
  375. {
  376. CLegacyConfigParser parser("DATA/HELP.TXT");
  377. size_t index = 0;
  378. do
  379. {
  380. std::string first = parser.readString();
  381. std::string second = parser.readString();
  382. registerString("core.help." + std::to_string(index) + ".hover", first);
  383. registerString("core.help." + std::to_string(index) + ".help", second);
  384. index += 1;
  385. }
  386. while (parser.endLine());
  387. }
  388. {
  389. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  390. size_t index = 0;
  391. do
  392. {
  393. std::string color = parser.readString();
  394. registerH3String("core.plcolors", index, color);
  395. color[0] = toupper(color[0]);
  396. registerH3String("vcmi.capitalColors", index, color);
  397. index += 1;
  398. }
  399. while (parser.endLine());
  400. }
  401. {
  402. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  403. //skip header
  404. parser.endLine();
  405. for (int i = 0; i < 6; ++i)
  406. {
  407. registerH3String("core.seerhut.empty", i, parser.readString());
  408. }
  409. parser.endLine();
  410. for (int i = 0; i < 9; ++i) //9 types of quests
  411. {
  412. std::string questName = CQuest::missionName(CQuest::Emission(1+i));
  413. for (int j = 0; j < 5; ++j)
  414. {
  415. std::string questState = CQuest::missionState(j);
  416. parser.readString(); //front description
  417. for (int k = 0; k < 6; ++k)
  418. {
  419. registerH3String("core.seerhut.quest." + questName + "." + questState, k, parser.readString());
  420. }
  421. parser.endLine();
  422. }
  423. }
  424. for (int k = 0; k < 6; ++k) //Time limit
  425. {
  426. registerH3String("core.seerhut.time", k, parser.readString());
  427. }
  428. parser.endLine();
  429. parser.endLine(); // empty line
  430. parser.endLine(); // header
  431. for (int i = 0; i < 48; ++i)
  432. {
  433. registerH3String("core.seerhut.names", i, parser.readString());
  434. parser.endLine();
  435. }
  436. }
  437. {
  438. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  439. //skip header
  440. parser.endLine();
  441. std::string text;
  442. size_t campaignsCount = 0;
  443. do
  444. {
  445. text = parser.readString();
  446. if (!text.empty())
  447. {
  448. registerH3String("core.camptext.names", campaignsCount, text);
  449. campaignsCount += 1;
  450. }
  451. }
  452. while (parser.endLine() && !text.empty());
  453. for (size_t campaign=0; campaign<campaignsCount; campaign++)
  454. {
  455. size_t region = 0;
  456. do // skip empty space and header
  457. {
  458. text = parser.readString();
  459. }
  460. while (parser.endLine() && text.empty());
  461. do
  462. {
  463. text = parser.readString();
  464. if (!text.empty())
  465. {
  466. registerH3String("core.camptext.regions." + std::to_string(campaign), region, text);
  467. region += 1;
  468. }
  469. }
  470. while (parser.endLine() && !text.empty());
  471. scenariosCountPerCampaign.push_back(region);
  472. }
  473. }
  474. if (VLC->modh->modules.COMMANDERS)
  475. {
  476. if (CResourceHandler::get()->existsResource(ResourceID("DATA/ZNPC00.TXT", EResType::TEXT)))
  477. readToVector("vcmi.znpc00", "DATA/ZNPC00.TXT" );
  478. }
  479. }
  480. int32_t CGeneralTextHandler::pluralText(const int32_t textIndex, const int32_t count) const
  481. {
  482. if(textIndex == 0)
  483. return 0;
  484. else if(textIndex < 0)
  485. return -textIndex;
  486. else if(count == 1)
  487. return textIndex;
  488. else
  489. return textIndex + 1;
  490. }
  491. void CGeneralTextHandler::dumpAllTexts()
  492. {
  493. logGlobal->info("BEGIN TEXT EXPORT");
  494. for ( auto const & entry : stringsLocalizations)
  495. {
  496. auto cleanString = entry.second;
  497. boost::replace_all(cleanString, "\\", "\\\\");
  498. boost::replace_all(cleanString, "\n", "\\n");
  499. boost::replace_all(cleanString, "\r", "\\r");
  500. boost::replace_all(cleanString, "\t", "\\t");
  501. boost::replace_all(cleanString, "\"", "\\\"");
  502. logGlobal->info("\"%s\" : \"%s\",", entry.first, cleanString);
  503. }
  504. logGlobal->info("END TEXT EXPORT");
  505. }
  506. size_t CGeneralTextHandler::getCampaignLength(size_t campaignID) const
  507. {
  508. assert(campaignID < scenariosCountPerCampaign.size());
  509. if ( campaignID < scenariosCountPerCampaign.size())
  510. return scenariosCountPerCampaign[campaignID];
  511. return 0;
  512. }
  513. std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(std::string const & prefix)
  514. {
  515. std::vector<std::string> result;
  516. for (auto const & entry : stringsLocalizations)
  517. {
  518. if (boost::algorithm::starts_with(entry.first, prefix))
  519. result.push_back(entry.first);
  520. }
  521. return result;
  522. }
  523. LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::string const & basePath):
  524. owner(owner),
  525. basePath(basePath)
  526. {}
  527. const std::string & LegacyTextContainer::operator[](size_t index) const
  528. {
  529. return owner.translate(basePath + "." + std::to_string(index));
  530. }
  531. LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string const & basePath):
  532. owner(owner),
  533. basePath(basePath)
  534. {}
  535. std::pair<std::string, std::string> LegacyHelpContainer::operator[](size_t index) const
  536. {
  537. return {
  538. owner.translate(basePath + "." + std::to_string(index) + ".hover"),
  539. owner.translate(basePath + "." + std::to_string(index) + ".help")
  540. };
  541. }
  542. VCMI_LIB_NAMESPACE_END