cmodmanager.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * cmodmanager.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "cmodmanager.h"
  12. #include "../../lib/VCMIDirs.h"
  13. #include "../../lib/filesystem/Filesystem.h"
  14. #include "../../lib/filesystem/CZipLoader.h"
  15. #include "../../lib/modding/CModHandler.h"
  16. #include "../../lib/modding/CModInfo.h"
  17. #include "../../lib/modding/IdentifierStorage.h"
  18. #include "../jsonutils.h"
  19. #include "../launcherdirs.h"
  20. namespace
  21. {
  22. QString detectModArchive(QString path, QString modName)
  23. {
  24. auto files = ZipArchive::listFiles(qstringToPath(path));
  25. QString modDirName;
  26. for(int folderLevel : {0, 1}) //search in subfolder if there is no mod.json in the root
  27. {
  28. for(auto file : files)
  29. {
  30. QString filename = QString::fromUtf8(file.c_str());
  31. modDirName = filename.section('/', 0, folderLevel);
  32. if(filename == modDirName + "/mod.json")
  33. {
  34. return modDirName;
  35. }
  36. }
  37. }
  38. return "";
  39. }
  40. }
  41. CModManager::CModManager(CModList * modList)
  42. : modList(modList)
  43. {
  44. loadMods();
  45. loadModSettings();
  46. }
  47. QString CModManager::settingsPath()
  48. {
  49. return pathToQString(VCMIDirs::get().userConfigPath() / "modSettings.json");
  50. }
  51. void CModManager::loadModSettings()
  52. {
  53. modSettings = JsonUtils::JsonFromFile(settingsPath()).toMap();
  54. modList->setModSettings(modSettings["activeMods"]);
  55. }
  56. void CModManager::resetRepositories()
  57. {
  58. modList->resetRepositories();
  59. }
  60. void CModManager::loadRepository(QVariantMap repomap)
  61. {
  62. modList->addRepository(repomap);
  63. }
  64. void CModManager::loadMods()
  65. {
  66. CModHandler handler;
  67. handler.loadMods();
  68. auto installedMods = handler.getAllMods();
  69. localMods.clear();
  70. for(auto modname : installedMods)
  71. {
  72. auto resID = CModInfo::getModFile(modname);
  73. if(CResourceHandler::get()->existsResource(resID))
  74. {
  75. //calculate mod size
  76. qint64 total = 0;
  77. ResourcePath resDir(CModInfo::getModDir(modname), EResType::DIRECTORY);
  78. if(CResourceHandler::get()->existsResource(resDir))
  79. {
  80. for(QDirIterator iter(QString::fromStdString(CResourceHandler::get()->getResourceName(resDir)->string()), QDirIterator::Subdirectories); iter.hasNext(); iter.next())
  81. total += iter.fileInfo().size();
  82. }
  83. boost::filesystem::path name = *CResourceHandler::get()->getResourceName(resID);
  84. auto mod = JsonUtils::JsonFromFile(pathToQString(name));
  85. auto json = JsonUtils::toJson(mod);
  86. json["localSizeBytes"].Float() = total;
  87. if(!name.is_absolute())
  88. json["storedLocaly"].Bool() = true;
  89. mod = JsonUtils::toVariant(json);
  90. localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
  91. }
  92. }
  93. modList->setLocalModList(localMods);
  94. }
  95. bool CModManager::addError(QString modname, QString message)
  96. {
  97. recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
  98. return false;
  99. }
  100. QStringList CModManager::getErrors()
  101. {
  102. QStringList ret = recentErrors;
  103. recentErrors.clear();
  104. return ret;
  105. }
  106. bool CModManager::installMod(QString modname, QString archivePath)
  107. {
  108. return canInstallMod(modname) && doInstallMod(modname, archivePath);
  109. }
  110. bool CModManager::uninstallMod(QString modname)
  111. {
  112. return canUninstallMod(modname) && doUninstallMod(modname);
  113. }
  114. bool CModManager::enableMod(QString modname)
  115. {
  116. return canEnableMod(modname) && doEnableMod(modname, true);
  117. }
  118. bool CModManager::disableMod(QString modname)
  119. {
  120. return canDisableMod(modname) && doEnableMod(modname, false);
  121. }
  122. bool CModManager::canInstallMod(QString modname)
  123. {
  124. auto mod = modList->getMod(modname);
  125. if(mod.getName().contains('.'))
  126. return addError(modname, "Can not install submod");
  127. if(mod.isInstalled())
  128. return addError(modname, "Mod is already installed");
  129. if(!mod.isAvailable())
  130. return addError(modname, "Mod is not available");
  131. return true;
  132. }
  133. bool CModManager::canUninstallMod(QString modname)
  134. {
  135. auto mod = modList->getMod(modname);
  136. if(mod.getName().contains('.'))
  137. return addError(modname, "Can not uninstall submod");
  138. if(!mod.isInstalled())
  139. return addError(modname, "Mod is not installed");
  140. return true;
  141. }
  142. bool CModManager::canEnableMod(QString modname)
  143. {
  144. auto mod = modList->getMod(modname);
  145. if(mod.isEnabled())
  146. return addError(modname, "Mod is already enabled");
  147. if(!mod.isInstalled())
  148. return addError(modname, "Mod must be installed first");
  149. //check for compatibility
  150. if(!mod.isCompatible())
  151. return addError(modname, "Mod is not compatible, please update VCMI and checkout latest mod revisions");
  152. for(auto modEntry : mod.getValue("depends").toStringList())
  153. {
  154. if(!modList->hasMod(modEntry)) // required mod is not available
  155. return addError(modname, QString("Required mod %1 is missing").arg(modEntry));
  156. if(!modList->getMod(modEntry).isEnabled())
  157. return addError(modname, QString("Required mod %1 is not enabled").arg(modEntry));
  158. }
  159. for(QString modEntry : modList->getModList())
  160. {
  161. auto mod = modList->getMod(modEntry);
  162. // "reverse conflict" - enabled mod has this one as conflict
  163. if(mod.isEnabled() && mod.getValue("conflicts").toStringList().contains(modname))
  164. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  165. }
  166. for(auto modEntry : mod.getValue("conflicts").toStringList())
  167. {
  168. // check if conflicting mod installed and enabled
  169. if(modList->hasMod(modEntry) && modList->getMod(modEntry).isEnabled())
  170. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  171. }
  172. return true;
  173. }
  174. bool CModManager::canDisableMod(QString modname)
  175. {
  176. auto mod = modList->getMod(modname);
  177. if(mod.isDisabled())
  178. return addError(modname, "Mod is already disabled");
  179. if(!mod.isInstalled())
  180. return addError(modname, "Mod must be installed first");
  181. for(QString modEntry : modList->getModList())
  182. {
  183. auto current = modList->getMod(modEntry);
  184. if(current.getValue("depends").toStringList().contains(modname) && current.isEnabled())
  185. return addError(modname, QString("This mod is needed to run %1").arg(modEntry));
  186. }
  187. return true;
  188. }
  189. static QVariant writeValue(QString path, QVariantMap input, QVariant value)
  190. {
  191. if(path.size() > 1)
  192. {
  193. QString entryName = path.section('/', 0, 1);
  194. QString remainder = "/" + path.section('/', 2, -1);
  195. entryName.remove(0, 1);
  196. input.insert(entryName, writeValue(remainder, input.value(entryName).toMap(), value));
  197. return input;
  198. }
  199. else
  200. {
  201. return value;
  202. }
  203. }
  204. bool CModManager::doEnableMod(QString mod, bool on)
  205. {
  206. QString path = mod;
  207. path = "/activeMods/" + path.replace(".", "/mods/") + "/active";
  208. modSettings = writeValue(path, modSettings, QVariant(on)).toMap();
  209. modList->setModSettings(modSettings["activeMods"]);
  210. modList->modChanged(mod);
  211. JsonUtils::JsonToFile(settingsPath(), modSettings);
  212. return true;
  213. }
  214. bool CModManager::doInstallMod(QString modname, QString archivePath)
  215. {
  216. QString destDir = CLauncherDirs::get().modsPath() + "/";
  217. if(!QFile(archivePath).exists())
  218. return addError(modname, "Mod archive is missing");
  219. if(localMods.contains(modname))
  220. return addError(modname, "Mod with such name is already installed");
  221. QString modDirName = ::detectModArchive(archivePath, modname);
  222. if(!modDirName.size())
  223. return addError(modname, "Mod archive is invalid or corrupted");
  224. if(!ZipArchive::extract(qstringToPath(archivePath), qstringToPath(destDir)))
  225. {
  226. removeModDir(destDir + modDirName);
  227. return addError(modname, "Failed to extract mod data");
  228. }
  229. //rename folder and fix the path
  230. QDir extractedDir(destDir + modDirName);
  231. auto rc = QFile::rename(destDir + modDirName, destDir + modname);
  232. if (rc)
  233. extractedDir.setPath(destDir + modname);
  234. //there are possible excessive files - remove them
  235. QString upperLevel = modDirName.section('/', 0, 0);
  236. if(upperLevel != modDirName)
  237. removeModDir(destDir + upperLevel);
  238. CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &) { return true; });
  239. loadMods();
  240. modList->reloadRepositories();
  241. return true;
  242. }
  243. bool CModManager::doUninstallMod(QString modname)
  244. {
  245. ResourcePath resID(std::string("Mods/") + modname.toStdString(), EResType::DIRECTORY);
  246. // Get location of the mod, in case-insensitive way
  247. QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));
  248. if(!QDir(modDir).exists())
  249. return addError(modname, "Data with this mod was not found");
  250. QDir modFullDir(modDir);
  251. if(!removeModDir(modDir))
  252. return addError(modname, "Mod is located in protected directory, please remove it manually:\n" + modFullDir.absolutePath());
  253. CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &){ return true; });
  254. loadMods();
  255. modList->reloadRepositories();
  256. return true;
  257. }
  258. bool CModManager::removeModDir(QString path)
  259. {
  260. // issues 2673 and 2680 its why you do not recursively remove without sanity check
  261. QDir checkDir(path);
  262. QDir dir(path);
  263. if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
  264. return false;
  265. #ifndef VCMI_IOS //ios applications are stored in the isolated container
  266. if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
  267. return false;
  268. if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
  269. return false;
  270. #endif
  271. if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
  272. return false;
  273. return dir.removeRecursively();
  274. }