CGeneralTextHandler.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 "VCMI_Lib.h"
  18. #include "Terrain.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. size_t Unicode::getCharacterSize(char firstByte)
  21. {
  22. // length of utf-8 character can be determined from 1st byte by counting number of highest bits set to 1:
  23. // 0xxxxxxx -> 1 - ASCII chars
  24. // 110xxxxx -> 2
  25. // 11110xxx -> 4 - last allowed in current standard
  26. // 1111110x -> 6 - last allowed in original standard
  27. if ((ui8)firstByte < 0x80)
  28. return 1; // ASCII
  29. size_t ret = 0;
  30. for (size_t i=0; i<8; i++)
  31. {
  32. if (((ui8)firstByte & (0x80 >> i)) != 0)
  33. ret++;
  34. else
  35. break;
  36. }
  37. return ret;
  38. }
  39. bool Unicode::isValidCharacter(const char * character, size_t maxSize)
  40. {
  41. // can't be first byte in UTF8
  42. if ((ui8)character[0] >= 0x80 && (ui8)character[0] < 0xC0)
  43. return false;
  44. // first character must follow rules checked in getCharacterSize
  45. size_t size = getCharacterSize((ui8)character[0]);
  46. if ((ui8)character[0] > 0xF4)
  47. return false; // above maximum allowed in standard (UTF codepoints are capped at 0x0010FFFF)
  48. if (size > maxSize)
  49. return false;
  50. // remaining characters must have highest bit set to 1
  51. for (size_t i = 1; i < size; i++)
  52. {
  53. if (((ui8)character[i] & 0x80) == 0)
  54. return false;
  55. }
  56. return true;
  57. }
  58. bool Unicode::isValidASCII(const std::string & text)
  59. {
  60. for (const char & ch : text)
  61. if (ui8(ch) >= 0x80 )
  62. return false;
  63. return true;
  64. }
  65. bool Unicode::isValidASCII(const char * data, size_t size)
  66. {
  67. for (size_t i=0; i<size; i++)
  68. if (ui8(data[i]) >= 0x80 )
  69. return false;
  70. return true;
  71. }
  72. bool Unicode::isValidString(const std::string & text)
  73. {
  74. for (size_t i=0; i<text.size(); i += getCharacterSize(text[i]))
  75. {
  76. if (!isValidCharacter(text.data() + i, text.size() - i))
  77. return false;
  78. }
  79. return true;
  80. }
  81. bool Unicode::isValidString(const char * data, size_t size)
  82. {
  83. for (size_t i=0; i<size; i += getCharacterSize(data[i]))
  84. {
  85. if (!isValidCharacter(data + i, size - i))
  86. return false;
  87. }
  88. return true;
  89. }
  90. static std::string getSelectedEncoding()
  91. {
  92. return settings["general"]["encoding"].String();
  93. }
  94. std::string Unicode::toUnicode(const std::string &text)
  95. {
  96. return toUnicode(text, getSelectedEncoding());
  97. }
  98. std::string Unicode::toUnicode(const std::string &text, const std::string &encoding)
  99. {
  100. return boost::locale::conv::to_utf<char>(text, encoding);
  101. }
  102. std::string Unicode::fromUnicode(const std::string & text)
  103. {
  104. return fromUnicode(text, getSelectedEncoding());
  105. }
  106. std::string Unicode::fromUnicode(const std::string &text, const std::string &encoding)
  107. {
  108. return boost::locale::conv::from_utf<char>(text, encoding);
  109. }
  110. void Unicode::trimRight(std::string & text, const size_t amount)
  111. {
  112. if(text.empty())
  113. return;
  114. //todo: more efficient algorithm
  115. for(int i = 0; i< amount; i++){
  116. auto b = text.begin();
  117. auto e = text.end();
  118. size_t lastLen = 0;
  119. size_t len = 0;
  120. while (b != e) {
  121. lastLen = len;
  122. size_t n = getCharacterSize(*b);
  123. if(!isValidCharacter(&(*b),e-b))
  124. {
  125. logGlobal->error("Invalid UTF8 sequence");
  126. break;//invalid sequence will be trimmed
  127. }
  128. len += n;
  129. b += n;
  130. }
  131. text.resize(lastLen);
  132. }
  133. }
  134. //Helper for string -> float conversion
  135. class LocaleWithComma: public std::numpunct<char>
  136. {
  137. protected:
  138. char do_decimal_point() const override
  139. {
  140. return ',';
  141. }
  142. };
  143. CLegacyConfigParser::CLegacyConfigParser(std::string URI)
  144. {
  145. init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT)));
  146. }
  147. CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input)
  148. {
  149. init(input);
  150. }
  151. void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input)
  152. {
  153. data.reset(new char[input->getSize()]);
  154. input->read((ui8*)data.get(), input->getSize());
  155. curr = data.get();
  156. end = curr + input->getSize();
  157. }
  158. std::string CLegacyConfigParser::extractQuotedPart()
  159. {
  160. assert(*curr == '\"');
  161. curr++; // skip quote
  162. char * begin = curr;
  163. while (curr != end && *curr != '\"' && *curr != '\t')
  164. curr++;
  165. return std::string(begin, curr++); //increment curr to close quote
  166. }
  167. std::string CLegacyConfigParser::extractQuotedString()
  168. {
  169. assert(*curr == '\"');
  170. std::string ret;
  171. while (true)
  172. {
  173. ret += extractQuotedPart();
  174. // double quote - add it to string and continue quoted part
  175. if (curr < end && *curr == '\"')
  176. {
  177. ret += '\"';
  178. }
  179. //extract normal part
  180. else if(curr < end && *curr != '\t' && *curr != '\r')
  181. {
  182. char * begin = curr;
  183. while (curr < end && *curr != '\t' && *curr != '\r' && *curr != '\"')//find end of string or next quoted part start
  184. curr++;
  185. ret += std::string(begin, curr);
  186. if(curr>=end || *curr != '\"')
  187. return ret;
  188. }
  189. else // end of string
  190. return ret;
  191. }
  192. }
  193. std::string CLegacyConfigParser::extractNormalString()
  194. {
  195. char * begin = curr;
  196. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  197. curr++;
  198. return std::string(begin, curr);
  199. }
  200. std::string CLegacyConfigParser::readRawString()
  201. {
  202. if (curr >= end || *curr == '\n')
  203. return "";
  204. std::string ret;
  205. if (*curr == '\"')
  206. ret = extractQuotedString();// quoted text - find closing quote
  207. else
  208. ret = extractNormalString();//string without quotes - copy till \t or \r
  209. curr++;
  210. return ret;
  211. }
  212. std::string CLegacyConfigParser::readString()
  213. {
  214. // do not convert strings that are already in ASCII - this will only slow down loading process
  215. std::string str = readRawString();
  216. if (Unicode::isValidASCII(str))
  217. return str;
  218. return Unicode::toUnicode(str);
  219. }
  220. float CLegacyConfigParser::readNumber()
  221. {
  222. std::string input = readRawString();
  223. std::istringstream stream(input);
  224. if(input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  225. stream.imbue(std::locale(std::locale(), new LocaleWithComma()));
  226. float result;
  227. if ( !(stream >> result) )
  228. return 0;
  229. return result;
  230. }
  231. bool CLegacyConfigParser::isNextEntryEmpty() const
  232. {
  233. char * nextSymbol = curr;
  234. while (nextSymbol < end && *nextSymbol == ' ')
  235. nextSymbol++; //find next meaningfull symbol
  236. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  237. }
  238. bool CLegacyConfigParser::endLine()
  239. {
  240. while (curr < end && *curr != '\n')
  241. readString();
  242. curr++;
  243. return curr < end;
  244. }
  245. void CGeneralTextHandler::readToVector(std::string sourceID, std::string sourceName)
  246. {
  247. CLegacyConfigParser parser(sourceName);
  248. size_t index = 0;
  249. do
  250. {
  251. registerH3String(sourceID, index, parser.readString());
  252. index += 1;
  253. }
  254. while (parser.endLine());
  255. }
  256. const std::string & CGeneralTextHandler::translate(const std::string & identifier, size_t index) const
  257. {
  258. return translate(identifier + std::to_string(index));
  259. }
  260. const std::string & CGeneralTextHandler::translate(const std::string & identifier) const
  261. {
  262. return deserialize(identifier);
  263. }
  264. const std::string & CGeneralTextHandler::serialize(const std::string & identifier) const
  265. {
  266. assert(stringsIdentifiers.count(identifier));
  267. return stringsIdentifiers.at(identifier);
  268. }
  269. const std::string & CGeneralTextHandler::deserialize(const std::string & identifier) const
  270. {
  271. static const std::string emptyString;
  272. if (stringsLocalizations.count(identifier))
  273. return stringsLocalizations.at(identifier);
  274. logGlobal->error("Unable to find localization for string '%s'", identifier);
  275. return emptyString;
  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. // pseudo-array, that don't have H3 file with same name
  312. capColors (*this, "vcmi.capitalColors" ),
  313. qeModCommands (*this, "vcmi.quickExchange" )
  314. {
  315. readToVector("core.vcdesc", "DATA/VCDESC.TXT" );
  316. readToVector("core.lcdesc", "DATA/LCDESC.TXT" );
  317. readToVector("core.tcommand", "DATA/TCOMMAND.TXT" );
  318. readToVector("core.hallinfo", "DATA/HALLINFO.TXT" );
  319. readToVector("core.castinfo", "DATA/CASTINFO.TXT" );
  320. readToVector("core.advevent", "DATA/ADVEVENT.TXT" );
  321. readToVector("core.xtrainfo", "DATA/XTRAINFO.TXT" );
  322. readToVector("core.restypes", "DATA/RESTYPES.TXT" );
  323. readToVector("core.terrname", "DATA/TERRNAME.TXT" );
  324. readToVector("core.randsign", "DATA/RANDSIGN.TXT" );
  325. readToVector("core.crgen1", "DATA/CRGEN1.TXT" );
  326. readToVector("core.crgen4", "DATA/CRGEN4.TXT" );
  327. readToVector("core.overview", "DATA/OVERVIEW.TXT" );
  328. readToVector("core.arraytxt", "DATA/ARRAYTXT.TXT" );
  329. readToVector("core.priskill", "DATA/PRISKILL.TXT" );
  330. readToVector("core.jktext", "DATA/JKTEXT.TXT" );
  331. readToVector("core.tvrninfo", "DATA/TVRNINFO.TXT" );
  332. readToVector("core.randtvrn", "DATA/RANDTVRN.TXT" );
  333. readToVector("core.turndur", "DATA/TURNDUR.TXT" );
  334. readToVector("core.heroscrn", "DATA/HEROSCRN.TXT" );
  335. readToVector("core.tentcolr", "DATA/TENTCOLR.TXT" );
  336. readToVector("core.skilllev", "DATA/SKILLLEV.TXT" );
  337. readToVector("core.cmpmusic", "DATA/CMPMUSIC.TXT" );
  338. readToVector("core.minename", "DATA/MINENAME.TXT" );
  339. readToVector("core.mineevnt", "DATA/MINEEVNT.TXT" );
  340. static const char * QE_MOD_COMMANDS = "DATA/QECOMMANDS.TXT";
  341. if (CResourceHandler::get()->existsResource(ResourceID(QE_MOD_COMMANDS, EResType::TEXT)))
  342. readToVector("vcmi.quickExchange", QE_MOD_COMMANDS);
  343. auto vcmiTexts = JsonNode(ResourceID("config/translate.json", EResType::TEXT));
  344. for ( auto const & node : vcmiTexts.Struct())
  345. registerString(node.first, node.second.String());
  346. {
  347. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  348. parser.endLine();
  349. do
  350. {
  351. allTexts.push_back(parser.readString());
  352. registerH3String("core.genrltxt", allTexts.size(), allTexts.back());
  353. }
  354. while (parser.endLine());
  355. }
  356. {
  357. CLegacyConfigParser parser("DATA/HELP.TXT");
  358. size_t index = 0;
  359. do
  360. {
  361. std::string first = parser.readString();
  362. std::string second = parser.readString();
  363. registerString("core.help." + std::to_string(index) + ".label", first);
  364. registerString("core.help." + std::to_string(index) + ".help", second);
  365. index += 1;
  366. }
  367. while (parser.endLine());
  368. }
  369. {
  370. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  371. size_t index = 0;
  372. do
  373. {
  374. std::string color = parser.readString();
  375. registerH3String("core.plcolors", index, color);
  376. color[0] = toupper(color[0]);
  377. registerH3String("vcmi.capitalColors", index, color);
  378. index += 1;
  379. }
  380. while (parser.endLine());
  381. }
  382. {
  383. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  384. //skip header
  385. parser.endLine();
  386. for (int i = 0; i < 6; ++i)
  387. {
  388. seerEmpty.push_back(parser.readString());
  389. registerH3String("core.seerhut.empty", seerEmpty.size(), seerEmpty.back());
  390. }
  391. parser.endLine();
  392. quests.resize(10);
  393. for (int i = 0; i < 9; ++i) //9 types of quests
  394. {
  395. quests[i].resize(5);
  396. for (int j = 0; j < 5; ++j)
  397. {
  398. parser.readString(); //front description
  399. for (int k = 0; k < 6; ++k)
  400. {
  401. quests[i][j].push_back(parser.readString());
  402. registerH3String("core.seerhut.quest." + std::to_string(i) + "." + std::to_string(j), k, quests[i][j].back());
  403. }
  404. parser.endLine();
  405. }
  406. }
  407. quests[9].resize(1);
  408. for (int k = 0; k < 6; ++k) //Time limit
  409. {
  410. quests[9][0].push_back(parser.readString());
  411. }
  412. parser.endLine();
  413. parser.endLine(); // empty line
  414. parser.endLine(); // header
  415. for (int i = 0; i < 48; ++i)
  416. {
  417. seerNames.push_back(parser.readString());
  418. parser.endLine();
  419. }
  420. }
  421. {
  422. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  423. //skip header
  424. parser.endLine();
  425. std::string text;
  426. do
  427. {
  428. text = parser.readString();
  429. if (!text.empty())
  430. campaignMapNames.push_back(text);
  431. }
  432. while (parser.endLine() && !text.empty());
  433. for (size_t i=0; i<campaignMapNames.size(); i++)
  434. {
  435. do // skip empty space and header
  436. {
  437. text = parser.readString();
  438. }
  439. while (parser.endLine() && text.empty());
  440. campaignRegionNames.push_back(std::vector<std::string>());
  441. do
  442. {
  443. text = parser.readString();
  444. if (!text.empty())
  445. campaignRegionNames.back().push_back(text);
  446. }
  447. while (parser.endLine() && !text.empty());
  448. }
  449. }
  450. if (VLC->modh->modules.STACK_EXP)
  451. {
  452. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  453. parser.endLine();//header
  454. for (size_t iter=0; iter<325; iter++)
  455. {
  456. parser.readString(); //ignore 1st column with description
  457. zcrexp.push_back(parser.readString());
  458. parser.endLine();
  459. }
  460. // line 325 - some weird formatting here
  461. zcrexp.push_back(parser.readString());
  462. parser.readString();
  463. parser.endLine();
  464. do // rest of file can be read normally
  465. {
  466. parser.readString(); //ignore 1st column with description
  467. zcrexp.push_back(parser.readString());
  468. }
  469. while (parser.endLine());
  470. }
  471. if (VLC->modh->modules.COMMANDERS)
  472. {
  473. try
  474. {
  475. CLegacyConfigParser parser("DATA/ZNPC00.TXT");
  476. parser.endLine();//header
  477. do
  478. {
  479. znpc00.push_back(parser.readString());
  480. } while (parser.endLine());
  481. }
  482. catch (const std::runtime_error &)
  483. {
  484. logGlobal->warn("WoG file ZNPC00.TXT containing commander texts was not found");
  485. }
  486. }
  487. }
  488. int32_t CGeneralTextHandler::pluralText(const int32_t textIndex, const int32_t count) const
  489. {
  490. if(textIndex == 0)
  491. return 0;
  492. else if(textIndex < 0)
  493. return -textIndex;
  494. else if(count == 1)
  495. return textIndex;
  496. else
  497. return textIndex + 1;
  498. }
  499. std::vector<std::string> CGeneralTextHandler::findStringsWithPrefix(std::string const & prefix)
  500. {
  501. std::vector<std::string> result;
  502. for (auto const & entry : stringsLocalizations)
  503. {
  504. if (boost::algorithm::starts_with(entry.first, prefix))
  505. result.push_back(entry.first);
  506. }
  507. return result;
  508. }
  509. LegacyTextContainer::LegacyTextContainer(CGeneralTextHandler & owner, std::string const & basePath):
  510. owner(owner),
  511. basePath(basePath)
  512. {}
  513. const std::string & LegacyTextContainer::operator[](size_t index) const
  514. {
  515. return owner.translate(basePath + "." + std::to_string(index));
  516. }
  517. LegacyHelpContainer::LegacyHelpContainer(CGeneralTextHandler & owner, std::string const & basePath):
  518. owner(owner),
  519. basePath(basePath)
  520. {}
  521. std::pair<std::string, std::string> LegacyHelpContainer::operator[](size_t index) const
  522. {
  523. return {
  524. owner.translate(basePath + "." + std::to_string(index) + ".label"),
  525. owner.translate(basePath + "." + std::to_string(index) + ".help")
  526. };
  527. }
  528. VCMI_LIB_NAMESPACE_END