cmodmanager.cpp 8.6 KB

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