cmodlist.cpp 9.1 KB

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