CGeneralTextHandler.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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/TENTCOLR.TXT", tentColors);
  137. readToVector("DATA/SKILLLEV.TXT", levels);
  138. readToVector("DATA/OBJNAMES.TXT", names);
  139. {
  140. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  141. parser.endLine();
  142. do
  143. {
  144. allTexts.push_back(parser.readString());
  145. }
  146. while (parser.endLine());
  147. }
  148. {
  149. CLegacyConfigParser parser("DATA/ZELP.TXT");
  150. do
  151. {
  152. std::string first = parser.readString();
  153. std::string second = parser.readString();
  154. zelp.push_back(std::make_pair(first, second));
  155. }
  156. while (parser.endLine());
  157. }
  158. {
  159. CLegacyConfigParser parser("DATA/HEROSPEC.TXT");
  160. CLegacyConfigParser bioParser("DATA/HEROBIOS.TXT");
  161. //skip header
  162. parser.endLine();
  163. parser.endLine();
  164. do
  165. {
  166. HeroTexts texts;
  167. texts.bonusName = parser.readString();
  168. texts.shortBonus = parser.readString();
  169. texts.longBonus = parser.readString();
  170. texts.biography = bioParser.readString();
  171. hTxts.push_back(texts);
  172. }
  173. while (parser.endLine() && bioParser.endLine());
  174. }
  175. {
  176. CLegacyConfigParser nameParser("DATA/MINENAME.TXT");
  177. CLegacyConfigParser eventParser("DATA/MINEEVNT.TXT");
  178. do
  179. {
  180. std::string name = nameParser.readString();
  181. std::string event = eventParser.readString();
  182. mines.push_back(std::make_pair(name, event));
  183. }
  184. while (nameParser.endLine() && eventParser.endLine());
  185. }
  186. {
  187. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  188. do
  189. {
  190. std::string color = parser.readString();
  191. colors.push_back(color);
  192. color[0] = toupper(color[0]);
  193. capColors.push_back(color);
  194. }
  195. while (parser.endLine());
  196. }
  197. {
  198. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  199. //skip header
  200. parser.endLine();
  201. parser.endLine();
  202. do
  203. {
  204. skillName.push_back(parser.readString());
  205. skillInfoTexts.push_back(std::vector<std::string>());
  206. for(int j = 0; j < 3; j++)
  207. skillInfoTexts.back().push_back(parser.readString());
  208. }
  209. while (parser.endLine());
  210. }
  211. {
  212. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  213. //skip header
  214. parser.endLine();
  215. parser.endLine();
  216. for (int i = 0; i < 6; ++i)
  217. seerEmpty.push_back(parser.readString());
  218. quests.resize(10);
  219. for (int i = 0; i < 9; ++i) //9 types of quests
  220. {
  221. quests[i].resize(5);
  222. for (int j = 0; j < 5; ++j)
  223. {
  224. parser.readString(); //front description
  225. for (int k = 0; k < 6; ++k)
  226. quests[i][j].push_back(parser.readString());
  227. parser.endLine();
  228. }
  229. }
  230. quests[9].resize(1);
  231. for (int k = 0; k < 6; ++k) //Time limit
  232. {
  233. quests[9][0].push_back(parser.readString());
  234. }
  235. parser.endLine();
  236. parser.endLine(); // empty line
  237. parser.endLine(); // header
  238. for (int i = 0; i < 48; ++i)
  239. {
  240. seerNames.push_back(parser.readString());
  241. parser.endLine();
  242. }
  243. }
  244. {
  245. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  246. //skip header
  247. parser.endLine();
  248. std::string text;
  249. do
  250. {
  251. text = parser.readString();
  252. if (!text.empty())
  253. campaignMapNames.push_back(text);
  254. }
  255. while (parser.endLine() && !text.empty());
  256. for (size_t i=0; i<campaignMapNames.size(); i++)
  257. {
  258. do // skip empty space and header
  259. {
  260. text = parser.readString();
  261. }
  262. while (parser.endLine() && text.empty());
  263. campaignRegionNames.push_back(std::vector<std::string>());
  264. do
  265. {
  266. text = parser.readString();
  267. if (!text.empty())
  268. campaignRegionNames.back().push_back(text);
  269. }
  270. while (parser.endLine() && !text.empty());
  271. }
  272. }
  273. {
  274. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  275. parser.endLine();//header
  276. do
  277. {
  278. parser.readString(); //ignore 1st column with description
  279. zcrexp.push_back(parser.readString());
  280. }
  281. while (parser.endLine());
  282. }
  283. std::string buffer;
  284. std::ifstream ifs(CResourceHandler::get()->getResourceName(ResourceID("config/threatlevel.txt")), std::ios::binary);
  285. getline(ifs, buffer); //skip 1st line
  286. for (int i = 0; i < 13; ++i)
  287. {
  288. getline(ifs, buffer);
  289. threat.push_back(buffer);
  290. }
  291. }
  292. std::string CGeneralTextHandler::getTitle(const std::string & text)
  293. {
  294. std::string ret;
  295. int i=0;
  296. while ((text[i++]!='{'));
  297. while ((text[i]!='}') && (i<text.length()))
  298. ret+=text[i++];
  299. return ret;
  300. }
  301. std::string CGeneralTextHandler::getDescr(const std::string & text)
  302. {
  303. std::string ret;
  304. int i=0;
  305. while ((text[i++]!='}'));
  306. i+=2;
  307. while ((text[i]!='"') && (i<text.length()))
  308. ret+=text[i++];
  309. return ret;
  310. }
  311. CGeneralTextHandler::CGeneralTextHandler()
  312. {
  313. }