cmodlist.cpp 9.0 KB

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