CConfigHandler.cpp 4.8 KB

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