cmodlist.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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))
  90. return repository[value];
  91. if (localData.contains(value))
  92. return localData[value];
  93. return QVariant();
  94. }
  95. QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
  96. {
  97. QVariantMap renamed;
  98. for (auto it = data.begin(); it != data.end(); it++)
  99. {
  100. QVariantMap modConf = it.value().toMap();
  101. modConf.insert(to, modConf.value(from));
  102. renamed.insert(it.key(), modConf);
  103. }
  104. return renamed;
  105. }
  106. void CModList::resetRepositories()
  107. {
  108. repositories.clear();
  109. }
  110. void CModList::addRepository(QVariantMap data)
  111. {
  112. repositories.push_back(copyField(data, "version", "latestVersion"));
  113. }
  114. void CModList::setLocalModList(QVariantMap data)
  115. {
  116. localModList = copyField(data, "version", "installedVersion");
  117. }
  118. void CModList::setModSettings(QVariant data)
  119. {
  120. modSettings = data.toMap();
  121. }
  122. CModEntry CModList::getMod(QString modname) const
  123. {
  124. assert(hasMod(modname));
  125. QVariantMap repo;
  126. QVariantMap local = localModList[modname].toMap();
  127. QVariantMap settings;
  128. QVariant conf = modSettings[modname];
  129. if (conf.canConvert<QVariantMap>())
  130. settings = modSettings[modname].toMap();
  131. else
  132. settings.insert("active", conf);
  133. for (auto entry : repositories)
  134. {
  135. if (entry.contains(modname))
  136. {
  137. if (repo.empty())
  138. repo = entry[modname].toMap();
  139. else
  140. {
  141. if (CModEntry::compareVersions(repo["version"].toString(),
  142. entry[modname].toMap()["version"].toString()))
  143. repo = entry[modname].toMap();
  144. }
  145. }
  146. }
  147. return CModEntry(repo, local, settings, modname);
  148. }
  149. bool CModList::hasMod(QString modname) const
  150. {
  151. if (localModList.contains(modname))
  152. return true;
  153. for (auto entry : repositories)
  154. if (entry.contains(modname))
  155. return true;
  156. return false;
  157. }
  158. QStringList CModList::getRequirements(QString modname)
  159. {
  160. QStringList ret;
  161. if (hasMod(modname))
  162. {
  163. auto mod = getMod(modname);
  164. for (auto entry : mod.getValue("depends").toStringList())
  165. ret += getRequirements(entry);
  166. }
  167. ret += modname;
  168. return ret;
  169. }
  170. QVector<QString> CModList::getModList() const
  171. {
  172. QSet<QString> knownMods;
  173. QVector<QString> modList;
  174. for (auto repo : repositories)
  175. {
  176. for (auto it = repo.begin(); it != repo.end(); it++)
  177. {
  178. knownMods.insert(it.key());
  179. }
  180. }
  181. for (auto it = localModList.begin(); it != localModList.end(); it++)
  182. {
  183. knownMods.insert(it.key());
  184. }
  185. for (auto entry : knownMods)
  186. {
  187. modList.push_back(entry);
  188. }
  189. return modList;
  190. }