cmodmanager.cpp 8.9 KB

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