cmodlist.cpp 5.9 KB

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