2
0

CConfigHandler.cpp 14 KB

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