1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-22 22:13:35 +02:00
vcmi/launcher/modManager/modstatecontroller.cpp
Ivan Savenko 30ed066cea Restored mod uninstall functionality, restored translatable mod fields,
added more fields to translatable list
2024-11-26 13:55:46 +00:00

281 lines
7.3 KiB
C++

/*
* modstatecontroller.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 "modstatecontroller.h"
#include "modstatemodel.h"
#include "../../lib/VCMIDirs.h"
#include "../../lib/filesystem/Filesystem.h"
#include "../../lib/filesystem/CZipLoader.h"
#include "../../lib/modding/CModHandler.h"
#include "../../lib/modding/IdentifierStorage.h"
#include "../../lib/json/JsonNode.h"
#include "../vcmiqt/jsonutils.h"
#include "../vcmiqt/launcherdirs.h"
#include <future>
namespace
{
QString detectModArchive(QString path, QString modName, std::vector<std::string> & filesToExtract)
{
try {
ZipArchive archive(qstringToPath(path));
filesToExtract = archive.listFiles();
}
catch (const std::runtime_error & e)
{
logGlobal->error("Failed to open zip archive. Reason: %s", e.what());
return "";
}
QString modDirName;
for(int folderLevel : {0, 1}) //search in subfolder if there is no mod.json in the root
{
for(const auto & file : filesToExtract)
{
QString filename = QString::fromUtf8(file.c_str());
modDirName = filename.section('/', 0, folderLevel);
if(filename == modDirName + "/mod.json")
{
return modDirName;
}
}
}
logGlobal->error("Failed to detect mod path in archive!");
logGlobal->debug("List of file in archive:");
for(const auto & file : filesToExtract)
logGlobal->debug("%s", file.c_str());
return "";
}
}
ModStateController::ModStateController(std::shared_ptr<ModStateModel> modList)
: modList(modList)
{
}
ModStateController::~ModStateController() = default;
void ModStateController::appendRepositories(const JsonNode & repomap)
{
modList->appendRepositories(repomap);
}
bool ModStateController::addError(QString modname, QString message)
{
recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
return false;
}
QStringList ModStateController::getErrors()
{
QStringList ret = recentErrors;
recentErrors.clear();
return ret;
}
bool ModStateController::installMod(QString modname, QString archivePath)
{
return canInstallMod(modname) && doInstallMod(modname, archivePath);
}
bool ModStateController::uninstallMod(QString modname)
{
return canUninstallMod(modname) && doUninstallMod(modname);
}
bool ModStateController::enableMod(QString modname)
{
return canEnableMod(modname) && doEnableMod(modname, true);
}
bool ModStateController::disableMod(QString modname)
{
return canDisableMod(modname) && doEnableMod(modname, false);
}
bool ModStateController::canInstallMod(QString modname)
{
auto mod = modList->getMod(modname);
if(mod.isSubmod())
return addError(modname, tr("Can not install submod"));
if(mod.isInstalled())
return addError(modname, tr("Mod is already installed"));
return true;
}
bool ModStateController::canUninstallMod(QString modname)
{
auto mod = modList->getMod(modname);
if(mod.isSubmod())
return addError(modname, tr("Can not uninstall submod"));
if(!mod.isInstalled())
return addError(modname, tr("Mod is not installed"));
return true;
}
bool ModStateController::canEnableMod(QString modname)
{
auto mod = modList->getMod(modname);
if(modList->isModEnabled(modname))
return addError(modname, tr("Mod is already enabled"));
if(!mod.isInstalled())
return addError(modname, tr("Mod must be installed first"));
//check for compatibility
if(!mod.isCompatible())
return addError(modname, tr("Mod is not compatible, please update VCMI and checkout latest mod revisions"));
for(const auto & modEntry : mod.getDependencies())
{
if(!modList->isModExists(modEntry)) // required mod is not available
return addError(modname, tr("Required mod %1 is missing").arg(modEntry));
}
return true;
}
bool ModStateController::canDisableMod(QString modname)
{
auto mod = modList->getMod(modname);
if(!modList->isModEnabled(modname))
return addError(modname, tr("Mod is already disabled"));
if(!mod.isInstalled())
return addError(modname, tr("Mod must be installed first"));
return true;
}
bool ModStateController::doEnableMod(QString mod, bool on)
{
if (on)
modList->doEnableMod(mod);
else
modList->doDisableMod(mod);
return true;
}
bool ModStateController::doInstallMod(QString modname, QString archivePath)
{
const auto destDir = CLauncherDirs::modsPath() + QChar{'/'};
if(!QFile(archivePath).exists())
return addError(modname, tr("Mod archive is missing"));
if(localMods.contains(modname))
return addError(modname, tr("Mod with such name is already installed"));
std::vector<std::string> filesToExtract;
QString modDirName = ::detectModArchive(archivePath, modname, filesToExtract);
if(!modDirName.size())
return addError(modname, tr("Mod archive is invalid or corrupted"));
std::atomic<int> filesCounter = 0;
auto futureExtract = std::async(std::launch::async, [&archivePath, &destDir, &filesCounter, &filesToExtract]()
{
const auto destDirFsPath = qstringToPath(destDir);
ZipArchive archive(qstringToPath(archivePath));
for(const auto & file : filesToExtract)
{
if (!archive.extract(destDirFsPath, file))
return false;
++filesCounter;
}
return true;
});
while(futureExtract.wait_for(std::chrono::milliseconds(10)) != std::future_status::ready)
{
emit extractionProgress(filesCounter, filesToExtract.size());
qApp->processEvents();
}
if(!futureExtract.get())
{
removeModDir(destDir + modDirName);
return addError(modname, tr("Failed to extract mod data"));
}
//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);
CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &) { return true; });
//modList->reloadLocalMods();
return true;
}
bool ModStateController::doUninstallMod(QString modname)
{
ResourcePath resID(std::string("Mods/") + modname.toStdString(), EResType::DIRECTORY);
// Get location of the mod, in case-insensitive way
QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));
if(!QDir(modDir).exists())
return addError(modname, tr("Data with this mod was not found"));
QDir modFullDir(modDir);
if(!removeModDir(modDir))
return addError(modname, tr("Mod is located in protected directory, please remove it manually:\n") + modFullDir.absolutePath());
CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &){ return true; });
modList->reloadLocalState();
return true;
}
bool ModStateController::removeModDir(QString path)
{
// issues 2673 and 2680 its why you do not recursively remove without sanity check
QDir checkDir(path);
QDir dir(path);
if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
return false;
#ifndef VCMI_MOBILE // ios and android applications are stored in the isolated container
if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
return false;
if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
return false;
#endif
if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
return false;
return dir.removeRecursively();
}