cmodmanager.cpp 8.6 KB

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