modstatecontroller.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * modstatecontroller.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 "modstatecontroller.h"
  12. #include "modstatemodel.h"
  13. #include "../../lib/VCMIDirs.h"
  14. #include "../../lib/filesystem/Filesystem.h"
  15. #include "../../lib/filesystem/CZipLoader.h"
  16. #include "../../lib/modding/CModHandler.h"
  17. #include "../../lib/modding/IdentifierStorage.h"
  18. #include "../../lib/json/JsonNode.h"
  19. #include "../vcmiqt/jsonutils.h"
  20. #include "../vcmiqt/launcherdirs.h"
  21. #include <future>
  22. namespace
  23. {
  24. QString detectModArchive(QString path, QString modName, std::vector<std::string> & filesToExtract)
  25. {
  26. try {
  27. ZipArchive archive(qstringToPath(path));
  28. filesToExtract = archive.listFiles();
  29. }
  30. catch (const std::runtime_error & e)
  31. {
  32. logGlobal->error("Failed to open zip archive. Reason: %s", e.what());
  33. return "";
  34. }
  35. QString modDirName;
  36. for(int folderLevel : {0, 1}) //search in subfolder if there is no mod.json in the root
  37. {
  38. for(const auto & file : filesToExtract)
  39. {
  40. QString filename = QString::fromUtf8(file.c_str());
  41. modDirName = filename.section('/', 0, folderLevel);
  42. if(filename == modDirName + "/mod.json")
  43. {
  44. return modDirName;
  45. }
  46. }
  47. }
  48. logGlobal->error("Failed to detect mod path in archive!");
  49. logGlobal->debug("List of file in archive:");
  50. for(const auto & file : filesToExtract)
  51. logGlobal->debug("%s", file.c_str());
  52. return "";
  53. }
  54. }
  55. ModStateController::ModStateController(std::shared_ptr<ModStateModel> modList)
  56. : modList(modList)
  57. {
  58. }
  59. ModStateController::~ModStateController() = default;
  60. void ModStateController::appendRepositories(const JsonNode & repomap)
  61. {
  62. modList->appendRepositories(repomap);
  63. }
  64. bool ModStateController::addError(QString modname, QString message)
  65. {
  66. recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
  67. return false;
  68. }
  69. QStringList ModStateController::getErrors()
  70. {
  71. QStringList ret = recentErrors;
  72. recentErrors.clear();
  73. return ret;
  74. }
  75. bool ModStateController::installMod(QString modname, QString archivePath)
  76. {
  77. return canInstallMod(modname) && doInstallMod(modname, archivePath);
  78. }
  79. bool ModStateController::uninstallMod(QString modname)
  80. {
  81. return canUninstallMod(modname) && doUninstallMod(modname);
  82. }
  83. bool ModStateController::enableMod(QString modname)
  84. {
  85. return canEnableMod(modname) && doEnableMod(modname, true);
  86. }
  87. bool ModStateController::disableMod(QString modname)
  88. {
  89. return canDisableMod(modname) && doEnableMod(modname, false);
  90. }
  91. bool ModStateController::canInstallMod(QString modname)
  92. {
  93. auto mod = modList->getMod(modname);
  94. if(mod.isSubmod())
  95. return addError(modname, tr("Can not install submod"));
  96. if(mod.isInstalled())
  97. return addError(modname, tr("Mod is already installed"));
  98. return true;
  99. }
  100. bool ModStateController::canUninstallMod(QString modname)
  101. {
  102. auto mod = modList->getMod(modname);
  103. if(mod.isSubmod())
  104. return addError(modname, tr("Can not uninstall submod"));
  105. if(!mod.isInstalled())
  106. return addError(modname, tr("Mod is not installed"));
  107. return true;
  108. }
  109. bool ModStateController::canEnableMod(QString modname)
  110. {
  111. auto mod = modList->getMod(modname);
  112. if(modList->isModEnabled(modname))
  113. return addError(modname, tr("Mod is already enabled"));
  114. if(!mod.isInstalled())
  115. return addError(modname, tr("Mod must be installed first"));
  116. //check for compatibility
  117. if(!mod.isCompatible())
  118. return addError(modname, tr("Mod is not compatible, please update VCMI and checkout latest mod revisions"));
  119. for(const auto & modEntry : mod.getDependencies())
  120. {
  121. if(!modList->isModExists(modEntry)) // required mod is not available
  122. return addError(modname, tr("Required mod %1 is missing").arg(modEntry));
  123. }
  124. return true;
  125. }
  126. bool ModStateController::canDisableMod(QString modname)
  127. {
  128. auto mod = modList->getMod(modname);
  129. if(!modList->isModEnabled(modname))
  130. return addError(modname, tr("Mod is already disabled"));
  131. if(!mod.isInstalled())
  132. return addError(modname, tr("Mod must be installed first"));
  133. return true;
  134. }
  135. bool ModStateController::doEnableMod(QString mod, bool on)
  136. {
  137. if (on)
  138. modList->doEnableMod(mod);
  139. else
  140. modList->doDisableMod(mod);
  141. return true;
  142. }
  143. bool ModStateController::doInstallMod(QString modname, QString archivePath)
  144. {
  145. const auto destDir = CLauncherDirs::modsPath() + QChar{'/'};
  146. if(!QFile(archivePath).exists())
  147. return addError(modname, tr("Mod archive is missing"));
  148. if(localMods.contains(modname))
  149. return addError(modname, tr("Mod with such name is already installed"));
  150. std::vector<std::string> filesToExtract;
  151. QString modDirName = ::detectModArchive(archivePath, modname, filesToExtract);
  152. if(!modDirName.size())
  153. return addError(modname, tr("Mod archive is invalid or corrupted"));
  154. std::atomic<int> filesCounter = 0;
  155. auto futureExtract = std::async(std::launch::async, [&archivePath, &destDir, &filesCounter, &filesToExtract]()
  156. {
  157. const auto destDirFsPath = qstringToPath(destDir);
  158. ZipArchive archive(qstringToPath(archivePath));
  159. for(const auto & file : filesToExtract)
  160. {
  161. if (!archive.extract(destDirFsPath, file))
  162. return false;
  163. ++filesCounter;
  164. }
  165. return true;
  166. });
  167. while(futureExtract.wait_for(std::chrono::milliseconds(10)) != std::future_status::ready)
  168. {
  169. emit extractionProgress(filesCounter, filesToExtract.size());
  170. qApp->processEvents();
  171. }
  172. if(!futureExtract.get())
  173. {
  174. removeModDir(destDir + modDirName);
  175. return addError(modname, tr("Failed to extract mod data"));
  176. }
  177. //rename folder and fix the path
  178. QDir extractedDir(destDir + modDirName);
  179. auto rc = QFile::rename(destDir + modDirName, destDir + modname);
  180. if (rc)
  181. extractedDir.setPath(destDir + modname);
  182. //there are possible excessive files - remove them
  183. QString upperLevel = modDirName.section('/', 0, 0);
  184. if(upperLevel != modDirName)
  185. removeModDir(destDir + upperLevel);
  186. CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &) { return true; });
  187. //modList->reloadLocalMods();
  188. return true;
  189. }
  190. bool ModStateController::doUninstallMod(QString modname)
  191. {
  192. ResourcePath resID(std::string("Mods/") + modname.toStdString(), EResType::DIRECTORY);
  193. // Get location of the mod, in case-insensitive way
  194. QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));
  195. if(!QDir(modDir).exists())
  196. return addError(modname, tr("Data with this mod was not found"));
  197. QDir modFullDir(modDir);
  198. if(!removeModDir(modDir))
  199. return addError(modname, tr("Mod is located in protected directory, please remove it manually:\n") + modFullDir.absolutePath());
  200. CResourceHandler::get("initial")->updateFilteredFiles([](const std::string &){ return true; });
  201. modList->reloadLocalState();
  202. return true;
  203. }
  204. bool ModStateController::removeModDir(QString path)
  205. {
  206. // issues 2673 and 2680 its why you do not recursively remove without sanity check
  207. QDir checkDir(path);
  208. QDir dir(path);
  209. if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
  210. return false;
  211. #ifndef VCMI_MOBILE // ios and android applications are stored in the isolated container
  212. if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
  213. return false;
  214. if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
  215. return false;
  216. #endif
  217. if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
  218. return false;
  219. return dir.removeRecursively();
  220. }