CGeneralTextHandler.cpp 14 KB

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