cmodmanager.cpp 8.7 KB

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