cmodmanager.cpp 8.0 KB

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