cmodlist.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. bool CModEntry::compareVersions(QString lesser, QString greater)
  15. {
  16. static const int maxSections = 3; // versions consist from up to 3 sections, major.minor.patch
  17. QStringList lesserList = lesser.split(".");
  18. QStringList greaterList = greater.split(".");
  19. assert(lesserList.size() <= maxSections);
  20. assert(greaterList.size() <= maxSections);
  21. for (int i=0; i< maxSections; i++)
  22. {
  23. if (greaterList.size() <= i) // 1.1.1 > 1.1
  24. return false;
  25. if (lesserList.size() <= i) // 1.1 < 1.1.1
  26. return true;
  27. if (lesserList[i].toInt() != greaterList[i].toInt())
  28. return lesserList[i].toInt() < greaterList[i].toInt(); // 1.1 < 1.2
  29. }
  30. return false;
  31. }
  32. QString CModEntry::sizeToString(double size)
  33. {
  34. static const QString sizes[] =
  35. {
  36. /*"%1 B", */"%1 KiB", "%1 MiB", "%1 GiB", "%1 TiB"
  37. };
  38. size_t index = 0;
  39. while (size > 1024 && index < 4)
  40. {
  41. size /= 1024;
  42. index++;
  43. }
  44. return sizes[index].arg(QString::number(size, 'f', 1));
  45. }
  46. CModEntry::CModEntry(QVariantMap repository, QVariantMap localData, QVariantMap modSettings, QString modname):
  47. repository(repository),
  48. localData(localData),
  49. modSettings(modSettings),
  50. modname(modname)
  51. {
  52. }
  53. bool CModEntry::isEnabled() const
  54. {
  55. if (!isInstalled())
  56. return false;
  57. return modSettings["active"].toBool();
  58. }
  59. bool CModEntry::isDisabled() const
  60. {
  61. if (!isInstalled())
  62. return false;
  63. return !isEnabled();
  64. }
  65. bool CModEntry::isAvailable() const
  66. {
  67. if (isInstalled())
  68. return false;
  69. return !repository.isEmpty();
  70. }
  71. bool CModEntry::isUpdateable() const
  72. {
  73. if (!isInstalled())
  74. return false;
  75. QString installedVer = localData["installedVersion"].toString();
  76. QString availableVer = repository["latestVersion"].toString();
  77. if (compareVersions(installedVer, availableVer))
  78. return true;
  79. return false;
  80. }
  81. bool CModEntry::isInstalled() const
  82. {
  83. return !localData.isEmpty();
  84. }
  85. int CModEntry::getModStatus() const
  86. {
  87. return
  88. (isEnabled() ? ModStatus::ENABLED : 0) |
  89. (isInstalled() ? ModStatus::INSTALLED : 0) |
  90. (isUpdateable()? ModStatus::UPDATEABLE : 0);
  91. }
  92. QString CModEntry::getName() const
  93. {
  94. return modname;
  95. }
  96. QVariant CModEntry::getValue(QString value) const
  97. {
  98. if (repository.contains(value) && localData.contains(value))
  99. {
  100. // value is present in both repo and locally installed. Select one from latest version
  101. QString installedVer = localData["installedVersion"].toString();
  102. QString availableVer = repository["latestVersion"].toString();
  103. if (compareVersions(installedVer, availableVer))
  104. return repository[value];
  105. else
  106. return localData[value];
  107. }
  108. if (repository.contains(value))
  109. return repository[value];
  110. if (localData.contains(value))
  111. return localData[value];
  112. return QVariant();
  113. }
  114. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
  115. {
  116. QVariantMap renamed;
  117. for (auto it = data.begin(); it != data.end(); it++)
  118. {
  119. QVariantMap modConf = it.value().toMap();
  120. modConf.insert(to, modConf.value(from));
  121. renamed.insert(it.key(), modConf);
  122. }
  123. return renamed;
  124. }
  125. void CModList::resetRepositories()
  126. {
  127. repositories.clear();
  128. }
  129. void CModList::addRepository(QVariantMap data)
  130. {
  131. repositories.push_back(copyField(data, "version", "latestVersion"));
  132. }
  133. void CModList::setLocalModList(QVariantMap data)
  134. {
  135. localModList = copyField(data, "version", "installedVersion");
  136. }
  137. void CModList::setModSettings(QVariant data)
  138. {
  139. modSettings = data.toMap();
  140. }
  141. void CModList::modChanged(QString modID)
  142. {
  143. }
  144. static QVariant getValue(QVariantMap input, QString path)
  145. {
  146. if (path.size() > 1)
  147. {
  148. QString entryName = path.section('/', 0, 1);
  149. QString remainder = "/" + path.section('/', 2, -1);
  150. entryName.remove(0, 1);
  151. return getValue(input.value(entryName).toMap(), remainder);
  152. }
  153. else
  154. {
  155. return input;
  156. }
  157. }
  158. CModEntry CModList::getMod(QString modname) const
  159. {
  160. QVariantMap repo;
  161. QVariantMap local = localModList[modname].toMap();
  162. QVariantMap settings;
  163. QString path = modname;
  164. path = "/" + path.replace(".", "/mods/");
  165. QVariant conf = getValue(modSettings, path);
  166. if (conf.isNull())
  167. {
  168. settings["active"] = true; // default
  169. }
  170. else
  171. {
  172. if (conf.canConvert<QVariantMap>())
  173. settings = conf.toMap();
  174. else
  175. settings.insert("active", conf);
  176. }
  177. for (auto entry : repositories)
  178. {
  179. QVariant repoVal = getValue(entry, path);
  180. if (repoVal.isValid())
  181. {
  182. if (repo.empty())
  183. repo = repoVal.toMap();
  184. else
  185. {
  186. if (CModEntry::compareVersions(repo["version"].toString(), repoVal.toMap()["version"].toString()))
  187. repo = repoVal.toMap();
  188. }
  189. }
  190. }
  191. return CModEntry(repo, local, settings, modname);
  192. }
  193. bool CModList::hasMod(QString modname) const
  194. {
  195. if (localModList.contains(modname))
  196. return true;
  197. for (auto entry : repositories)
  198. if (entry.contains(modname))
  199. return true;
  200. return false;
  201. }
  202. QStringList CModList::getRequirements(QString modname)
  203. {
  204. QStringList ret;
  205. if (hasMod(modname))
  206. {
  207. auto mod = getMod(modname);
  208. for (auto entry : mod.getValue("depends").toStringList())
  209. ret += getRequirements(entry);
  210. }
  211. ret += modname;
  212. return ret;
  213. }
  214. QVector<QString> CModList::getModList() const
  215. {
  216. QSet<QString> knownMods;
  217. QVector<QString> modList;
  218. for (auto repo : repositories)
  219. {
  220. for (auto it = repo.begin(); it != repo.end(); it++)
  221. {
  222. knownMods.insert(it.key());
  223. }
  224. }
  225. for (auto it = localModList.begin(); it != localModList.end(); it++)
  226. {
  227. knownMods.insert(it.key());
  228. }
  229. for (auto entry : knownMods)
  230. {
  231. modList.push_back(entry);
  232. }
  233. return modList;
  234. }
  235. QVector<QString> CModList::getChildren(QString parent) const
  236. {
  237. QVector<QString> children;
  238. int depth = parent.count('.') + 1;
  239. for (const QString & mod : getModList())
  240. {
  241. if (mod.count('.') == depth && mod.startsWith(parent))
  242. children.push_back(mod);
  243. }
  244. return children;
  245. }