CConfigHandler.cpp 9.5 KB

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