cmodlist.cpp 8.2 KB

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