cmodlist.cpp 5.9 KB

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