2013-08-22 17:22:49 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "cmodlist.h"
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
#include "../../lib/JsonNode.h"
|
|
|
|
#include "../../lib/filesystem/CFileInputStream.h"
|
|
|
|
|
2013-08-22 17:22:49 +03:00
|
|
|
bool CModEntry::compareVersions(QString lesser, QString greater)
|
|
|
|
{
|
|
|
|
static const int maxSections = 3; // versions consist from up to 3 sections, major.minor.patch
|
|
|
|
|
|
|
|
QStringList lesserList = lesser.split(".");
|
|
|
|
QStringList greaterList = greater.split(".");
|
|
|
|
|
|
|
|
assert(lesserList.size() <= maxSections);
|
|
|
|
assert(greaterList.size() <= maxSections);
|
|
|
|
|
|
|
|
for (int i=0; i< maxSections; i++)
|
|
|
|
{
|
|
|
|
if (greaterList.size() <= i) // 1.1.1 > 1.1
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (lesserList.size() <= i) // 1.1 < 1.1.1
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (lesserList[i].toInt() != greaterList[i].toInt())
|
|
|
|
return lesserList[i].toInt() < greaterList[i].toInt(); // 1.1 < 1.2
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
CModEntry::CModEntry(QVariantMap repository, QVariantMap localData, QVariant modSettings, QString modname):
|
2013-08-22 17:22:49 +03:00
|
|
|
repository(repository),
|
|
|
|
localData(localData),
|
|
|
|
modSettings(modSettings),
|
|
|
|
modname(modname)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModEntry::isEnabled() const
|
|
|
|
{
|
|
|
|
if (!isInstalled())
|
|
|
|
return false;
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
return modSettings.toBool();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CModEntry::isDisabled() const
|
|
|
|
{
|
|
|
|
if (!isInstalled())
|
|
|
|
return false;
|
|
|
|
return !isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModEntry::isAvailable() const
|
|
|
|
{
|
|
|
|
if (isInstalled())
|
|
|
|
return false;
|
|
|
|
return !repository.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModEntry::isUpdateable() const
|
|
|
|
{
|
|
|
|
if (!isInstalled())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QString installedVer = localData["installedVersion"].toString();
|
|
|
|
QString availableVer = repository["latestVersion"].toString();
|
|
|
|
|
|
|
|
if (compareVersions(installedVer, availableVer))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModEntry::isInstalled() const
|
|
|
|
{
|
|
|
|
return !localData.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CModEntry::getModStatus() const
|
|
|
|
{
|
|
|
|
return
|
|
|
|
(isEnabled() ? ModStatus::ENABLED : 0) |
|
|
|
|
(isInstalled() ? ModStatus::INSTALLED : 0) |
|
|
|
|
(isUpdateable()? ModStatus::UPDATEABLE : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CModEntry::getName() const
|
|
|
|
{
|
|
|
|
return modname;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant CModEntry::getValue(QString value) const
|
|
|
|
{
|
|
|
|
if (repository.contains(value))
|
2013-09-21 21:29:26 +03:00
|
|
|
return repository[value];
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
if (localData.contains(value))
|
2013-09-21 21:29:26 +03:00
|
|
|
return localData[value];
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
QVariantMap CModList::copyField(QVariantMap data, QString from, QString to)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2013-09-21 21:29:26 +03:00
|
|
|
QVariantMap renamed;
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
for (auto it = data.begin(); it != data.end(); it++)
|
|
|
|
{
|
2013-09-21 21:29:26 +03:00
|
|
|
QVariant object = it.value();
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
object.toMap().insert(to, object.toMap().value(from));
|
|
|
|
renamed.insert(it.key(), object.toMap());
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
return renamed;
|
|
|
|
}
|
|
|
|
|
2013-08-24 23:11:51 +03:00
|
|
|
void CModList::resetRepositories()
|
|
|
|
{
|
|
|
|
repositories.clear();
|
|
|
|
}
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
void CModList::addRepository(QVariantMap data)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2013-08-24 23:11:51 +03:00
|
|
|
repositories.push_back(copyField(data, "version", "latestVersion"));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
void CModList::setLocalModList(QVariantMap data)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
localModList = copyField(data, "version", "installedVersion");
|
|
|
|
}
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
void CModList::setModSettings(QVariant data)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2013-09-21 21:29:26 +03:00
|
|
|
modSettings = data.toMap();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CModEntry CModList::getMod(QString modname) const
|
|
|
|
{
|
|
|
|
assert(hasMod(modname));
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
QVariantMap repo;
|
|
|
|
QVariantMap local = localModList[modname].toMap();
|
|
|
|
QVariant settings = modSettings[modname];
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2013-08-24 23:11:51 +03:00
|
|
|
for (auto entry : repositories)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
if (entry.contains(modname))
|
|
|
|
{
|
|
|
|
if (repo.empty())
|
2013-09-21 21:29:26 +03:00
|
|
|
repo = entry[modname].toMap();
|
2013-08-22 17:22:49 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (CModEntry::compareVersions(repo["version"].toString(),
|
2013-09-21 21:29:26 +03:00
|
|
|
entry[modname].toMap()["version"].toString()))
|
|
|
|
repo = entry[modname].toMap();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CModEntry(repo, local, settings, modname);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModList::hasMod(QString modname) const
|
|
|
|
{
|
|
|
|
if (localModList.contains(modname))
|
|
|
|
return true;
|
|
|
|
|
2013-08-24 23:11:51 +03:00
|
|
|
for (auto entry : repositories)
|
2013-08-22 17:22:49 +03:00
|
|
|
if (entry.contains(modname))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList CModList::getRequirements(QString modname)
|
|
|
|
{
|
|
|
|
QStringList ret;
|
|
|
|
|
|
|
|
if (hasMod(modname))
|
|
|
|
{
|
|
|
|
auto mod = getMod(modname);
|
|
|
|
|
|
|
|
for (auto entry : mod.getValue("depends").toStringList())
|
|
|
|
ret += getRequirements(entry);
|
|
|
|
}
|
|
|
|
ret += modname;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVector<QString> CModList::getModList() const
|
|
|
|
{
|
|
|
|
QSet<QString> knownMods;
|
|
|
|
QVector<QString> modList;
|
2013-08-24 23:11:51 +03:00
|
|
|
for (auto repo : repositories)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
for (auto it = repo.begin(); it != repo.end(); it++)
|
|
|
|
{
|
|
|
|
knownMods.insert(it.key());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto it = localModList.begin(); it != localModList.end(); it++)
|
|
|
|
{
|
|
|
|
knownMods.insert(it.key());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto entry : knownMods)
|
|
|
|
{
|
|
|
|
modList.push_back(entry);
|
|
|
|
}
|
|
|
|
return modList;
|
|
|
|
}
|