CGeneralTextHandler.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "../lib/VCMI_Lib.h"
  4. #include "CGeneralTextHandler.h"
  5. #include "CLodHandler.h"
  6. #include <boost/algorithm/string.hpp>
  7. #include <boost/algorithm/string/replace.hpp>
  8. #include <fstream>
  9. #include <sstream>
  10. /*
  11. * CGeneralTextHandler.cpp, part of VCMI engine
  12. *
  13. * Authors: listed in file AUTHORS in main folder
  14. *
  15. * License: GNU General Public License v2.0 or later
  16. * Full text of license available in license.txt file, in main folder
  17. *
  18. */
  19. std::string readTo(std::string &in, int &it, char end)
  20. {
  21. int pom = it;
  22. int last = in.find_first_of(end,it);
  23. it+=(1+last-it);
  24. return in.substr(pom,last-pom);
  25. }
  26. void CGeneralTextHandler::load()
  27. {
  28. std::string buf1 = bitmaph->getTextFile("ZELP.TXT");
  29. int itr=0, eol=-1, eolnext=-1, pom;
  30. eolnext = buf1.find_first_of('\r',itr);
  31. while(itr<buf1.size())
  32. {
  33. eol = eolnext; //end of this line
  34. eolnext = buf1.find_first_of('\r',eol+1); //end of the next line
  35. pom=buf1.find_first_of('\t',itr); //upcoming tab
  36. if(eol<0 || pom<0)
  37. break;
  38. if(pom>eol) //in current line there is not tab
  39. zelp.push_back(std::pair<std::string,std::string>());
  40. else
  41. {
  42. zelp.push_back
  43. (std::pair<std::string,std::string>
  44. (buf1.substr(itr,pom-itr),
  45. buf1.substr(pom+1,eol-pom-1)));
  46. boost::algorithm::replace_all(zelp[zelp.size()-1].first,"\t","");
  47. boost::algorithm::replace_all(zelp[zelp.size()-1].second,"\t","");
  48. }
  49. itr=eol+2;
  50. }
  51. std::string buf = bitmaph->getTextFile("VCDESC.TXT");
  52. int andame = buf.size();
  53. int i=0; //buf iterator
  54. for(int gg=0; gg<14; ++gg)
  55. {
  56. int befi=i;
  57. for(i; i<andame; ++i)
  58. {
  59. if(buf[i]=='\r')
  60. break;
  61. }
  62. victoryConditions[gg] = buf.substr(befi, i-befi);
  63. i+=2;
  64. }
  65. buf = bitmaph->getTextFile("LCDESC.TXT");
  66. andame = buf.size();
  67. i=0; //buf iterator
  68. for(int gg=0; gg<4; ++gg)
  69. {
  70. int befi=i;
  71. for(i; i<andame; ++i)
  72. {
  73. if(buf[i]=='\r')
  74. break;
  75. }
  76. lossCondtions[gg] = buf.substr(befi, i-befi);
  77. i+=2;
  78. }
  79. hTxts.resize(HEROES_QUANTITY);
  80. buf = bitmaph->getTextFile("HEROSPEC.TXT");
  81. i=0;
  82. std::string dump;
  83. for(int iii=0; iii<2; ++iii)
  84. {
  85. loadToIt(dump,buf,i,3);
  86. }
  87. for (int iii=0;iii<hTxts.size();iii++)
  88. {
  89. loadToIt(hTxts[iii].bonusName,buf,i,4);
  90. loadToIt(hTxts[iii].shortBonus,buf,i,4);
  91. loadToIt(hTxts[iii].longBonus,buf,i,3);
  92. }
  93. buf = bitmaph->getTextFile("HEROBIOS.TXT");
  94. i=0;
  95. for (int iii=0;iii<hTxts.size();iii++)
  96. {
  97. loadToIt(hTxts[iii].biography,buf,i,3);
  98. }
  99. int it;
  100. buf = bitmaph->getTextFile("BLDGNEUT.TXT");
  101. andame = buf.size(), it=0;
  102. for(int b=0;b<15;b++)
  103. {
  104. std::string name = readTo(buf,it,'\t'),
  105. description = readTo(buf,it,'\n');
  106. for(int fi=0;fi<F_NUMBER;fi++)
  107. {
  108. buildings[fi][b].first = name;
  109. buildings[fi][b].second = description;
  110. }
  111. }
  112. buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');buf1 = readTo(buf,it,'\n');//silo,blacksmith,moat - useless???
  113. //shipyard with the ship
  114. std::string name = readTo(buf,it,'\t'),
  115. description = readTo(buf,it,'\n');
  116. for(int fi=0;fi<F_NUMBER;fi++)
  117. {
  118. buildings[fi][20].first = name;
  119. buildings[fi][20].second = description;
  120. }
  121. for(int fi=0;fi<F_NUMBER;fi++)
  122. {
  123. buildings[fi][16].first = readTo(buf,it,'\t'),
  124. buildings[fi][16].second = readTo(buf,it,'\n');
  125. }
  126. /////done reading "BLDGNEUT.TXT"******************************
  127. buf = bitmaph->getTextFile("BLDGSPEC.TXT");
  128. andame = buf.size(), it=0;
  129. for(int f=0;f<F_NUMBER;f++)
  130. {
  131. for(int b=0;b<9;b++)
  132. {
  133. buildings[f][17+b].first = readTo(buf,it,'\t');
  134. buildings[f][17+b].second = readTo(buf,it,'\n');
  135. }
  136. buildings[f][26].first = readTo(buf,it,'\t');
  137. buildings[f][26].second = readTo(buf,it,'\n');
  138. buildings[f][15].first = readTo(buf,it,'\t'); //resource silo
  139. buildings[f][15].second = readTo(buf,it,'\n');//resource silo
  140. }
  141. /////done reading BLDGSPEC.TXT*********************************
  142. buf = bitmaph->getTextFile("DWELLING.TXT");
  143. andame = buf.size(), it=0;
  144. for(int f=0;f<F_NUMBER;f++)
  145. {
  146. for(int b=0;b<14;b++)
  147. {
  148. buildings[f][30+b].first = readTo(buf,it,'\t');
  149. buildings[f][30+b].second = readTo(buf,it,'\n');
  150. }
  151. }
  152. //remove prceeding / trailing whitespaces nad quoation marks from buildings descriptions
  153. for(std::map<int, std::map<int, std::pair<std::string, std::string> > >::iterator i = buildings.begin(); i != buildings.end(); i++)
  154. {
  155. for(std::map<int, std::pair<std::string, std::string> >::iterator j = i->second.begin(); j != i->second.end(); j++)
  156. {
  157. std::string &str = j->second.second;
  158. boost::algorithm::trim(str);
  159. if(str[0] == '"' && str[str.size()-1] == '"')
  160. str = str.substr(1,str.size()-2);
  161. }
  162. }
  163. buf = bitmaph->getTextFile("TCOMMAND.TXT");
  164. itr=0;
  165. while(itr<buf.length()-1)
  166. {
  167. std::string tmp;
  168. loadToIt(tmp, buf, itr, 3);
  169. tcommands.push_back(tmp);
  170. }
  171. buf = bitmaph->getTextFile("HALLINFO.TXT");
  172. itr=0;
  173. while(itr<buf.length()-1)
  174. {
  175. std::string tmp;
  176. loadToIt(tmp, buf, itr, 3);
  177. hcommands.push_back(tmp);
  178. }
  179. std::istringstream ins, namess;
  180. ins.str(bitmaph->getTextFile("TOWNTYPE.TXT"));
  181. namess.str(bitmaph->getTextFile("TOWNNAME.TXT"));
  182. int si=0;
  183. char bufname[75];
  184. while (!ins.eof())
  185. {
  186. ins.getline(bufname,50);
  187. townTypes.push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  188. townNames.resize(si+1);
  189. for (int i=0; i<NAMES_PER_TOWN; i++)
  190. {
  191. namess.getline(bufname,50);
  192. townNames[si].push_back(std::string(bufname).substr(0,strlen(bufname)-1));
  193. }
  194. si++;
  195. }
  196. tlog5 << "\t\tReading OBJNAMES \n";
  197. buf = bitmaph->getTextFile("OBJNAMES.TXT");
  198. it=0; //hope that -1 will not break this
  199. while (it<buf.length()-1)
  200. {
  201. std::string nobj;
  202. loadToIt(nobj, buf, it, 3);
  203. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  204. {
  205. nobj = nobj.substr(0, nobj.size()-1);
  206. }
  207. names.push_back(nobj);
  208. }
  209. tlog5 << "\t\tReading ADVEVENT \n";
  210. buf = bitmaph->getTextFile("ADVEVENT.TXT");
  211. it=0;
  212. std::string temp;
  213. while (it<buf.length()-1)
  214. {
  215. loadToIt(temp,buf,it,3);
  216. if (temp[0]=='\"')
  217. {
  218. temp = temp.substr(1,temp.length()-2);
  219. }
  220. boost::algorithm::replace_all(temp,"\"\"","\"");
  221. advobtxt.push_back(temp);
  222. }
  223. tlog5 << "\t\tReading XTRAINFO \n";
  224. buf = bitmaph->getTextFile("XTRAINFO.TXT");
  225. it=0;
  226. while (it<buf.length()-1)
  227. {
  228. loadToIt(temp,buf,it,3);
  229. xtrainfo.push_back(temp);
  230. }
  231. tlog5 << "\t\tReading MINENAME \n";
  232. buf = bitmaph->getTextFile("MINENAME.TXT");
  233. it=0;
  234. while (it<buf.length()-1)
  235. {
  236. loadToIt(temp,buf,it,3);
  237. mines.push_back(std::pair<std::string,std::string>(temp,""));
  238. }
  239. tlog5 << "\t\tReading MINEEVNT \n";
  240. buf = bitmaph->getTextFile("MINEEVNT.TXT");
  241. it=0;
  242. i=0;
  243. while (it<buf.length()-1)
  244. {
  245. loadToIt(temp,buf,it,3);
  246. temp = temp.substr(1,temp.length()-2);
  247. if(i < mines.size())
  248. mines[i++].second = temp;
  249. else
  250. tlog2 << "Warning - too much entries in MINEEVNT. Omitting this one: " << temp << std::endl;
  251. }
  252. tlog5 << "\t\tReading RESTYPES \n";
  253. buf = bitmaph->getTextFile("RESTYPES.TXT");
  254. it=0;
  255. while (it<buf.length()-1)
  256. {
  257. loadToIt(temp,buf,it,3);
  258. restypes.push_back(temp);
  259. }
  260. tlog5 << "\t\tReading RANDSIGN \n";
  261. buf = bitmaph->getTextFile("RANDSIGN.TXT");
  262. it=0;
  263. while (it<buf.length()-1)
  264. {
  265. loadToIt(temp,buf,it,3);
  266. randsign.push_back(temp);
  267. }
  268. tlog5 << "\t\tReading ZCRGN1 \n";
  269. buf = bitmaph->getTextFile("ZCRGN1.TXT");
  270. it=0;
  271. while (it<buf.length()-1)
  272. {
  273. loadToIt(temp,buf,it,3);
  274. creGens.push_back(temp);
  275. }
  276. buf = bitmaph->getTextFile("GENRLTXT.TXT");
  277. std::string tmp;
  278. andame = buf.size();
  279. i=0; //buf iterator
  280. for(i; i<andame; ++i)
  281. {
  282. if(buf[i]=='\r')
  283. break;
  284. }
  285. i+=2;
  286. std::string buflet;
  287. for(int jj=0; jj<764; ++jj)
  288. {
  289. loadToIt(buflet, buf, i, 2);
  290. if(buflet[0] == '"' && buflet[buflet.size()-1] == '"')
  291. buflet = buflet.substr(1,buflet.size()-2);
  292. allTexts.push_back(buflet);
  293. }
  294. std::string strs = bitmaph->getTextFile("ARRAYTXT.TXT");
  295. itr=0;
  296. while(itr<strs.length()-1)
  297. {
  298. loadToIt(tmp, strs, itr, 3);
  299. if(tmp[0] == '"' && tmp[tmp.size()-1] == '"')
  300. tmp = tmp.substr(1,tmp.size()-2);
  301. arraytxt.push_back(tmp);
  302. }
  303. itr = 0;
  304. std::string strin = bitmaph->getTextFile("PRISKILL.TXT");
  305. for(int hh=0; hh<4; ++hh)
  306. {
  307. loadToIt(tmp, strin, itr, 3);
  308. primarySkillNames.push_back(tmp);
  309. }
  310. itr = 0;
  311. strin = bitmaph->getTextFile("JKTEXT.TXT");
  312. for(int hh=0; hh<45; ++hh)
  313. {
  314. loadToIt(tmp, strin, itr, 3);
  315. jktexts.push_back(tmp);
  316. }
  317. itr = 0;
  318. strin = bitmaph->getTextFile("TVRNINFO.TXT");
  319. for(int hh=0; hh<8; ++hh)
  320. {
  321. loadToIt(tmp, strin, itr, 3);
  322. tavernInfo.push_back(tmp);
  323. }
  324. itr = 0;
  325. strin = bitmaph->getTextFile("HEROSCRN.TXT");
  326. for(int hh=0; hh<33; ++hh)
  327. {
  328. loadToIt(tmp, strin, itr, 3);
  329. heroscrn.push_back(tmp);
  330. }
  331. strin = bitmaph->getTextFile("ARTEVENT.TXT");
  332. for(itr = 0; itr<strin.size();itr++)
  333. {
  334. loadToIt(tmp, strin, itr, 3);
  335. artifEvents.push_back(tmp);
  336. }
  337. buf = bitmaph->getTextFile("SSTRAITS.TXT");
  338. it=0;
  339. for(int i=0; i<2; ++i)
  340. loadToIt(dump,buf,it,3);
  341. skillName.resize(SKILL_QUANTITY);
  342. skillInfoTexts.resize(SKILL_QUANTITY);
  343. for (int i=0; i<SKILL_QUANTITY; i++)
  344. {
  345. skillInfoTexts[i].resize(3);
  346. loadToIt(skillName[i],buf,it,4);
  347. loadToIt(skillInfoTexts[i][0],buf,it,4);
  348. loadToIt(skillInfoTexts[i][1],buf,it,4);
  349. loadToIt(skillInfoTexts[i][2],buf,it,3);
  350. }
  351. buf = bitmaph->getTextFile("SKILLLEV.TXT");
  352. it=0;
  353. for(int i=0; i<6; ++i)
  354. {
  355. std::string buffo;
  356. loadToIt(buffo,buf,it,3);
  357. levels.push_back(buffo);
  358. }
  359. }
  360. std::string CGeneralTextHandler::getTitle(std::string text)
  361. {
  362. std::string ret;
  363. int i=0;
  364. while ((text[i++]!='{'));
  365. while ((text[i]!='}') && (i<text.length()))
  366. ret+=text[i++];
  367. return ret;
  368. }
  369. std::string CGeneralTextHandler::getDescr(std::string text)
  370. {
  371. std::string ret;
  372. int i=0;
  373. while ((text[i++]!='}'));
  374. i+=2;
  375. while ((text[i]!='"') && (i<text.length()))
  376. ret+=text[i++];
  377. return ret;
  378. }
  379. CGeneralTextHandler::CGeneralTextHandler()
  380. {
  381. }