cmodmanager.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/CModHandler.h"
  16. #include "../jsonutils.h"
  17. #include "../launcherdirs.h"
  18. namespace
  19. {
  20. const QLatin1String extraResolutionsMod{"vcmi-extras.extraresolutions"};
  21. QString detectModArchive(QString path, QString modName)
  22. {
  23. auto files = ZipArchive::listFiles(qstringToPath(path));
  24. QString modDirName;
  25. for(int folderLevel : {0, 1}) //search in subfolder if there is no mod.json in the root
  26. {
  27. for(auto file : files)
  28. {
  29. QString filename = QString::fromUtf8(file.c_str());
  30. modDirName = filename.section('/', 0, folderLevel);
  31. if(filename == modDirName + "/mod.json")
  32. {
  33. return modDirName;
  34. }
  35. }
  36. }
  37. return "";
  38. }
  39. }
  40. CModManager::CModManager(CModList * modList)
  41. : modList(modList)
  42. {
  43. loadMods();
  44. loadModSettings();
  45. }
  46. QString CModManager::settingsPath()
  47. {
  48. return pathToQString(VCMIDirs::get().userConfigPath() / "modSettings.json");
  49. }
  50. void CModManager::loadModSettings()
  51. {
  52. modSettings = JsonUtils::JsonFromFile(settingsPath()).toMap();
  53. modList->setModSettings(modSettings["activeMods"]);
  54. }
  55. void CModManager::resetRepositories()
  56. {
  57. modList->resetRepositories();
  58. }
  59. void CModManager::loadRepository(QVariantMap repomap)
  60. {
  61. modList->addRepository(repomap);
  62. }
  63. void CModManager::loadMods()
  64. {
  65. CModHandler handler;
  66. handler.loadMods();
  67. auto installedMods = handler.getAllMods();
  68. localMods.clear();
  69. for(auto modname : installedMods)
  70. {
  71. ResourceID resID(CModInfo::getModFile(modname));
  72. if(CResourceHandler::get()->existsResource(resID))
  73. {
  74. boost::filesystem::path name = *CResourceHandler::get()->getResourceName(resID);
  75. auto mod = JsonUtils::JsonFromFile(pathToQString(name));
  76. if(!name.is_absolute())
  77. {
  78. auto json = JsonUtils::toJson(mod);
  79. json["storedLocaly"].Bool() = true;
  80. mod = JsonUtils::toVariant(json);
  81. }
  82. localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
  83. }
  84. }
  85. modList->setLocalModList(localMods);
  86. }
  87. bool CModManager::addError(QString modname, QString message)
  88. {
  89. recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
  90. return false;
  91. }
  92. QStringList CModManager::getErrors()
  93. {
  94. QStringList ret = recentErrors;
  95. recentErrors.clear();
  96. return ret;
  97. }
  98. bool CModManager::installMod(QString modname, QString archivePath)
  99. {
  100. return canInstallMod(modname) && doInstallMod(modname, archivePath);
  101. }
  102. bool CModManager::uninstallMod(QString modname)
  103. {
  104. return canUninstallMod(modname) && doUninstallMod(modname);
  105. }
  106. bool CModManager::enableMod(QString modname)
  107. {
  108. return canEnableMod(modname) && doEnableMod(modname, true);
  109. }
  110. bool CModManager::disableMod(QString modname)
  111. {
  112. return canDisableMod(modname) && doEnableMod(modname, false);
  113. }
  114. bool CModManager::canInstallMod(QString modname)
  115. {
  116. auto mod = modList->getMod(modname);
  117. if(mod.getName().contains('.'))
  118. return addError(modname, "Can not install submod");
  119. if(mod.isInstalled())
  120. return addError(modname, "Mod is already installed");
  121. if(!mod.isAvailable())
  122. return addError(modname, "Mod is not available");
  123. return true;
  124. }
  125. bool CModManager::canUninstallMod(QString modname)
  126. {
  127. auto mod = modList->getMod(modname);
  128. if(mod.getName().contains('.'))
  129. return addError(modname, "Can not uninstall submod");
  130. if(!mod.isInstalled())
  131. return addError(modname, "Mod is not installed");
  132. if(mod.isEnabled())
  133. return addError(modname, "Mod must be disabled first");
  134. return true;
  135. }
  136. bool CModManager::canEnableMod(QString modname)
  137. {
  138. auto mod = modList->getMod(modname);
  139. if(mod.isEnabled())
  140. return addError(modname, "Mod is already enabled");
  141. if(!mod.isInstalled())
  142. return addError(modname, "Mod must be installed first");
  143. //check for compatibility
  144. if(!mod.isCompatible())
  145. return addError(modname, "Mod is not compatible, please update VCMI and checkout latest mod revisions");
  146. for(auto modEntry : mod.getValue("depends").toStringList())
  147. {
  148. if(!modList->hasMod(modEntry)) // required mod is not available
  149. return addError(modname, QString("Required mod %1 is missing").arg(modEntry));
  150. if(!modList->getMod(modEntry).isEnabled())
  151. return addError(modname, QString("Required mod %1 is not enabled").arg(modEntry));
  152. }
  153. for(QString modEntry : modList->getModList())
  154. {
  155. auto mod = modList->getMod(modEntry);
  156. // "reverse conflict" - enabled mod has this one as conflict
  157. if(mod.isEnabled() && mod.getValue("conflicts").toStringList().contains(modname))
  158. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  159. }
  160. for(auto modEntry : mod.getValue("conflicts").toStringList())
  161. {
  162. // check if conflicting mod installed and enabled
  163. if(modList->hasMod(modEntry) && modList->getMod(modEntry).isEnabled())
  164. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  165. }
  166. return true;
  167. }
  168. bool CModManager::canDisableMod(QString modname)
  169. {
  170. auto mod = modList->getMod(modname);
  171. if(mod.isDisabled())
  172. return addError(modname, "Mod is already disabled");
  173. if(!mod.isInstalled())
  174. return addError(modname, "Mod must be installed first");
  175. for(QString modEntry : modList->getModList())
  176. {
  177. auto current = modList->getMod(modEntry);
  178. if(current.getValue("depends").toStringList().contains(modname) && current.isEnabled())
  179. return addError(modname, QString("This mod is needed to run %1").arg(modEntry));
  180. }
  181. return true;
  182. }
  183. bool CModManager::isExtraResolutionsModEnabled() const
  184. {
  185. return modList->hasMod(extraResolutionsMod) && modList->getMod(extraResolutionsMod).isEnabled();
  186. }
  187. static QVariant writeValue(QString path, QVariantMap input, QVariant value)
  188. {
  189. if(path.size() > 1)
  190. {
  191. QString entryName = path.section('/', 0, 1);
  192. QString remainder = "/" + path.section('/', 2, -1);
  193. entryName.remove(0, 1);
  194. input.insert(entryName, writeValue(remainder, input.value(entryName).toMap(), value));
  195. return input;
  196. }
  197. else
  198. {
  199. return value;
  200. }
  201. }
  202. bool CModManager::doEnableMod(QString mod, bool on)
  203. {
  204. QString path = mod;
  205. path = "/activeMods/" + path.replace(".", "/mods/") + "/active";
  206. modSettings = writeValue(path, modSettings, QVariant(on)).toMap();
  207. modList->setModSettings(modSettings["activeMods"]);
  208. modList->modChanged(mod);
  209. if(mod == extraResolutionsMod)
  210. sendExtraResolutionsEnabledChanged(on);
  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. if(modname == extraResolutionsMod)
  242. sendExtraResolutionsEnabledChanged(true);
  243. return true;
  244. }
  245. bool CModManager::doUninstallMod(QString modname)
  246. {
  247. ResourceID resID(std::string("Mods/") + modname.toStdString(), EResType::DIRECTORY);
  248. // Get location of the mod, in case-insensitive way
  249. QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));
  250. if(!QDir(modDir).exists())
  251. return addError(modname, "Data with this mod was not found");
  252. QDir modFullDir(modDir);
  253. if(!removeModDir(modDir))
  254. return addError(modname, "Mod is located in protected directory, please remove it manually:\n" + modFullDir.absolutePath());
  255. CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &){ return true; });
  256. loadMods();
  257. modList->reloadRepositories();
  258. if(modname == extraResolutionsMod)
  259. sendExtraResolutionsEnabledChanged(false);
  260. return true;
  261. }
  262. bool CModManager::removeModDir(QString path)
  263. {
  264. // issues 2673 and 2680 its why you do not recursively remove without sanity check
  265. QDir checkDir(path);
  266. QDir dir(path);
  267. if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
  268. return false;
  269. #ifndef VCMI_IOS //ios applications are stored in the isolated container
  270. if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
  271. return false;
  272. if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
  273. return false;
  274. #endif
  275. if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
  276. return false;
  277. return dir.removeRecursively();
  278. }
  279. void CModManager::sendExtraResolutionsEnabledChanged(bool enabled)
  280. {
  281. emit extraResolutionsEnabledChanged(enabled);
  282. }