CGeneralTextHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. #include "StdInc.h"
  2. #include "CGeneralTextHandler.h"
  3. #include "Filesystem/CResourceLoader.h"
  4. #include "VCMI_Lib.h"
  5. #include "CLodHandler.h"
  6. #include "GameConstants.h"
  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. std::string readTo(const std::string &in, int &it, char end)
  17. {
  18. int pom = it;
  19. int last = in.find_first_of(end,it);
  20. it+=(1+last-it);
  21. return in.substr(pom,last-pom);
  22. }
  23. void trimQuotation(std::string &op)
  24. {
  25. if(op.length() && op[0] == '\"' && op[op.size()-1] == '\"')
  26. op = op.substr(1,op.size()-2);
  27. }
  28. std::string getTextFile(std::string filename)
  29. {
  30. auto file = CResourceHandler::get()->loadData(
  31. ResourceID(std::string("DATA/") + filename, EResType::TEXT));
  32. return std::string((char*)file.first.get(), file.second);
  33. }
  34. void CGeneralTextHandler::load()
  35. {
  36. std::string buf1 = getTextFile("ZELP.TXT");
  37. int itr=0, eol=-1, eolnext=-1, pom;
  38. eolnext = buf1.find_first_of('\r',itr);
  39. while(itr<buf1.size())
  40. {
  41. eol = eolnext; //end of this line
  42. eolnext = buf1.find_first_of('\r',eol+1); //end of the next line
  43. pom=buf1.find_first_of('\t',itr); //upcoming tab
  44. if(eol<0 || pom<0)
  45. break;
  46. if(pom>eol) //in current line there is not tab
  47. zelp.push_back(std::pair<std::string,std::string>());
  48. else
  49. {
  50. zelp.push_back
  51. (std::pair<std::string,std::string>
  52. (buf1.substr(itr,pom-itr),
  53. buf1.substr(pom+1,eol-pom-1)));
  54. boost::algorithm::replace_all(zelp[zelp.size()-1].first,"\t","");
  55. boost::algorithm::replace_all(zelp[zelp.size()-1].second,"\t","");
  56. trimQuotation(zelp.back().second);
  57. }
  58. itr=eol+2;
  59. }
  60. std::string buf = getTextFile("VCDESC.TXT");
  61. int andame = buf.size();
  62. int i=0; //buf iterator
  63. for(int gg=0; gg<14; ++gg)
  64. {
  65. int befi=i;
  66. for(; i<andame; ++i)
  67. {
  68. if(buf[i]=='\r')
  69. break;
  70. }
  71. victoryConditions[gg] = buf.substr(befi, i-befi);
  72. i+=2;
  73. }
  74. buf = getTextFile("LCDESC.TXT");
  75. andame = buf.size();
  76. i=0; //buf iterator
  77. for(int gg=0; gg<4; ++gg)
  78. {
  79. int befi=i;
  80. for(; i<andame; ++i)
  81. {
  82. if(buf[i]=='\r')
  83. break;
  84. }
  85. lossCondtions[gg] = buf.substr(befi, i-befi);
  86. i+=2;
  87. }
  88. hTxts.resize(GameConstants::HEROES_QUANTITY);
  89. buf = getTextFile("HEROSPEC.TXT");
  90. i=0;
  91. std::string dump;
  92. for(int iii=0; iii<2; ++iii)
  93. {
  94. loadToIt(dump,buf,i,3);
  95. }
  96. for (int iii=0;iii<hTxts.size();iii++)
  97. {
  98. loadToIt(hTxts[iii].bonusName,buf,i,4);
  99. loadToIt(hTxts[iii].shortBonus,buf,i,4);
  100. loadToIt(hTxts[iii].longBonus,buf,i,3);
  101. trimQuotation(hTxts[iii].longBonus);
  102. }
  103. buf = getTextFile("HEROBIOS.TXT");
  104. i=0;
  105. for (int iii=0;iii<hTxts.size();iii++)
  106. {
  107. loadToIt(hTxts[iii].biography,buf,i,3);
  108. trimQuotation(hTxts[iii].biography);
  109. }
  110. int it;
  111. buf = getTextFile("BLDGNEUT.TXT");
  112. andame = buf.size(), it=0;
  113. for(int b=0;b<15;b++)
  114. {
  115. std::string name = readTo(buf,it,'\t'),
  116. description = readTo(buf,it,'\n');
  117. for(int fi=0;fi<GameConstants::F_NUMBER;fi++)
  118. {
  119. buildings[fi][b].first = name;
  120. buildings[fi][b].second = description;
  121. }
  122. }
  123. buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');//silo,blacksmith,moat - useless???
  124. //shipyard with the ship
  125. std::string name = readTo(buf,it,'\t'),
  126. description = readTo(buf,it,'\n');
  127. for(int fi=0;fi<GameConstants::F_NUMBER;fi++)
  128. {
  129. buildings[fi][20].first = name;
  130. buildings[fi][20].second = description;
  131. }
  132. for(int fi=0;fi<GameConstants::F_NUMBER;fi++)
  133. {
  134. buildings[fi][16].first = readTo(buf,it,'\t'),
  135. buildings[fi][16].second = readTo(buf,it,'\n');
  136. }
  137. /////done reading "BLDGNEUT.TXT"******************************
  138. buf = getTextFile("BLDGSPEC.TXT");
  139. andame = buf.size(), it=0;
  140. for(int f=0;f<GameConstants::F_NUMBER;f++)
  141. {
  142. for(int b=0;b<9;b++)
  143. {
  144. buildings[f][17+b].first = readTo(buf,it,'\t');
  145. buildings[f][17+b].second = readTo(buf,it,'\n');
  146. }
  147. buildings[f][26].first = readTo(buf,it,'\t');
  148. buildings[f][26].second = readTo(buf,it,'\n');
  149. buildings[f][15].first = readTo(buf,it,'\t'); //resource silo
  150. buildings[f][15].second = readTo(buf,it,'\n');//resource silo
  151. }
  152. /////done reading BLDGSPEC.TXT*********************************
  153. buf = getTextFile("DWELLING.TXT");
  154. andame = buf.size(), it=0;
  155. for(int f=0;f<GameConstants::F_NUMBER;f++)
  156. {
  157. for(int b=0;b<14;b++)
  158. {
  159. buildings[f][30+b].first = readTo(buf,it,'\t');
  160. buildings[f][30+b].second = readTo(buf,it,'\n');
  161. }
  162. }
  163. //remove prceeding / trailing whitespaces nad quoation marks from buildings descriptions
  164. for(std::map<int, std::map<int, std::pair<std::string, std::string> > >::iterator i = buildings.begin(); i != buildings.end(); i++)
  165. {
  166. for(std::map<int, std::pair<std::string, std::string> >::iterator j = i->second.begin(); j != i->second.end(); j++)
  167. {
  168. std::string &str = j->second.second;
  169. boost::algorithm::trim(str);
  170. trimQuotation(str);
  171. }
  172. }
  173. buf = getTextFile("TCOMMAND.TXT");
  174. itr=0;
  175. while(itr<buf.length()-1)
  176. {
  177. std::string tmp;
  178. loadToIt(tmp, buf, itr, 3);
  179. tcommands.push_back(tmp);
  180. }
  181. buf = getTextFile("HALLINFO.TXT");
  182. itr=0;
  183. while(itr<buf.length()-1)
  184. {
  185. std::string tmp;
  186. loadToIt(tmp, buf, itr, 3);
  187. hcommands.push_back(tmp);
  188. }
  189. buf = getTextFile("CASTINFO.TXT");
  190. itr=0;
  191. while(itr<buf.length()-1)
  192. {
  193. std::string tmp;
  194. loadToIt(tmp, buf, itr, 3);
  195. fcommands.push_back(tmp);
  196. }
  197. std::istringstream ins, namess;
  198. ins.str(getTextFile("TOWNTYPE.TXT"));
  199. namess.str(getTextFile("TOWNNAME.TXT"));
  200. int si=0;
  201. char bufname[75];
  202. while (!ins.eof())
  203. {
  204. ins.getline(bufname,50);
  205. townTypes.push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  206. townNames.resize(si+1);
  207. for (int i=0; i<GameConstants::NAMES_PER_TOWN; i++)
  208. {
  209. namess.getline(bufname,50);
  210. townNames[si].push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  211. }
  212. si++;
  213. }
  214. tlog5 << "\t\tReading OBJNAMES \n";
  215. buf = getTextFile("OBJNAMES.TXT");
  216. it=0; //hope that -1 will not break this
  217. while (it<buf.length()-1)
  218. {
  219. std::string nobj;
  220. loadToIt(nobj, buf, it, 3);
  221. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  222. {
  223. nobj = nobj.substr(0, nobj.size()-1);
  224. }
  225. names.push_back(nobj);
  226. }
  227. tlog5 << "\t\tReading ADVEVENT \n";
  228. buf = getTextFile("ADVEVENT.TXT");
  229. it=0;
  230. std::string temp;
  231. while (it<buf.length()-1)
  232. {
  233. loadToIt(temp,buf,it,3);
  234. if (temp[0]=='\"')
  235. {
  236. temp = temp.substr(1,temp.length()-2);
  237. }
  238. boost::algorithm::replace_all(temp,"\"\"","\"");
  239. advobtxt.push_back(temp);
  240. }
  241. tlog5 << "\t\tReading XTRAINFO \n";
  242. buf = getTextFile("XTRAINFO.TXT");
  243. it=0;
  244. while (it<buf.length()-1)
  245. {
  246. loadToIt(temp,buf,it,3);
  247. xtrainfo.push_back(temp);
  248. }
  249. tlog5 << "\t\tReading MINENAME \n";
  250. buf = getTextFile("MINENAME.TXT");
  251. it=0;
  252. while (it<buf.length()-1)
  253. {
  254. loadToIt(temp,buf,it,3);
  255. mines.push_back(std::pair<std::string,std::string>(temp,""));
  256. }
  257. tlog5 << "\t\tReading MINEEVNT \n";
  258. buf = getTextFile("MINEEVNT.TXT");
  259. it=0;
  260. i=0;
  261. while (it<buf.length()-1)
  262. {
  263. loadToIt(temp,buf,it,3);
  264. temp = temp.substr(1,temp.length()-2);
  265. if(i < mines.size())
  266. mines[i++].second = temp;
  267. else
  268. tlog2 << "Warning - too much entries in MINEEVNT. Omitting this one: " << temp << std::endl;
  269. }
  270. tlog5 << "\t\tReading RESTYPES \n";
  271. buf = getTextFile("RESTYPES.TXT");
  272. it=0;
  273. while (it<buf.length()-1)
  274. {
  275. loadToIt(temp,buf,it,3);
  276. restypes.push_back(temp);
  277. }
  278. tlog5 << "\t\tReading TERRNAME \n";
  279. buf = getTextFile("TERRNAME.TXT");
  280. it=0;
  281. while (it<buf.length()-1)
  282. {
  283. loadToIt(temp,buf,it,3);
  284. terrainNames.push_back(temp);
  285. }
  286. tlog5 << "\t\tReading RANDSIGN \n";
  287. buf = getTextFile("RANDSIGN.TXT");
  288. it=0;
  289. while (it<buf.length()-1)
  290. {
  291. loadToIt(temp,buf,it,3);
  292. randsign.push_back(temp);
  293. }
  294. tlog5 << "\t\tReading ZCRGN1 \n";
  295. buf = getTextFile("ZCRGN1.TXT");
  296. it=0;
  297. while (it<buf.length()-1)
  298. {
  299. loadToIt(temp,buf,it,3);
  300. creGens.push_back(temp);
  301. }
  302. tlog5 << "\t\tReading CRGN4 \n";
  303. buf = getTextFile("CRGEN4.TXT");
  304. it=0;
  305. while (it<buf.length()-1)
  306. {
  307. loadToIt(temp,buf,it,3);
  308. creGens4.push_back(temp);
  309. }
  310. buf = getTextFile("GENRLTXT.TXT");
  311. std::string tmp;
  312. andame = buf.size();
  313. i=0; //buf iterator
  314. for(; i<andame; ++i)
  315. {
  316. if(buf[i]=='\r')
  317. break;
  318. }
  319. i+=2;
  320. std::string buflet;
  321. for(int jj=0; jj<764; ++jj)
  322. {
  323. loadToIt(buflet, buf, i, 2);
  324. trimQuotation(buflet);
  325. boost::algorithm::replace_all(buflet,"\"\"","\"");
  326. allTexts.push_back(buflet);
  327. }
  328. std::string stro = getTextFile("Overview.txt");
  329. itr=0;
  330. while(itr<stro.length()-1)
  331. {
  332. loadToIt(tmp, stro, itr, 3);
  333. trimQuotation(tmp);
  334. overview.push_back(tmp);
  335. }
  336. std::string strc = getTextFile("PLCOLORS.TXT");
  337. itr=0;
  338. while(itr<strc.length()-1)
  339. {
  340. loadToIt(tmp, strc, itr, 3);
  341. colors.push_back(tmp);
  342. tmp[0] = toupper(tmp[0]);
  343. capColors.push_back(tmp);
  344. }
  345. std::string strs = getTextFile("ARRAYTXT.TXT");
  346. itr=0;
  347. while(itr<strs.length()-1)
  348. {
  349. loadToIt(tmp, strs, itr, 3);
  350. trimQuotation(tmp);
  351. arraytxt.push_back(tmp);
  352. }
  353. itr = 0;
  354. std::string strin = getTextFile("PRISKILL.TXT");
  355. for(int hh=0; hh<4; ++hh)
  356. {
  357. loadToIt(tmp, strin, itr, 3);
  358. primarySkillNames.push_back(tmp);
  359. }
  360. itr = 0;
  361. strin = getTextFile("JKTEXT.TXT");
  362. for(int hh=0; hh<45; ++hh)
  363. {
  364. loadToIt(tmp, strin, itr, 3);
  365. trimQuotation(tmp);
  366. jktexts.push_back(tmp);
  367. }
  368. itr = 0;
  369. strin = getTextFile("TVRNINFO.TXT");
  370. for(int hh=0; hh<8; ++hh)
  371. {
  372. loadToIt(tmp, strin, itr, 3);
  373. tavernInfo.push_back(tmp);
  374. }
  375. itr = 0;
  376. strin = getTextFile("TURNDUR.TXT");
  377. for(int hh=0; hh<11; ++hh)
  378. {
  379. loadToIt(tmp, strin, itr, 3);
  380. turnDurations.push_back(tmp);
  381. }
  382. itr = 0;
  383. strin = getTextFile("HEROSCRN.TXT");
  384. for(int hh=0; hh<33; ++hh)
  385. {
  386. loadToIt(tmp, strin, itr, 3);
  387. heroscrn.push_back(tmp);
  388. }
  389. itr = 0;
  390. strin = getTextFile("ARTEVENT.TXT");
  391. for(; itr<strin.size();)
  392. {
  393. loadToIt(tmp, strin, itr, 2);
  394. // boost::algorithm::trim(tmp);
  395. trimQuotation(tmp);
  396. boost::algorithm::replace_all(tmp,"\"\"","\"");
  397. artifEvents.push_back(tmp);
  398. }
  399. buf = getTextFile("SSTRAITS.TXT");
  400. it=0;
  401. for(int i=0; i<2; ++i)
  402. loadToIt(dump,buf,it,3);
  403. skillName.resize(GameConstants::SKILL_QUANTITY);
  404. skillInfoTexts.resize(GameConstants::SKILL_QUANTITY);
  405. for (int i=0; i<GameConstants::SKILL_QUANTITY; i++)
  406. {
  407. skillInfoTexts[i].resize(3);
  408. loadToIt(skillName[i],buf,it,4);
  409. loadToIt(skillInfoTexts[i][0],buf,it,4);
  410. loadToIt(skillInfoTexts[i][1],buf,it,4);
  411. loadToIt(skillInfoTexts[i][2],buf,it,3);
  412. for(int j = 0; j < 3; j++)
  413. trimQuotation(skillInfoTexts[i][j]);
  414. }
  415. buf = getTextFile("SKILLLEV.TXT");
  416. it=0;
  417. for(int i=0; i<6; ++i)
  418. {
  419. std::string buffo;
  420. loadToIt(buffo,buf,it,3);
  421. levels.push_back(buffo);
  422. }
  423. buf = getTextFile ("SEERHUT.TXT");
  424. it = 0;
  425. loadToIt (dump, buf, it, 3);
  426. loadToIt (dump, buf, it, 4); //dump description
  427. seerEmpty.resize(6);
  428. for (i = 0; i < 5; ++i)
  429. {
  430. loadToIt(seerEmpty[i], buf, it, 4);
  431. trimQuotation (seerEmpty[i]);
  432. }
  433. loadToIt (seerEmpty[5], buf, it, 3);
  434. trimQuotation (seerEmpty[5]);
  435. int j,k;
  436. quests.resize(10);
  437. for (i = 0; i < 9; ++i) //9 types of quests
  438. {
  439. quests[i].resize(5);
  440. for (j = 0; j < 5; ++j)
  441. {
  442. loadToIt (dump, buf, it, 4); //front description
  443. quests[i][j].resize(6);
  444. for (k = 0; k < 5; ++k)
  445. {
  446. loadToIt (quests[i][j][k], buf, it, 4);
  447. trimQuotation (quests[i][j][k]);
  448. }
  449. loadToIt (quests[i][j][5], buf, it, 3);
  450. trimQuotation (quests[i][j][5]);
  451. }
  452. }
  453. quests[9].resize(1);
  454. quests[9][0].resize(6);
  455. for (k = 0; k < 5; ++k) //Time limit
  456. {
  457. loadToIt (quests[9][0][k], buf, it, 4);
  458. }
  459. loadToIt (quests[9][0][k], buf, it, 3);
  460. for (i = 0; i < 2; ++i) //gap description
  461. loadToIt(dump,buf,it,3);
  462. seerNames.resize(48);
  463. for (i = 0; i < 48; ++i)
  464. loadToIt(seerNames[i], buf, it, 3);
  465. buf = getTextFile("TENTCOLR.TXT");
  466. itr=0;
  467. while(itr<buf.length()-1)
  468. {
  469. std::string tmp;
  470. loadToIt(tmp, buf, itr, 3);
  471. tentColors.push_back(tmp);
  472. }
  473. //campaigns
  474. buf = getTextFile ("CAMPTEXT.TXT");
  475. it = 0;
  476. loadToIt (dump, buf, it, 3); //comment
  477. std::string nameBuf;
  478. do //map names
  479. {
  480. loadToIt(nameBuf, buf, it, 3);
  481. if(nameBuf.size())
  482. {
  483. campaignMapNames.push_back(nameBuf);
  484. }
  485. } while (nameBuf.size());
  486. campaignRegionNames.resize(campaignMapNames.size()); //allocating space
  487. for(int g=0; g<campaignMapNames.size(); ++g) //region names
  488. {
  489. do //dump comments and empty lines
  490. {
  491. loadToIt(nameBuf, buf, it, 3);
  492. } while (!nameBuf.size() || nameBuf[0] != '/');
  493. do //actual names
  494. {
  495. loadToIt(nameBuf, buf, it, 3);
  496. if(nameBuf.size())
  497. {
  498. campaignRegionNames[g].push_back(nameBuf);
  499. }
  500. } while (nameBuf.size());
  501. }
  502. buf = getTextFile ("ZCREXP.TXT");
  503. it = 0;
  504. loadToIt (dump, buf, it, 3); //comment
  505. for (int i = 0; i < 459; ++i) //some texts seem to be empty
  506. {
  507. loadToIt(dump, buf, it, 4); //description, usually useless
  508. loadToIt(nameBuf, buf, it, 3);
  509. zcrexp.push_back(nameBuf);
  510. }
  511. std::string threatLevelDir = GameConstants::DATA_DIR + "/config/threatlevel.txt";
  512. std::ifstream ifs(threatLevelDir.c_str(), std::ios::in | std::ios::binary);
  513. getline(ifs, buf); //skip 1st line
  514. for (int i = 0; i < 13; ++i)
  515. {
  516. getline(ifs, buf);
  517. threat.push_back(buf);
  518. }
  519. }
  520. std::string CGeneralTextHandler::getTitle(const std::string & text)
  521. {
  522. std::string ret;
  523. int i=0;
  524. while ((text[i++]!='{'));
  525. while ((text[i]!='}') && (i<text.length()))
  526. ret+=text[i++];
  527. return ret;
  528. }
  529. std::string CGeneralTextHandler::getDescr(const std::string & text)
  530. {
  531. std::string ret;
  532. int i=0;
  533. while ((text[i++]!='}'));
  534. i+=2;
  535. while ((text[i]!='"') && (i<text.length()))
  536. ret+=text[i++];
  537. return ret;
  538. }
  539. CGeneralTextHandler::CGeneralTextHandler()
  540. {
  541. }