CConfigHandler.cpp 9.1 KB

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