ModDescription.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. const JsonNode & localValue = getLocalValue(keyName);
  99. if (localValue.isNull())
  100. return getRepositoryValue(keyName);
  101. else
  102. return getLocalValue(keyName);
  103. }
  104. const JsonNode & ModDescription::getLocalValue(const std::string & keyName) const
  105. {
  106. return getLocalConfig()[keyName];
  107. }
  108. const JsonNode & ModDescription::getRepositoryValue(const std::string & keyName) const
  109. {
  110. return (*repositoryConfig)[keyName];
  111. }
  112. CModVersion ModDescription::getVersion() const
  113. {
  114. return CModVersion::fromString(getValue("version").String());
  115. }
  116. ModVerificationInfo ModDescription::getVerificationInfo() const
  117. {
  118. ModVerificationInfo result;
  119. result.name = getName();
  120. result.version = getVersion();
  121. result.impactsGameplay = affectsGameplay();
  122. result.parent = getParentID();
  123. return result;
  124. }
  125. bool ModDescription::isCompatible() const
  126. {
  127. const JsonNode & compatibility = getValue("compatibility");
  128. if (compatibility.isNull())
  129. return true;
  130. auto vcmiCompatibleMin = CModVersion::fromString(compatibility["min"].String());
  131. auto vcmiCompatibleMax = CModVersion::fromString(compatibility["max"].String());
  132. bool compatible = true;
  133. compatible &= (vcmiCompatibleMin.isNull() || CModVersion::GameVersion().compatible(vcmiCompatibleMin, true, true));
  134. compatible &= (vcmiCompatibleMax.isNull() || vcmiCompatibleMax.compatible(CModVersion::GameVersion(), true, true));
  135. return compatible;
  136. }
  137. bool ModDescription::isCompatibility() const
  138. {
  139. return getValue("modType").String() == "Compatibility";
  140. }
  141. bool ModDescription::isTranslation() const
  142. {
  143. return getValue("modType").String() == "Translation";
  144. }
  145. bool ModDescription::keepDisabled() const
  146. {
  147. return getValue("keepDisabled").Bool();
  148. }
  149. bool ModDescription::isInstalled() const
  150. {
  151. return !localConfig->isNull();
  152. }
  153. bool ModDescription::affectsGameplay() const
  154. {
  155. static const std::array keysToTest = {
  156. "artifacts",
  157. "battlefields",
  158. "creatures",
  159. "factions",
  160. "heroClasses",
  161. "heroes",
  162. "objects",
  163. "obstacles",
  164. "rivers",
  165. "roads",
  166. "settings",
  167. "skills",
  168. "spells",
  169. "terrains",
  170. };
  171. for(const auto & key : keysToTest)
  172. if (!getLocalValue(key).isNull())
  173. return true;
  174. return false;
  175. }
  176. VCMI_LIB_NAMESPACE_END