CConfigHandler.cpp 4.9 KB

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