CGeneralTextHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include "StdInc.h"
  2. #include "CGeneralTextHandler.h"
  3. #include <boost/locale.hpp>
  4. #include "filesystem/Filesystem.h"
  5. #include "CConfigHandler.h"
  6. #include "CModHandler.h"
  7. #include "GameConstants.h"
  8. #include "VCMI_Lib.h"
  9. /*
  10. * CGeneralTextHandler.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. size_t Unicode::getCharacterSize(ui8 firstByte)
  19. {
  20. // length of utf-8 character can be determined from 1st byte by counting number of highest bits set to 1:
  21. // 0xxxxxxx -> 1 - ASCII chars
  22. // 110xxxxx -> 2
  23. // 11110xxx -> 4 - last allowed in current standard
  24. // 1111110x -> 6 - last allowed in original standard
  25. if (firstByte < 0x80)
  26. return 1; // ASCII
  27. size_t ret = 0;
  28. for (size_t i=0; i<8; i++)
  29. {
  30. if ((firstByte & (0x80 >> i)) != 0)
  31. ret++;
  32. else
  33. break;
  34. }
  35. return ret;
  36. }
  37. bool Unicode::isValidCharacter(const ui8 *character, size_t maxSize)
  38. {
  39. // first character must follow rules checked in getCharacterSize
  40. size_t size = getCharacterSize(character[0]);
  41. if (character[0] > 0xF4)
  42. return false; // above maximum allowed in standard (UTF codepoints are capped at 0x0010FFFF)
  43. if (size > maxSize)
  44. return false;
  45. // remaining characters must have highest bit set to 1
  46. for (size_t i = 1; i < size; i++)
  47. {
  48. if ((character[i] & 0x80) == 0)
  49. return false;
  50. }
  51. return true;
  52. }
  53. bool Unicode::isValidASCII(const std::string & text)
  54. {
  55. for (const char & ch : text)
  56. if (ui8(ch) >= 0x80 )
  57. return false;
  58. return true;
  59. }
  60. bool Unicode::isValidASCII(const char * data, size_t size)
  61. {
  62. for (size_t i=0; i<size; i++)
  63. if (ui8(data[i]) >= 0x80 )
  64. return false;
  65. return true;
  66. }
  67. bool Unicode::isValidString(const std::string & text)
  68. {
  69. for (size_t i=0; i<text.size(); i += getCharacterSize(text[i]))
  70. {
  71. if (!isValidCharacter(reinterpret_cast<const ui8*>(text.data() + i), text.size() - i))
  72. return false;
  73. }
  74. return true;
  75. }
  76. bool Unicode::isValidString(const char * data, size_t size)
  77. {
  78. for (size_t i=0; i<size; i += getCharacterSize(data[i]))
  79. {
  80. if (!isValidCharacter(reinterpret_cast<const ui8*>(data + i), size - i))
  81. return false;
  82. }
  83. return true;
  84. }
  85. static std::string getSelectedEncoding()
  86. {
  87. return settings["general"]["encoding"].String();
  88. }
  89. std::string Unicode::toUnicode(const std::string &text)
  90. {
  91. return toUnicode(text, getSelectedEncoding());
  92. }
  93. std::string Unicode::toUnicode(const std::string &text, const std::string &encoding)
  94. {
  95. return boost::locale::conv::to_utf<char>(text, encoding);
  96. }
  97. std::string Unicode::fromUnicode(const std::string & text)
  98. {
  99. return fromUnicode(text, getSelectedEncoding());
  100. }
  101. std::string Unicode::fromUnicode(const std::string &text, const std::string &encoding)
  102. {
  103. return boost::locale::conv::from_utf<char>(text, encoding);
  104. }
  105. //Helper for string -> float conversion
  106. class LocaleWithComma: public std::numpunct<char>
  107. {
  108. protected:
  109. char do_decimal_point() const
  110. {
  111. return ',';
  112. }
  113. };
  114. CLegacyConfigParser::CLegacyConfigParser(std::string URI)
  115. {
  116. init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT)));
  117. }
  118. CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input)
  119. {
  120. init(input);
  121. }
  122. void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input)
  123. {
  124. data.reset(new char[input->getSize()]);
  125. input->read((ui8*)data.get(), input->getSize());
  126. curr = data.get();
  127. end = curr + input->getSize();
  128. }
  129. std::string CLegacyConfigParser::extractQuotedPart()
  130. {
  131. assert(*curr == '\"');
  132. curr++; // skip quote
  133. char * begin = curr;
  134. while (curr != end && *curr != '\"' && *curr != '\t')
  135. curr++;
  136. return std::string(begin, curr++); //increment curr to close quote
  137. }
  138. std::string CLegacyConfigParser::extractQuotedString()
  139. {
  140. assert(*curr == '\"');
  141. std::string ret;
  142. while (true)
  143. {
  144. ret += extractQuotedPart();
  145. // double quote - add it to string and continue unless
  146. // line terminated using tabulation
  147. if (curr < end && *curr == '\"' && *curr != '\t')
  148. {
  149. ret += '\"';
  150. }
  151. else // end of string
  152. return ret;
  153. }
  154. }
  155. std::string CLegacyConfigParser::extractNormalString()
  156. {
  157. char * begin = curr;
  158. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  159. curr++;
  160. return std::string(begin, curr);
  161. }
  162. std::string CLegacyConfigParser::readRawString()
  163. {
  164. if (curr >= end || *curr == '\n')
  165. return "";
  166. std::string ret;
  167. if (*curr == '\"')
  168. ret = extractQuotedString();// quoted text - find closing quote
  169. else
  170. ret = extractNormalString();//string without quotes - copy till \t or \r
  171. curr++;
  172. return ret;
  173. }
  174. std::string CLegacyConfigParser::readString()
  175. {
  176. // do not convert strings that are already in ASCII - this will only slow down loading process
  177. std::string str = readRawString();
  178. if (Unicode::isValidASCII(str))
  179. return str;
  180. return Unicode::toUnicode(str);
  181. }
  182. float CLegacyConfigParser::readNumber()
  183. {
  184. std::string input = readRawString();
  185. std::istringstream stream(input);
  186. if (input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  187. stream.imbue(std::locale(std::locale(), new LocaleWithComma));
  188. float result;
  189. if ( !(stream >> result) )
  190. return 0;
  191. return result;
  192. }
  193. bool CLegacyConfigParser::isNextEntryEmpty() const
  194. {
  195. char * nextSymbol = curr;
  196. while (nextSymbol < end && *nextSymbol == ' ')
  197. nextSymbol++; //find next meaningfull symbol
  198. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  199. }
  200. bool CLegacyConfigParser::endLine()
  201. {
  202. while (curr < end && *curr != '\n')
  203. readString();
  204. curr++;
  205. return curr < end;
  206. }
  207. void CGeneralTextHandler::readToVector(std::string sourceName, std::vector<std::string> & dest)
  208. {
  209. CLegacyConfigParser parser(sourceName);
  210. do
  211. {
  212. dest.push_back(parser.readString());
  213. }
  214. while (parser.endLine());
  215. }
  216. CGeneralTextHandler::CGeneralTextHandler()
  217. {
  218. readToVector("DATA/VCDESC.TXT", victoryConditions);
  219. readToVector("DATA/LCDESC.TXT", lossCondtions);
  220. readToVector("DATA/TCOMMAND.TXT", tcommands);
  221. readToVector("DATA/HALLINFO.TXT", hcommands);
  222. readToVector("DATA/CASTINFO.TXT", fcommands);
  223. readToVector("DATA/ADVEVENT.TXT", advobtxt);
  224. readToVector("DATA/XTRAINFO.TXT", xtrainfo);
  225. readToVector("DATA/RESTYPES.TXT", restypes);
  226. readToVector("DATA/TERRNAME.TXT", terrainNames);
  227. readToVector("DATA/RANDSIGN.TXT", randsign);
  228. readToVector("DATA/CRGEN1.TXT", creGens);
  229. readToVector("DATA/CRGEN4.TXT", creGens4);
  230. readToVector("DATA/OVERVIEW.TXT", overview);
  231. readToVector("DATA/ARRAYTXT.TXT", arraytxt);
  232. readToVector("DATA/PRISKILL.TXT", primarySkillNames);
  233. readToVector("DATA/JKTEXT.TXT", jktexts);
  234. readToVector("DATA/TVRNINFO.TXT", tavernInfo);
  235. readToVector("DATA/TURNDUR.TXT", turnDurations);
  236. readToVector("DATA/HEROSCRN.TXT", heroscrn);
  237. readToVector("DATA/TENTCOLR.TXT", tentColors);
  238. readToVector("DATA/SKILLLEV.TXT", levels);
  239. readToVector("DATA/OBJNAMES.TXT", names);
  240. localizedTexts = JsonNode(ResourceID("config/translate.json", EResType::TEXT));
  241. {
  242. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  243. parser.endLine();
  244. do
  245. {
  246. allTexts.push_back(parser.readString());
  247. }
  248. while (parser.endLine());
  249. }
  250. {
  251. CLegacyConfigParser parser("DATA/HELP.TXT");
  252. do
  253. {
  254. std::string first = parser.readString();
  255. std::string second = parser.readString();
  256. zelp.push_back(std::make_pair(first, second));
  257. }
  258. while (parser.endLine());
  259. }
  260. {
  261. CLegacyConfigParser nameParser("DATA/MINENAME.TXT");
  262. CLegacyConfigParser eventParser("DATA/MINEEVNT.TXT");
  263. do
  264. {
  265. std::string name = nameParser.readString();
  266. std::string event = eventParser.readString();
  267. mines.push_back(std::make_pair(name, event));
  268. }
  269. while (nameParser.endLine() && eventParser.endLine());
  270. }
  271. {
  272. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  273. do
  274. {
  275. std::string color = parser.readString();
  276. colors.push_back(color);
  277. color[0] = toupper(color[0]);
  278. capColors.push_back(color);
  279. }
  280. while (parser.endLine());
  281. }
  282. {
  283. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  284. //skip header
  285. parser.endLine();
  286. parser.endLine();
  287. do
  288. {
  289. skillName.push_back(parser.readString());
  290. skillInfoTexts.push_back(std::vector<std::string>());
  291. for(int j = 0; j < 3; j++)
  292. skillInfoTexts.back().push_back(parser.readString());
  293. }
  294. while (parser.endLine());
  295. }
  296. {
  297. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  298. //skip header
  299. parser.endLine();
  300. for (int i = 0; i < 6; ++i)
  301. seerEmpty.push_back(parser.readString());
  302. parser.endLine();
  303. quests.resize(10);
  304. for (int i = 0; i < 9; ++i) //9 types of quests
  305. {
  306. quests[i].resize(5);
  307. for (int j = 0; j < 5; ++j)
  308. {
  309. parser.readString(); //front description
  310. for (int k = 0; k < 6; ++k)
  311. quests[i][j].push_back(parser.readString());
  312. parser.endLine();
  313. }
  314. }
  315. quests[9].resize(1);
  316. for (int k = 0; k < 6; ++k) //Time limit
  317. {
  318. quests[9][0].push_back(parser.readString());
  319. }
  320. parser.endLine();
  321. parser.endLine(); // empty line
  322. parser.endLine(); // header
  323. for (int i = 0; i < 48; ++i)
  324. {
  325. seerNames.push_back(parser.readString());
  326. parser.endLine();
  327. }
  328. }
  329. {
  330. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  331. //skip header
  332. parser.endLine();
  333. std::string text;
  334. do
  335. {
  336. text = parser.readString();
  337. if (!text.empty())
  338. campaignMapNames.push_back(text);
  339. }
  340. while (parser.endLine() && !text.empty());
  341. for (size_t i=0; i<campaignMapNames.size(); i++)
  342. {
  343. do // skip empty space and header
  344. {
  345. text = parser.readString();
  346. }
  347. while (parser.endLine() && text.empty());
  348. campaignRegionNames.push_back(std::vector<std::string>());
  349. do
  350. {
  351. text = parser.readString();
  352. if (!text.empty())
  353. campaignRegionNames.back().push_back(text);
  354. }
  355. while (parser.endLine() && !text.empty());
  356. }
  357. }
  358. if (VLC->modh->modules.STACK_EXP)
  359. {
  360. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  361. parser.endLine();//header
  362. do
  363. {
  364. parser.readString(); //ignore 1st column with description
  365. zcrexp.push_back(parser.readString());
  366. }
  367. while (parser.endLine());
  368. }
  369. }