CConfigHandler.cpp 13 KB

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