CConfigHandler.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/GameConstants.h"
  14. #include "../lib/VCMIDirs.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. SettingsStorage settings;
  17. template<typename Accessor>
  18. SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):
  19. parent(_parent),
  20. path(std::move(_path))
  21. {
  22. }
  23. template<typename Accessor>
  24. SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator[](const std::string & nextNode) const
  25. {
  26. std::vector<std::string> newPath = path;
  27. newPath.push_back(nextNode);
  28. return NodeAccessor(parent, newPath);
  29. }
  30. template<typename Accessor>
  31. SettingsStorage::NodeAccessor<Accessor>::operator Accessor() const
  32. {
  33. return Accessor(parent, path);
  34. }
  35. template<typename Accessor>
  36. SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator () (std::vector<std::string> _path) const
  37. {
  38. std::vector<std::string> newPath = path;
  39. newPath.insert( newPath.end(), _path.begin(), _path.end());
  40. return NodeAccessor(parent, newPath);
  41. }
  42. SettingsStorage::SettingsStorage():
  43. write(NodeAccessor<Settings>(*this, std::vector<std::string>() )),
  44. listen(NodeAccessor<SettingsListener>(*this, std::vector<std::string>() ))
  45. {
  46. }
  47. void SettingsStorage::init()
  48. {
  49. JsonPath confName = JsonPath::builtin("config/settings.json");
  50. JsonUtils::assembleFromFiles(confName.getOriginalName()).swap(config);
  51. // Probably new install. Create config file to save settings to
  52. if (!CResourceHandler::get("local")->existsResource(confName))
  53. CResourceHandler::get("local")->createResource("config/settings.json");
  54. JsonUtils::maximize(config, "vcmi:settings");
  55. JsonUtils::validate(config, "vcmi:settings", "settings");
  56. }
  57. void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
  58. {
  59. for(SettingsListener * listener : listeners)
  60. listener->nodeInvalidated(changedPath);
  61. JsonNode savedConf = config;
  62. savedConf.Struct().erase("session");
  63. JsonUtils::minimize(savedConf, "vcmi:settings");
  64. std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin("config/settings.json"))->c_str(), std::ofstream::out | std::ofstream::trunc);
  65. file << savedConf.toJson();
  66. }
  67. JsonNode & SettingsStorage::getNode(const std::vector<std::string> & path)
  68. {
  69. JsonNode *node = &config;
  70. for(const std::string & value : path)
  71. node = &(*node)[value];
  72. return *node;
  73. }
  74. Settings SettingsStorage::get(const std::vector<std::string> & path)
  75. {
  76. return Settings(*this, path);
  77. }
  78. const JsonNode & SettingsStorage::operator[](const std::string & value) const
  79. {
  80. return config[value];
  81. }
  82. const JsonNode & SettingsStorage::toJsonNode() const
  83. {
  84. return config;
  85. }
  86. SettingsListener::SettingsListener(SettingsStorage & _parent, std::vector<std::string> _path):
  87. parent(_parent),
  88. path(std::move(_path))
  89. {
  90. parent.listeners.insert(this);
  91. }
  92. SettingsListener::SettingsListener(const SettingsListener &sl):
  93. parent(sl.parent),
  94. path(sl.path),
  95. callback(sl.callback)
  96. {
  97. parent.listeners.insert(this);
  98. }
  99. SettingsListener::~SettingsListener()
  100. {
  101. parent.listeners.erase(this);
  102. }
  103. void SettingsListener::nodeInvalidated(const std::vector<std::string> &changedPath)
  104. {
  105. if (!callback)
  106. return;
  107. size_t min = std::min(path.size(), changedPath.size());
  108. size_t mismatch = std::mismatch(path.begin(), path.begin()+min, changedPath.begin()).first - path.begin();
  109. if (min == mismatch)
  110. callback(parent.getNode(path));
  111. }
  112. void SettingsListener::operator() (std::function<void(const JsonNode&)> _callback)
  113. {
  114. callback = std::move(_callback);
  115. }
  116. Settings::Settings(SettingsStorage &_parent, const std::vector<std::string> &_path):
  117. parent(_parent),
  118. path(_path),
  119. node(_parent.getNode(_path)),
  120. copy(_parent.getNode(_path))
  121. {
  122. }
  123. Settings::~Settings()
  124. {
  125. if (node != copy)
  126. parent.invalidateNode(path);
  127. }
  128. JsonNode* Settings::operator -> ()
  129. {
  130. return &node;
  131. }
  132. const JsonNode* Settings::operator ->() const
  133. {
  134. return &node;
  135. }
  136. const JsonNode & Settings::operator[](const std::string & value) const
  137. {
  138. return node[value];
  139. }
  140. JsonNode & Settings::operator[](const std::string & value)
  141. {
  142. return node[value];
  143. }
  144. // Force instantiation of the SettingsStorage::NodeAccessor class template.
  145. // That way method definitions can sit in the cpp file
  146. template struct SettingsStorage::NodeAccessor<SettingsListener>;
  147. template struct SettingsStorage::NodeAccessor<Settings>;
  148. VCMI_LIB_NAMESPACE_END