2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* cmodmanager.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
|
|
|
|
*
|
|
|
|
*/
|
2013-08-22 17:22:49 +03:00
|
|
|
#include "StdInc.h"
|
|
|
|
#include "cmodmanager.h"
|
|
|
|
|
2013-09-02 20:26:52 +03:00
|
|
|
#include "../../lib/VCMIDirs.h"
|
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
|
|
|
#include "../../lib/filesystem/CZipLoader.h"
|
2014-03-15 13:57:34 +03:00
|
|
|
#include "../../lib/CModHandler.h"
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
#include "../jsonutils.h"
|
2013-08-22 17:22:49 +03:00
|
|
|
#include "../launcherdirs.h"
|
|
|
|
|
|
|
|
static QString detectModArchive(QString path, QString modName)
|
|
|
|
{
|
2016-01-09 22:23:55 +02:00
|
|
|
auto files = ZipArchive::listFiles(qstringToPath(path));
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
QString modDirName;
|
|
|
|
|
2022-09-04 18:32:48 +02:00
|
|
|
for(int folderLevel : {0, 1}) //search in subfolder if there is no mod.json in the root
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2022-09-04 18:32:48 +02:00
|
|
|
for(auto file : files)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2022-09-04 18:32:48 +02:00
|
|
|
QString filename = QString::fromUtf8(file.c_str());
|
|
|
|
modDirName = filename.section('/', 0, folderLevel);
|
|
|
|
|
|
|
|
if(filename == modDirName + "/mod.json")
|
|
|
|
{
|
|
|
|
return modDirName;
|
|
|
|
}
|
2018-04-13 07:34:58 +02:00
|
|
|
}
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
2022-09-04 18:32:48 +02:00
|
|
|
|
|
|
|
return "";
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
CModManager::CModManager(CModList * modList)
|
|
|
|
: modList(modList)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
loadMods();
|
|
|
|
loadModSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString CModManager::settingsPath()
|
|
|
|
{
|
2014-08-21 23:26:28 +03:00
|
|
|
return pathToQString(VCMIDirs::get().userConfigPath() / "modSettings.json");
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::loadModSettings()
|
|
|
|
{
|
2013-09-21 21:29:26 +03:00
|
|
|
modSettings = JsonUtils::JsonFromFile(settingsPath()).toMap();
|
|
|
|
modList->setModSettings(modSettings["activeMods"]);
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2013-08-24 23:11:51 +03:00
|
|
|
void CModManager::resetRepositories()
|
|
|
|
{
|
|
|
|
modList->resetRepositories();
|
|
|
|
}
|
|
|
|
|
2013-08-22 17:22:49 +03:00
|
|
|
void CModManager::loadRepository(QString file)
|
|
|
|
{
|
2013-09-21 21:29:26 +03:00
|
|
|
modList->addRepository(JsonUtils::JsonFromFile(file).toMap());
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModManager::loadMods()
|
|
|
|
{
|
2014-03-15 13:57:34 +03:00
|
|
|
CModHandler handler;
|
|
|
|
handler.loadMods();
|
|
|
|
auto installedMods = handler.getAllMods();
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto modname : installedMods)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2014-03-20 20:06:25 +03:00
|
|
|
ResourceID resID(CModInfo::getModFile(modname));
|
2018-04-13 07:34:58 +02:00
|
|
|
if(CResourceHandler::get()->existsResource(resID))
|
2013-09-26 00:15:15 +03:00
|
|
|
{
|
2016-01-16 17:36:16 +02:00
|
|
|
boost::filesystem::path name = *CResourceHandler::get()->getResourceName(resID);
|
2016-01-16 20:44:15 +02:00
|
|
|
auto mod = JsonUtils::JsonFromFile(pathToQString(name));
|
2013-09-26 00:15:15 +03:00
|
|
|
localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
|
|
|
|
}
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
modList->setLocalModList(localMods);
|
|
|
|
}
|
|
|
|
|
2013-09-06 00:25:03 +03:00
|
|
|
bool CModManager::addError(QString modname, QString message)
|
|
|
|
{
|
|
|
|
recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList CModManager::getErrors()
|
|
|
|
{
|
|
|
|
QStringList ret = recentErrors;
|
|
|
|
recentErrors.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-08-22 17:22:49 +03:00
|
|
|
bool CModManager::installMod(QString modname, QString archivePath)
|
|
|
|
{
|
|
|
|
return canInstallMod(modname) && doInstallMod(modname, archivePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::uninstallMod(QString modname)
|
|
|
|
{
|
|
|
|
return canUninstallMod(modname) && doUninstallMod(modname);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::enableMod(QString modname)
|
|
|
|
{
|
|
|
|
return canEnableMod(modname) && doEnableMod(modname, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::disableMod(QString modname)
|
|
|
|
{
|
|
|
|
return canDisableMod(modname) && doEnableMod(modname, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::canInstallMod(QString modname)
|
|
|
|
{
|
|
|
|
auto mod = modList->getMod(modname);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(mod.getName().contains('.'))
|
2014-03-20 20:06:25 +03:00
|
|
|
return addError(modname, "Can not install submod");
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(mod.isInstalled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod is already installed");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!mod.isAvailable())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod is not available");
|
2013-08-22 17:22:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::canUninstallMod(QString modname)
|
|
|
|
{
|
|
|
|
auto mod = modList->getMod(modname);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(mod.getName().contains('.'))
|
2014-03-20 20:06:25 +03:00
|
|
|
return addError(modname, "Can not uninstall submod");
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!mod.isInstalled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod is not installed");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(mod.isEnabled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod must be disabled first");
|
2013-08-22 17:22:49 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::canEnableMod(QString modname)
|
|
|
|
{
|
|
|
|
auto mod = modList->getMod(modname);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(mod.isEnabled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod is already enabled");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!mod.isInstalled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod must be installed first");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto modEntry : mod.getValue("depends").toStringList())
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!modList->hasMod(modEntry)) // required mod is not available
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, QString("Required mod %1 is missing").arg(modEntry));
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!modList->getMod(modEntry).isEnabled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, QString("Required mod %1 is not enabled").arg(modEntry));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
for(QString modEntry : modList->getModList())
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2013-09-06 00:25:03 +03:00
|
|
|
auto mod = modList->getMod(modEntry);
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2013-09-06 00:25:03 +03:00
|
|
|
// "reverse conflict" - enabled mod has this one as conflict
|
2018-04-13 07:34:58 +02:00
|
|
|
if(mod.isEnabled() && mod.getValue("conflicts").toStringList().contains(modname))
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto modEntry : mod.getValue("conflicts").toStringList())
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
// check if conflicting mod installed and enabled
|
|
|
|
if(modList->hasMod(modEntry) && modList->getMod(modEntry).isEnabled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::canDisableMod(QString modname)
|
|
|
|
{
|
|
|
|
auto mod = modList->getMod(modname);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(mod.isDisabled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod is already disabled");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!mod.isInstalled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod must be installed first");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
for(QString modEntry : modList->getModList())
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
auto current = modList->getMod(modEntry);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(current.getValue("depends").toStringList().contains(modname) && current.isEnabled())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, QString("This mod is needed to run %1").arg(modEntry));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:06:25 +03:00
|
|
|
static QVariant writeValue(QString path, QVariantMap input, QVariant value)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(path.size() > 1)
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2014-03-20 20:06:25 +03:00
|
|
|
QString entryName = path.section('/', 0, 1);
|
|
|
|
QString remainder = "/" + path.section('/', 2, -1);
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2014-03-20 20:06:25 +03:00
|
|
|
entryName.remove(0, 1);
|
|
|
|
input.insert(entryName, writeValue(remainder, input.value(entryName).toMap(), value));
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::doEnableMod(QString mod, bool on)
|
|
|
|
{
|
|
|
|
QString path = mod;
|
|
|
|
path = "/activeMods/" + path.replace(".", "/mods/") + "/active";
|
|
|
|
|
|
|
|
modSettings = writeValue(path, modSettings, QVariant(on)).toMap();
|
2013-09-21 21:29:26 +03:00
|
|
|
modList->setModSettings(modSettings["activeMods"]);
|
2014-03-20 20:06:25 +03:00
|
|
|
modList->modChanged(mod);
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
JsonUtils::JsonToFile(settingsPath(), modSettings);
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::doInstallMod(QString modname, QString archivePath)
|
|
|
|
{
|
|
|
|
QString destDir = CLauncherDirs::get().modsPath() + "/";
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!QFile(archivePath).exists())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod archive is missing");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(localMods.contains(modname))
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod with such name is already installed");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
QString modDirName = detectModArchive(archivePath, modname);
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!modDirName.size())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Mod archive is invalid or corrupted");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!ZipArchive::extract(qstringToPath(archivePath), qstringToPath(destDir)))
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2017-05-25 02:03:02 +02:00
|
|
|
removeModDir(destDir + modDirName);
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Failed to extract mod data");
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2022-09-04 18:32:48 +02:00
|
|
|
//rename folder and fix the path
|
|
|
|
QDir extractedDir(destDir + modDirName);
|
|
|
|
auto rc = QFile::rename(destDir + modDirName, destDir + modname);
|
|
|
|
if (rc)
|
|
|
|
extractedDir.setPath(destDir + modname);
|
|
|
|
|
|
|
|
//there are possible excessive files - remove them
|
|
|
|
QString upperLevel = modDirName.section('/', 0, 0);
|
|
|
|
if(upperLevel != modDirName)
|
|
|
|
removeModDir(destDir + upperLevel);
|
|
|
|
|
|
|
|
QVariantMap json = JsonUtils::JsonFromFile(destDir + modname + "/mod.json").toMap();
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
localMods.insert(modname, json);
|
|
|
|
modList->setLocalModList(localMods);
|
2014-03-20 20:06:25 +03:00
|
|
|
modList->modChanged(modname);
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CModManager::doUninstallMod(QString modname)
|
|
|
|
{
|
|
|
|
ResourceID resID(std::string("Mods/") + modname.toUtf8().data(), EResType::DIRECTORY);
|
|
|
|
// Get location of the mod, in case-insensitive way
|
2016-01-16 20:44:15 +02:00
|
|
|
QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!QDir(modDir).exists())
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Data with this mod was not found");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!localMods.contains(modname))
|
2013-09-06 00:25:03 +03:00
|
|
|
return addError(modname, "Data with this mod was not found");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2022-09-04 18:32:48 +02:00
|
|
|
QDir modFullDir(modDir);
|
2018-04-13 07:34:58 +02:00
|
|
|
if(!removeModDir(modDir))
|
2022-09-04 18:32:48 +02:00
|
|
|
return addError(modname, "Mod is located in protected directory, plase remove it manually:\n" + modFullDir.absolutePath());
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
localMods.remove(modname);
|
|
|
|
modList->setLocalModList(localMods);
|
2014-03-20 20:06:25 +03:00
|
|
|
modList->modChanged(modname);
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-25 02:03:02 +02:00
|
|
|
|
|
|
|
bool CModManager::removeModDir(QString path)
|
|
|
|
{
|
|
|
|
// issues 2673 and 2680 its why you do not recursively remove without sanity check
|
|
|
|
QDir checkDir(path);
|
|
|
|
if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
|
|
|
|
return false;
|
|
|
|
if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QDir dir(path);
|
|
|
|
if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
|
|
|
|
return false;
|
|
|
|
if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return dir.removeRecursively();
|
|
|
|
}
|