CConfigHandler.cpp 9.4 KB

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