cmodlist.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * cmodlist.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 "cmodlist.h"
  12. #include "../lib/CConfigHandler.h"
  13. #include "../../lib/filesystem/CFileInputStream.h"
  14. #include "../../lib/GameConstants.h"
  15. #include "../../lib/modding/CModVersion.h"
  16. QString CModEntry::sizeToString(double size)
  17. {
  18. static const std::array sizes {
  19. QT_TRANSLATE_NOOP("File size", "%1 B"),
  20. QT_TRANSLATE_NOOP("File size", "%1 KiB"),
  21. QT_TRANSLATE_NOOP("File size", "%1 MiB"),
  22. QT_TRANSLATE_NOOP("File size", "%1 GiB"),
  23. QT_TRANSLATE_NOOP("File size", "%1 TiB")
  24. };
  25. size_t index = 0;
  26. while(size > 1024 && index < sizes.size())
  27. {
  28. size /= 1024;
  29. index++;
  30. }
  31. return QCoreApplication::translate("File size", sizes[index]).arg(QString::number(size, 'f', 1));
  32. }
  33. CModEntry::CModEntry(QVariantMap repository, QVariantMap localData, bool modActive, QString modname)
  34. : repository(repository), localData(localData), modActive(modActive), modname(modname)
  35. {
  36. }
  37. bool CModEntry::isEnabled() const
  38. {
  39. if(!isInstalled())
  40. return false;
  41. if (!isVisible())
  42. return false;
  43. return modActive;
  44. }
  45. bool CModEntry::isDisabled() const
  46. {
  47. if(!isInstalled())
  48. return false;
  49. return !isEnabled();
  50. }
  51. bool CModEntry::isAvailable() const
  52. {
  53. if(isInstalled())
  54. return false;
  55. return !repository.isEmpty();
  56. }
  57. bool CModEntry::isUpdateable() const
  58. {
  59. if(!isInstalled())
  60. return false;
  61. auto installedVer = localData["installedVersion"].toString().toStdString();
  62. auto availableVer = repository["latestVersion"].toString().toStdString();
  63. return (CModVersion::fromString(installedVer) < CModVersion::fromString(availableVer));
  64. }
  65. bool isCompatible(const QVariantMap & compatibility)
  66. {
  67. auto compatibleMin = CModVersion::fromString(compatibility["min"].toString().toStdString());
  68. auto compatibleMax = CModVersion::fromString(compatibility["max"].toString().toStdString());
  69. return (compatibleMin.isNull() || CModVersion::GameVersion().compatible(compatibleMin, true, true))
  70. && (compatibleMax.isNull() || compatibleMax.compatible(CModVersion::GameVersion(), true, true));
  71. }
  72. bool CModEntry::isCompatible() const
  73. {
  74. return ::isCompatible(localData["compatibility"].toMap());
  75. }
  76. bool CModEntry::isEssential() const
  77. {
  78. return getName() == "vcmi";
  79. }
  80. bool CModEntry::isInstalled() const
  81. {
  82. return !localData.isEmpty();
  83. }
  84. bool CModEntry::isVisible() const
  85. {
  86. if (isCompatibilityPatch())
  87. {
  88. if (isSubmod())
  89. return false;
  90. }
  91. if (isTranslation())
  92. {
  93. // Do not show not installed translation mods to languages other than player language
  94. if (localData.empty() && getBaseValue("language") != QString::fromStdString(settings["general"]["language"].String()) )
  95. return false;
  96. }
  97. return !localData.isEmpty() || (!repository.isEmpty() && !repository.contains("mod"));
  98. }
  99. bool CModEntry::isTranslation() const
  100. {
  101. return getBaseValue("modType").toString() == "Translation";
  102. }
  103. bool CModEntry::isCompatibilityPatch() const
  104. {
  105. return getBaseValue("modType").toString() == "Compatibility";
  106. }
  107. bool CModEntry::isSubmod() const
  108. {
  109. return getName().contains('.');
  110. }
  111. int CModEntry::getModStatus() const
  112. {
  113. int status = 0;
  114. if(isEnabled())
  115. status |= ModStatus::ENABLED;
  116. if(isInstalled())
  117. status |= ModStatus::INSTALLED;
  118. if(isUpdateable())
  119. status |= ModStatus::UPDATEABLE;
  120. return status;
  121. }
  122. QString CModEntry::getName() const
  123. {
  124. return modname;
  125. }
  126. QString CModEntry::getTopParentName() const
  127. {
  128. assert(isSubmod());
  129. return modname.section('.', 0, 0);
  130. }
  131. QVariant CModEntry::getValue(QString value) const
  132. {
  133. return getValueImpl(value, true);
  134. }
  135. QStringList CModEntry::getDependencies() const
  136. {
  137. QStringList result;
  138. for (auto const & entry : getValue("depends").toStringList())
  139. result.push_back(entry.toLower());
  140. return result;
  141. }
  142. QStringList CModEntry::getConflicts() const
  143. {
  144. QStringList result;
  145. for (auto const & entry : getValue("conflicts").toStringList())
  146. result.push_back(entry.toLower());
  147. return result;
  148. }
  149. QVariant CModEntry::getBaseValue(QString value) const
  150. {
  151. return getValueImpl(value, false);
  152. }
  153. QVariant CModEntry::getValueImpl(QString value, bool localized) const
  154. {
  155. QString langValue = QString::fromStdString(settings["general"]["language"].String());
  156. // Priorities
  157. // 1) data from newest version
  158. // 2) data from preferred language
  159. bool useRepositoryData = repository.contains(value);
  160. if(repository.contains(value) && localData.contains(value))
  161. {
  162. // value is present in both repo and locally installed. Select one from latest version
  163. auto installedVer = localData["installedVersion"].toString().toStdString();
  164. auto availableVer = repository["latestVersion"].toString().toStdString();
  165. useRepositoryData = CModVersion::fromString(installedVer) < CModVersion::fromString(availableVer);
  166. }
  167. auto & storage = useRepositoryData ? repository : localData;
  168. if(localized && storage.contains(langValue))
  169. {
  170. auto langStorage = storage[langValue].toMap();
  171. if (langStorage.contains(value))
  172. return langStorage[value];
  173. }
  174. if(storage.contains(value))
  175. return storage[value];
  176. return QVariant();
  177. }
  178. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to) const
  179. {
  180. QVariantMap renamed;
  181. for(auto it = data.begin(); it != data.end(); it++)
  182. {
  183. QVariantMap modConf = it.value().toMap();
  184. modConf.insert(to, modConf.value(from));
  185. renamed.insert(it.key(), modConf);
  186. }
  187. return renamed;
  188. }
  189. void CModList::reloadRepositories()
  190. {
  191. cachedMods.clear();
  192. }
  193. void CModList::resetRepositories()
  194. {
  195. repositories.clear();
  196. cachedMods.clear();
  197. }
  198. void CModList::addRepository(QVariantMap data)
  199. {
  200. for(auto & key : data.keys())
  201. data[key.toLower()] = data.take(key);
  202. repositories.push_back(copyField(data, "version", "latestVersion"));
  203. cachedMods.clear();
  204. }
  205. void CModList::setLocalModList(QVariantMap data)
  206. {
  207. localModList = copyField(data, "version", "installedVersion");
  208. cachedMods.clear();
  209. }
  210. void CModList::setModSettings(std::shared_ptr<ModSettingsStorage> data)
  211. {
  212. modSettings = data;
  213. cachedMods.clear();
  214. }
  215. void CModList::modChanged(QString modID)
  216. {
  217. cachedMods.clear();
  218. }
  219. static QVariant getValue(QVariant input, QString path)
  220. {
  221. if(path.size() > 1)
  222. {
  223. QString entryName = path.section('/', 0, 1);
  224. QString remainder = "/" + path.section('/', 2, -1);
  225. entryName.remove(0, 1);
  226. return getValue(input.toMap().value(entryName), remainder);
  227. }
  228. else
  229. {
  230. return input;
  231. }
  232. }
  233. const CModEntry & CModList::getMod(QString modName) const
  234. {
  235. modName = modName.toLower();
  236. auto it = cachedMods.find(modName);
  237. if (it != cachedMods.end())
  238. return it.value();
  239. auto itNew = cachedMods.insert(modName, getModUncached(modName));
  240. return *itNew;
  241. }
  242. CModEntry CModList::getModUncached(QString modname) const
  243. {
  244. QVariantMap repo;
  245. QVariantMap local = localModList[modname].toMap();
  246. QString path = modname;
  247. path = "/" + path.replace(".", "/mods/");
  248. bool modActive = modSettings->isModActive(modname);
  249. if(modActive)
  250. {
  251. QString rootModName = modname.section('.', 0, 1);
  252. if (!modSettings->isModActive(rootModName))
  253. modActive = false; // parent mod is inactive -> submod is also inactive
  254. }
  255. if(modActive)
  256. {
  257. if(!::isCompatible(local.value("compatibility").toMap()))
  258. modActive = false; // mod not compatible with our version of vcmi -> inactive
  259. }
  260. for(auto entry : repositories)
  261. {
  262. QVariant repoVal = getValue(entry, path);
  263. if(repoVal.isValid())
  264. {
  265. auto repoValMap = repoVal.toMap();
  266. if(::isCompatible(repoValMap["compatibility"].toMap()))
  267. {
  268. if(repo.empty()
  269. || CModVersion::fromString(repo["version"].toString().toStdString())
  270. < CModVersion::fromString(repoValMap["version"].toString().toStdString()))
  271. {
  272. //take valid download link, screenshots and mod size before assignment
  273. auto download = repo.value("download");
  274. auto screenshots = repo.value("screenshots");
  275. auto size = repo.value("downloadSize");
  276. repo = repoValMap;
  277. if(repo.value("download").isNull())
  278. {
  279. repo["download"] = download;
  280. if(repo.value("screenshots").isNull()) //taking screenshot from the downloadable version
  281. repo["screenshots"] = screenshots;
  282. }
  283. if(repo.value("downloadSize").isNull())
  284. repo["downloadSize"] = size;
  285. }
  286. }
  287. }
  288. }
  289. return CModEntry(repo, local, modActive, modname);
  290. }
  291. bool CModList::hasMod(QString modname) const
  292. {
  293. if(localModList.contains(modname))
  294. return true;
  295. for(auto entry : repositories)
  296. if(entry.contains(modname))
  297. return true;
  298. return false;
  299. }
  300. QStringList CModList::getRequirements(QString modname)
  301. {
  302. QStringList ret;
  303. if(hasMod(modname))
  304. {
  305. auto mod = getMod(modname);
  306. for(auto entry : mod.getDependencies())
  307. ret += getRequirements(entry.toLower());
  308. }
  309. ret += modname;
  310. return ret;
  311. }
  312. QVector<QString> CModList::getModList() const
  313. {
  314. QSet<QString> knownMods;
  315. QVector<QString> modList;
  316. for(auto repo : repositories)
  317. {
  318. for(auto it = repo.begin(); it != repo.end(); it++)
  319. {
  320. knownMods.insert(it.key().toLower());
  321. }
  322. }
  323. for(auto it = localModList.begin(); it != localModList.end(); it++)
  324. {
  325. knownMods.insert(it.key().toLower());
  326. }
  327. for(auto entry : knownMods)
  328. {
  329. modList.push_back(entry);
  330. }
  331. return modList;
  332. }
  333. QVector<QString> CModList::getChildren(QString parent) const
  334. {
  335. QVector<QString> children;
  336. int depth = parent.count('.') + 1;
  337. for(const QString & mod : getModList())
  338. {
  339. if(mod.count('.') == depth && mod.startsWith(parent))
  340. children.push_back(mod);
  341. }
  342. return children;
  343. }