| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 | 
							- /*
 
-  * cmodlist.cpp, part of VCMI engine
 
-  *
 
-  * Authors: listed in file AUTHORS in main folder
 
-  *
 
-  * License: GNU General Public License v2.0 or later
 
-  * Full text of license available in license.txt file, in main folder
 
-  *
 
-  */
 
- #include "StdInc.h"
 
- #include "cmodlist.h"
 
- #include "../lib/CConfigHandler.h"
 
- #include "../../lib/filesystem/CFileInputStream.h"
 
- #include "../../lib/GameConstants.h"
 
- #include "../../lib/modding/CModVersion.h"
 
- QString CModEntry::sizeToString(double size)
 
- {
 
- 	static const std::array sizes {
 
- 		QT_TRANSLATE_NOOP("File size", "%1 B"),
 
- 		QT_TRANSLATE_NOOP("File size", "%1 KiB"),
 
- 		QT_TRANSLATE_NOOP("File size", "%1 MiB"),
 
- 		QT_TRANSLATE_NOOP("File size", "%1 GiB"),
 
- 		QT_TRANSLATE_NOOP("File size", "%1 TiB")
 
- 	};
 
- 	size_t index = 0;
 
- 	while(size > 1024 && index < sizes.size())
 
- 	{
 
- 		size /= 1024;
 
- 		index++;
 
- 	}
 
- 	return QCoreApplication::translate("File size", sizes[index]).arg(QString::number(size, 'f', 1));
 
- }
 
- CModEntry::CModEntry(QVariantMap repository, QVariantMap localData, QVariantMap modSettings, QString modname)
 
- 	: repository(repository), localData(localData), modSettings(modSettings), modname(modname)
 
- {
 
- }
 
- bool CModEntry::isEnabled() const
 
- {
 
- 	if(!isInstalled())
 
- 		return false;
 
- 	if (!isVisible())
 
- 		return false;
 
- 	return modSettings["active"].toBool();
 
- }
 
- 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;
 
- 	auto installedVer = localData["installedVersion"].toString().toStdString();
 
- 	auto availableVer = repository["latestVersion"].toString().toStdString();
 
- 	return (CModVersion::fromString(installedVer) < CModVersion::fromString(availableVer));
 
- }
 
- bool isCompatible(const QVariantMap & compatibility)
 
- {
 
- 	auto compatibleMin = CModVersion::fromString(compatibility["min"].toString().toStdString());
 
- 	auto compatibleMax = CModVersion::fromString(compatibility["max"].toString().toStdString());
 
- 	return (compatibleMin.isNull() || CModVersion::GameVersion().compatible(compatibleMin, true, true))
 
- 			&& (compatibleMax.isNull() || compatibleMax.compatible(CModVersion::GameVersion(), true, true));
 
- }
 
- bool CModEntry::isCompatible() const
 
- {
 
- 	return ::isCompatible(localData["compatibility"].toMap());
 
- }
 
- bool CModEntry::isEssential() const
 
- {
 
- 	return getName() == "vcmi";
 
- }
 
- bool CModEntry::isInstalled() const
 
- {
 
- 	return !localData.isEmpty();
 
- }
 
- bool CModEntry::isVisible() const
 
- {
 
- 	if (isCompatibilityPatch())
 
- 	{
 
- 		if (isSubmod())
 
- 			return false;
 
- 	}
 
- 	if (isTranslation())
 
- 	{
 
- 		// Do not show not installed translation mods to languages other than player language
 
- 		if (localData.empty() && getBaseValue("language") != QString::fromStdString(settings["general"]["language"].String()) )
 
- 			return false;
 
- 	}
 
- 	return !localData.isEmpty() || (!repository.isEmpty() && !repository.contains("mod"));
 
- }
 
- bool CModEntry::isTranslation() const
 
- {
 
- 	return getBaseValue("modType").toString() == "Translation";
 
- }
 
- bool CModEntry::isCompatibilityPatch() const
 
- {
 
- 	return getBaseValue("modType").toString() == "Compatibility";
 
- }
 
- bool CModEntry::isSubmod() const
 
- {
 
- 	return getName().contains('.');
 
- }
 
- int CModEntry::getModStatus() const
 
- {
 
- 	int status = 0;
 
- 	if(isEnabled())
 
- 		status |= ModStatus::ENABLED;
 
- 	if(isInstalled())
 
- 		status |= ModStatus::INSTALLED;
 
- 	if(isUpdateable())
 
- 		status |= ModStatus::UPDATEABLE;
 
- 	return status;
 
- }
 
- QString CModEntry::getName() const
 
- {
 
- 	return modname;
 
- }
 
- QVariant CModEntry::getValue(QString value) const
 
- {
 
- 	return getValueImpl(value, true);
 
- }
 
- QStringList CModEntry::getDependencies() const
 
- {
 
- 	QStringList result;
 
- 	for (auto const & entry : getValue("depends").toStringList())
 
- 		result.push_back(entry.toLower());
 
- 	return result;
 
- }
 
- QStringList CModEntry::getConflicts() const
 
- {
 
- 	QStringList result;
 
- 	for (auto const & entry : getValue("conflicts").toStringList())
 
- 		result.push_back(entry.toLower());
 
- 	return result;
 
- }
 
- QVariant CModEntry::getBaseValue(QString value) const
 
- {
 
- 	return getValueImpl(value, false);
 
- }
 
- QVariant CModEntry::getValueImpl(QString value, bool localized) const
 
- {
 
- 	QString langValue = QString::fromStdString(settings["general"]["language"].String());
 
- 	// Priorities
 
- 	// 1) data from newest version
 
- 	// 2) data from preferred language
 
- 	bool useRepositoryData = repository.contains(value);
 
- 	if(repository.contains(value) && localData.contains(value))
 
- 	{
 
- 		// value is present in both repo and locally installed. Select one from latest version
 
- 		auto installedVer = localData["installedVersion"].toString().toStdString();
 
- 		auto availableVer = repository["latestVersion"].toString().toStdString();
 
- 		useRepositoryData = CModVersion::fromString(installedVer) < CModVersion::fromString(availableVer);
 
- 	}
 
- 	auto & storage = useRepositoryData ? repository : localData;
 
- 	if(localized && storage.contains(langValue))
 
- 	{
 
- 		auto langStorage = storage[langValue].toMap();
 
- 		if (langStorage.contains(value))
 
- 			return langStorage[value];
 
- 	}
 
- 	if(storage.contains(value))
 
- 		return storage[value];
 
- 	return QVariant();
 
- }
 
- QVariantMap CModList::copyField(QVariantMap data, QString from, QString to) const
 
- {
 
- 	QVariantMap renamed;
 
- 	for(auto it = data.begin(); it != data.end(); it++)
 
- 	{
 
- 		QVariantMap modConf = it.value().toMap();
 
- 		modConf.insert(to, modConf.value(from));
 
- 		renamed.insert(it.key(), modConf);
 
- 	}
 
- 	return renamed;
 
- }
 
- void CModList::reloadRepositories()
 
- {
 
- 	cachedMods.clear();
 
- }
 
- void CModList::resetRepositories()
 
- {
 
- 	repositories.clear();
 
- 	cachedMods.clear();
 
- }
 
- void CModList::addRepository(QVariantMap data)
 
- {
 
- 	for(auto & key : data.keys())
 
- 		data[key.toLower()] = data.take(key);
 
- 	repositories.push_back(copyField(data, "version", "latestVersion"));
 
- 	cachedMods.clear();
 
- }
 
- void CModList::setLocalModList(QVariantMap data)
 
- {
 
- 	localModList = copyField(data, "version", "installedVersion");
 
- 	cachedMods.clear();
 
- }
 
- void CModList::setModSettings(QVariant data)
 
- {
 
- 	modSettings = data.toMap();
 
- 	cachedMods.clear();
 
- }
 
- void CModList::modChanged(QString modID)
 
- {
 
- 	cachedMods.clear();
 
- }
 
- static QVariant getValue(QVariant input, QString path)
 
- {
 
- 	if(path.size() > 1)
 
- 	{
 
- 		QString entryName = path.section('/', 0, 1);
 
- 		QString remainder = "/" + path.section('/', 2, -1);
 
- 		entryName.remove(0, 1);
 
- 		return getValue(input.toMap().value(entryName), remainder);
 
- 	}
 
- 	else
 
- 	{
 
- 		return input;
 
- 	}
 
- }
 
- const CModEntry & CModList::getMod(QString modName) const
 
- {
 
- 	modName = modName.toLower();
 
- 	auto it = cachedMods.find(modName);
 
- 	if (it != cachedMods.end())
 
- 		return it.value();
 
- 	auto itNew = cachedMods.insert(modName, getModUncached(modName));
 
- 	return *itNew;
 
- }
 
- CModEntry CModList::getModUncached(QString modname) const
 
- {
 
- 	QVariantMap repo;
 
- 	QVariantMap local = localModList[modname].toMap();
 
- 	QVariantMap settings;
 
- 	QString path = modname;
 
- 	path = "/" + path.replace(".", "/mods/");
 
- 	QVariant conf = getValue(modSettings, path);
 
- 	if(conf.isNull())
 
- 	{
 
- 		settings["active"] = !local.value("keepDisabled").toBool();
 
- 	}
 
- 	else
 
- 	{
 
- 		if(!conf.toMap().isEmpty())
 
- 		{
 
- 			settings = conf.toMap();
 
- 			if(settings.value("active").isNull())
 
- 				settings["active"] = !local.value("keepDisabled").toBool();
 
- 		}
 
- 		else
 
- 			settings.insert("active", conf);
 
- 	}
 
- 	
 
- 	if(settings["active"].toBool())
 
- 	{
 
- 		QString rootPath = path.section('/', 0, 1);
 
- 		if(path != rootPath)
 
- 		{
 
- 			conf = getValue(modSettings, rootPath);
 
- 			const auto confMap = conf.toMap();
 
- 			if(!conf.isNull() && !confMap["active"].isNull() && !confMap["active"].toBool())
 
- 			{
 
- 				settings = confMap;
 
- 			}
 
- 		}
 
- 	}
 
- 	if(settings.value("active").toBool())
 
- 	{
 
- 		if(!::isCompatible(local.value("compatibility").toMap()))
 
- 			settings["active"] = false;
 
- 	}
 
- 	for(auto entry : repositories)
 
- 	{
 
- 		QVariant repoVal = getValue(entry, path);
 
- 		if(repoVal.isValid())
 
- 		{
 
- 			auto repoValMap = repoVal.toMap();
 
- 			if(::isCompatible(repoValMap["compatibility"].toMap()))
 
- 			{
 
- 				if(repo.empty()
 
- 					|| CModVersion::fromString(repo["version"].toString().toStdString())
 
- 					 < CModVersion::fromString(repoValMap["version"].toString().toStdString()))
 
- 				{
 
- 					//take valid download link, screenshots and mod size before assignment
 
- 					auto download = repo.value("download");
 
- 					auto screenshots = repo.value("screenshots");
 
- 					auto size = repo.value("downloadSize");
 
- 					repo = repoValMap;
 
- 					if(repo.value("download").isNull())
 
- 					{
 
- 						repo["download"] = download;
 
- 						if(repo.value("screenshots").isNull()) //taking screenshot from the downloadable version
 
- 							repo["screenshots"] = screenshots;
 
- 					}
 
- 					if(repo.value("downloadSize").isNull())
 
- 						repo["downloadSize"] = size;
 
- 				}
 
- 			}
 
- 		}
 
- 	}
 
- 	return CModEntry(repo, local, settings, modname);
 
- }
 
- bool CModList::hasMod(QString modname) const
 
- {
 
- 	if(localModList.contains(modname))
 
- 		return true;
 
- 	for(auto entry : repositories)
 
- 		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.getDependencies())
 
- 			ret += getRequirements(entry.toLower());
 
- 	}
 
- 	ret += modname;
 
- 	return ret;
 
- }
 
- QVector<QString> CModList::getModList() const
 
- {
 
- 	QSet<QString> knownMods;
 
- 	QVector<QString> modList;
 
- 	for(auto repo : repositories)
 
- 	{
 
- 		for(auto it = repo.begin(); it != repo.end(); it++)
 
- 		{
 
- 			knownMods.insert(it.key().toLower());
 
- 		}
 
- 	}
 
- 	for(auto it = localModList.begin(); it != localModList.end(); it++)
 
- 	{
 
- 		knownMods.insert(it.key().toLower());
 
- 	}
 
- 	for(auto entry : knownMods)
 
- 	{
 
- 		modList.push_back(entry);
 
- 	}
 
- 	return modList;
 
- }
 
- QVector<QString> CModList::getChildren(QString parent) const
 
- {
 
- 	QVector<QString> children;
 
- 	int depth = parent.count('.') + 1;
 
- 	for(const QString & mod : getModList())
 
- 	{
 
- 		if(mod.count('.') == depth && mod.startsWith(parent))
 
- 			children.push_back(mod);
 
- 	}
 
- 	return children;
 
- }
 
 
  |