cmodlist.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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/CConfigHandler.h"
  13. #include "../../lib/JsonNode.h"
  14. #include "../../lib/filesystem/CFileInputStream.h"
  15. #include "../../lib/GameConstants.h"
  16. #include "../../lib/modding/CModVersion.h"
  17. QString CModEntry::sizeToString(double size)
  18. {
  19. static const QString sizes[] =
  20. {
  21. /*"%1 B", */ "%1 KiB", "%1 MiB", "%1 GiB", "%1 TiB"
  22. };
  23. size_t index = 0;
  24. while(size > 1024 && index < 4)
  25. {
  26. size /= 1024;
  27. index++;
  28. }
  29. return sizes[index].arg(QString::number(size, 'f', 1));
  30. }
  31. CModEntry::CModEntry(QVariantMap repository, QVariantMap localData, QVariantMap modSettings, QString modname)
  32. : repository(repository), localData(localData), modSettings(modSettings), modname(modname)
  33. {
  34. }
  35. bool CModEntry::isEnabled() const
  36. {
  37. if(!isInstalled())
  38. return false;
  39. return modSettings["active"].toBool();
  40. }
  41. bool CModEntry::isDisabled() const
  42. {
  43. if(!isInstalled())
  44. return false;
  45. return !isEnabled();
  46. }
  47. bool CModEntry::isAvailable() const
  48. {
  49. if(isInstalled())
  50. return false;
  51. return !repository.isEmpty();
  52. }
  53. bool CModEntry::isUpdateable() const
  54. {
  55. if(!isInstalled())
  56. return false;
  57. auto installedVer = localData["installedVersion"].toString().toStdString();
  58. auto availableVer = repository["latestVersion"].toString().toStdString();
  59. return (CModVersion::fromString(installedVer) < CModVersion::fromString(availableVer));
  60. }
  61. bool isCompatible(const QVariantMap & compatibility)
  62. {
  63. auto compatibleMin = CModVersion::fromString(compatibility["min"].toString().toStdString());
  64. auto compatibleMax = CModVersion::fromString(compatibility["max"].toString().toStdString());
  65. return (compatibleMin.isNull() || CModVersion::GameVersion().compatible(compatibleMin, true, true))
  66. && (compatibleMax.isNull() || compatibleMax.compatible(CModVersion::GameVersion(), true, true));
  67. }
  68. bool CModEntry::isCompatible() const
  69. {
  70. return ::isCompatible(localData["compatibility"].toMap());
  71. }
  72. bool CModEntry::isEssential() const
  73. {
  74. return getName() == "vcmi";
  75. }
  76. bool CModEntry::isInstalled() const
  77. {
  78. return !localData.isEmpty();
  79. }
  80. bool CModEntry::isValid() const
  81. {
  82. return !localData.isEmpty() || !repository.isEmpty();
  83. }
  84. bool CModEntry::isTranslation() const
  85. {
  86. return getBaseValue("modType").toString().toLower() == "translation";
  87. }
  88. int CModEntry::getModStatus() const
  89. {
  90. int status = 0;
  91. if(isEnabled())
  92. status |= ModStatus::ENABLED;
  93. if(isInstalled())
  94. status |= ModStatus::INSTALLED;
  95. if(isUpdateable())
  96. status |= ModStatus::UPDATEABLE;
  97. return status;
  98. }
  99. QString CModEntry::getName() const
  100. {
  101. return modname;
  102. }
  103. QVariant CModEntry::getValue(QString value) const
  104. {
  105. return getValueImpl(value, true);
  106. }
  107. QVariant CModEntry::getBaseValue(QString value) const
  108. {
  109. return getValueImpl(value, false);
  110. }
  111. QVariant CModEntry::getValueImpl(QString value, bool localized) const
  112. {
  113. QString langValue = QString::fromStdString(settings["general"]["language"].String());
  114. // Priorities
  115. // 1) data from newest version
  116. // 2) data from preferred language
  117. bool useRepositoryData = repository.contains(value);
  118. if(repository.contains(value) && localData.contains(value))
  119. {
  120. // value is present in both repo and locally installed. Select one from latest version
  121. auto installedVer = localData["installedVersion"].toString().toStdString();
  122. auto availableVer = repository["latestVersion"].toString().toStdString();
  123. useRepositoryData = CModVersion::fromString(installedVer) < CModVersion::fromString(availableVer);
  124. }
  125. auto & storage = useRepositoryData ? repository : localData;
  126. if(localized && storage.contains(langValue))
  127. {
  128. auto langStorage = storage[langValue].toMap();
  129. if (langStorage.contains(value))
  130. return langStorage[value];
  131. }
  132. if(storage.contains(value))
  133. return storage[value];
  134. return QVariant();
  135. }
  136. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
  137. {
  138. QVariantMap renamed;
  139. for(auto it = data.begin(); it != data.end(); it++)
  140. {
  141. QVariantMap modConf = it.value().toMap();
  142. modConf.insert(to, modConf.value(from));
  143. renamed.insert(it.key(), modConf);
  144. }
  145. return renamed;
  146. }
  147. void CModList::reloadRepositories()
  148. {
  149. }
  150. void CModList::resetRepositories()
  151. {
  152. repositories.clear();
  153. }
  154. void CModList::addRepository(QVariantMap data)
  155. {
  156. for(auto & key : data.keys())
  157. data[key.toLower()] = data.take(key);
  158. repositories.push_back(copyField(data, "version", "latestVersion"));
  159. }
  160. void CModList::setLocalModList(QVariantMap data)
  161. {
  162. localModList = copyField(data, "version", "installedVersion");
  163. }
  164. void CModList::setModSettings(QVariant data)
  165. {
  166. modSettings = data.toMap();
  167. }
  168. void CModList::modChanged(QString modID)
  169. {
  170. }
  171. static QVariant getValue(QVariant input, QString path)
  172. {
  173. if(path.size() > 1)
  174. {
  175. QString entryName = path.section('/', 0, 1);
  176. QString remainder = "/" + path.section('/', 2, -1);
  177. entryName.remove(0, 1);
  178. return getValue(input.toMap().value(entryName), remainder);
  179. }
  180. else
  181. {
  182. return input;
  183. }
  184. }
  185. CModEntry CModList::getMod(QString modname) const
  186. {
  187. modname = modname.toLower();
  188. QVariantMap repo;
  189. QVariantMap local = localModList[modname].toMap();
  190. QVariantMap settings;
  191. QString path = modname;
  192. path = "/" + path.replace(".", "/mods/");
  193. QVariant conf = getValue(modSettings, path);
  194. if(conf.isNull())
  195. {
  196. settings["active"] = !local.value("keepDisabled").toBool();
  197. }
  198. else
  199. {
  200. if(!conf.toMap().isEmpty())
  201. {
  202. settings = conf.toMap();
  203. if(settings.value("active").isNull())
  204. settings["active"] = !local.value("keepDisabled").toBool();
  205. }
  206. else
  207. settings.insert("active", conf);
  208. }
  209. if(settings["active"].toBool())
  210. {
  211. QString rootPath = path.section('/', 0, 1);
  212. if(path != rootPath)
  213. {
  214. conf = getValue(modSettings, rootPath);
  215. const auto confMap = conf.toMap();
  216. if(!conf.isNull() && !confMap["active"].isNull() && !confMap["active"].toBool())
  217. {
  218. settings = confMap;
  219. }
  220. }
  221. }
  222. if(settings.value("active").toBool())
  223. {
  224. if(!::isCompatible(local.value("compatibility").toMap()))
  225. settings["active"] = false;
  226. }
  227. for(auto entry : repositories)
  228. {
  229. QVariant repoVal = getValue(entry, path);
  230. if(repoVal.isValid())
  231. {
  232. auto repoValMap = repoVal.toMap();
  233. if(::isCompatible(repoValMap["compatibility"].toMap()))
  234. {
  235. if(repo.empty()
  236. || CModVersion::fromString(repo["version"].toString().toStdString())
  237. < CModVersion::fromString(repoValMap["version"].toString().toStdString()))
  238. {
  239. //take valid download link and screenshots before assignment
  240. auto download = repo.value("download");
  241. auto screenshots = repo.value("screenshots");
  242. repo = repoValMap;
  243. if(repo.value("download").isNull())
  244. {
  245. repo["download"] = download;
  246. if(repo.value("screenshots").isNull()) //taking screenshot from the downloadable version
  247. repo["screenshots"] = screenshots;
  248. }
  249. }
  250. }
  251. }
  252. }
  253. return CModEntry(repo, local, settings, modname);
  254. }
  255. bool CModList::hasMod(QString modname) const
  256. {
  257. if(localModList.contains(modname))
  258. return true;
  259. for(auto entry : repositories)
  260. if(entry.contains(modname))
  261. return true;
  262. return false;
  263. }
  264. QStringList CModList::getRequirements(QString modname)
  265. {
  266. QStringList ret;
  267. if(hasMod(modname))
  268. {
  269. auto mod = getMod(modname);
  270. for(auto entry : mod.getValue("depends").toStringList())
  271. ret += getRequirements(entry);
  272. }
  273. ret += modname;
  274. return ret;
  275. }
  276. QVector<QString> CModList::getModList() const
  277. {
  278. QSet<QString> knownMods;
  279. QVector<QString> modList;
  280. for(auto repo : repositories)
  281. {
  282. for(auto it = repo.begin(); it != repo.end(); it++)
  283. {
  284. knownMods.insert(it.key().toLower());
  285. }
  286. }
  287. for(auto it = localModList.begin(); it != localModList.end(); it++)
  288. {
  289. knownMods.insert(it.key().toLower());
  290. }
  291. for(auto entry : knownMods)
  292. {
  293. modList.push_back(entry);
  294. }
  295. return modList;
  296. }
  297. QVector<QString> CModList::getChildren(QString parent) const
  298. {
  299. QVector<QString> children;
  300. int depth = parent.count('.') + 1;
  301. for(const QString & mod : getModList())
  302. {
  303. if(mod.count('.') == depth && mod.startsWith(parent))
  304. children.push_back(mod);
  305. }
  306. return children;
  307. }