CConfigHandler.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. | str_p("classicCreatureWindow=") >> (uint_p[assign_a(conf.cc.classicCreatureWindow)] | eps_p[lerror("Wrong classicCreatureWindow!")])
  210. | (+(anychar_p - '}'))[lerror2("Unrecognized client option: ")]
  211. ;
  212. clientOptionsSequence = *(clientOption >> (';' | eps_p[lerror("Semicolon lacking after client option!")]));
  213. ClientSettings = '{' >> clientOptionsSequence >> '}';
  214. AdvMapOption
  215. = str_p("Buttons") >> ((ch_p('{') >> '}') | eps_p[lerror("Wrong Buttons!")])
  216. | str_p("Minimap: ") >>
  217. *(
  218. "width=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapW)]//[assign_a(current->ac.minimapW)]
  219. | "height=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapH)]
  220. | "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapX)]
  221. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::minimapY)]
  222. )
  223. | str_p("Statusbar:") >>
  224. *(
  225. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::statusbarX)]
  226. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::statusbarY)]
  227. | "graphic=" >> fname[SetAdventureStr(&AdventureMapConfig::statusbarG)]
  228. )
  229. )
  230. | str_p("ResDataBar:") >>
  231. *(
  232. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resdatabarX)]
  233. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resdatabarY)]
  234. | "offsetX=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resOffsetX)]
  235. | "offsetY=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resOffsetY)]
  236. | "resSpace=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resDist)]
  237. | "resDateSpace=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::resDateDist)]
  238. | "graphic=" >> fname[SetAdventureStr(&AdventureMapConfig::resdatabarG)]
  239. )
  240. )
  241. | str_p("InfoBox:") >>
  242. *(
  243. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::infoboxX)]
  244. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::infoboxY)]
  245. )
  246. )
  247. | str_p("AdvMap:") >>
  248. *(
  249. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapX)]
  250. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapY)]
  251. | "width=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapW)]
  252. | "height=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapH)]
  253. | "smoothMove=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::smoothMove)]
  254. | "puzzleSepia=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::puzzleSepia)]
  255. )
  256. )
  257. | str_p("background=") >> fname[SetAdventureStr(&AdventureMapConfig::mainGraphic)]
  258. | str_p("Button") >> (+(anychar_p-':'))[SetCurButton()] >> ':' >>
  259. *(
  260. ( "x=" >> uint_p[SetButtonProp_a(&ButtonInfo::x)]
  261. | "y=" >> uint_p[SetButtonProp_a(&ButtonInfo::y)]
  262. | "playerColoured=" >> uint_p[SetButtonProp_a(&ButtonInfo::playerColoured)]
  263. | "graphic=" >> fname[SetButtonStr(&ButtonInfo::defName)]
  264. | str_p("additionalDefs=")[ClearAdditionalDefs()]
  265. >> ch_p('(') >> fname[AddDefForButton()]
  266. >> *(',' >> fname[AddDefForButton()]) >> ')'
  267. )
  268. )
  269. | str_p("HeroList:") >>
  270. *(
  271. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistX)]
  272. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistY)]
  273. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistSize)]
  274. | "movePoints=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistMB)]
  275. | "manaPoints=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistMN)]
  276. | "arrowUp=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistAU)]
  277. | "arrowDown=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistAD)]
  278. )
  279. )
  280. | str_p("TownList:") >>
  281. *(
  282. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistX)]
  283. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistY)]
  284. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistSize)]
  285. | "arrowUp=" >> fname[SetAdventureStr(&AdventureMapConfig::tlistAU)]
  286. | "arrowDown=" >> fname[SetAdventureStr(&AdventureMapConfig::tlistAD)]
  287. )
  288. )
  289. | str_p("gem") >> uint_p[var(gnb) = arg1] >> ':' >>
  290. *(
  291. ( "x=" >> uint_p[bind(&setGem,1,_1)]
  292. | "y=" >> uint_p[bind(&setGem,0,_1)]
  293. | "graphic=" >> fname[AddGemName()]
  294. )
  295. )
  296. | str_p("InGameConsole:") >>
  297. *(
  298. ( "maxInputPerLine=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::inputLineLength)]
  299. | "maxOutputPerLine=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::outputLineLength)]
  300. )
  301. )
  302. | str_p("Overview:") >>
  303. *(
  304. ( "pics=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::overviewPics)]
  305. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::overviewSize)]
  306. | "graphic=" >> fname[SetAdventureStr(&AdventureMapConfig::overviewBg)]
  307. )
  308. )
  309. ;
  310. AdvMapOptionsSequence = *(AdvMapOption >> (';' | eps_p[lerror("Semicolon lacking in advmapopt!")]));
  311. GUIResolution = (uint_p[assign_a(curRes.first)] >> 'x' >> uint_p[assign_a(curRes.second)])
  312. [dummy(&addGRes)];
  313. GUIOption = str_p("AdventureMap") >> ('{' >> AdvMapOptionsSequence >> '}' | eps_p[lerror("Wrong AdventureMap!")]);
  314. GUIOptionsSequence = *(GUIOption >> (';' | eps_p[lerror("Semicolon after GUIOption lacking!")]));
  315. GUISettings = +(GUIResolution >> '{' >> GUIOptionsSequence >> '}');
  316. r
  317. = str_p("clientSettings") >> (ClientSettings | eps_p[lerror("Wrong clientSettings!")])
  318. >> str_p("GUISettings") >> ('{' >> GUISettings >> '}' | eps_p[lerror("Wrong GUISettings!")]);
  319. #ifdef BOOST_SPIRIT_DEBUG
  320. BOOST_SPIRIT_DEBUG_RULE(clientOption);
  321. BOOST_SPIRIT_DEBUG_RULE(clientOptionsSequence);
  322. BOOST_SPIRIT_DEBUG_RULE(ClientSettings);
  323. BOOST_SPIRIT_DEBUG_RULE(AdvMapOption);
  324. BOOST_SPIRIT_DEBUG_RULE(AdvMapOptionsSequence);
  325. BOOST_SPIRIT_DEBUG_RULE(GUIOption);
  326. BOOST_SPIRIT_DEBUG_RULE(GUIOptionsSequence);
  327. BOOST_SPIRIT_DEBUG_RULE(GUISettings);
  328. BOOST_SPIRIT_DEBUG_RULE(GUIResolution);
  329. BOOST_SPIRIT_DEBUG_RULE(r);
  330. #endif
  331. }
  332. rule<ScannerT> const& start() const { return r; }
  333. };
  334. };
  335. struct CommentsGrammar : public grammar<CommentsGrammar>
  336. {
  337. template <typename ScannerT>
  338. struct definition
  339. {
  340. rule<ScannerT> comment;
  341. definition(CommentsGrammar const& self)
  342. {
  343. comment = comment_p("//") | comment_p("/*","*/") | space_p;
  344. BOOST_SPIRIT_DEBUG_RULE(comment);
  345. }
  346. rule<ScannerT> const& start() const { return comment; }
  347. };
  348. };
  349. CConfigHandler::CConfigHandler(void)
  350. {
  351. }
  352. CConfigHandler::~CConfigHandler(void)
  353. {
  354. }
  355. void config::CConfigHandler::init()
  356. {
  357. std::vector<char> settings;
  358. std::ifstream ifs(DATA_DIR "/config/settings.txt");
  359. if(!ifs)
  360. {
  361. tlog1 << "Cannot open " DATA_DIR "/config/settings.txt !" << std::endl;
  362. return;
  363. }
  364. ifs.unsetf(std::ios::skipws); // Turn of white space skipping on the stream
  365. std::copy(std::istream_iterator<char>(ifs),std::istream_iterator<char>(),std::back_inserter(settings));
  366. std::vector<char>::const_iterator first = settings.begin(), last = settings.end();
  367. SettingsGrammar sg;
  368. BOOST_SPIRIT_DEBUG_NODE(sg);
  369. CommentsGrammar cg;
  370. BOOST_SPIRIT_DEBUG_NODE(cg);
  371. parse_info<std::vector<char>::const_iterator> info = parse(first,last,sg,cg);
  372. if(!info.hit)
  373. tlog1 << "Cannot parse config/settings.txt file!\n";
  374. else if(!info.full)
  375. tlog2 << "Not entire config/settings.txt parsed!\n";
  376. //fixing screenx / screeny if set to 0x0
  377. if (cc.screenx == 0 && cc.screeny == 0)
  378. {
  379. cc.screenx = cc.resx;
  380. cc.screeny = cc.resy;
  381. }
  382. }
  383. GUIOptions * config::CConfigHandler::go()
  384. {
  385. return &guiOptions[std::pair<int,int>(cc.resx,cc.resy)];
  386. }