CGeneralTextHandler.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "StdInc.h"
  2. #include "CGeneralTextHandler.h"
  3. #include "filesystem/CResourceLoader.h"
  4. #include "filesystem/CInputStream.h"
  5. #include "GameConstants.h"
  6. #include "CModHandler.h"
  7. #include "VCMI_Lib.h"
  8. // #include <locale> //needed?
  9. /*
  10. * CGeneralTextHandler.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. //Helper for string -> float conversion
  19. class LocaleWithComma: public std::numpunct<char>
  20. {
  21. protected:
  22. char do_decimal_point() const
  23. {
  24. return ',';
  25. }
  26. };
  27. CLegacyConfigParser::CLegacyConfigParser(std::string URI)
  28. {
  29. init(CResourceHandler::get()->load(ResourceID(URI, EResType::TEXT)));
  30. }
  31. CLegacyConfigParser::CLegacyConfigParser(const std::unique_ptr<CInputStream> & input)
  32. {
  33. init(input);
  34. }
  35. void CLegacyConfigParser::init(const std::unique_ptr<CInputStream> & input)
  36. {
  37. data.reset(new char[input->getSize()]);
  38. input->read((ui8*)data.get(), input->getSize());
  39. curr = data.get();
  40. end = curr + input->getSize();
  41. }
  42. std::string CLegacyConfigParser::extractQuotedPart()
  43. {
  44. assert(*curr == '\"');
  45. curr++; // skip quote
  46. char * begin = curr;
  47. while (curr != end && *curr != '\"' && *curr != '\t')
  48. curr++;
  49. return std::string(begin, curr++); //increment curr to close quote
  50. }
  51. std::string CLegacyConfigParser::extractQuotedString()
  52. {
  53. assert(*curr == '\"');
  54. std::string ret;
  55. while (true)
  56. {
  57. ret += extractQuotedPart();
  58. // double quote - add it to string and continue unless
  59. // line terminated using tabulation
  60. if (curr < end && *curr == '\"' && *curr != '\t')
  61. {
  62. ret += '\"';
  63. }
  64. else // end of string
  65. return ret;
  66. }
  67. }
  68. std::string CLegacyConfigParser::extractNormalString()
  69. {
  70. char * begin = curr;
  71. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  72. curr++;
  73. return std::string(begin, curr);
  74. }
  75. std::string CLegacyConfigParser::readString()
  76. {
  77. if (curr >= end || *curr == '\n')
  78. return "";
  79. std::string ret;
  80. if (*curr == '\"')
  81. ret = extractQuotedString();// quoted text - find closing quote
  82. else
  83. ret = extractNormalString();//string without quotes - copy till \t or \r
  84. curr++;
  85. return ret;
  86. }
  87. float CLegacyConfigParser::readNumber()
  88. {
  89. std::string input = readString();
  90. std::istringstream stream(input);
  91. if (input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  92. stream.imbue(std::locale(std::locale(), new LocaleWithComma));
  93. int result;
  94. if ( !(stream >> result) )
  95. return 0;
  96. return result;
  97. }
  98. bool CLegacyConfigParser::isNextEntryEmpty()
  99. {
  100. char * nextSymbol = curr;
  101. while (nextSymbol < end && *nextSymbol == ' ')
  102. nextSymbol++; //find next meaningfull symbol
  103. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  104. }
  105. bool CLegacyConfigParser::endLine()
  106. {
  107. while (curr < end && *curr != '\n')
  108. readString();
  109. curr++;
  110. return curr < end;
  111. }
  112. void CGeneralTextHandler::readToVector(std::string sourceName, std::vector<std::string> & dest)
  113. {
  114. CLegacyConfigParser parser(sourceName);
  115. do
  116. {
  117. dest.push_back(parser.readString());
  118. }
  119. while (parser.endLine());
  120. }
  121. CGeneralTextHandler::CGeneralTextHandler()
  122. {
  123. readToVector("DATA/VCDESC.TXT", victoryConditions);
  124. readToVector("DATA/LCDESC.TXT", lossCondtions);
  125. readToVector("DATA/TCOMMAND.TXT", tcommands);
  126. readToVector("DATA/HALLINFO.TXT", hcommands);
  127. readToVector("DATA/CASTINFO.TXT", fcommands);
  128. readToVector("DATA/ADVEVENT.TXT", advobtxt);
  129. readToVector("DATA/XTRAINFO.TXT", xtrainfo);
  130. readToVector("DATA/RESTYPES.TXT", restypes);
  131. readToVector("DATA/TERRNAME.TXT", terrainNames);
  132. readToVector("DATA/RANDSIGN.TXT", randsign);
  133. readToVector("DATA/CRGEN1.TXT", creGens);
  134. readToVector("DATA/CRGEN4.TXT", creGens4);
  135. readToVector("DATA/OVERVIEW.TXT", overview);
  136. readToVector("DATA/ARRAYTXT.TXT", arraytxt);
  137. readToVector("DATA/PRISKILL.TXT", primarySkillNames);
  138. readToVector("DATA/JKTEXT.TXT", jktexts);
  139. readToVector("DATA/TVRNINFO.TXT", tavernInfo);
  140. readToVector("DATA/TURNDUR.TXT", turnDurations);
  141. readToVector("DATA/HEROSCRN.TXT", heroscrn);
  142. readToVector("DATA/TENTCOLR.TXT", tentColors);
  143. readToVector("DATA/SKILLLEV.TXT", levels);
  144. readToVector("DATA/OBJNAMES.TXT", names);
  145. {
  146. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  147. parser.endLine();
  148. do
  149. {
  150. allTexts.push_back(parser.readString());
  151. }
  152. while (parser.endLine());
  153. }
  154. {
  155. CLegacyConfigParser parser("DATA/HELP.TXT");
  156. do
  157. {
  158. std::string first = parser.readString();
  159. std::string second = parser.readString();
  160. zelp.push_back(std::make_pair(first, second));
  161. }
  162. while (parser.endLine());
  163. }
  164. {
  165. CLegacyConfigParser nameParser("DATA/MINENAME.TXT");
  166. CLegacyConfigParser eventParser("DATA/MINEEVNT.TXT");
  167. do
  168. {
  169. std::string name = nameParser.readString();
  170. std::string event = eventParser.readString();
  171. mines.push_back(std::make_pair(name, event));
  172. }
  173. while (nameParser.endLine() && eventParser.endLine());
  174. }
  175. {
  176. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  177. do
  178. {
  179. std::string color = parser.readString();
  180. colors.push_back(color);
  181. color[0] = toupper(color[0]);
  182. capColors.push_back(color);
  183. }
  184. while (parser.endLine());
  185. }
  186. {
  187. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  188. //skip header
  189. parser.endLine();
  190. parser.endLine();
  191. do
  192. {
  193. skillName.push_back(parser.readString());
  194. skillInfoTexts.push_back(std::vector<std::string>());
  195. for(int j = 0; j < 3; j++)
  196. skillInfoTexts.back().push_back(parser.readString());
  197. }
  198. while (parser.endLine());
  199. }
  200. {
  201. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  202. //skip header
  203. parser.endLine();
  204. parser.endLine();
  205. for (int i = 0; i < 6; ++i)
  206. seerEmpty.push_back(parser.readString());
  207. quests.resize(10);
  208. for (int i = 0; i < 9; ++i) //9 types of quests
  209. {
  210. quests[i].resize(5);
  211. for (int j = 0; j < 5; ++j)
  212. {
  213. parser.readString(); //front description
  214. for (int k = 0; k < 6; ++k)
  215. quests[i][j].push_back(parser.readString());
  216. parser.endLine();
  217. }
  218. }
  219. quests[9].resize(1);
  220. for (int k = 0; k < 6; ++k) //Time limit
  221. {
  222. quests[9][0].push_back(parser.readString());
  223. }
  224. parser.endLine();
  225. parser.endLine(); // empty line
  226. parser.endLine(); // header
  227. for (int i = 0; i < 48; ++i)
  228. {
  229. seerNames.push_back(parser.readString());
  230. parser.endLine();
  231. }
  232. }
  233. {
  234. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  235. //skip header
  236. parser.endLine();
  237. std::string text;
  238. do
  239. {
  240. text = parser.readString();
  241. if (!text.empty())
  242. campaignMapNames.push_back(text);
  243. }
  244. while (parser.endLine() && !text.empty());
  245. for (size_t i=0; i<campaignMapNames.size(); i++)
  246. {
  247. do // skip empty space and header
  248. {
  249. text = parser.readString();
  250. }
  251. while (parser.endLine() && text.empty());
  252. campaignRegionNames.push_back(std::vector<std::string>());
  253. do
  254. {
  255. text = parser.readString();
  256. if (!text.empty())
  257. campaignRegionNames.back().push_back(text);
  258. }
  259. while (parser.endLine() && !text.empty());
  260. }
  261. }
  262. if (VLC->modh->modules.STACK_EXP)
  263. {
  264. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  265. parser.endLine();//header
  266. do
  267. {
  268. parser.readString(); //ignore 1st column with description
  269. zcrexp.push_back(parser.readString());
  270. }
  271. while (parser.endLine());
  272. }
  273. std::string buffer;
  274. std::ifstream ifs(CResourceHandler::get()->getResourceName(ResourceID("config/threatlevel.txt")), std::ios::binary);
  275. getline(ifs, buffer); //skip 1st line
  276. for (int i = 0; i < 13; ++i)
  277. {
  278. getline(ifs, buffer);
  279. threat.push_back(buffer);
  280. }
  281. }