CConfigHandler.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "filesystem/Filesystem.h"
  13. #include "GameConstants.h"
  14. #include "VCMIDirs.h"
  15. #include "json/JsonUtils.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. SettingsStorage settings;
  18. SettingsStorage persistentStorage;
  19. template<typename Accessor>
  20. SettingsStorage::NodeAccessor<Accessor>::NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path):
  21. parent(_parent),
  22. path(std::move(_path))
  23. {
  24. }
  25. template<typename Accessor>
  26. SettingsStorage::NodeAccessor<Accessor> SettingsStorage::NodeAccessor<Accessor>::operator[](const 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. SettingsStorage::~SettingsStorage()
  50. {
  51. // hack for possible crash due to static destruction order (setting storage can be destroyed before all listeners have died)
  52. for(SettingsListener * listener : listeners)
  53. listener->terminate();
  54. }
  55. void SettingsStorage::init(const std::string & dataFilename, const std::string & schema)
  56. {
  57. this->dataFilename = dataFilename;
  58. this->schema = schema;
  59. JsonPath confName = JsonPath::builtin(dataFilename);
  60. config = JsonUtils::assembleFromFiles(confName.getOriginalName());
  61. // Probably new install. Create config file to save settings to
  62. if (!CResourceHandler::get("local")->existsResource(confName))
  63. {
  64. CResourceHandler::get("local")->createResource(dataFilename);
  65. if(schema.empty())
  66. invalidateNode(std::vector<std::string>());
  67. }
  68. if(!schema.empty())
  69. {
  70. JsonUtils::maximize(config, schema);
  71. JsonUtils::validate(config, schema, "settings");
  72. }
  73. }
  74. void SettingsStorage::invalidateNode(const std::vector<std::string> &changedPath)
  75. {
  76. for(SettingsListener * listener : listeners)
  77. listener->nodeInvalidated(changedPath);
  78. JsonNode savedConf = config;
  79. savedConf.Struct().erase("session");
  80. if(!schema.empty())
  81. JsonUtils::minimize(savedConf, schema);
  82. std::fstream file(CResourceHandler::get()->getResourceName(JsonPath::builtin(dataFilename))->c_str(), std::ofstream::out | std::ofstream::trunc);
  83. file << savedConf.toString();
  84. }
  85. JsonNode & SettingsStorage::getNode(const std::vector<std::string> & path)
  86. {
  87. JsonNode *node = &config;
  88. for(const std::string & value : path)
  89. node = &(*node)[value];
  90. return *node;
  91. }
  92. Settings SettingsStorage::get(const std::vector<std::string> & path)
  93. {
  94. return Settings(*this, path);
  95. }
  96. const JsonNode & SettingsStorage::operator[](const std::string & value) const
  97. {
  98. return config[value];
  99. }
  100. const JsonNode & SettingsStorage::toJsonNode() const
  101. {
  102. return config;
  103. }
  104. SettingsListener::SettingsListener(SettingsStorage & _parent, std::vector<std::string> _path):
  105. parent(_parent),
  106. path(std::move(_path))
  107. {
  108. parent.listeners.insert(this);
  109. }
  110. SettingsListener::SettingsListener(const SettingsListener &sl):
  111. parent(sl.parent),
  112. path(sl.path),
  113. callback(sl.callback)
  114. {
  115. parent.listeners.insert(this);
  116. }
  117. void SettingsListener::terminate()
  118. {
  119. wasTerminated = true;
  120. }
  121. SettingsListener::~SettingsListener()
  122. {
  123. if (!wasTerminated)
  124. parent.listeners.erase(this);
  125. }
  126. void SettingsListener::nodeInvalidated(const std::vector<std::string> &changedPath)
  127. {
  128. if (!callback)
  129. return;
  130. size_t min = std::min(path.size(), changedPath.size());
  131. size_t mismatch = std::mismatch(path.begin(), path.begin()+min, changedPath.begin()).first - path.begin();
  132. if (min == mismatch)
  133. callback(parent.getNode(path));
  134. }
  135. void SettingsListener::operator() (std::function<void(const JsonNode&)> _callback)
  136. {
  137. callback = std::move(_callback);
  138. }
  139. Settings::Settings(SettingsStorage &_parent, const std::vector<std::string> &_path):
  140. parent(_parent),
  141. path(_path),
  142. node(_parent.getNode(_path)),
  143. copy(_parent.getNode(_path))
  144. {
  145. }
  146. Settings::~Settings()
  147. {
  148. if (node != copy)
  149. parent.invalidateNode(path);
  150. }
  151. JsonNode* Settings::operator -> ()
  152. {
  153. return &node;
  154. }
  155. const JsonNode* Settings::operator ->() const
  156. {
  157. return &node;
  158. }
  159. const JsonNode & Settings::operator[](const std::string & value) const
  160. {
  161. return node[value];
  162. }
  163. JsonNode & Settings::operator[](const std::string & value)
  164. {
  165. return node[value];
  166. }
  167. // Force instantiation of the SettingsStorage::NodeAccessor class template.
  168. // That way method definitions can sit in the cpp file
  169. template struct SettingsStorage::NodeAccessor<SettingsListener>;
  170. template struct SettingsStorage::NodeAccessor<Settings>;
  171. VCMI_LIB_NAMESPACE_END