CConfigHandler.cpp 13 KB

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