1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
/*
* 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 "../../lib/texts/CGeneralTextHandler.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::setRepositoryData(const JsonNode & repomap)
{
modList->setRepositoryData(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::enableMods(QStringList modlist)
{
for (const auto & modname : modlist)
if (!canEnableMod(modname))
return false;
modList->doEnableMods(modlist);
return true;
}
bool ModStateController::disableMod(QString modname)
{
if (!canDisableMod(modname))
return false;
modList->doDisableMod(modname);
return true;
}
bool ModStateController::canInstallMod(QString modname)
{
if (!modList->isModExists(modname))
return true; // for installation of unknown mods, e.g. via "Install from file" option
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)
{
if (!modList->isModExists(modname))
return false;
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 check the latest mod revisions"));
if (mod.isTranslation() && CGeneralTextHandler::getPreferredLanguage() != mod.getBaseLanguage().toStdString())
return addError(modname, tr("Can not enable translation mod for a different language!"));
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::doInstallMod(QString modname, QString archivePath)
{
const auto destDir = CLauncherDirs::modsPath() + QChar{'/'};
if(!QFile(archivePath).exists())
return addError(modname, tr("Mod archive is missing"));
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)
{
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);
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("Mod data was not found"));
QDir modFullDir(modDir);
if(!removeModDir(modDir))
return addError(modname, tr("Mod is located in a protected directory, please remove it manually:\n") + modFullDir.absolutePath());
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();
}
|