cmodmanager.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. return true;
  133. }
  134. bool CModManager::canEnableMod(QString modname)
  135. {
  136. auto mod = modList->getMod(modname);
  137. if(mod.isEnabled())
  138. return addError(modname, "Mod is already enabled");
  139. if(!mod.isInstalled())
  140. return addError(modname, "Mod must be installed first");
  141. //check for compatibility
  142. if(!mod.isCompatible())
  143. return addError(modname, "Mod is not compatible, please update VCMI and checkout latest mod revisions");
  144. for(auto modEntry : mod.getValue("depends").toStringList())
  145. {
  146. if(!modList->hasMod(modEntry)) // required mod is not available
  147. return addError(modname, QString("Required mod %1 is missing").arg(modEntry));
  148. if(!modList->getMod(modEntry).isEnabled())
  149. return addError(modname, QString("Required mod %1 is not enabled").arg(modEntry));
  150. }
  151. for(QString modEntry : modList->getModList())
  152. {
  153. auto mod = modList->getMod(modEntry);
  154. // "reverse conflict" - enabled mod has this one as conflict
  155. if(mod.isEnabled() && mod.getValue("conflicts").toStringList().contains(modname))
  156. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  157. }
  158. for(auto modEntry : mod.getValue("conflicts").toStringList())
  159. {
  160. // check if conflicting mod installed and enabled
  161. if(modList->hasMod(modEntry) && modList->getMod(modEntry).isEnabled())
  162. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  163. }
  164. return true;
  165. }
  166. bool CModManager::canDisableMod(QString modname)
  167. {
  168. auto mod = modList->getMod(modname);
  169. if(mod.isDisabled())
  170. return addError(modname, "Mod is already disabled");
  171. if(!mod.isInstalled())
  172. return addError(modname, "Mod must be installed first");
  173. for(QString modEntry : modList->getModList())
  174. {
  175. auto current = modList->getMod(modEntry);
  176. if(current.getValue("depends").toStringList().contains(modname) && current.isEnabled())
  177. return addError(modname, QString("This mod is needed to run %1").arg(modEntry));
  178. }
  179. return true;
  180. }
  181. bool CModManager::isExtraResolutionsModEnabled() const
  182. {
  183. return modList->hasMod(extraResolutionsMod) && modList->getMod(extraResolutionsMod).isEnabled();
  184. }
  185. static QVariant writeValue(QString path, QVariantMap input, QVariant value)
  186. {
  187. if(path.size() > 1)
  188. {
  189. QString entryName = path.section('/', 0, 1);
  190. QString remainder = "/" + path.section('/', 2, -1);
  191. entryName.remove(0, 1);
  192. input.insert(entryName, writeValue(remainder, input.value(entryName).toMap(), value));
  193. return input;
  194. }
  195. else
  196. {
  197. return value;
  198. }
  199. }
  200. bool CModManager::doEnableMod(QString mod, bool on)
  201. {
  202. QString path = mod;
  203. path = "/activeMods/" + path.replace(".", "/mods/") + "/active";
  204. modSettings = writeValue(path, modSettings, QVariant(on)).toMap();
  205. modList->setModSettings(modSettings["activeMods"]);
  206. modList->modChanged(mod);
  207. if(mod == extraResolutionsMod)
  208. sendExtraResolutionsEnabledChanged(on);
  209. JsonUtils::JsonToFile(settingsPath(), modSettings);
  210. return true;
  211. }
  212. bool CModManager::doInstallMod(QString modname, QString archivePath)
  213. {
  214. QString destDir = CLauncherDirs::get().modsPath() + "/";
  215. if(!QFile(archivePath).exists())
  216. return addError(modname, "Mod archive is missing");
  217. if(localMods.contains(modname))
  218. return addError(modname, "Mod with such name is already installed");
  219. QString modDirName = ::detectModArchive(archivePath, modname);
  220. if(!modDirName.size())
  221. return addError(modname, "Mod archive is invalid or corrupted");
  222. if(!ZipArchive::extract(qstringToPath(archivePath), qstringToPath(destDir)))
  223. {
  224. removeModDir(destDir + modDirName);
  225. return addError(modname, "Failed to extract mod data");
  226. }
  227. //rename folder and fix the path
  228. QDir extractedDir(destDir + modDirName);
  229. auto rc = QFile::rename(destDir + modDirName, destDir + modname);
  230. if (rc)
  231. extractedDir.setPath(destDir + modname);
  232. //there are possible excessive files - remove them
  233. QString upperLevel = modDirName.section('/', 0, 0);
  234. if(upperLevel != modDirName)
  235. removeModDir(destDir + upperLevel);
  236. CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &) { return true; });
  237. loadMods();
  238. modList->reloadRepositories();
  239. if(modname == extraResolutionsMod)
  240. sendExtraResolutionsEnabledChanged(true);
  241. return true;
  242. }
  243. bool CModManager::doUninstallMod(QString modname)
  244. {
  245. ResourceID 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. if(modname == extraResolutionsMod)
  257. sendExtraResolutionsEnabledChanged(false);
  258. return true;
  259. }
  260. bool CModManager::removeModDir(QString path)
  261. {
  262. // issues 2673 and 2680 its why you do not recursively remove without sanity check
  263. QDir checkDir(path);
  264. QDir dir(path);
  265. if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
  266. return false;
  267. #ifndef VCMI_IOS //ios applications are stored in the isolated container
  268. if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
  269. return false;
  270. if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
  271. return false;
  272. #endif
  273. if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
  274. return false;
  275. return dir.removeRecursively();
  276. }
  277. void CModManager::sendExtraResolutionsEnabledChanged(bool enabled)
  278. {
  279. emit extraResolutionsEnabledChanged(enabled);
  280. }