cmodlist.cpp 8.4 KB

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