2
0

ModDescription.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * ModDescription.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 "ModDescription.h"
  12. #include "CModVersion.h"
  13. #include "ModVerificationInfo.h"
  14. #include "../json/JsonNode.h"
  15. #include "../texts/CGeneralTextHandler.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. ModDescription::ModDescription(const TModID & fullID, const JsonNode & localConfig, const JsonNode & repositoryConfig)
  18. : identifier(fullID)
  19. , localConfig(std::make_unique<JsonNode>(localConfig))
  20. , repositoryConfig(std::make_unique<JsonNode>(repositoryConfig))
  21. , dependencies(loadModList(getValue("depends")))
  22. , softDependencies(loadModList(getValue("softDepends")))
  23. , conflicts(loadModList(getValue("conflicts")))
  24. {
  25. if(getID() != "core")
  26. dependencies.emplace("core");
  27. if (!getParentID().empty())
  28. dependencies.emplace(getParentID());
  29. }
  30. ModDescription::~ModDescription() = default;
  31. TModSet ModDescription::loadModList(const JsonNode & configNode) const
  32. {
  33. TModSet result;
  34. for(const auto & entry : configNode.Vector())
  35. result.insert(boost::algorithm::to_lower_copy(entry.String()));
  36. return result;
  37. }
  38. const TModID & ModDescription::getID() const
  39. {
  40. return identifier;
  41. }
  42. TModID ModDescription::getParentID() const
  43. {
  44. size_t dotPos = identifier.find_last_of('.');
  45. if(dotPos == std::string::npos)
  46. return {};
  47. return identifier.substr(0, dotPos);
  48. }
  49. TModID ModDescription::getTopParentID() const
  50. {
  51. size_t dotPos = identifier.find('.');
  52. if(dotPos == std::string::npos)
  53. return {};
  54. return identifier.substr(0, dotPos);
  55. }
  56. const TModSet & ModDescription::getDependencies() const
  57. {
  58. return dependencies;
  59. }
  60. const TModSet & ModDescription::getSoftDependencies() const
  61. {
  62. return softDependencies;
  63. }
  64. const TModSet & ModDescription::getConflicts() const
  65. {
  66. return conflicts;
  67. }
  68. const std::string & ModDescription::getBaseLanguage() const
  69. {
  70. static const std::string defaultLanguage = "english";
  71. return getValue("language").isString() ? getValue("language").String() : defaultLanguage;
  72. }
  73. const std::string & ModDescription::getName() const
  74. {
  75. return getLocalizedValue("name").String();
  76. }
  77. const JsonNode & ModDescription::getFilesystemConfig() const
  78. {
  79. return getLocalValue("filesystem");
  80. }
  81. const JsonNode & ModDescription::getLocalConfig() const
  82. {
  83. return *localConfig;
  84. }
  85. const JsonNode & ModDescription::getLocalizedValue(const std::string & keyName) const
  86. {
  87. const std::string language = CGeneralTextHandler::getPreferredLanguage();
  88. const JsonNode & languageNode = getValue(language);
  89. const JsonNode & baseValue = getValue(keyName);
  90. const JsonNode & localizedValue = languageNode[keyName];
  91. if (localizedValue.isNull())
  92. return baseValue;
  93. else
  94. return localizedValue;
  95. }
  96. const JsonNode & ModDescription::getValue(const std::string & keyName) const
  97. {
  98. if (!isInstalled() || isUpdateAvailable())
  99. return getRepositoryValue(keyName);
  100. else
  101. return getLocalValue(keyName);
  102. }
  103. const JsonNode & ModDescription::getLocalValue(const std::string & keyName) const
  104. {
  105. return getLocalConfig()[keyName];
  106. }
  107. const JsonNode & ModDescription::getRepositoryValue(const std::string & keyName) const
  108. {
  109. return (*repositoryConfig)[keyName];
  110. }
  111. CModVersion ModDescription::getVersion() const
  112. {
  113. return CModVersion::fromString(getValue("version").String());
  114. }
  115. ModVerificationInfo ModDescription::getVerificationInfo() const
  116. {
  117. ModVerificationInfo result;
  118. result.name = getName();
  119. result.version = getVersion();
  120. result.impactsGameplay = affectsGameplay();
  121. result.parent = getParentID();
  122. return result;
  123. }
  124. bool ModDescription::isCompatible() const
  125. {
  126. const JsonNode & compatibility = getValue("compatibility");
  127. if (compatibility.isNull())
  128. return true;
  129. auto vcmiCompatibleMin = CModVersion::fromString(compatibility["min"].String());
  130. auto vcmiCompatibleMax = CModVersion::fromString(compatibility["max"].String());
  131. bool compatible = true;
  132. compatible &= (vcmiCompatibleMin.isNull() || CModVersion::GameVersion().compatible(vcmiCompatibleMin, true, true));
  133. compatible &= (vcmiCompatibleMax.isNull() || vcmiCompatibleMax.compatible(CModVersion::GameVersion(), true, true));
  134. return compatible;
  135. }
  136. bool ModDescription::isCompatibility() const
  137. {
  138. return getValue("modType").String() == "Compatibility";
  139. }
  140. bool ModDescription::isTranslation() const
  141. {
  142. return getValue("modType").String() == "Translation";
  143. }
  144. bool ModDescription::keepDisabled() const
  145. {
  146. return getValue("keepDisabled").Bool();
  147. }
  148. bool ModDescription::isInstalled() const
  149. {
  150. return !localConfig->isNull();
  151. }
  152. bool ModDescription::affectsGameplay() const
  153. {
  154. static const std::array keysToTest = {
  155. "artifacts",
  156. "battlefields",
  157. "creatures",
  158. "factions",
  159. "heroClasses",
  160. "heroes",
  161. "objects",
  162. "obstacles",
  163. "rivers",
  164. "roads",
  165. "settings",
  166. "skills",
  167. "spells",
  168. "terrains",
  169. };
  170. for(const auto & key : keysToTest)
  171. if (!getLocalValue(key).isNull())
  172. return true;
  173. return false;
  174. }
  175. bool ModDescription::isUpdateAvailable() const
  176. {
  177. if (getRepositoryValue("version").isNull())
  178. return false;
  179. if (getLocalValue("version").isNull())
  180. return false;
  181. auto localVersion = CModVersion::fromString(getLocalValue("version").String());
  182. auto repositoryVersion = CModVersion::fromString(getRepositoryValue("version").String());
  183. return localVersion < repositoryVersion;
  184. }
  185. VCMI_LIB_NAMESPACE_END