CConfigHandler.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "StdInc.h"
  2. #include <boost/version.hpp>
  3. #include "CConfigHandler.h"
  4. #include "../lib/JsonNode.h"
  5. #include "../lib/GameConstants.h"
  6. using namespace config;
  7. #if BOOST_VERSION >= 103800
  8. #include <boost/spirit/include/classic.hpp>
  9. using namespace boost::spirit::classic;
  10. #else
  11. #include <boost/spirit.hpp>
  12. using namespace boost::spirit;
  13. #endif
  14. using namespace phoenix;
  15. /*
  16. * CConfigHandler.cpp, part of VCMI engine
  17. *
  18. * Authors: listed in file AUTHORS in main folder
  19. *
  20. * License: GNU General Public License v2.0 or later
  21. * Full text of license available in license.txt file, in main folder
  22. *
  23. */
  24. CConfigHandler conf;
  25. struct lerror
  26. {
  27. std::string txt;
  28. lerror(const std::string & TXT):txt(TXT){};
  29. void operator()() const
  30. {
  31. tlog1 << txt << std::endl;
  32. }
  33. template<typename IteratorT>
  34. void operator()(IteratorT t1, IteratorT t2) const
  35. {
  36. tlog1 << txt << std::endl;
  37. }
  38. };
  39. struct lerror2
  40. {
  41. std::string txt;
  42. lerror2(const std::string & TXT):txt(TXT){};
  43. template<typename IteratorT>
  44. void operator()(IteratorT t1, IteratorT t2) const
  45. {
  46. std::string txt2(t1,t2);
  47. tlog1 << txt << txt2 << std::endl;
  48. }
  49. };
  50. struct SettingsGrammar : public grammar<SettingsGrammar>
  51. {
  52. template <typename ScannerT>
  53. struct definition
  54. {
  55. rule<ScannerT> r, clientOption, clientOptionsSequence, ClientSettings;
  56. definition(SettingsGrammar const& self)
  57. {
  58. clientOption
  59. = str_p("resolution=") >> (uint_p[assign_a(conf.cc.resx)] >> 'x' >> uint_p[assign_a(conf.cc.resy)] | eps_p[lerror("Wrong resolution!")])
  60. | str_p("pregameRes=") >> (uint_p[assign_a(conf.cc.pregameResx)] >> 'x' >> uint_p[assign_a(conf.cc.pregameResy)] | eps_p[lerror("Wrong pregame size!")])
  61. | str_p("screenSize=") >> (uint_p[assign_a(conf.cc.screenx)] >> 'x' >> uint_p[assign_a(conf.cc.screeny)] | eps_p[lerror("Wrong screen size!")])
  62. | str_p("port=") >> (uint_p[assign_a(conf.cc.port)] | eps_p[lerror("Wrong port!")])
  63. | str_p("bpp=") >> (uint_p[assign_a(conf.cc.bpp)] | eps_p[lerror("Wrong bpp!")])
  64. | str_p("localInformation=") >> (uint_p[assign_a(conf.cc.localInformation)] | eps_p[lerror("Wrong localInformation!")])
  65. | str_p("fullscreen=") >> (uint_p[assign_a(conf.cc.fullscreen)] | eps_p[lerror("Wrong fullscreen!")])
  66. | str_p("server=") >> ( ( +digit_p >> *('.' >> +digit_p) )[assign_a(conf.cc.server)] | eps_p[lerror("Wrong server!")])
  67. | str_p("defaultPlayerAI=") >> ((+(anychar_p - ';'))[assign_a(conf.cc.defaultPlayerAI)] | eps_p[lerror("Wrong defaultAI!")])
  68. | str_p("neutralBattleAI=") >> ((+(anychar_p - ';'))[assign_a(conf.cc.defaultBattleAI)] | eps_p[lerror("Wrong defaultAI!")])
  69. | str_p("showFPS=") >> (uint_p[assign_a(conf.cc.showFPS)] | eps_p[lerror("Wrong showFPS!")])
  70. | str_p("classicCreatureWindow=") >> (uint_p[assign_a(conf.cc.classicCreatureWindow)] | eps_p[lerror("Wrong classicCreatureWindow!")])
  71. | (+(anychar_p - '}'))[lerror2("Unrecognized client option: ")]
  72. ;
  73. clientOptionsSequence = *(clientOption >> (';' | eps_p[lerror("Semicolon lacking after client option!")]));
  74. ClientSettings = '{' >> clientOptionsSequence >> '}';
  75. r = str_p("clientSettings") >> (ClientSettings | eps_p[lerror("Wrong clientSettings!")]);
  76. #ifdef BOOST_SPIRIT_DEBUG
  77. BOOST_SPIRIT_DEBUG_RULE(clientOption);
  78. BOOST_SPIRIT_DEBUG_RULE(clientOptionsSequence);
  79. BOOST_SPIRIT_DEBUG_RULE(ClientSettings);
  80. BOOST_SPIRIT_DEBUG_RULE(r);
  81. #endif
  82. }
  83. rule<ScannerT> const& start() const { return r; }
  84. };
  85. };
  86. struct CommentsGrammar : public grammar<CommentsGrammar>
  87. {
  88. template <typename ScannerT>
  89. struct definition
  90. {
  91. rule<ScannerT> comment;
  92. definition(CommentsGrammar const& self)
  93. {
  94. comment = comment_p("//") | comment_p("/*","*/") | space_p;
  95. BOOST_SPIRIT_DEBUG_RULE(comment);
  96. }
  97. rule<ScannerT> const& start() const { return comment; }
  98. };
  99. };
  100. static void setButton(ButtonInfo &button, const JsonNode &g)
  101. {
  102. button.x = g["x"].Float();
  103. button.y = g["y"].Float();
  104. button.playerColoured = g["playerColoured"].Float();
  105. button.defName = g["graphic"].String();
  106. if (!g["additionalDefs"].isNull()) {
  107. const JsonVector &defs_vec = g["additionalDefs"].Vector();
  108. BOOST_FOREACH(const JsonNode &def, defs_vec) {
  109. button.additionalDefs.push_back(def.String());
  110. }
  111. }
  112. }
  113. static void setGem(AdventureMapConfig &ac, const int gem, const JsonNode &g)
  114. {
  115. ac.gemX[gem] = g["x"].Float();
  116. ac.gemY[gem] = g["y"].Float();
  117. ac.gemG.push_back(g["graphic"].String());
  118. }
  119. CConfigHandler::CConfigHandler(void): current(NULL)
  120. {
  121. }
  122. CConfigHandler::~CConfigHandler(void)
  123. {
  124. }
  125. void config::CConfigHandler::init()
  126. {
  127. std::vector<char> settings;
  128. std::string settingsDir = GameConstants::DATA_DIR + "/config/settings.txt";
  129. std::ifstream ifs(settingsDir.c_str());
  130. if(!ifs)
  131. {
  132. tlog1 << "Cannot open " << GameConstants::DATA_DIR << "/config/settings.txt !" << std::endl;
  133. return;
  134. }
  135. ifs.unsetf(std::ios::skipws); // Turn of white space skipping on the stream
  136. std::copy(std::istream_iterator<char>(ifs),std::istream_iterator<char>(),std::back_inserter(settings));
  137. std::vector<char>::const_iterator first = settings.begin(), last = settings.end();
  138. SettingsGrammar sg;
  139. BOOST_SPIRIT_DEBUG_NODE(sg);
  140. CommentsGrammar cg;
  141. BOOST_SPIRIT_DEBUG_NODE(cg);
  142. parse_info<std::vector<char>::const_iterator> info = parse(first,last,sg,cg);
  143. if(!info.hit)
  144. tlog1 << "Cannot parse config/settings.txt file!\n";
  145. else if(!info.full)
  146. tlog2 << "Not entire config/settings.txt parsed!\n";
  147. /* Read resolutions. */
  148. const JsonNode config(GameConstants::DATA_DIR + "/config/resolutions.json");
  149. const JsonVector &guisettings_vec = config["GUISettings"].Vector();
  150. BOOST_FOREACH(const JsonNode &g, guisettings_vec)
  151. {
  152. std::pair<int,int> curRes(g["resolution"]["x"].Float(), g["resolution"]["y"].Float());
  153. GUIOptions *current = &conf.guiOptions[curRes];
  154. current->ac.inputLineLength = g["InGameConsole"]["maxInputPerLine"].Float();
  155. current->ac.outputLineLength = g["InGameConsole"]["maxOutputPerLine"].Float();
  156. current->ac.advmapX = g["AdvMap"]["x"].Float();
  157. current->ac.advmapY = g["AdvMap"]["y"].Float();
  158. current->ac.advmapW = g["AdvMap"]["width"].Float();
  159. current->ac.advmapH = g["AdvMap"]["height"].Float();
  160. current->ac.smoothMove = g["AdvMap"]["smoothMove"].Float();
  161. current->ac.puzzleSepia = g["AdvMap"]["puzzleSepia"].Float();
  162. current->ac.infoboxX = g["InfoBox"]["x"].Float();
  163. current->ac.infoboxY = g["InfoBox"]["y"].Float();
  164. setGem(current->ac, 0, g["gem0"]);
  165. setGem(current->ac, 1, g["gem1"]);
  166. setGem(current->ac, 2, g["gem2"]);
  167. setGem(current->ac, 3, g["gem3"]);
  168. current->ac.mainGraphic = g["background"].String();
  169. current->ac.hlistX = g["HeroList"]["x"].Float();
  170. current->ac.hlistY = g["HeroList"]["y"].Float();
  171. current->ac.hlistSize = g["HeroList"]["size"].Float();
  172. current->ac.hlistMB = g["HeroList"]["movePoints"].String();
  173. current->ac.hlistMN = g["HeroList"]["manaPoints"].String();
  174. current->ac.hlistAU = g["HeroList"]["arrowUp"].String();
  175. current->ac.hlistAD = g["HeroList"]["arrowDown"].String();
  176. current->ac.tlistX = g["TownList"]["x"].Float();
  177. current->ac.tlistY = g["TownList"]["y"].Float();
  178. current->ac.tlistSize = g["TownList"]["size"].Float();
  179. current->ac.tlistAU = g["TownList"]["arrowUp"].String();
  180. current->ac.tlistAD = g["TownList"]["arrowDown"].String();
  181. current->ac.minimapW = g["Minimap"]["width"].Float();
  182. current->ac.minimapH = g["Minimap"]["height"].Float();
  183. current->ac.minimapX = g["Minimap"]["x"].Float();
  184. current->ac.minimapY = g["Minimap"]["y"].Float();
  185. current->ac.overviewPics = g["Overview"]["pics"].Float();
  186. current->ac.overviewSize = g["Overview"]["size"].Float();
  187. current->ac.overviewBg = g["Overview"]["graphic"].String();
  188. current->ac.statusbarX = g["Statusbar"]["x"].Float();
  189. current->ac.statusbarY = g["Statusbar"]["y"].Float();
  190. current->ac.statusbarG = g["Statusbar"]["graphic"].String();
  191. current->ac.resdatabarX = g["ResDataBar"]["x"].Float();
  192. current->ac.resdatabarY = g["ResDataBar"]["y"].Float();
  193. current->ac.resOffsetX = g["ResDataBar"]["offsetX"].Float();
  194. current->ac.resOffsetY = g["ResDataBar"]["offsetY"].Float();
  195. current->ac.resDist = g["ResDataBar"]["resSpace"].Float();
  196. current->ac.resDateDist = g["ResDataBar"]["resDateSpace"].Float();
  197. current->ac.resdatabarG = g["ResDataBar"]["graphic"].String();
  198. setButton(current->ac.kingOverview, g["ButtonKingdomOv"]);
  199. setButton(current->ac.underground, g["ButtonUnderground"]);
  200. setButton(current->ac.questlog, g["ButtonQuestLog"]);
  201. setButton(current->ac.sleepWake, g["ButtonSleepWake"]);
  202. setButton(current->ac.moveHero, g["ButtonMoveHero"]);
  203. setButton(current->ac.spellbook, g["ButtonSpellbook"]);
  204. setButton(current->ac.advOptions, g["ButtonAdvOptions"]);
  205. setButton(current->ac.sysOptions, g["ButtonSysOptions"]);
  206. setButton(current->ac.nextHero, g["ButtonNextHero"]);
  207. setButton(current->ac.endTurn, g["ButtonEndTurn"]);
  208. }
  209. //fixing screenx / screeny if set to 0x0
  210. if (cc.screenx == 0 && cc.screeny == 0)
  211. {
  212. cc.screenx = cc.resx;
  213. cc.screeny = cc.resy;
  214. }
  215. cc.autoSkip = false;
  216. cc.oneGoodAI = false;
  217. SetResolution(cc.resx, cc.resy);
  218. }