cmodlist.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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), localData(localData), modSettings(modSettings), modname(modname)
  48. {
  49. }
  50. bool CModEntry::isEnabled() const
  51. {
  52. if(!isInstalled())
  53. return false;
  54. return modSettings["active"].toBool();
  55. }
  56. bool CModEntry::isDisabled() const
  57. {
  58. if(!isInstalled())
  59. return false;
  60. return !isEnabled();
  61. }
  62. bool CModEntry::isAvailable() const
  63. {
  64. if(isInstalled())
  65. return false;
  66. return !repository.isEmpty();
  67. }
  68. bool CModEntry::isUpdateable() const
  69. {
  70. if(!isInstalled())
  71. return false;
  72. QString installedVer = localData["installedVersion"].toString();
  73. QString availableVer = repository["latestVersion"].toString();
  74. if(compareVersions(installedVer, availableVer))
  75. return true;
  76. return false;
  77. }
  78. bool CModEntry::isEssential() const
  79. {
  80. return getValue("storedLocaly").toBool();
  81. }
  82. bool CModEntry::isInstalled() const
  83. {
  84. return !localData.isEmpty();
  85. }
  86. int CModEntry::getModStatus() const
  87. {
  88. int status = 0;
  89. if(isEnabled())
  90. status |= ModStatus::ENABLED;
  91. if(isInstalled())
  92. status |= ModStatus::INSTALLED;
  93. if(isUpdateable())
  94. status |= ModStatus::UPDATEABLE;
  95. return status;
  96. }
  97. QString CModEntry::getName() const
  98. {
  99. return modname;
  100. }
  101. QVariant CModEntry::getValue(QString value) const
  102. {
  103. if(repository.contains(value) && localData.contains(value))
  104. {
  105. // value is present in both repo and locally installed. Select one from latest version
  106. QString installedVer = localData["installedVersion"].toString();
  107. QString availableVer = repository["latestVersion"].toString();
  108. if(compareVersions(installedVer, availableVer))
  109. return repository[value];
  110. else
  111. return localData[value];
  112. }
  113. if(repository.contains(value))
  114. return repository[value];
  115. if(localData.contains(value))
  116. return localData[value];
  117. return QVariant();
  118. }
  119. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
  120. {
  121. QVariantMap renamed;
  122. for(auto it = data.begin(); it != data.end(); it++)
  123. {
  124. QVariantMap modConf = it.value().toMap();
  125. modConf.insert(to, modConf.value(from));
  126. renamed.insert(it.key(), modConf);
  127. }
  128. return renamed;
  129. }
  130. void CModList::reloadRepositories()
  131. {
  132. }
  133. void CModList::resetRepositories()
  134. {
  135. repositories.clear();
  136. }
  137. void CModList::addRepository(QVariantMap data)
  138. {
  139. repositories.push_back(copyField(data, "version", "latestVersion"));
  140. }
  141. void CModList::setLocalModList(QVariantMap data)
  142. {
  143. localModList = copyField(data, "version", "installedVersion");
  144. }
  145. void CModList::setModSettings(QVariant data)
  146. {
  147. modSettings = data.toMap();
  148. }
  149. void CModList::modChanged(QString modID)
  150. {
  151. }
  152. static QVariant getValue(QVariant input, QString path)
  153. {
  154. if(path.size() > 1)
  155. {
  156. QString entryName = path.section('/', 0, 1);
  157. QString remainder = "/" + path.section('/', 2, -1);
  158. entryName.remove(0, 1);
  159. return getValue(input.toMap().value(entryName), remainder);
  160. }
  161. else
  162. {
  163. return input;
  164. }
  165. }
  166. CModEntry CModList::getMod(QString modname) const
  167. {
  168. QVariantMap repo;
  169. QVariantMap local = localModList[modname].toMap();
  170. QVariantMap settings;
  171. QString path = modname;
  172. path = "/" + path.replace(".", "/mods/");
  173. QVariant conf = getValue(modSettings, path);
  174. if(conf.isNull())
  175. {
  176. settings["active"] = true; // default
  177. }
  178. else
  179. {
  180. if(!conf.toMap().isEmpty())
  181. {
  182. settings = conf.toMap();
  183. if(settings.value("active").isNull())
  184. settings["active"] = true; // default
  185. }
  186. else
  187. settings.insert("active", conf);
  188. }
  189. if(settings["active"].toBool())
  190. {
  191. QString rootPath = path.section('/', 0, 1);
  192. if(path != rootPath)
  193. {
  194. conf = getValue(modSettings, rootPath);
  195. const auto confMap = conf.toMap();
  196. if(!conf.isNull() && !confMap["active"].isNull() && !confMap["active"].toBool())
  197. {
  198. settings = confMap;
  199. }
  200. }
  201. }
  202. for(auto entry : repositories)
  203. {
  204. QVariant repoVal = getValue(entry, path);
  205. if(repoVal.isValid())
  206. {
  207. if(repo.empty())
  208. {
  209. repo = repoVal.toMap();
  210. }
  211. else
  212. {
  213. if(CModEntry::compareVersions(repo["version"].toString(), repoVal.toMap()["version"].toString()))
  214. repo = repoVal.toMap();
  215. }
  216. }
  217. }
  218. return CModEntry(repo, local, settings, modname);
  219. }
  220. bool CModList::hasMod(QString modname) const
  221. {
  222. if(localModList.contains(modname))
  223. return true;
  224. for(auto entry : repositories)
  225. if(entry.contains(modname))
  226. return true;
  227. return false;
  228. }
  229. QStringList CModList::getRequirements(QString modname)
  230. {
  231. QStringList ret;
  232. if(hasMod(modname))
  233. {
  234. auto mod = getMod(modname);
  235. for(auto entry : mod.getValue("depends").toStringList())
  236. ret += getRequirements(entry);
  237. }
  238. ret += modname;
  239. return ret;
  240. }
  241. QVector<QString> CModList::getModList() const
  242. {
  243. QSet<QString> knownMods;
  244. QVector<QString> modList;
  245. for(auto repo : repositories)
  246. {
  247. for(auto it = repo.begin(); it != repo.end(); it++)
  248. {
  249. knownMods.insert(it.key());
  250. }
  251. }
  252. for(auto it = localModList.begin(); it != localModList.end(); it++)
  253. {
  254. knownMods.insert(it.key());
  255. }
  256. for(auto entry : knownMods)
  257. {
  258. modList.push_back(entry);
  259. }
  260. return modList;
  261. }
  262. QVector<QString> CModList::getChildren(QString parent) const
  263. {
  264. QVector<QString> children;
  265. int depth = parent.count('.') + 1;
  266. for(const QString & mod : getModList())
  267. {
  268. if(mod.count('.') == depth && mod.startsWith(parent))
  269. children.push_back(mod);
  270. }
  271. return children;
  272. }