CConfigHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 <fstream>
  7. using namespace config;
  8. #if BOOST_VERSION >= 103800
  9. #include <boost/spirit/include/classic.hpp>
  10. using namespace boost::spirit::classic;
  11. #else
  12. #include <boost/spirit.hpp>
  13. using namespace boost::spirit;
  14. #endif
  15. using namespace phoenix;
  16. /*
  17. * CConfigHandler.cpp, part of VCMI engine
  18. *
  19. * Authors: listed in file AUTHORS in main folder
  20. *
  21. * License: GNU General Public License v2.0 or later
  22. * Full text of license available in license.txt file, in main folder
  23. *
  24. */
  25. CConfigHandler conf;
  26. GUIOptions *current = NULL;
  27. std::pair<int,int> curRes;
  28. ButtonInfo *currentButton;
  29. int gnb=-1;
  30. struct lerror
  31. {
  32. std::string txt;
  33. lerror(const std::string & TXT):txt(TXT){};
  34. void operator()() const
  35. {
  36. tlog1 << txt << std::endl;
  37. }
  38. template<typename IteratorT>
  39. void operator()(IteratorT t1, IteratorT t2) const
  40. {
  41. tlog1 << txt << std::endl;
  42. }
  43. };
  44. struct SetCurButton
  45. {
  46. template<typename IteratorT>
  47. void operator()(IteratorT t1, IteratorT t2) const
  48. {
  49. std::string str(t1,t2);
  50. if(str=="KingdomOv")
  51. currentButton = &current->ac.kingOverview;
  52. else if(str=="Underground")
  53. currentButton = &current->ac.underground;
  54. else if(str=="QuestLog")
  55. currentButton = &current->ac.questlog;
  56. else if(str=="SleepWake")
  57. currentButton = &current->ac.sleepWake;
  58. else if(str=="MoveHero")
  59. currentButton = &current->ac.moveHero;
  60. else if(str=="Spellbook")
  61. currentButton = &current->ac.spellbook;
  62. else if(str=="AdvOptions")
  63. currentButton = &current->ac.advOptions;
  64. else if(str=="SysOptions")
  65. currentButton = &current->ac.sysOptions;
  66. else if(str=="NextHero")
  67. currentButton = &current->ac.nextHero;
  68. else if(str=="EndTurn")
  69. currentButton = &current->ac.endTurn;
  70. }
  71. };
  72. struct lerror2
  73. {
  74. std::string txt;
  75. lerror2(const std::string & TXT):txt(TXT){};
  76. template<typename IteratorT>
  77. void operator()(IteratorT t1, IteratorT t2) const
  78. {
  79. std::string txt2(t1,t2);
  80. tlog1 << txt << txt2 << std::endl;
  81. }
  82. };
  83. struct dummy
  84. {
  85. boost::function<void()> func;
  86. dummy(const boost::function<void()> & F)
  87. :func(F){}
  88. template<typename IteratorT>
  89. void operator()(IteratorT t1, IteratorT t2) const
  90. {
  91. func();
  92. }
  93. };
  94. template<typename T>struct SetButtonProp
  95. {
  96. T point;
  97. SetButtonProp(T p)
  98. :point(p){}
  99. template <typename Z>
  100. void operator()(const Z & val) const
  101. {
  102. currentButton->*point = val;
  103. }
  104. };
  105. template<typename T> SetButtonProp<T> SetButtonProp_a(T p)
  106. {
  107. return SetButtonProp<T>(p);
  108. }
  109. struct SetButtonStr
  110. {
  111. std::string ButtonInfo::*point;
  112. SetButtonStr(std::string ButtonInfo::* p)
  113. :point(p){}
  114. template <typename Z>
  115. void operator()(const Z first, const Z last) const
  116. {
  117. std::string str(first,last);
  118. currentButton->*point = str;
  119. }
  120. };
  121. template<typename T>struct SetAdventureProp
  122. {
  123. T point;
  124. SetAdventureProp(T p)
  125. :point(p){}
  126. template <typename Z>
  127. void operator()(const Z & val) const
  128. {
  129. current->ac.*point = val;
  130. }
  131. };
  132. template<typename T> SetAdventureProp<T> SetAdventureProp_a(T p)
  133. {
  134. return SetAdventureProp<T>(p);
  135. }
  136. struct SetAdventureStr
  137. {
  138. std::string AdventureMapConfig::*point;
  139. SetAdventureStr(std::string AdventureMapConfig::* p)
  140. :point(p){}
  141. template <typename Z>
  142. void operator()(const Z first, const Z last) const
  143. {
  144. std::string str(first,last);
  145. current->ac.*point = str;
  146. }
  147. };
  148. struct AddDefForButton
  149. {
  150. template <typename Z>
  151. void operator()(const Z first, const Z last) const
  152. {
  153. std::string str(first,last);
  154. currentButton->additionalDefs.push_back(str);
  155. }
  156. };
  157. struct ClearAdditionalDefs
  158. {
  159. template <typename Z>
  160. void operator()(const Z first, const Z last) const
  161. {
  162. currentButton->additionalDefs.clear();
  163. }
  164. };
  165. static void addGRes()
  166. {
  167. if(current)
  168. conf.guiOptions[curRes] = *current; //we'll use by default settings from previous resolution
  169. current = &conf.guiOptions[curRes];
  170. }
  171. static void setGem(int x, int val)
  172. {
  173. if(x)
  174. current->ac.gemX[gnb] = val;
  175. else
  176. current->ac.gemY[gnb] = val;
  177. }
  178. struct AddGemName
  179. {
  180. template <typename Z>
  181. void operator()(const Z first, const Z last) const
  182. {
  183. current->ac.gemG.push_back(std::string(first,last));
  184. }
  185. };
  186. struct SettingsGrammar : public grammar<SettingsGrammar>
  187. {
  188. template <typename ScannerT>
  189. struct definition
  190. {
  191. rule<ScannerT> r, clientOption, clientOptionsSequence, ClientSettings;
  192. rule<ScannerT> GUISettings, GUIOption, GUIOptionsSequence, AdvMapOptionsSequence, AdvMapOption;
  193. rule<ScannerT> GUIResolution, fname;
  194. definition(SettingsGrammar const& self)
  195. {
  196. fname = lexeme_d[+(alnum_p | '.')];
  197. clientOption
  198. = str_p("resolution=") >> (uint_p[assign_a(conf.cc.resx)] >> 'x' >> uint_p[assign_a(conf.cc.resy)] | eps_p[lerror("Wrong resolution!")])
  199. | str_p("pregameRes=") >> (uint_p[assign_a(conf.cc.pregameResx)] >> 'x' >> uint_p[assign_a(conf.cc.pregameResy)] | eps_p[lerror("Wrong pregame size!")])
  200. | str_p("screenSize=") >> (uint_p[assign_a(conf.cc.screenx)] >> 'x' >> uint_p[assign_a(conf.cc.screeny)] | eps_p[lerror("Wrong screen size!")])
  201. | str_p("port=") >> (uint_p[assign_a(conf.cc.port)] | eps_p[lerror("Wrong port!")])
  202. | str_p("bpp=") >> (uint_p[assign_a(conf.cc.bpp)] | eps_p[lerror("Wrong bpp!")])
  203. | str_p("localInformation=") >> (uint_p[assign_a(conf.cc.localInformation)] | eps_p[lerror("Wrong localInformation!")])
  204. | str_p("fullscreen=") >> (uint_p[assign_a(conf.cc.fullscreen)] | eps_p[lerror("Wrong fullscreen!")])
  205. | str_p("server=") >> ( ( +digit_p >> *('.' >> +digit_p) )[assign_a(conf.cc.server)] | eps_p[lerror("Wrong server!")])
  206. | str_p("defaultPlayerAI=") >> ((+(anychar_p - ';'))[assign_a(conf.cc.defaultPlayerAI)] | eps_p[lerror("Wrong defaultAI!")])
  207. | str_p("neutralBattleAI=") >> ((+(anychar_p - ';'))[assign_a(conf.cc.defaultBattleAI)] | eps_p[lerror("Wrong defaultAI!")])
  208. | str_p("showFPS=") >> (uint_p[assign_a(conf.cc.showFPS)] | eps_p[lerror("Wrong showFPS!")])
  209. | (+(anychar_p - '}'))[lerror2("Unrecognized client option: ")]
  210. ;
  211. clientOptionsSequence = *(clientOption >> (';' | eps_p[lerror("Semicolon lacking after client option!")]));
  212. ClientSettings = '{' >> clientOptionsSequence >> '}';
  213. AdvMapOption
  214. = str_p("Buttons") >> ((ch_p('{') >> '}') | eps_p[lerror("Wrong Buttons!")])
  215. | str_p("Minimap: ") >>
  216. *(
  217. "width=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapW)]//[assign_a(current->ac.minimapW)]
  218. | "height=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapH)]
  219. | "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapX)]
  220. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapY)]
  221. )
  222. | str_p("Statusbar:") >>
  223. *(
  224. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::statusbarX)]
  225. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::statusbarY)]
  226. | "graphic=" >> fname[SetAdventureStr(&AdventureMapConfig::statusbarG)]
  227. )
  228. )
  229. | str_p("ResDataBar:") >>
  230. *(
  231. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resdatabarX)]
  232. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resdatabarY)]
  233. | "offsetX=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resOffsetX)]
  234. | "offsetY=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resOffsetY)]
  235. | "resSpace=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resDist)]
  236. | "resDateSpace=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resDateDist)]
  237. | "graphic=" >> fname[SetAdventureStr(&AdventureMapConfig::resdatabarG)]
  238. )
  239. )
  240. | str_p("InfoBox:") >>
  241. *(
  242. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::infoboxX)]
  243. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::infoboxY)]
  244. )
  245. )
  246. | str_p("AdvMap:") >>
  247. *(
  248. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapX)]
  249. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapY)]
  250. | "width=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapW)]
  251. | "height=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapH)]
  252. | "smoothMove=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::smoothMove)]
  253. | "puzzleSepia=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::puzzleSepia)]
  254. )
  255. )
  256. | str_p("background=") >> fname[SetAdventureStr(&AdventureMapConfig::mainGraphic)]
  257. | str_p("Button") >> (+(anychar_p-':'))[SetCurButton()] >> ':' >>
  258. *(
  259. ( "x=" >> uint_p[SetButtonProp_a(&ButtonInfo::x)]
  260. | "y=" >> uint_p[SetButtonProp_a(&ButtonInfo::y)]
  261. | "playerColoured=" >> uint_p[SetButtonProp_a(&ButtonInfo::playerColoured)]
  262. | "graphic=" >> fname[SetButtonStr(&ButtonInfo::defName)]
  263. | str_p("additionalDefs=")[ClearAdditionalDefs()]
  264. >> ch_p('(') >> fname[AddDefForButton()]
  265. >> *(',' >> fname[AddDefForButton()]) >> ')'
  266. )
  267. )
  268. | str_p("HeroList:") >>
  269. *(
  270. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistX)]
  271. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistY)]
  272. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistSize)]
  273. | "movePoints=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistMB)]
  274. | "manaPoints=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistMN)]
  275. | "arrowUp=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistAU)]
  276. | "arrowDown=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistAD)]
  277. )
  278. )
  279. | str_p("TownList:") >>
  280. *(
  281. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistX)]
  282. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistY)]
  283. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistSize)]
  284. | "arrowUp=" >> fname[SetAdventureStr(&AdventureMapConfig::tlistAU)]
  285. | "arrowDown=" >> fname[SetAdventureStr(&AdventureMapConfig::tlistAD)]
  286. )
  287. )
  288. | str_p("gem") >> uint_p[var(gnb) = arg1] >> ':' >>
  289. *(
  290. ( "x=" >> uint_p[bind(&setGem,1,_1)]
  291. | "y=" >> uint_p[bind(&setGem,0,_1)]
  292. | "graphic=" >> fname[AddGemName()]
  293. )
  294. )
  295. | str_p("InGameConsole:") >>
  296. *(
  297. ( "maxInputPerLine=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::inputLineLength)]
  298. | "maxOutputPerLine=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::outputLineLength)]
  299. )
  300. )
  301. | str_p("Overview:") >>
  302. *(
  303. ( "pics=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::overviewPics)]
  304. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::overviewSize)]
  305. | "graphic=" >> fname[SetAdventureStr(&AdventureMapConfig::overviewBg)]
  306. )
  307. )
  308. ;
  309. AdvMapOptionsSequence = *(AdvMapOption >> (';' | eps_p[lerror("Semicolon lacking in advmapopt!")]));
  310. GUIResolution = (uint_p[assign_a(curRes.first)] >> 'x' >> uint_p[assign_a(curRes.second)])
  311. [dummy(&addGRes)];
  312. GUIOption = str_p("AdventureMap") >> ('{' >> AdvMapOptionsSequence >> '}' | eps_p[lerror("Wrong AdventureMap!")]);
  313. GUIOptionsSequence = *(GUIOption >> (';' | eps_p[lerror("Semicolon after GUIOption lacking!")]));
  314. GUISettings = +(GUIResolution >> '{' >> GUIOptionsSequence >> '}');
  315. r
  316. = str_p("clientSettings") >> (ClientSettings | eps_p[lerror("Wrong clientSettings!")])
  317. >> str_p("GUISettings") >> ('{' >> GUISettings >> '}' | eps_p[lerror("Wrong GUISettings!")]);
  318. #ifdef BOOST_SPIRIT_DEBUG
  319. BOOST_SPIRIT_DEBUG_RULE(clientOption);
  320. BOOST_SPIRIT_DEBUG_RULE(clientOptionsSequence);
  321. BOOST_SPIRIT_DEBUG_RULE(ClientSettings);
  322. BOOST_SPIRIT_DEBUG_RULE(AdvMapOption);
  323. BOOST_SPIRIT_DEBUG_RULE(AdvMapOptionsSequence);
  324. BOOST_SPIRIT_DEBUG_RULE(GUIOption);
  325. BOOST_SPIRIT_DEBUG_RULE(GUIOptionsSequence);
  326. BOOST_SPIRIT_DEBUG_RULE(GUISettings);
  327. BOOST_SPIRIT_DEBUG_RULE(GUIResolution);
  328. BOOST_SPIRIT_DEBUG_RULE(r);
  329. #endif
  330. }
  331. rule<ScannerT> const& start() const { return r; }
  332. };
  333. };
  334. struct CommentsGrammar : public grammar<CommentsGrammar>
  335. {
  336. template <typename ScannerT>
  337. struct definition
  338. {
  339. rule<ScannerT> comment;
  340. definition(CommentsGrammar const& self)
  341. {
  342. comment = comment_p("//") | comment_p("/*","*/") | space_p;
  343. BOOST_SPIRIT_DEBUG_RULE(comment);
  344. }
  345. rule<ScannerT> const& start() const { return comment; }
  346. };
  347. };
  348. CConfigHandler::CConfigHandler(void)
  349. {
  350. }
  351. CConfigHandler::~CConfigHandler(void)
  352. {
  353. }
  354. void config::CConfigHandler::init()
  355. {
  356. std::vector<char> settings;
  357. std::ifstream ifs(DATA_DIR "/config/settings.txt");
  358. if(!ifs)
  359. {
  360. tlog1 << "Cannot open " DATA_DIR "/config/settings.txt !" << std::endl;
  361. return;
  362. }
  363. ifs.unsetf(std::ios::skipws); // Turn of white space skipping on the stream
  364. std::copy(std::istream_iterator<char>(ifs),std::istream_iterator<char>(),std::back_inserter(settings));
  365. std::vector<char>::const_iterator first = settings.begin(), last = settings.end();
  366. SettingsGrammar sg;
  367. BOOST_SPIRIT_DEBUG_NODE(sg);
  368. CommentsGrammar cg;
  369. BOOST_SPIRIT_DEBUG_NODE(cg);
  370. parse_info<std::vector<char>::const_iterator> info = parse(first,last,sg,cg);
  371. if(!info.hit)
  372. tlog1 << "Cannot parse config/settings.txt file!\n";
  373. else if(!info.full)
  374. tlog2 << "Not entire config/settings.txt parsed!\n";
  375. //fixing screenx / screeny if set to 0x0
  376. if (cc.screenx == 0 && cc.screeny == 0)
  377. {
  378. cc.screenx = cc.resx;
  379. cc.screeny = cc.resy;
  380. }
  381. }
  382. GUIOptions * config::CConfigHandler::go()
  383. {
  384. return &guiOptions[std::pair<int,int>(cc.resx,cc.resy)];
  385. }