cmodlist.cpp 5.4 KB

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