2
0

CGeneralTextHandler.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 nameParser("DATA/MINENAME.TXT");
  160. CLegacyConfigParser eventParser("DATA/MINEEVNT.TXT");
  161. do
  162. {
  163. std::string name = nameParser.readString();
  164. std::string event = eventParser.readString();
  165. mines.push_back(std::make_pair(name, event));
  166. }
  167. while (nameParser.endLine() && eventParser.endLine());
  168. }
  169. {
  170. CLegacyConfigParser parser("DATA/PLCOLORS.TXT");
  171. do
  172. {
  173. std::string color = parser.readString();
  174. colors.push_back(color);
  175. color[0] = toupper(color[0]);
  176. capColors.push_back(color);
  177. }
  178. while (parser.endLine());
  179. }
  180. {
  181. CLegacyConfigParser parser("DATA/SSTRAITS.TXT");
  182. //skip header
  183. parser.endLine();
  184. parser.endLine();
  185. do
  186. {
  187. skillName.push_back(parser.readString());
  188. skillInfoTexts.push_back(std::vector<std::string>());
  189. for(int j = 0; j < 3; j++)
  190. skillInfoTexts.back().push_back(parser.readString());
  191. }
  192. while (parser.endLine());
  193. }
  194. {
  195. CLegacyConfigParser parser("DATA/SEERHUT.TXT");
  196. //skip header
  197. parser.endLine();
  198. parser.endLine();
  199. for (int i = 0; i < 6; ++i)
  200. seerEmpty.push_back(parser.readString());
  201. quests.resize(10);
  202. for (int i = 0; i < 9; ++i) //9 types of quests
  203. {
  204. quests[i].resize(5);
  205. for (int j = 0; j < 5; ++j)
  206. {
  207. parser.readString(); //front description
  208. for (int k = 0; k < 6; ++k)
  209. quests[i][j].push_back(parser.readString());
  210. parser.endLine();
  211. }
  212. }
  213. quests[9].resize(1);
  214. for (int k = 0; k < 6; ++k) //Time limit
  215. {
  216. quests[9][0].push_back(parser.readString());
  217. }
  218. parser.endLine();
  219. parser.endLine(); // empty line
  220. parser.endLine(); // header
  221. for (int i = 0; i < 48; ++i)
  222. {
  223. seerNames.push_back(parser.readString());
  224. parser.endLine();
  225. }
  226. }
  227. {
  228. CLegacyConfigParser parser("DATA/CAMPTEXT.TXT");
  229. //skip header
  230. parser.endLine();
  231. std::string text;
  232. do
  233. {
  234. text = parser.readString();
  235. if (!text.empty())
  236. campaignMapNames.push_back(text);
  237. }
  238. while (parser.endLine() && !text.empty());
  239. for (size_t i=0; i<campaignMapNames.size(); i++)
  240. {
  241. do // skip empty space and header
  242. {
  243. text = parser.readString();
  244. }
  245. while (parser.endLine() && text.empty());
  246. campaignRegionNames.push_back(std::vector<std::string>());
  247. do
  248. {
  249. text = parser.readString();
  250. if (!text.empty())
  251. campaignRegionNames.back().push_back(text);
  252. }
  253. while (parser.endLine() && !text.empty());
  254. }
  255. }
  256. {
  257. CLegacyConfigParser parser("DATA/ZCREXP.TXT");
  258. parser.endLine();//header
  259. do
  260. {
  261. parser.readString(); //ignore 1st column with description
  262. zcrexp.push_back(parser.readString());
  263. }
  264. while (parser.endLine());
  265. }
  266. std::string buffer;
  267. std::ifstream ifs(CResourceHandler::get()->getResourceName(ResourceID("config/threatlevel.txt")), std::ios::binary);
  268. getline(ifs, buffer); //skip 1st line
  269. for (int i = 0; i < 13; ++i)
  270. {
  271. getline(ifs, buffer);
  272. threat.push_back(buffer);
  273. }
  274. }
  275. std::string CGeneralTextHandler::getTitle(const std::string & text)
  276. {
  277. std::string ret;
  278. int i=0;
  279. while ((text[i++]!='{'));
  280. while ((text[i]!='}') && (i<text.length()))
  281. ret+=text[i++];
  282. return ret;
  283. }
  284. std::string CGeneralTextHandler::getDescr(const std::string & text)
  285. {
  286. std::string ret;
  287. int i=0;
  288. while ((text[i++]!='}'));
  289. i+=2;
  290. while ((text[i]!='"') && (i<text.length()))
  291. ret+=text[i++];
  292. return ret;
  293. }
  294. CGeneralTextHandler::CGeneralTextHandler()
  295. {
  296. }