CModInfo.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CModInfo.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 "CModInfo.h"
  12. #include "../CGeneralTextHandler.h"
  13. #include "../VCMI_Lib.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. static JsonNode addMeta(JsonNode config, const std::string & meta)
  16. {
  17. config.setMeta(meta);
  18. return config;
  19. }
  20. CModInfo::CModInfo():
  21. checksum(0),
  22. explicitlyEnabled(false),
  23. implicitlyEnabled(true),
  24. validation(PENDING)
  25. {
  26. }
  27. CModInfo::CModInfo(const std::string & identifier, const JsonNode & local, const JsonNode & config):
  28. identifier(identifier),
  29. name(config["name"].String()),
  30. description(config["description"].String()),
  31. dependencies(config["depends"].convertTo<std::set<std::string>>()),
  32. conflicts(config["conflicts"].convertTo<std::set<std::string>>()),
  33. checksum(0),
  34. explicitlyEnabled(false),
  35. implicitlyEnabled(true),
  36. validation(PENDING),
  37. config(addMeta(config, identifier))
  38. {
  39. version = CModVersion::fromString(config["version"].String());
  40. if(!config["compatibility"].isNull())
  41. {
  42. vcmiCompatibleMin = CModVersion::fromString(config["compatibility"]["min"].String());
  43. vcmiCompatibleMax = CModVersion::fromString(config["compatibility"]["max"].String());
  44. }
  45. if (!config["language"].isNull())
  46. baseLanguage = config["language"].String();
  47. else
  48. baseLanguage = "english";
  49. loadLocalData(local);
  50. }
  51. JsonNode CModInfo::saveLocalData() const
  52. {
  53. std::ostringstream stream;
  54. stream << std::noshowbase << std::hex << std::setw(8) << std::setfill('0') << checksum;
  55. JsonNode conf;
  56. conf["active"].Bool() = explicitlyEnabled;
  57. conf["validated"].Bool() = validation != FAILED;
  58. conf["checksum"].String() = stream.str();
  59. return conf;
  60. }
  61. std::string CModInfo::getModDir(const std::string & name)
  62. {
  63. return "MODS/" + boost::algorithm::replace_all_copy(name, ".", "/MODS/");
  64. }
  65. std::string CModInfo::getModFile(const std::string & name)
  66. {
  67. return getModDir(name) + "/mod.json";
  68. }
  69. void CModInfo::updateChecksum(ui32 newChecksum)
  70. {
  71. // comment-out next line to force validation of all mods ignoring checksum
  72. if (newChecksum != checksum)
  73. {
  74. checksum = newChecksum;
  75. validation = PENDING;
  76. }
  77. }
  78. void CModInfo::loadLocalData(const JsonNode & data)
  79. {
  80. bool validated = false;
  81. implicitlyEnabled = true;
  82. explicitlyEnabled = !config["keepDisabled"].Bool();
  83. checksum = 0;
  84. if (data.getType() == JsonNode::JsonType::DATA_BOOL)
  85. {
  86. explicitlyEnabled = data.Bool();
  87. }
  88. if (data.getType() == JsonNode::JsonType::DATA_STRUCT)
  89. {
  90. explicitlyEnabled = data["active"].Bool();
  91. validated = data["validated"].Bool();
  92. checksum = strtol(data["checksum"].String().c_str(), nullptr, 16);
  93. }
  94. //check compatibility
  95. implicitlyEnabled &= (vcmiCompatibleMin.isNull() || CModVersion::GameVersion().compatible(vcmiCompatibleMin));
  96. implicitlyEnabled &= (vcmiCompatibleMax.isNull() || vcmiCompatibleMax.compatible(CModVersion::GameVersion()));
  97. if(!implicitlyEnabled)
  98. logGlobal->warn("Mod %s is incompatible with current version of VCMI and cannot be enabled", name);
  99. if (boost::iequals(config["modType"].String(), "translation")) // compatibility code - mods use "Translation" type at the moment
  100. {
  101. if (baseLanguage != VLC->generaltexth->getPreferredLanguage())
  102. {
  103. logGlobal->warn("Translation mod %s was not loaded: language mismatch!", name);
  104. implicitlyEnabled = false;
  105. }
  106. }
  107. if (isEnabled())
  108. validation = validated ? PASSED : PENDING;
  109. else
  110. validation = validated ? PASSED : FAILED;
  111. }
  112. bool CModInfo::isEnabled() const
  113. {
  114. return implicitlyEnabled && explicitlyEnabled;
  115. }
  116. void CModInfo::setEnabled(bool on)
  117. {
  118. explicitlyEnabled = on;
  119. }
  120. VCMI_LIB_NAMESPACE_END