2
0

CGeneralTextHandler.cpp 8.5 KB

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