cmodlist.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. const int maxSections = 3; // versions consist from up to 3 sections, major.minor.patch
  21. QVersionNumber vcmiVersion(VCMI_VERSION_MAJOR,
  22. VCMI_VERSION_MINOR,
  23. VCMI_VERSION_PATCH);
  24. auto versionMin = QVersionNumber::fromString(verMin);
  25. auto versionMax = QVersionNumber::fromString(verMax);
  26. auto buildVersion = [maxSections](QVersionNumber & ver)
  27. {
  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 getValue("storedLocaly").toBool();
  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. int CModEntry::getModStatus() const
  121. {
  122. int status = 0;
  123. if(isEnabled())
  124. status |= ModStatus::ENABLED;
  125. if(isInstalled())
  126. status |= ModStatus::INSTALLED;
  127. if(isUpdateable())
  128. status |= ModStatus::UPDATEABLE;
  129. return status;
  130. }
  131. QString CModEntry::getName() const
  132. {
  133. return modname;
  134. }
  135. QVariant CModEntry::getValue(QString value) const
  136. {
  137. QString langValue = QString::fromStdString(settings["general"]["language"].String());
  138. // Priorities
  139. // 1) data from newest version
  140. // 2) data from preferred language
  141. bool useRepositoryData = repository.contains(value);
  142. if(repository.contains(value) && localData.contains(value))
  143. {
  144. // value is present in both repo and locally installed. Select one from latest version
  145. QString installedVer = localData["installedVersion"].toString();
  146. QString availableVer = repository["latestVersion"].toString();
  147. useRepositoryData = compareVersions(installedVer, availableVer);
  148. }
  149. auto & storage = useRepositoryData ? repository : localData;
  150. if (storage.contains(langValue))
  151. {
  152. auto langStorage = storage[langValue].toMap();
  153. if (langStorage.contains(value))
  154. return langStorage[value];
  155. }
  156. if (storage.contains(value))
  157. return storage[value];
  158. return QVariant();
  159. }
  160. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
  161. {
  162. QVariantMap renamed;
  163. for(auto it = data.begin(); it != data.end(); it++)
  164. {
  165. QVariantMap modConf = it.value().toMap();
  166. modConf.insert(to, modConf.value(from));
  167. renamed.insert(it.key(), modConf);
  168. }
  169. return renamed;
  170. }
  171. void CModList::reloadRepositories()
  172. {
  173. }
  174. void CModList::resetRepositories()
  175. {
  176. repositories.clear();
  177. }
  178. void CModList::addRepository(QVariantMap data)
  179. {
  180. for(auto & key : data.keys())
  181. data[key.toLower()] = data.take(key);
  182. repositories.push_back(copyField(data, "version", "latestVersion"));
  183. }
  184. void CModList::setLocalModList(QVariantMap data)
  185. {
  186. localModList = copyField(data, "version", "installedVersion");
  187. }
  188. void CModList::setModSettings(QVariant data)
  189. {
  190. modSettings = data.toMap();
  191. }
  192. void CModList::modChanged(QString modID)
  193. {
  194. }
  195. static QVariant getValue(QVariant input, QString path)
  196. {
  197. if(path.size() > 1)
  198. {
  199. QString entryName = path.section('/', 0, 1);
  200. QString remainder = "/" + path.section('/', 2, -1);
  201. entryName.remove(0, 1);
  202. return getValue(input.toMap().value(entryName), remainder);
  203. }
  204. else
  205. {
  206. return input;
  207. }
  208. }
  209. CModEntry CModList::getMod(QString modname) const
  210. {
  211. modname = modname.toLower();
  212. QVariantMap repo;
  213. QVariantMap local = localModList[modname].toMap();
  214. QVariantMap settings;
  215. QString path = modname;
  216. path = "/" + path.replace(".", "/mods/");
  217. QVariant conf = getValue(modSettings, path);
  218. if(conf.isNull())
  219. {
  220. settings["active"] = true; // default
  221. }
  222. else
  223. {
  224. if(!conf.toMap().isEmpty())
  225. {
  226. settings = conf.toMap();
  227. if(settings.value("active").isNull())
  228. settings["active"] = true; // default
  229. }
  230. else
  231. settings.insert("active", conf);
  232. }
  233. if(settings["active"].toBool())
  234. {
  235. QString rootPath = path.section('/', 0, 1);
  236. if(path != rootPath)
  237. {
  238. conf = getValue(modSettings, rootPath);
  239. const auto confMap = conf.toMap();
  240. if(!conf.isNull() && !confMap["active"].isNull() && !confMap["active"].toBool())
  241. {
  242. settings = confMap;
  243. }
  244. }
  245. }
  246. if(settings.value("active").toBool())
  247. {
  248. auto compatibility = local.value("compatibility").toMap();
  249. if(compatibility["min"].isValid() || compatibility["max"].isValid())
  250. if(!isCompatible(compatibility["min"].toString(), compatibility["max"].toString()))
  251. settings["active"] = false;
  252. }
  253. for(auto entry : repositories)
  254. {
  255. QVariant repoVal = getValue(entry, path);
  256. if(repoVal.isValid())
  257. {
  258. auto repoValMap = repoVal.toMap();
  259. auto compatibility = repoValMap["compatibility"].toMap();
  260. if(isCompatible(compatibility["min"].toString(), compatibility["max"].toString()))
  261. {
  262. if(repo.empty() || CModEntry::compareVersions(repo["version"].toString(), repoValMap["version"].toString()))
  263. {
  264. //take valid download link and screenshots before assignment
  265. auto download = repo.value("download");
  266. auto screenshots = repo.value("screenshots");
  267. repo = repoValMap;
  268. if(repo.value("download").isNull())
  269. {
  270. repo["download"] = download;
  271. if(repo.value("screenshots").isNull()) //taking screenshot from the downloadable version
  272. repo["screenshots"] = screenshots;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. return CModEntry(repo, local, settings, modname);
  279. }
  280. bool CModList::hasMod(QString modname) const
  281. {
  282. if(localModList.contains(modname))
  283. return true;
  284. for(auto entry : repositories)
  285. if(entry.contains(modname))
  286. return true;
  287. return false;
  288. }
  289. QStringList CModList::getRequirements(QString modname)
  290. {
  291. QStringList ret;
  292. if(hasMod(modname))
  293. {
  294. auto mod = getMod(modname);
  295. for(auto entry : mod.getValue("depends").toStringList())
  296. ret += getRequirements(entry);
  297. }
  298. ret += modname;
  299. return ret;
  300. }
  301. QVector<QString> CModList::getModList() const
  302. {
  303. QSet<QString> knownMods;
  304. QVector<QString> modList;
  305. for(auto repo : repositories)
  306. {
  307. for(auto it = repo.begin(); it != repo.end(); it++)
  308. {
  309. knownMods.insert(it.key().toLower());
  310. }
  311. }
  312. for(auto it = localModList.begin(); it != localModList.end(); it++)
  313. {
  314. knownMods.insert(it.key().toLower());
  315. }
  316. for(auto entry : knownMods)
  317. {
  318. modList.push_back(entry);
  319. }
  320. return modList;
  321. }
  322. QVector<QString> CModList::getChildren(QString parent) const
  323. {
  324. QVector<QString> children;
  325. int depth = parent.count('.') + 1;
  326. for(const QString & mod : getModList())
  327. {
  328. if(mod.count('.') == depth && mod.startsWith(parent))
  329. children.push_back(mod);
  330. }
  331. return children;
  332. }