cmodlist.cpp 9.1 KB

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