modstatecontroller.cpp 7.3 KB

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