CGeneralTextHandler.cpp 7.7 KB

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