cmodlist.cpp 8.5 KB

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