cmodlist.cpp 8.1 KB

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