cmodlist.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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, QVariantMap modSettings, QString modname)
  34. : repository(repository), localData(localData), modSettings(modSettings), modname(modname)
  35. {
  36. }
  37. bool CModEntry::isEnabled() const
  38. {
  39. if(!isInstalled())
  40. return false;
  41. if (!isVisible())
  42. return false;
  43. return modSettings["active"].toBool();
  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. QVariant CModEntry::getValue(QString value) const
  127. {
  128. return getValueImpl(value, true);
  129. }
  130. QStringList CModEntry::getDependencies() const
  131. {
  132. QStringList result;
  133. for (auto const & entry : getValue("depends").toStringList())
  134. result.push_back(entry.toLower());
  135. return result;
  136. }
  137. QStringList CModEntry::getConflicts() const
  138. {
  139. QStringList result;
  140. for (auto const & entry : getValue("conflicts").toStringList())
  141. result.push_back(entry.toLower());
  142. return result;
  143. }
  144. QVariant CModEntry::getBaseValue(QString value) const
  145. {
  146. return getValueImpl(value, false);
  147. }
  148. QVariant CModEntry::getValueImpl(QString value, bool localized) const
  149. {
  150. QString langValue = QString::fromStdString(settings["general"]["language"].String());
  151. // Priorities
  152. // 1) data from newest version
  153. // 2) data from preferred language
  154. bool useRepositoryData = repository.contains(value);
  155. if(repository.contains(value) && localData.contains(value))
  156. {
  157. // value is present in both repo and locally installed. Select one from latest version
  158. auto installedVer = localData["installedVersion"].toString().toStdString();
  159. auto availableVer = repository["latestVersion"].toString().toStdString();
  160. useRepositoryData = CModVersion::fromString(installedVer) < CModVersion::fromString(availableVer);
  161. }
  162. auto & storage = useRepositoryData ? repository : localData;
  163. if(localized && storage.contains(langValue))
  164. {
  165. auto langStorage = storage[langValue].toMap();
  166. if (langStorage.contains(value))
  167. return langStorage[value];
  168. }
  169. if(storage.contains(value))
  170. return storage[value];
  171. return QVariant();
  172. }
  173. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to) const
  174. {
  175. QVariantMap renamed;
  176. for(auto it = data.begin(); it != data.end(); it++)
  177. {
  178. QVariantMap modConf = it.value().toMap();
  179. modConf.insert(to, modConf.value(from));
  180. renamed.insert(it.key(), modConf);
  181. }
  182. return renamed;
  183. }
  184. void CModList::reloadRepositories()
  185. {
  186. cachedMods.clear();
  187. }
  188. void CModList::resetRepositories()
  189. {
  190. repositories.clear();
  191. cachedMods.clear();
  192. }
  193. void CModList::addRepository(QVariantMap data)
  194. {
  195. for(auto & key : data.keys())
  196. data[key.toLower()] = data.take(key);
  197. repositories.push_back(copyField(data, "version", "latestVersion"));
  198. cachedMods.clear();
  199. }
  200. void CModList::setLocalModList(QVariantMap data)
  201. {
  202. localModList = copyField(data, "version", "installedVersion");
  203. cachedMods.clear();
  204. }
  205. void CModList::setModSettings(QVariant data)
  206. {
  207. modSettings = data.toMap();
  208. cachedMods.clear();
  209. }
  210. void CModList::modChanged(QString modID)
  211. {
  212. cachedMods.clear();
  213. }
  214. static QVariant getValue(QVariant input, QString path)
  215. {
  216. if(path.size() > 1)
  217. {
  218. QString entryName = path.section('/', 0, 1);
  219. QString remainder = "/" + path.section('/', 2, -1);
  220. entryName.remove(0, 1);
  221. return getValue(input.toMap().value(entryName), remainder);
  222. }
  223. else
  224. {
  225. return input;
  226. }
  227. }
  228. const CModEntry & CModList::getMod(QString modName) const
  229. {
  230. modName = modName.toLower();
  231. auto it = cachedMods.find(modName);
  232. if (it != cachedMods.end())
  233. return it.value();
  234. auto itNew = cachedMods.insert(modName, getModUncached(modName));
  235. return *itNew;
  236. }
  237. CModEntry CModList::getModUncached(QString modname) const
  238. {
  239. QVariantMap repo;
  240. QVariantMap local = localModList[modname].toMap();
  241. QVariantMap settings;
  242. QString path = modname;
  243. path = "/" + path.replace(".", "/mods/");
  244. QVariant conf = getValue(modSettings, path);
  245. if(conf.isNull())
  246. {
  247. settings["active"] = !local.value("keepDisabled").toBool();
  248. }
  249. else
  250. {
  251. if(!conf.toMap().isEmpty())
  252. {
  253. settings = conf.toMap();
  254. if(settings.value("active").isNull())
  255. settings["active"] = !local.value("keepDisabled").toBool();
  256. }
  257. else
  258. settings.insert("active", conf);
  259. }
  260. if(settings["active"].toBool())
  261. {
  262. QString rootPath = path.section('/', 0, 1);
  263. if(path != rootPath)
  264. {
  265. conf = getValue(modSettings, rootPath);
  266. const auto confMap = conf.toMap();
  267. if(!conf.isNull() && !confMap["active"].isNull() && !confMap["active"].toBool())
  268. {
  269. settings = confMap;
  270. }
  271. }
  272. }
  273. if(settings.value("active").toBool())
  274. {
  275. if(!::isCompatible(local.value("compatibility").toMap()))
  276. settings["active"] = false;
  277. }
  278. for(auto entry : repositories)
  279. {
  280. QVariant repoVal = getValue(entry, path);
  281. if(repoVal.isValid())
  282. {
  283. auto repoValMap = repoVal.toMap();
  284. if(::isCompatible(repoValMap["compatibility"].toMap()))
  285. {
  286. if(repo.empty()
  287. || CModVersion::fromString(repo["version"].toString().toStdString())
  288. < CModVersion::fromString(repoValMap["version"].toString().toStdString()))
  289. {
  290. //take valid download link, screenshots and mod size before assignment
  291. auto download = repo.value("download");
  292. auto screenshots = repo.value("screenshots");
  293. auto size = repo.value("downloadSize");
  294. repo = repoValMap;
  295. if(repo.value("download").isNull())
  296. {
  297. repo["download"] = download;
  298. if(repo.value("screenshots").isNull()) //taking screenshot from the downloadable version
  299. repo["screenshots"] = screenshots;
  300. }
  301. if(repo.value("downloadSize").isNull())
  302. repo["downloadSize"] = size;
  303. }
  304. }
  305. }
  306. }
  307. return CModEntry(repo, local, settings, modname);
  308. }
  309. bool CModList::hasMod(QString modname) const
  310. {
  311. if(localModList.contains(modname))
  312. return true;
  313. for(auto entry : repositories)
  314. if(entry.contains(modname))
  315. return true;
  316. return false;
  317. }
  318. QStringList CModList::getRequirements(QString modname)
  319. {
  320. QStringList ret;
  321. if(hasMod(modname))
  322. {
  323. auto mod = getMod(modname);
  324. for(auto entry : mod.getDependencies())
  325. ret += getRequirements(entry.toLower());
  326. }
  327. ret += modname;
  328. return ret;
  329. }
  330. QVector<QString> CModList::getModList() const
  331. {
  332. QSet<QString> knownMods;
  333. QVector<QString> modList;
  334. for(auto repo : repositories)
  335. {
  336. for(auto it = repo.begin(); it != repo.end(); it++)
  337. {
  338. knownMods.insert(it.key().toLower());
  339. }
  340. }
  341. for(auto it = localModList.begin(); it != localModList.end(); it++)
  342. {
  343. knownMods.insert(it.key().toLower());
  344. }
  345. for(auto entry : knownMods)
  346. {
  347. modList.push_back(entry);
  348. }
  349. return modList;
  350. }
  351. QVector<QString> CModList::getChildren(QString parent) const
  352. {
  353. QVector<QString> children;
  354. int depth = parent.count('.') + 1;
  355. for(const QString & mod : getModList())
  356. {
  357. if(mod.count('.') == depth && mod.startsWith(parent))
  358. children.push_back(mod);
  359. }
  360. return children;
  361. }