CConfigHandler.cpp 12 KB

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