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