cmodlist.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.isNull())
  130. {
  131. settings["active"] = true; // default
  132. }
  133. else
  134. {
  135. if (conf.canConvert<QVariantMap>())
  136. settings = modSettings[modname].toMap();
  137. else
  138. settings.insert("active", conf);
  139. }
  140. for (auto entry : repositories)
  141. {
  142. if (entry.contains(modname))
  143. {
  144. if (repo.empty())
  145. repo = entry[modname].toMap();
  146. else
  147. {
  148. if (CModEntry::compareVersions(repo["version"].toString(),
  149. entry[modname].toMap()["version"].toString()))
  150. repo = entry[modname].toMap();
  151. }
  152. }
  153. }
  154. return CModEntry(repo, local, settings, modname);
  155. }
  156. bool CModList::hasMod(QString modname) const
  157. {
  158. if (localModList.contains(modname))
  159. return true;
  160. for (auto entry : repositories)
  161. if (entry.contains(modname))
  162. return true;
  163. return false;
  164. }
  165. QStringList CModList::getRequirements(QString modname)
  166. {
  167. QStringList ret;
  168. if (hasMod(modname))
  169. {
  170. auto mod = getMod(modname);
  171. for (auto entry : mod.getValue("depends").toStringList())
  172. ret += getRequirements(entry);
  173. }
  174. ret += modname;
  175. return ret;
  176. }
  177. QVector<QString> CModList::getModList() const
  178. {
  179. QSet<QString> knownMods;
  180. QVector<QString> modList;
  181. for (auto repo : repositories)
  182. {
  183. for (auto it = repo.begin(); it != repo.end(); it++)
  184. {
  185. knownMods.insert(it.key());
  186. }
  187. }
  188. for (auto it = localModList.begin(); it != localModList.end(); it++)
  189. {
  190. knownMods.insert(it.key());
  191. }
  192. for (auto entry : knownMods)
  193. {
  194. modList.push_back(entry);
  195. }
  196. return modList;
  197. }