CConfigHandler.cpp 5.0 KB

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