cmodmanager.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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(auto file : files)
  23. {
  24. QString filename = QString::fromUtf8(file.c_str());
  25. if(filename.toLower().startsWith(modName))
  26. {
  27. // archive must contain mod.json file
  28. if(filename.toLower() == modName + "/mod.json")
  29. modDirName = filename.section('/', 0, 0);
  30. }
  31. else // all files must be in <modname> directory
  32. return "";
  33. }
  34. return modDirName;
  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. for(auto modname : installedMods)
  65. {
  66. ResourceID resID(CModInfo::getModFile(modname));
  67. if(CResourceHandler::get()->existsResource(resID))
  68. {
  69. boost::filesystem::path name = *CResourceHandler::get()->getResourceName(resID);
  70. auto mod = JsonUtils::JsonFromFile(pathToQString(name));
  71. localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
  72. }
  73. }
  74. modList->setLocalModList(localMods);
  75. }
  76. bool CModManager::addError(QString modname, QString message)
  77. {
  78. recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
  79. return false;
  80. }
  81. QStringList CModManager::getErrors()
  82. {
  83. QStringList ret = recentErrors;
  84. recentErrors.clear();
  85. return ret;
  86. }
  87. bool CModManager::installMod(QString modname, QString archivePath)
  88. {
  89. return canInstallMod(modname) && doInstallMod(modname, archivePath);
  90. }
  91. bool CModManager::uninstallMod(QString modname)
  92. {
  93. return canUninstallMod(modname) && doUninstallMod(modname);
  94. }
  95. bool CModManager::enableMod(QString modname)
  96. {
  97. return canEnableMod(modname) && doEnableMod(modname, true);
  98. }
  99. bool CModManager::disableMod(QString modname)
  100. {
  101. return canDisableMod(modname) && doEnableMod(modname, false);
  102. }
  103. bool CModManager::canInstallMod(QString modname)
  104. {
  105. auto mod = modList->getMod(modname);
  106. if(mod.getName().contains('.'))
  107. return addError(modname, "Can not install submod");
  108. if(mod.isInstalled())
  109. return addError(modname, "Mod is already installed");
  110. if(!mod.isAvailable())
  111. return addError(modname, "Mod is not available");
  112. return true;
  113. }
  114. bool CModManager::canUninstallMod(QString modname)
  115. {
  116. auto mod = modList->getMod(modname);
  117. if(mod.getName().contains('.'))
  118. return addError(modname, "Can not uninstall submod");
  119. if(!mod.isInstalled())
  120. return addError(modname, "Mod is not installed");
  121. if(mod.isEnabled())
  122. return addError(modname, "Mod must be disabled first");
  123. return true;
  124. }
  125. bool CModManager::canEnableMod(QString modname)
  126. {
  127. auto mod = modList->getMod(modname);
  128. if(mod.isEnabled())
  129. return addError(modname, "Mod is already enabled");
  130. if(!mod.isInstalled())
  131. return addError(modname, "Mod must be installed first");
  132. for(auto modEntry : mod.getValue("depends").toStringList())
  133. {
  134. if(!modList->hasMod(modEntry)) // required mod is not available
  135. return addError(modname, QString("Required mod %1 is missing").arg(modEntry));
  136. if(!modList->getMod(modEntry).isEnabled())
  137. return addError(modname, QString("Required mod %1 is not enabled").arg(modEntry));
  138. }
  139. for(QString modEntry : modList->getModList())
  140. {
  141. auto mod = modList->getMod(modEntry);
  142. // "reverse conflict" - enabled mod has this one as conflict
  143. if(mod.isEnabled() && mod.getValue("conflicts").toStringList().contains(modname))
  144. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  145. }
  146. for(auto modEntry : mod.getValue("conflicts").toStringList())
  147. {
  148. if(modList->hasMod(modEntry) &&
  149. modList->getMod(modEntry).isEnabled()) // conflicting mod installed and enabled
  150. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  151. }
  152. return true;
  153. }
  154. bool CModManager::canDisableMod(QString modname)
  155. {
  156. auto mod = modList->getMod(modname);
  157. if(mod.isDisabled())
  158. return addError(modname, "Mod is already disabled");
  159. if(!mod.isInstalled())
  160. return addError(modname, "Mod must be installed first");
  161. for(QString modEntry : modList->getModList())
  162. {
  163. auto current = modList->getMod(modEntry);
  164. if(current.getValue("depends").toStringList().contains(modname) &&
  165. current.isEnabled())
  166. return addError(modname, QString("This mod is needed to run %1").arg(modEntry));
  167. }
  168. return true;
  169. }
  170. static QVariant writeValue(QString path, QVariantMap input, QVariant value)
  171. {
  172. if(path.size() > 1)
  173. {
  174. QString entryName = path.section('/', 0, 1);
  175. QString remainder = "/" + path.section('/', 2, -1);
  176. entryName.remove(0, 1);
  177. input.insert(entryName, writeValue(remainder, input.value(entryName).toMap(), value));
  178. return input;
  179. }
  180. else
  181. {
  182. return value;
  183. }
  184. }
  185. bool CModManager::doEnableMod(QString mod, bool on)
  186. {
  187. QString path = mod;
  188. path = "/activeMods/" + path.replace(".", "/mods/") + "/active";
  189. modSettings = writeValue(path, modSettings, QVariant(on)).toMap();
  190. modList->setModSettings(modSettings["activeMods"]);
  191. modList->modChanged(mod);
  192. JsonUtils::JsonToFile(settingsPath(), modSettings);
  193. return true;
  194. }
  195. bool CModManager::doInstallMod(QString modname, QString archivePath)
  196. {
  197. QString destDir = CLauncherDirs::get().modsPath() + "/";
  198. if(!QFile(archivePath).exists())
  199. return addError(modname, "Mod archive is missing");
  200. if(localMods.contains(modname))
  201. return addError(modname, "Mod with such name is already installed");
  202. QString modDirName = detectModArchive(archivePath, modname);
  203. if(!modDirName.size())
  204. return addError(modname, "Mod archive is invalid or corrupted");
  205. if(!ZipArchive::extract(qstringToPath(archivePath), qstringToPath(destDir)))
  206. {
  207. removeModDir(destDir + modDirName);
  208. return addError(modname, "Failed to extract mod data");
  209. }
  210. QVariantMap json = JsonUtils::JsonFromFile(destDir + modDirName + "/mod.json").toMap();
  211. localMods.insert(modname, json);
  212. modList->setLocalModList(localMods);
  213. modList->modChanged(modname);
  214. return true;
  215. }
  216. bool CModManager::doUninstallMod(QString modname)
  217. {
  218. ResourceID resID(std::string("Mods/") + modname.toUtf8().data(), EResType::DIRECTORY);
  219. // Get location of the mod, in case-insensitive way
  220. QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));
  221. if(!QDir(modDir).exists())
  222. return addError(modname, "Data with this mod was not found");
  223. if(!localMods.contains(modname))
  224. return addError(modname, "Data with this mod was not found");
  225. if(!removeModDir(modDir))
  226. return addError(modname, "Failed to delete mod data");
  227. localMods.remove(modname);
  228. modList->setLocalModList(localMods);
  229. modList->modChanged(modname);
  230. return true;
  231. }
  232. bool CModManager::removeModDir(QString path)
  233. {
  234. // issues 2673 and 2680 its why you do not recursively remove without sanity check
  235. QDir checkDir(path);
  236. if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
  237. return false;
  238. if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
  239. return false;
  240. QDir dir(path);
  241. if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
  242. return false;
  243. if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
  244. return false;
  245. return dir.removeRecursively();
  246. }