cmodlist.cpp 9.1 KB

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