cmodlist.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. const int maxSections = 3; // versions consist from up to 3 sections, major.minor.patch
  20. QVersionNumber vcmiVersion(GameConstants::VCMI_VERSION_MAJOR,
  21. GameConstants::VCMI_VERSION_MINOR,
  22. GameConstants::VCMI_VERSION_PATCH);
  23. auto versionMin = QVersionNumber::fromString(verMin);
  24. auto versionMax = QVersionNumber::fromString(verMax);
  25. auto buildVersion = [maxSections](QVersionNumber & ver)
  26. {
  27. if(ver.segmentCount() < maxSections)
  28. {
  29. auto segments = ver.segments();
  30. for(int i = segments.size() - 1; 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. if(!isInstalled())
  105. return false;
  106. auto compatibility = localData["compatibility"].toMap();
  107. return ::isCompatible(compatibility["min"].toString(), compatibility["max"].toString());
  108. }
  109. bool CModEntry::isEssential() const
  110. {
  111. return getValue("storedLocaly").toBool();
  112. }
  113. bool CModEntry::isInstalled() const
  114. {
  115. return !localData.isEmpty();
  116. }
  117. bool CModEntry::isValid() const
  118. {
  119. return !localData.isEmpty() || !repository.isEmpty();
  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. if(repository.contains(value) && localData.contains(value))
  139. {
  140. // value is present in both repo and locally installed. Select one from latest version
  141. QString installedVer = localData["installedVersion"].toString();
  142. QString availableVer = repository["latestVersion"].toString();
  143. if(compareVersions(installedVer, availableVer))
  144. return repository[value];
  145. else
  146. return localData[value];
  147. }
  148. if(repository.contains(value))
  149. return repository[value];
  150. if(localData.contains(value))
  151. return localData[value];
  152. return QVariant();
  153. }
  154. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
  155. {
  156. QVariantMap renamed;
  157. for(auto it = data.begin(); it != data.end(); it++)
  158. {
  159. QVariantMap modConf = it.value().toMap();
  160. modConf.insert(to, modConf.value(from));
  161. renamed.insert(it.key(), modConf);
  162. }
  163. return renamed;
  164. }
  165. void CModList::reloadRepositories()
  166. {
  167. }
  168. void CModList::resetRepositories()
  169. {
  170. repositories.clear();
  171. }
  172. void CModList::addRepository(QVariantMap data)
  173. {
  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. QMap<QString, QString> keyNormalize;
  195. for(auto & key : input.toMap().keys())
  196. keyNormalize[key.toLower()] = key;
  197. return getValue(input.toMap().value(keyNormalize[entryName]), remainder);
  198. }
  199. else
  200. {
  201. return input;
  202. }
  203. }
  204. CModEntry CModList::getMod(QString modname) const
  205. {
  206. modname = modname.toLower();
  207. QVariantMap repo;
  208. QVariantMap local = localModList[modname].toMap();
  209. QVariantMap settings;
  210. QString path = modname;
  211. path = "/" + path.replace(".", "/mods/");
  212. QVariant conf = getValue(modSettings, path);
  213. if(conf.isNull())
  214. {
  215. settings["active"] = true; // default
  216. }
  217. else
  218. {
  219. if(!conf.toMap().isEmpty())
  220. {
  221. settings = conf.toMap();
  222. if(settings.value("active").isNull())
  223. settings["active"] = true; // default
  224. }
  225. else
  226. settings.insert("active", conf);
  227. }
  228. if(settings["active"].toBool())
  229. {
  230. QString rootPath = path.section('/', 0, 1);
  231. if(path != rootPath)
  232. {
  233. conf = getValue(modSettings, rootPath);
  234. const auto confMap = conf.toMap();
  235. if(!conf.isNull() && !confMap["active"].isNull() && !confMap["active"].toBool())
  236. {
  237. settings = confMap;
  238. }
  239. }
  240. }
  241. for(auto entry : repositories)
  242. {
  243. QVariant repoVal = getValue(entry, path);
  244. if(repoVal.isValid())
  245. {
  246. auto repoValMap = repoVal.toMap();
  247. auto compatibility = repoValMap["compatibility"].toMap();
  248. if(isCompatible(compatibility["min"].toString(), compatibility["max"].toString()))
  249. {
  250. if(repo.empty() || CModEntry::compareVersions(repo["version"].toString(), repoValMap["version"].toString()))
  251. {
  252. repo = repoValMap;
  253. }
  254. }
  255. }
  256. }
  257. return CModEntry(repo, local, settings, modname);
  258. }
  259. bool CModList::hasMod(QString modname) const
  260. {
  261. if(localModList.contains(modname))
  262. return true;
  263. for(auto entry : repositories)
  264. if(entry.contains(modname))
  265. return true;
  266. return false;
  267. }
  268. QStringList CModList::getRequirements(QString modname)
  269. {
  270. QStringList ret;
  271. if(hasMod(modname))
  272. {
  273. auto mod = getMod(modname);
  274. for(auto entry : mod.getValue("depends").toStringList())
  275. ret += getRequirements(entry);
  276. }
  277. ret += modname;
  278. return ret;
  279. }
  280. QVector<QString> CModList::getModList() const
  281. {
  282. QSet<QString> knownMods;
  283. QVector<QString> modList;
  284. for(auto repo : repositories)
  285. {
  286. for(auto it = repo.begin(); it != repo.end(); it++)
  287. {
  288. knownMods.insert(it.key().toLower());
  289. }
  290. }
  291. for(auto it = localModList.begin(); it != localModList.end(); it++)
  292. {
  293. knownMods.insert(it.key().toLower());
  294. }
  295. for(auto entry : knownMods)
  296. {
  297. modList.push_back(entry);
  298. }
  299. return modList;
  300. }
  301. QVector<QString> CModList::getChildren(QString parent) const
  302. {
  303. QVector<QString> children;
  304. int depth = parent.count('.') + 1;
  305. for(const QString & mod : getModList())
  306. {
  307. if(mod.count('.') == depth && mod.startsWith(parent))
  308. children.push_back(mod);
  309. }
  310. return children;
  311. }