CGeneralTextHandler.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 != '\"' && *curr != '\t')
  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. // double quote - add it to string and continue unless
  57. // line terminated using tabulation
  58. if (curr < end && *curr == '\"' && *curr != '\t')
  59. {
  60. ret += '\"';
  61. }
  62. else // end of string
  63. return ret;
  64. }
  65. }
  66. std::string CLegacyConfigParser::extractNormalString()
  67. {
  68. char * begin = curr;
  69. while (curr < end && *curr != '\t' && *curr != '\r')//find end of string
  70. curr++;
  71. return std::string(begin, curr);
  72. }
  73. std::string CLegacyConfigParser::readString()
  74. {
  75. if (curr >= end || *curr == '\n')
  76. return "";
  77. std::string ret;
  78. if (*curr == '\"')
  79. ret = extractQuotedString();// quoted text - find closing quote
  80. else
  81. ret = extractNormalString();//string without quotes - copy till \t or \r
  82. curr++;
  83. return ret;
  84. }
  85. float CLegacyConfigParser::readNumber()
  86. {
  87. std::string input = readString();
  88. std::istringstream stream(input);
  89. if (input.find(',') != std::string::npos) // code to handle conversion with comma as decimal separator
  90. stream.imbue(std::locale(std::locale(), new LocaleWithComma));
  91. int result;
  92. if ( !(stream >> result) )
  93. return 0;
  94. return result;
  95. }
  96. bool CLegacyConfigParser::isNextEntryEmpty()
  97. {
  98. char * nextSymbol = curr;
  99. while (nextSymbol < end && *nextSymbol == ' ')
  100. nextSymbol++; //find next meaningfull symbol
  101. return nextSymbol >= end || *nextSymbol == '\n' || *nextSymbol == '\r' || *nextSymbol == '\t';
  102. }
  103. bool CLegacyConfigParser::endLine()
  104. {
  105. while (curr < end && *curr != '\n')
  106. readString();
  107. curr++;
  108. return curr < end;
  109. }
  110. void CGeneralTextHandler::readToVector(std::string sourceName, std::vector<std::string> & dest)
  111. {
  112. CLegacyConfigParser parser(sourceName);
  113. do
  114. {
  115. dest.push_back(parser.readString());
  116. }
  117. while (parser.endLine());
  118. }
  119. void CGeneralTextHandler::load()
  120. {
  121. readToVector("DATA/VCDESC.TXT", victoryConditions);
  122. readToVector("DATA/LCDESC.TXT", lossCondtions);
  123. readToVector("DATA/TCOMMAND.TXT", tcommands);
  124. readToVector("DATA/HALLINFO.TXT", hcommands);
  125. readToVector("DATA/CASTINFO.TXT", fcommands);
  126. readToVector("DATA/ADVEVENT.TXT", advobtxt);
  127. readToVector("DATA/XTRAINFO.TXT", xtrainfo);
  128. readToVector("DATA/RESTYPES.TXT", restypes);
  129. readToVector("DATA/TERRNAME.TXT", terrainNames);
  130. readToVector("DATA/RANDSIGN.TXT", randsign);
  131. readToVector("DATA/ZCRGN1.TXT", creGens);
  132. readToVector("DATA/CRGEN4.TXT", creGens4);
  133. readToVector("DATA/OVERVIEW.TXT", overview);
  134. readToVector("DATA/ARRAYTXT.TXT", arraytxt);
  135. readToVector("DATA/PRISKILL.TXT", primarySkillNames);
  136. readToVector("DATA/JKTEXT.TXT", jktexts);
  137. readToVector("DATA/TVRNINFO.TXT", tavernInfo);
  138. readToVector("DATA/TURNDUR.TXT", turnDurations);
  139. readToVector("DATA/HEROSCRN.TXT", heroscrn);
  140. readToVector("DATA/TENTCOLR.TXT", tentColors);
  141. readToVector("DATA/SKILLLEV.TXT", levels);
  142. readToVector("DATA/OBJNAMES.TXT", names);
  143. {
  144. CLegacyConfigParser parser("DATA/GENRLTXT.TXT");
  145. parser.endLine();
  146. do
  147. {
  148. allTexts.push_back(parser.readString());
  149. }
  150. while (parser.endLine());
  151. }
  152. {
  153. CLegacyConfigParser parser("DATA/ZELP.TXT");
  154. do
  155. {
  156. std::string first = parser.readString();
  157. std::string second = parser.readString();
  158. zelp.push_back(std::make_pair(first, second));
  159. }
  160. while (parser.endLine());
  161. }
  162. {
  163. CLegacyConfigParser nameParser("DATA/MINENAME.TXT");
  164. CLegacyConfigParser eventParser("DATA/MINEEVNT.TXT");
  165. do
  166. {
  167. std::string name = nameParser.readString();
  168. std::string event = eventParser.readString();
  169. mines.push_back(std::make_pair(name, event));
  170. }
  171. while (nameParser.endLine() && eventParser.endLine());
  172. }
  173. {
  174. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  175. do
  176. {
  177. std::string color = parser.readString();
  178. colors.push_back(color);
  179. color[0] = toupper(color[0]);
  180. capColors.push_back(color);
  181. }
  182. while (parser.endLine());
  183. }
  184. {
  185. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  186. //skip header
  187. parser.endLine();
  188. parser.endLine();
  189. do
  190. {
  191. skillName.push_back(parser.readString());
  192. skillInfoTexts.push_back(std::vector<std::string>());
  193. for(int j = 0; j < 3; j++)
  194. skillInfoTexts.back().push_back(parser.readString());
  195. }
  196. while (parser.endLine());
  197. }
  198. {
  199. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  200. //skip header
  201. parser.endLine();
  202. parser.endLine();
  203. for (int i = 0; i < 6; ++i)
  204. seerEmpty.push_back(parser.readString());
  205. quests.resize(10);
  206. for (int i = 0; i < 9; ++i) //9 types of quests
  207. {
  208. quests[i].resize(5);
  209. for (int j = 0; j < 5; ++j)
  210. {
  211. parser.readString(); //front description
  212. for (int k = 0; k < 6; ++k)
  213. quests[i][j].push_back(parser.readString());
  214. parser.endLine();
  215. }
  216. }
  217. quests[9].resize(1);
  218. for (int k = 0; k < 6; ++k) //Time limit
  219. {
  220. quests[9][0].push_back(parser.readString());
  221. }
  222. parser.endLine();
  223. parser.endLine(); // empty line
  224. parser.endLine(); // header
  225. for (int i = 0; i < 48; ++i)
  226. {
  227. seerNames.push_back(parser.readString());
  228. parser.endLine();
  229. }
  230. }
  231. {
  232. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  233. //skip header
  234. parser.endLine();
  235. std::string text;
  236. do
  237. {
  238. text = parser.readString();
  239. if (!text.empty())
  240. campaignMapNames.push_back(text);
  241. }
  242. while (parser.endLine() && !text.empty());
  243. for (size_t i=0; i<campaignMapNames.size(); i++)
  244. {
  245. do // skip empty space and header
  246. {
  247. text = parser.readString();
  248. }
  249. while (parser.endLine() && text.empty());
  250. campaignRegionNames.push_back(std::vector<std::string>());
  251. do
  252. {
  253. text = parser.readString();
  254. if (!text.empty())
  255. campaignRegionNames.back().push_back(text);
  256. }
  257. while (parser.endLine() && !text.empty());
  258. }
  259. }
  260. {
  261. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  262. parser.endLine();//header
  263. do
  264. {
  265. parser.readString(); //ignore 1st column with description
  266. zcrexp.push_back(parser.readString());
  267. }
  268. while (parser.endLine());
  269. }
  270. std::string buffer;
  271. std::ifstream ifs(CResourceHandler::get()->getResourceName(ResourceID("config/threatlevel.txt")), std::ios::binary);
  272. getline(ifs, buffer); //skip 1st line
  273. for (int i = 0; i < 13; ++i)
  274. {
  275. getline(ifs, buffer);
  276. threat.push_back(buffer);
  277. }
  278. }
  279. CGeneralTextHandler::CGeneralTextHandler()
  280. {
  281. }