CConfigHandler.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include "StdInc.h"
  2. #include "CConfigHandler.h"
  3. #include "../lib/filesystem/CResourceLoader.h"
  4. #include "../lib/GameConstants.h"
  5. #include "../lib/VCMIDirs.h"
  6. using namespace config;
  7. /*
  8. * CConfigHandler.cpp, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. SettingsStorage settings;
  17. CConfigHandler conf;
  18. template<typename Accessor>
  19. SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):
  20. parent(_parent),
  21. path(_path)
  22. {
  23. }
  24. template<typename Accessor>
  25. SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator [](std::string nextNode) const
  26. {
  27. std::vector<std::string> newPath = path;
  28. newPath.push_back(nextNode);
  29. return NodeAccessor(parent, newPath);
  30. }
  31. template<typename Accessor>
  32. SettingsStorage::NodeAccessor<Accessor>::operator Accessor() const
  33. {
  34. return Accessor(parent, path);
  35. }
  36. template<typename Accessor>
  37. SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator () (std::vector<std::string> _path)
  38. {
  39. std::vector<std::string> newPath = path;
  40. newPath.insert( newPath.end(), _path.begin(), _path.end());
  41. return NodeAccessor(parent, newPath);
  42. }
  43. SettingsStorage::SettingsStorage():
  44. write(NodeAccessor<Settings>(*this, std::vector<std::string>() )),
  45. listen(NodeAccessor<SettingsListener>(*this, std::vector<std::string>() ))
  46. {
  47. }
  48. void SettingsStorage::init()
  49. {
  50. std::string confName = "config/settings.json";
  51. // Porbably new install. Create initial configuration
  52. if (!CResourceHandler::get()->existsResource(ResourceID(confName)))
  53. CResourceHandler::get()->createResource(confName);
  54. else
  55. JsonNode(ResourceID("config/settings.json")).swap(config);
  56. JsonUtils::maximize(config, "vcmi:settings");
  57. JsonUtils::validate(config, "vcmi:settings", "settings");
  58. }
  59. void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
  60. {
  61. BOOST_FOREACH(SettingsListener * listener, listeners)
  62. listener->nodeInvalidated(changedPath);
  63. JsonNode savedConf = config;
  64. JsonNode schema(ResourceID("config/schemas/settings.json"));
  65. savedConf.Struct().erase("session");
  66. JsonUtils::minimize(savedConf, "vcmi:settings");
  67. std::ofstream file(CResourceHandler::get()->getResourceName(ResourceID("config/settings.json")), std::ofstream::trunc);
  68. file << savedConf;
  69. }
  70. JsonNode & SettingsStorage::getNode(std::vector<std::string> path)
  71. {
  72. JsonNode *node = &config;
  73. BOOST_FOREACH(std::string& value, path)
  74. node = &(*node)[value];
  75. return *node;
  76. }
  77. Settings SettingsStorage::get(std::vector<std::string> path)
  78. {
  79. return Settings(*this, path);
  80. }
  81. const JsonNode& SettingsStorage::operator [](std::string value)
  82. {
  83. return config[value];
  84. }
  85. SettingsListener::SettingsListener(SettingsStorage &_parent, const std::vector<std::string> &_path):
  86. parent(_parent),
  87. path(_path)
  88. {
  89. parent.listeners.insert(this);
  90. }
  91. SettingsListener::SettingsListener(const SettingsListener &sl):
  92. parent(sl.parent),
  93. path(sl.path),
  94. callback(sl.callback)
  95. {
  96. parent.listeners.insert(this);
  97. }
  98. SettingsListener::~SettingsListener()
  99. {
  100. parent.listeners.erase(this);
  101. }
  102. void SettingsListener::nodeInvalidated(const std::vector<std::string> &changedPath)
  103. {
  104. if (!callback)
  105. return;
  106. size_t min = std::min(path.size(), changedPath.size());
  107. size_t mismatch = std::mismatch(path.begin(), path.begin()+min, changedPath.begin()).first - path.begin();
  108. if (min == mismatch)
  109. callback(parent.getNode(path));
  110. }
  111. void SettingsListener::operator() (std::function<void(const JsonNode&)> _callback)
  112. {
  113. callback = _callback;
  114. }
  115. Settings::Settings(SettingsStorage &_parent, const std::vector<std::string> &_path):
  116. parent(_parent),
  117. path(_path),
  118. node(_parent.getNode(_path)),
  119. copy(_parent.getNode(_path))
  120. {
  121. }
  122. Settings::~Settings()
  123. {
  124. if (node != copy)
  125. parent.invalidateNode(path);
  126. }
  127. JsonNode* Settings::operator -> ()
  128. {
  129. return &node;
  130. }
  131. const JsonNode* Settings::operator ->() const
  132. {
  133. return &node;
  134. }
  135. const JsonNode& Settings::operator [](std::string value) const
  136. {
  137. return node[value];
  138. }
  139. JsonNode& Settings::operator [](std::string value)
  140. {
  141. return node[value];
  142. }
  143. //
  144. // template DLL_LINKAGE struct SettingsStorage::NodeAccessor<SettingsListener>;
  145. // template DLL_LINKAGE struct SettingsStorage::NodeAccessor<Settings>;
  146. static void setButton(ButtonInfo &button, const JsonNode &g)
  147. {
  148. button.x = g["x"].Float();
  149. button.y = g["y"].Float();
  150. button.playerColoured = g["playerColoured"].Float();
  151. button.defName = g["graphic"].String();
  152. if (!g["additionalDefs"].isNull()) {
  153. const JsonVector &defs_vec = g["additionalDefs"].Vector();
  154. BOOST_FOREACH(const JsonNode &def, defs_vec) {
  155. button.additionalDefs.push_back(def.String());
  156. }
  157. }
  158. }
  159. static void setGem(AdventureMapConfig &ac, const int gem, const JsonNode &g)
  160. {
  161. ac.gemX[gem] = g["x"].Float();
  162. ac.gemY[gem] = g["y"].Float();
  163. ac.gemG.push_back(g["graphic"].String());
  164. }
  165. CConfigHandler::CConfigHandler(void): current(nullptr)
  166. {
  167. }
  168. CConfigHandler::~CConfigHandler(void)
  169. {
  170. }
  171. void config::CConfigHandler::init()
  172. {
  173. /* Read resolutions. */
  174. const JsonNode config(ResourceID("config/resolutions.json"));
  175. const JsonVector &guisettings_vec = config["GUISettings"].Vector();
  176. BOOST_FOREACH(const JsonNode &g, guisettings_vec)
  177. {
  178. std::pair<int,int> curRes(g["resolution"]["x"].Float(), g["resolution"]["y"].Float());
  179. GUIOptions *current = &conf.guiOptions[curRes];
  180. current->ac.inputLineLength = g["InGameConsole"]["maxInputPerLine"].Float();
  181. current->ac.outputLineLength = g["InGameConsole"]["maxOutputPerLine"].Float();
  182. current->ac.advmapX = g["AdvMap"]["x"].Float();
  183. current->ac.advmapY = g["AdvMap"]["y"].Float();
  184. current->ac.advmapW = g["AdvMap"]["width"].Float();
  185. current->ac.advmapH = g["AdvMap"]["height"].Float();
  186. current->ac.smoothMove = g["AdvMap"]["smoothMove"].Float();
  187. current->ac.puzzleSepia = g["AdvMap"]["puzzleSepia"].Float();
  188. current->ac.infoboxX = g["InfoBox"]["x"].Float();
  189. current->ac.infoboxY = g["InfoBox"]["y"].Float();
  190. setGem(current->ac, 0, g["gem0"]);
  191. setGem(current->ac, 1, g["gem1"]);
  192. setGem(current->ac, 2, g["gem2"]);
  193. setGem(current->ac, 3, g["gem3"]);
  194. current->ac.mainGraphic = g["background"].String();
  195. current->ac.hlistX = g["HeroList"]["x"].Float();
  196. current->ac.hlistY = g["HeroList"]["y"].Float();
  197. current->ac.hlistSize = g["HeroList"]["size"].Float();
  198. current->ac.hlistMB = g["HeroList"]["movePoints"].String();
  199. current->ac.hlistMN = g["HeroList"]["manaPoints"].String();
  200. current->ac.hlistAU = g["HeroList"]["arrowUp"].String();
  201. current->ac.hlistAD = g["HeroList"]["arrowDown"].String();
  202. current->ac.tlistX = g["TownList"]["x"].Float();
  203. current->ac.tlistY = g["TownList"]["y"].Float();
  204. current->ac.tlistSize = g["TownList"]["size"].Float();
  205. current->ac.tlistAU = g["TownList"]["arrowUp"].String();
  206. current->ac.tlistAD = g["TownList"]["arrowDown"].String();
  207. current->ac.minimapW = g["Minimap"]["width"].Float();
  208. current->ac.minimapH = g["Minimap"]["height"].Float();
  209. current->ac.minimapX = g["Minimap"]["x"].Float();
  210. current->ac.minimapY = g["Minimap"]["y"].Float();
  211. current->ac.overviewPics = g["Overview"]["pics"].Float();
  212. current->ac.overviewSize = g["Overview"]["size"].Float();
  213. current->ac.overviewBg = g["Overview"]["graphic"].String();
  214. current->ac.statusbarX = g["Statusbar"]["x"].Float();
  215. current->ac.statusbarY = g["Statusbar"]["y"].Float();
  216. current->ac.statusbarG = g["Statusbar"]["graphic"].String();
  217. current->ac.resdatabarX = g["ResDataBar"]["x"].Float();
  218. current->ac.resdatabarY = g["ResDataBar"]["y"].Float();
  219. current->ac.resOffsetX = g["ResDataBar"]["offsetX"].Float();
  220. current->ac.resOffsetY = g["ResDataBar"]["offsetY"].Float();
  221. current->ac.resDist = g["ResDataBar"]["resSpace"].Float();
  222. current->ac.resDateDist = g["ResDataBar"]["resDateSpace"].Float();
  223. current->ac.resdatabarG = g["ResDataBar"]["graphic"].String();
  224. setButton(current->ac.kingOverview, g["ButtonKingdomOv"]);
  225. setButton(current->ac.underground, g["ButtonUnderground"]);
  226. setButton(current->ac.questlog, g["ButtonQuestLog"]);
  227. setButton(current->ac.sleepWake, g["ButtonSleepWake"]);
  228. setButton(current->ac.moveHero, g["ButtonMoveHero"]);
  229. setButton(current->ac.spellbook, g["ButtonSpellbook"]);
  230. setButton(current->ac.advOptions, g["ButtonAdvOptions"]);
  231. setButton(current->ac.sysOptions, g["ButtonSysOptions"]);
  232. setButton(current->ac.nextHero, g["ButtonNextHero"]);
  233. setButton(current->ac.endTurn, g["ButtonEndTurn"]);
  234. }
  235. const JsonNode& screenRes = settings["video"]["screenRes"];
  236. SetResolution(screenRes["width"].Float(), screenRes["height"].Float());
  237. }
  238. // Force instantiation of the SettingsStorage::NodeAccessor class template.
  239. // That way method definitions can sit in the cpp file
  240. template struct SettingsStorage::NodeAccessor<SettingsListener>;
  241. template struct SettingsStorage::NodeAccessor<Settings>;