CConfigHandler.cpp 12 KB

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