CConfigHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. static 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. static 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. | "width=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapW)]
  233. | "height=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::advmapH)]
  234. | "smoothMove=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::smoothMove)]
  235. | "puzzleSepia=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::puzzleSepia)]
  236. )
  237. )
  238. | str_p("background=") >> fname[SetAdventureStr(&AdventureMapConfig::mainGraphic)]
  239. | str_p("Button") >> (+(anychar_p-':'))[SetCurButton()] >> ':' >>
  240. *(
  241. ( "x=" >> uint_p[SetButtonProp_a(&ButtonInfo::x)]
  242. | "y=" >> uint_p[SetButtonProp_a(&ButtonInfo::y)]
  243. | "playerColoured=" >> uint_p[SetButtonProp_a(&ButtonInfo::playerColoured)]
  244. | "graphic=" >> fname[SetButtonStr(&ButtonInfo::defName)]
  245. | "additionalDefs=" >> ch_p('(') >> fname[AddDefForButton()]
  246. >> *(',' >> fname[AddDefForButton()]) >> ')'
  247. )
  248. )
  249. | str_p("HeroList:") >>
  250. *(
  251. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistX)]
  252. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistY)]
  253. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::hlistSize)]
  254. | "movePoints=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistMB)]
  255. | "manaPoints=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistMN)]
  256. | "arrowUp=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistAU)]
  257. | "arrowDown=" >> fname[SetAdventureStr(&AdventureMapConfig::hlistAD)]
  258. )
  259. )
  260. | str_p("TownList:") >>
  261. *(
  262. ( "x=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistX)]
  263. | "y=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistY)]
  264. | "size=" >> uint_p[SetAdventureProp_a(&AdventureMapConfig::tlistSize)]
  265. | "arrowUp=" >> fname[SetAdventureStr(&AdventureMapConfig::tlistAU)]
  266. | "arrowDown=" >> fname[SetAdventureStr(&AdventureMapConfig::tlistAD)]
  267. )
  268. )
  269. | str_p("gem") >> uint_p[var(gnb) = arg1] >> ':' >>
  270. *(
  271. ( "x=" >> uint_p[bind(&setGem,1,_1)]
  272. | "y=" >> uint_p[bind(&setGem,0,_1)]
  273. | "graphic=" >> fname[AddGemName()]
  274. )
  275. )
  276. ;
  277. AdvMapOptionsSequence = *(AdvMapOption >> (';' | eps_p[lerror("Semicolon lacking in advmapopt!")]));
  278. GUIResolution = (uint_p[assign_a(curRes.first)] >> 'x' >> uint_p[assign_a(curRes.second)])
  279. [dummy(&addGRes)];
  280. GUIOption = str_p("AdventureMap") >> ('{' >> AdvMapOptionsSequence >> '}' | eps_p[lerror("Wrong AdventureMap!")]);
  281. GUIOptionsSequence = *(GUIOption >> (';' | eps_p[lerror("Semicolon after GUIOption lacking!")]));
  282. GUISettings = +(GUIResolution >> '{' >> GUIOptionsSequence >> '}');
  283. r
  284. = str_p("clientSettings") >> (ClientSettings | eps_p[lerror("Wrong clientSettings!")])
  285. >> str_p("GUISettings") >> ('{' >> GUISettings >> '}' | eps_p[lerror("Wrong GUISettings!")]);
  286. #ifdef BOOST_SPIRIT_DEBUG
  287. BOOST_SPIRIT_DEBUG_RULE(clientOption);
  288. BOOST_SPIRIT_DEBUG_RULE(clientOptionsSequence);
  289. BOOST_SPIRIT_DEBUG_RULE(ClientSettings);
  290. BOOST_SPIRIT_DEBUG_RULE(AdvMapOption);
  291. BOOST_SPIRIT_DEBUG_RULE(AdvMapOptionsSequence);
  292. BOOST_SPIRIT_DEBUG_RULE(GUIOption);
  293. BOOST_SPIRIT_DEBUG_RULE(GUIOptionsSequence);
  294. BOOST_SPIRIT_DEBUG_RULE(GUISettings);
  295. BOOST_SPIRIT_DEBUG_RULE(GUIResolution);
  296. BOOST_SPIRIT_DEBUG_RULE(r);
  297. #endif
  298. }
  299. rule<ScannerT> const& start() const { return r; }
  300. };
  301. };
  302. struct CommentsGrammar : public grammar<CommentsGrammar>
  303. {
  304. template <typename ScannerT>
  305. struct definition
  306. {
  307. rule<ScannerT> comment;
  308. definition(CommentsGrammar const& self)
  309. {
  310. comment = comment_p("//") | comment_p("/*","*/") | space_p;
  311. BOOST_SPIRIT_DEBUG_RULE(comment);
  312. }
  313. rule<ScannerT> const& start() const { return comment; }
  314. };
  315. };
  316. CConfigHandler::CConfigHandler(void)
  317. {
  318. }
  319. CConfigHandler::~CConfigHandler(void)
  320. {
  321. }
  322. void config::CConfigHandler::init()
  323. {
  324. std::vector<char> settings;
  325. std::ifstream ifs("config/settings.txt");
  326. if(!ifs)
  327. {
  328. tlog1 << "Cannot open config/settings.txt !\n";
  329. return;
  330. }
  331. ifs.unsetf(std::ios::skipws); // Turn of white space skipping on the stream
  332. std::copy(std::istream_iterator<char>(ifs),std::istream_iterator<char>(),std::back_inserter(settings));
  333. std::vector<char>::const_iterator first = settings.begin(), last = settings.end();
  334. SettingsGrammar sg;
  335. BOOST_SPIRIT_DEBUG_NODE(sg);
  336. CommentsGrammar cg;
  337. BOOST_SPIRIT_DEBUG_NODE(cg);
  338. parse_info<std::vector<char>::const_iterator> info = parse(first,last,sg,cg);
  339. if(!info.hit)
  340. tlog1 << "Cannot parse config/settings.txt file!\n";
  341. else if(!info.full)
  342. tlog2 << "Not entire config/settings.txt parsed!\n";
  343. }
  344. GUIOptions * config::CConfigHandler::go()
  345. {
  346. return &guiOptions[std::pair<int,int>(cc.resx,cc.resy)];
  347. }