modstatecontroller.cpp 7.1 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 "../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::enableMods(QStringList modlist)
  84. {
  85. for (const auto & modname : modlist)
  86. if (!canEnableMod(modname))
  87. return false;
  88. modList->doEnableMods(modlist);
  89. return true;
  90. }
  91. bool ModStateController::disableMod(QString modname)
  92. {
  93. if (!canDisableMod(modname))
  94. return false;
  95. modList->doDisableMod(modname);
  96. return true;
  97. }
  98. bool ModStateController::canInstallMod(QString modname)
  99. {
  100. auto mod = modList->getMod(modname);
  101. if(mod.isSubmod())
  102. return addError(modname, tr("Can not install submod"));
  103. if(mod.isInstalled())
  104. return addError(modname, tr("Mod is already installed"));
  105. return true;
  106. }
  107. bool ModStateController::canUninstallMod(QString modname)
  108. {
  109. auto mod = modList->getMod(modname);
  110. if(mod.isSubmod())
  111. return addError(modname, tr("Can not uninstall submod"));
  112. if(!mod.isInstalled())
  113. return addError(modname, tr("Mod is not installed"));
  114. return true;
  115. }
  116. bool ModStateController::canEnableMod(QString modname)
  117. {
  118. auto mod = modList->getMod(modname);
  119. if(modList->isModEnabled(modname))
  120. return addError(modname, tr("Mod is already enabled"));
  121. if(!mod.isInstalled())
  122. return addError(modname, tr("Mod must be installed first"));
  123. //check for compatibility
  124. if(!mod.isCompatible())
  125. return addError(modname, tr("Mod is not compatible, please update VCMI and checkout latest mod revisions"));
  126. for(const auto & modEntry : mod.getDependencies())
  127. {
  128. if(!modList->isModExists(modEntry)) // required mod is not available
  129. return addError(modname, tr("Required mod %1 is missing").arg(modEntry));
  130. }
  131. return true;
  132. }
  133. bool ModStateController::canDisableMod(QString modname)
  134. {
  135. auto mod = modList->getMod(modname);
  136. if(!modList->isModEnabled(modname))
  137. return addError(modname, tr("Mod is already disabled"));
  138. if(!mod.isInstalled())
  139. return addError(modname, tr("Mod must be installed first"));
  140. return true;
  141. }
  142. bool ModStateController::doInstallMod(QString modname, QString archivePath)
  143. {
  144. const auto destDir = CLauncherDirs::modsPath() + QChar{'/'};
  145. if(!QFile(archivePath).exists())
  146. return addError(modname, tr("Mod archive is missing"));
  147. if(localMods.contains(modname))
  148. return addError(modname, tr("Mod with such name is already installed"));
  149. std::vector<std::string> filesToExtract;
  150. QString modDirName = ::detectModArchive(archivePath, modname, filesToExtract);
  151. if(!modDirName.size())
  152. return addError(modname, tr("Mod archive is invalid or corrupted"));
  153. std::atomic<int> filesCounter = 0;
  154. auto futureExtract = std::async(std::launch::async, [&archivePath, &destDir, &filesCounter, &filesToExtract]()
  155. {
  156. const auto destDirFsPath = qstringToPath(destDir);
  157. ZipArchive archive(qstringToPath(archivePath));
  158. for(const auto & file : filesToExtract)
  159. {
  160. if (!archive.extract(destDirFsPath, file))
  161. return false;
  162. ++filesCounter;
  163. }
  164. return true;
  165. });
  166. while(futureExtract.wait_for(std::chrono::milliseconds(10)) != std::future_status::ready)
  167. {
  168. emit extractionProgress(filesCounter, filesToExtract.size());
  169. qApp->processEvents();
  170. }
  171. if(!futureExtract.get())
  172. {
  173. removeModDir(destDir + modDirName);
  174. return addError(modname, tr("Failed to extract mod data"));
  175. }
  176. //rename folder and fix the path
  177. QDir extractedDir(destDir + modDirName);
  178. auto rc = QFile::rename(destDir + modDirName, destDir + modname);
  179. if (rc)
  180. extractedDir.setPath(destDir + modname);
  181. //there are possible excessive files - remove them
  182. QString upperLevel = modDirName.section('/', 0, 0);
  183. if(upperLevel != modDirName)
  184. removeModDir(destDir + upperLevel);
  185. modList->reloadLocalState();
  186. return true;
  187. }
  188. bool ModStateController::doUninstallMod(QString modname)
  189. {
  190. ResourcePath resID(std::string("Mods/") + modname.toStdString(), EResType::DIRECTORY);
  191. // Get location of the mod, in case-insensitive way
  192. QString modDir = pathToQString(*CResourceHandler::get()->getResourceName(resID));
  193. if(!QDir(modDir).exists())
  194. return addError(modname, tr("Data with this mod was not found"));
  195. QDir modFullDir(modDir);
  196. if(!removeModDir(modDir))
  197. return addError(modname, tr("Mod is located in protected directory, please remove it manually:\n") + modFullDir.absolutePath());
  198. modList->reloadLocalState();
  199. return true;
  200. }
  201. bool ModStateController::removeModDir(QString path)
  202. {
  203. // issues 2673 and 2680 its why you do not recursively remove without sanity check
  204. QDir checkDir(path);
  205. QDir dir(path);
  206. if(!checkDir.cdUp() || QString::compare("Mods", checkDir.dirName(), Qt::CaseInsensitive))
  207. return false;
  208. #ifndef VCMI_MOBILE // ios and android applications are stored in the isolated container
  209. if(!checkDir.cdUp() || QString::compare("vcmi", checkDir.dirName(), Qt::CaseInsensitive))
  210. return false;
  211. if(!dir.absolutePath().contains("vcmi", Qt::CaseInsensitive))
  212. return false;
  213. #endif
  214. if(!dir.absolutePath().contains("Mods", Qt::CaseInsensitive))
  215. return false;
  216. return dir.removeRecursively();
  217. }