CGeneralTextHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #include "StdInc.h"
  2. #include "CGeneralTextHandler.h"
  3. #include "Filesystem/CResourceLoader.h"
  4. #include "Filesystem/CInputStream.h"
  5. #include "GameConstants.h"
  6. // #include <locale> //needed?
  7. /*
  8. * CGeneralTextHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. //Helper for string -> float conversion
  17. class LocaleWithComma: public std::numpunct<char>
  18. {
  19. protected:
  20. char do_decimal_point() const
  21. {
  22. return ',';
  23. }
  24. };
  25. CLegacyConfigParser::CLegacyConfigParser(std::string URI)
  26. {
  27. init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT)));
  28. }
  29. CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input)
  30. {
  31. init(input);
  32. }
  33. void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input)
  34. {
  35. data.reset(new char[input->getSize()]);
  36. input->read((ui8*)data.get(), input->getSize());
  37. curr = data.get();
  38. end = curr + input->getSize();
  39. }
  40. std::string CLegacyConfigParser::extractQuotedPart()
  41. {
  42. assert(*curr == '\"');
  43. curr++; // skip quote
  44. char * begin = curr;
  45. while (curr != end && *curr != '\"')
  46. curr++;
  47. return std::string(begin, curr++); //increment curr to close quote
  48. }
  49. std::string CLegacyConfigParser::extractQuotedString()
  50. {
  51. assert(*curr == '\"');
  52. std::string ret;
  53. while (true)
  54. {
  55. ret += extractQuotedPart();
  56. if (curr < end && *curr == '\"') //double quote - add it to string and continue
  57. ret += '\"';
  58. else // end of string
  59. return ret;
  60. }
  61. }
  62. std::string CLegacyConfigParser::extractNormalString()
  63. {
  64. char * begin = curr;
  65. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  66. curr++;
  67. return std::string(begin, curr);
  68. }
  69. std::string CLegacyConfigParser::readString()
  70. {
  71. if (curr >= end || *curr == '\n')
  72. return "";
  73. std::string ret;
  74. if (*curr == '\"')
  75. ret = extractQuotedString();// quoted text - find closing quote
  76. else
  77. ret = extractNormalString();//string without quotes - copy till \t or \r
  78. curr++;
  79. return ret;
  80. }
  81. float CLegacyConfigParser::readNumber()
  82. {
  83. std::string input = readString();
  84. std::istringstream stream(input);
  85. if (input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  86. stream.imbue(std::locale(std::locale(), new LocaleWithComma));
  87. int result;
  88. if ( !(stream >> result) )
  89. return 0;
  90. return result;
  91. }
  92. bool CLegacyConfigParser::isNextEntryEmpty()
  93. {
  94. char * nextSymbol = curr;
  95. while (nextSymbol < end && *nextSymbol == ' ')
  96. nextSymbol++; //find next meaningfull symbol
  97. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  98. }
  99. bool CLegacyConfigParser::endLine()
  100. {
  101. while (curr < end && *curr != '\n')
  102. readString();
  103. curr++;
  104. return curr < end;
  105. }
  106. void readToVector(std::string sourceName, std::vector<std::string> & dest)
  107. {
  108. CLegacyConfigParser parser(sourceName);
  109. do
  110. {
  111. dest.push_back(parser.readString());
  112. }
  113. while (parser.endLine());
  114. }
  115. void CGeneralTextHandler::load()
  116. {
  117. readToVector("DATA/VCDESC.TXT", victoryConditions);
  118. readToVector("DATA/LCDESC.TXT", lossCondtions);
  119. readToVector("DATA/TCOMMAND.TXT", tcommands);
  120. readToVector("DATA/HALLINFO.TXT", hcommands);
  121. readToVector("DATA/CASTINFO.TXT", fcommands);
  122. readToVector("DATA/ADVEVENT.TXT", advobtxt);
  123. readToVector("DATA/XTRAINFO.TXT", xtrainfo);
  124. readToVector("DATA/RESTYPES.TXT", restypes);
  125. readToVector("DATA/TERRNAME.TXT", terrainNames);
  126. readToVector("DATA/RANDSIGN.TXT", randsign);
  127. readToVector("DATA/ZCRGN1.TXT", creGens);
  128. readToVector("DATA/CRGEN4.TXT", creGens4);
  129. readToVector("DATA/OVERVIEW.TXT", overview);
  130. readToVector("DATA/ARRAYTXT.TXT", arraytxt);
  131. readToVector("DATA/PRISKILL.TXT", primarySkillNames);
  132. readToVector("DATA/JKTEXT.TXT", jktexts);
  133. readToVector("DATA/TVRNINFO.TXT", tavernInfo);
  134. readToVector("DATA/TURNDUR.TXT", turnDurations);
  135. readToVector("DATA/HEROSCRN.TXT", heroscrn);
  136. readToVector("DATA/ARTEVENT.TXT", artifEvents);
  137. readToVector("DATA/TENTCOLR.TXT", tentColors);
  138. readToVector("DATA/SKILLLEV.TXT", levels);
  139. readToVector("DATA/OBJNAMES.TXT", names);
  140. {
  141. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  142. parser.endLine();
  143. do
  144. {
  145. allTexts.push_back(parser.readString());
  146. }
  147. while (parser.endLine());
  148. }
  149. {
  150. CLegacyConfigParser parser("DATA/ZELP.TXT");
  151. do
  152. {
  153. std::string first = parser.readString();
  154. std::string second = parser.readString();
  155. zelp.push_back(std::make_pair(first, second));
  156. }
  157. while (parser.endLine());
  158. }
  159. {
  160. CLegacyConfigParser parser("DATA/HEROSPEC.TXT");
  161. CLegacyConfigParser bioParser("DATA/HEROBIOS.TXT");
  162. //skip header
  163. parser.endLine();
  164. parser.endLine();
  165. do
  166. {
  167. HeroTexts texts;
  168. texts.bonusName = parser.readString();
  169. texts.shortBonus = parser.readString();
  170. texts.longBonus = parser.readString();
  171. texts.biography = bioParser.readString();
  172. hTxts.push_back(texts);
  173. }
  174. while (parser.endLine() && bioParser.endLine());
  175. }
  176. {
  177. CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
  178. for(int i=0; i<15; i++)
  179. {
  180. std::string name = parser.readString();
  181. std::string descr = parser.readString();
  182. parser.endLine();
  183. for(int j=0; j<GameConstants::F_NUMBER; j++)
  184. {
  185. buildings[j][i].first = name;
  186. buildings[j][i].second = descr;
  187. }
  188. }
  189. parser.endLine(); // silo
  190. parser.endLine(); // blacksmith //unused entries
  191. parser.endLine(); // moat
  192. //shipyard with the ship
  193. std::string name = parser.readString();
  194. std::string descr = parser.readString();
  195. parser.endLine();
  196. for(int j=0; j<GameConstants::F_NUMBER; j++)
  197. {
  198. buildings[j][20].first = name;
  199. buildings[j][20].second = descr;
  200. }
  201. //blacksmith
  202. for(int j=0; j<GameConstants::F_NUMBER; j++)
  203. {
  204. buildings[j][16].first = parser.readString();
  205. buildings[j][16].second = parser.readString();
  206. parser.endLine();
  207. }
  208. }
  209. {
  210. CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
  211. for(int town=0; town<GameConstants::F_NUMBER; town++)
  212. {
  213. for(int build=0; build<9; build++)
  214. {
  215. buildings[town][17+build].first = parser.readString();
  216. buildings[town][17+build].second = parser.readString();
  217. parser.endLine();
  218. }
  219. buildings[town][26].first = parser.readString(); // Grail
  220. buildings[town][26].second = parser.readString();
  221. parser.endLine();
  222. buildings[town][15].first = parser.readString(); // Resource silo
  223. buildings[town][15].second = parser.readString();
  224. parser.endLine();
  225. }
  226. }
  227. {
  228. CLegacyConfigParser parser("DATA/DWELLING.TXT");
  229. for(int town=0; town<GameConstants::F_NUMBER; town++)
  230. {
  231. for(int build=0; build<14; build++)
  232. {
  233. buildings[town][30+build].first = parser.readString();
  234. buildings[town][30+build].second = parser.readString();
  235. parser.endLine();
  236. }
  237. }
  238. }
  239. {
  240. CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
  241. CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
  242. do
  243. {
  244. townTypes.push_back(typeParser.readString());
  245. townNames.push_back(std::vector<std::string>());
  246. for (int i=0; i<GameConstants::NAMES_PER_TOWN; i++)
  247. {
  248. townNames.back().push_back(nameParser.readString());
  249. nameParser.endLine();
  250. }
  251. }
  252. while (typeParser.endLine());
  253. }
  254. {
  255. CLegacyConfigParser nameParser("DATA/MINENAME.TXT");
  256. CLegacyConfigParser eventParser("DATA/MINEEVNT.TXT");
  257. do
  258. {
  259. std::string name = nameParser.readString();
  260. std::string event = eventParser.readString();
  261. mines.push_back(std::make_pair(name, event));
  262. }
  263. while (nameParser.endLine() && eventParser.endLine());
  264. }
  265. {
  266. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  267. do
  268. {
  269. std::string color = parser.readString();
  270. colors.push_back(color);
  271. color[0] = toupper(color[0]);
  272. capColors.push_back(color);
  273. }
  274. while (parser.endLine());
  275. }
  276. {
  277. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  278. //skip header
  279. parser.endLine();
  280. parser.endLine();
  281. do
  282. {
  283. skillName.push_back(parser.readString());
  284. skillInfoTexts.push_back(std::vector<std::string>());
  285. for(int j = 0; j < 3; j++)
  286. skillInfoTexts.back().push_back(parser.readString());
  287. }
  288. while (parser.endLine());
  289. }
  290. {
  291. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  292. //skip header
  293. parser.endLine();
  294. parser.endLine();
  295. for (int i = 0; i < 6; ++i)
  296. seerEmpty.push_back(parser.readString());
  297. quests.resize(10);
  298. for (int i = 0; i < 9; ++i) //9 types of quests
  299. {
  300. quests[i].resize(5);
  301. for (int j = 0; j < 5; ++j)
  302. {
  303. parser.readString(); //front description
  304. for (int k = 0; k < 6; ++k)
  305. quests[i][j].push_back(parser.readString());
  306. parser.endLine();
  307. }
  308. }
  309. quests[9].resize(1);
  310. for (int k = 0; k < 6; ++k) //Time limit
  311. {
  312. quests[9][0].push_back(parser.readString());
  313. }
  314. parser.endLine();
  315. parser.endLine(); // empty line
  316. parser.endLine(); // header
  317. for (int i = 0; i < 48; ++i)
  318. {
  319. seerNames.push_back(parser.readString());
  320. parser.endLine();
  321. }
  322. }
  323. {
  324. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  325. //skip header
  326. parser.endLine();
  327. std::string text;
  328. do
  329. {
  330. text = parser.readString();
  331. if (!text.empty())
  332. campaignMapNames.push_back(text);
  333. }
  334. while (parser.endLine() && !text.empty());
  335. for (size_t i=0; i<campaignMapNames.size(); i++)
  336. {
  337. do // skip empty space and header
  338. {
  339. text = parser.readString();
  340. }
  341. while (parser.endLine() && text.empty());
  342. campaignRegionNames.push_back(std::vector<std::string>());
  343. do
  344. {
  345. text = parser.readString();
  346. if (!text.empty())
  347. campaignRegionNames.back().push_back(text);
  348. }
  349. while (parser.endLine() && !text.empty());
  350. }
  351. }
  352. {
  353. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  354. parser.endLine();//header
  355. do
  356. {
  357. parser.readString(); //ignore 1st column with description
  358. zcrexp.push_back(parser.readString());
  359. }
  360. while (parser.endLine());
  361. }
  362. std::string buffer;
  363. std::ifstream ifs(CResourceHandler::get()->getResourceName(ResourceID("config/threatlevel.txt")), std::ios::binary);
  364. getline(ifs, buffer); //skip 1st line
  365. for (int i = 0; i < 13; ++i)
  366. {
  367. getline(ifs, buffer);
  368. threat.push_back(buffer);
  369. }
  370. }
  371. std::string CGeneralTextHandler::getTitle(const std::string & text)
  372. {
  373. std::string ret;
  374. int i=0;
  375. while ((text[i++]!='{'));
  376. while ((text[i]!='}') && (i<text.length()))
  377. ret+=text[i++];
  378. return ret;
  379. }
  380. std::string CGeneralTextHandler::getDescr(const std::string & text)
  381. {
  382. std::string ret;
  383. int i=0;
  384. while ((text[i++]!='}'));
  385. i+=2;
  386. while ((text[i]!='"') && (i<text.length()))
  387. ret+=text[i++];
  388. return ret;
  389. }
  390. CGeneralTextHandler::CGeneralTextHandler()
  391. {
  392. }