CGeneralTextHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. return curr >= end || *curr == '\n' || *curr == '\r' || *curr == '\t';
  95. }
  96. bool CLegacyConfigParser::endLine()
  97. {
  98. while (curr < end && *curr != '\n')
  99. readString();
  100. curr++;
  101. return curr < end;
  102. }
  103. void readToVector(std::string sourceName, std::vector<std::string> & dest)
  104. {
  105. CLegacyConfigParser parser(sourceName);
  106. do
  107. {
  108. dest.push_back(parser.readString());
  109. }
  110. while (parser.endLine());
  111. }
  112. void CGeneralTextHandler::load()
  113. {
  114. readToVector("DATA/VCDESC.TXT", victoryConditions);
  115. readToVector("DATA/LCDESC.TXT", lossCondtions);
  116. readToVector("DATA/TCOMMAND.TXT", tcommands);
  117. readToVector("DATA/HALLINFO.TXT", hcommands);
  118. readToVector("DATA/CASTINFO.TXT", fcommands);
  119. readToVector("DATA/ADVEVENT.TXT", advobtxt);
  120. readToVector("DATA/XTRAINFO.TXT", xtrainfo);
  121. readToVector("DATA/RESTYPES.TXT", restypes);
  122. readToVector("DATA/TERRNAME.TXT", terrainNames);
  123. readToVector("DATA/RANDSIGN.TXT", randsign);
  124. readToVector("DATA/ZCRGN1.TXT", creGens);
  125. readToVector("DATA/CRGEN4.TXT", creGens4);
  126. readToVector("DATA/OVERVIEW.TXT", overview);
  127. readToVector("DATA/ARRAYTXT.TXT", arraytxt);
  128. readToVector("DATA/PRISKILL.TXT", primarySkillNames);
  129. readToVector("DATA/JKTEXT.TXT", jktexts);
  130. readToVector("DATA/TVRNINFO.TXT", tavernInfo);
  131. readToVector("DATA/TURNDUR.TXT", turnDurations);
  132. readToVector("DATA/HEROSCRN.TXT", heroscrn);
  133. readToVector("DATA/ARTEVENT.TXT", artifEvents);
  134. readToVector("DATA/TENTCOLR.TXT", tentColors);
  135. readToVector("DATA/SKILLLEV.TXT", levels);
  136. readToVector("DATA/OBJNAMES.TXT", names);
  137. {
  138. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  139. parser.endLine();
  140. do
  141. {
  142. allTexts.push_back(parser.readString());
  143. }
  144. while (parser.endLine());
  145. }
  146. {
  147. CLegacyConfigParser parser("DATA/ZELP.TXT");
  148. do
  149. {
  150. std::string first = parser.readString();
  151. std::string second = parser.readString();
  152. zelp.push_back(std::make_pair(first, second));
  153. }
  154. while (parser.endLine());
  155. }
  156. {
  157. CLegacyConfigParser parser("DATA/HEROSPEC.TXT");
  158. CLegacyConfigParser bioParser("DATA/HEROBIOS.TXT");
  159. //skip header
  160. parser.endLine();
  161. parser.endLine();
  162. do
  163. {
  164. HeroTexts texts;
  165. texts.bonusName = parser.readString();
  166. texts.shortBonus = parser.readString();
  167. texts.longBonus = parser.readString();
  168. texts.biography = bioParser.readString();
  169. hTxts.push_back(texts);
  170. }
  171. while (parser.endLine() && bioParser.endLine());
  172. }
  173. {
  174. CLegacyConfigParser parser("DATA/BLDGNEUT.TXT");
  175. for(int i=0; i<15; i++)
  176. {
  177. std::string name = parser.readString();
  178. std::string descr = parser.readString();
  179. parser.endLine();
  180. for(int j=0; j<GameConstants::F_NUMBER; j++)
  181. {
  182. buildings[j][i].first = name;
  183. buildings[j][i].second = descr;
  184. }
  185. }
  186. parser.endLine(); // silo
  187. parser.endLine(); // blacksmith //unused entries
  188. parser.endLine(); // moat
  189. //shipyard with the ship
  190. std::string name = parser.readString();
  191. std::string descr = parser.readString();
  192. parser.endLine();
  193. for(int j=0; j<GameConstants::F_NUMBER; j++)
  194. {
  195. buildings[j][20].first = name;
  196. buildings[j][20].second = descr;
  197. }
  198. //blacksmith
  199. for(int j=0; j<GameConstants::F_NUMBER; j++)
  200. {
  201. buildings[j][16].first = parser.readString();
  202. buildings[j][16].second = parser.readString();
  203. parser.endLine();
  204. }
  205. }
  206. {
  207. CLegacyConfigParser parser("DATA/BLDGSPEC.TXT");
  208. for(int town=0; town<GameConstants::F_NUMBER; town++)
  209. {
  210. for(int build=0; build<9; build++)
  211. {
  212. buildings[town][17+build].first = parser.readString();
  213. buildings[town][17+build].second = parser.readString();
  214. parser.endLine();
  215. }
  216. buildings[town][26].first = parser.readString(); // Grail
  217. buildings[town][26].second = parser.readString();
  218. parser.endLine();
  219. buildings[town][15].first = parser.readString(); // Resource silo
  220. buildings[town][15].second = parser.readString();
  221. parser.endLine();
  222. }
  223. }
  224. {
  225. CLegacyConfigParser parser("DATA/DWELLING.TXT");
  226. for(int town=0; town<GameConstants::F_NUMBER; town++)
  227. {
  228. for(int build=0; build<14; build++)
  229. {
  230. buildings[town][30+build].first = parser.readString();
  231. buildings[town][30+build].second = parser.readString();
  232. parser.endLine();
  233. }
  234. }
  235. }
  236. {
  237. CLegacyConfigParser typeParser("DATA/TOWNTYPE.TXT");
  238. CLegacyConfigParser nameParser("DATA/TOWNNAME.TXT");
  239. do
  240. {
  241. townTypes.push_back(typeParser.readString());
  242. townNames.push_back(std::vector<std::string>());
  243. for (int i=0; i<GameConstants::NAMES_PER_TOWN; i++)
  244. {
  245. townNames.back().push_back(nameParser.readString());
  246. nameParser.endLine();
  247. }
  248. }
  249. while (typeParser.endLine());
  250. }
  251. {
  252. CLegacyConfigParser nameParser("DATA/MINENAME.TXT");
  253. CLegacyConfigParser eventParser("DATA/MINEEVNT.TXT");
  254. do
  255. {
  256. std::string name = nameParser.readString();
  257. std::string event = eventParser.readString();
  258. mines.push_back(std::make_pair(name, event));
  259. }
  260. while (nameParser.endLine() && eventParser.endLine());
  261. }
  262. {
  263. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  264. do
  265. {
  266. std::string color = parser.readString();
  267. colors.push_back(color);
  268. color[0] = toupper(color[0]);
  269. capColors.push_back(color);
  270. }
  271. while (parser.endLine());
  272. }
  273. {
  274. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  275. //skip header
  276. parser.endLine();
  277. parser.endLine();
  278. do
  279. {
  280. skillName.push_back(parser.readString());
  281. skillInfoTexts.push_back(std::vector<std::string>());
  282. for(int j = 0; j < 3; j++)
  283. skillInfoTexts.back().push_back(parser.readString());
  284. }
  285. while (parser.endLine());
  286. }
  287. {
  288. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  289. //skip header
  290. parser.endLine();
  291. parser.endLine();
  292. for (int i = 0; i < 6; ++i)
  293. seerEmpty.push_back(parser.readString());
  294. quests.resize(10);
  295. for (int i = 0; i < 9; ++i) //9 types of quests
  296. {
  297. quests[i].resize(5);
  298. for (int j = 0; j < 5; ++j)
  299. {
  300. parser.readString(); //front description
  301. for (int k = 0; k < 6; ++k)
  302. quests[i][j].push_back(parser.readString());
  303. parser.endLine();
  304. }
  305. }
  306. quests[9].resize(1);
  307. for (int k = 0; k < 6; ++k) //Time limit
  308. {
  309. quests[9][0].push_back(parser.readString());
  310. }
  311. parser.endLine();
  312. parser.endLine(); // empty line
  313. parser.endLine(); // header
  314. for (int i = 0; i < 48; ++i)
  315. {
  316. seerNames.push_back(parser.readString());
  317. parser.endLine();
  318. }
  319. }
  320. {
  321. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  322. //skip header
  323. parser.endLine();
  324. std::string text;
  325. do
  326. {
  327. text = parser.readString();
  328. if (!text.empty())
  329. campaignMapNames.push_back(text);
  330. }
  331. while (parser.endLine() && !text.empty());
  332. for (size_t i=0; i<campaignMapNames.size(); i++)
  333. {
  334. do // skip empty space and header
  335. {
  336. text = parser.readString();
  337. }
  338. while (parser.endLine() && text.empty());
  339. campaignRegionNames.push_back(std::vector<std::string>());
  340. do
  341. {
  342. text = parser.readString();
  343. if (!text.empty())
  344. campaignRegionNames.back().push_back(text);
  345. }
  346. while (parser.endLine() && !text.empty());
  347. }
  348. }
  349. {
  350. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  351. parser.endLine();//header
  352. do
  353. {
  354. parser.readString(); //ignore 1st column with description
  355. zcrexp.push_back(parser.readString());
  356. }
  357. while (parser.endLine());
  358. }
  359. std::string buffer;
  360. std::ifstream ifs(CResourceHandler::get()->getResourceName(ResourceID("config/threatlevel.txt")), std::ios::binary);
  361. getline(ifs, buffer); //skip 1st line
  362. for (int i = 0; i < 13; ++i)
  363. {
  364. getline(ifs, buffer);
  365. threat.push_back(buffer);
  366. }
  367. }
  368. std::string CGeneralTextHandler::getTitle(const std::string & text)
  369. {
  370. std::string ret;
  371. int i=0;
  372. while ((text[i++]!='{'));
  373. while ((text[i]!='}') && (i<text.length()))
  374. ret+=text[i++];
  375. return ret;
  376. }
  377. std::string CGeneralTextHandler::getDescr(const std::string & text)
  378. {
  379. std::string ret;
  380. int i=0;
  381. while ((text[i++]!='}'));
  382. i+=2;
  383. while ((text[i]!='"') && (i<text.length()))
  384. ret+=text[i++];
  385. return ret;
  386. }
  387. CGeneralTextHandler::CGeneralTextHandler()
  388. {
  389. }