CConfigHandler.cpp 9.2 KB

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