cmodmanager.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "StdInc.h"
  2. #include "cmodmanager.h"
  3. #include "../../lib/VCMIDirs.h"
  4. #include "../../lib/filesystem/Filesystem.h"
  5. #include "../../lib/filesystem/CZipLoader.h"
  6. #include "../jsonutils.h"
  7. #include "../launcherdirs.h"
  8. static QString detectModArchive(QString path, QString modName)
  9. {
  10. auto files = ZipArchive::listFiles(path.toUtf8().data());
  11. QString modDirName;
  12. for (auto file : files)
  13. {
  14. QString filename = QString::fromUtf8(file.c_str());
  15. if (filename.toLower().startsWith(modName))
  16. {
  17. // archive must contain mod.json file
  18. if (filename.toLower() == modName + "/mod.json")
  19. modDirName = filename.section('/', 0, 0);
  20. }
  21. else // all files must be in <modname> directory
  22. return "";
  23. }
  24. return modDirName;
  25. }
  26. CModManager::CModManager(CModList * modList):
  27. modList(modList)
  28. {
  29. loadMods();
  30. loadModSettings();
  31. }
  32. QString CModManager::settingsPath()
  33. {
  34. return QString::fromUtf8(VCMIDirs::get().userConfigPath().c_str()) + "/modSettings.json";
  35. }
  36. void CModManager::loadModSettings()
  37. {
  38. modSettings = JsonUtils::JsonFromFile(settingsPath()).toMap();
  39. modList->setModSettings(modSettings["activeMods"]);
  40. }
  41. void CModManager::resetRepositories()
  42. {
  43. modList->resetRepositories();
  44. }
  45. void CModManager::loadRepository(QString file)
  46. {
  47. modList->addRepository(JsonUtils::JsonFromFile(file).toMap());
  48. }
  49. void CModManager::loadMods()
  50. {
  51. auto installedMods = CResourceHandler::getAvailableMods();
  52. for (auto modname : installedMods)
  53. {
  54. ResourceID resID("Mods/" + modname + "/mod.json");
  55. if (CResourceHandler::get()->existsResource(resID))
  56. {
  57. std::string name = *CResourceHandler::get()->getResourceName(resID);
  58. auto mod = JsonUtils::JsonFromFile(QString::fromUtf8(name.c_str()));
  59. localMods.insert(QString::fromUtf8(modname.c_str()).toLower(), mod);
  60. }
  61. }
  62. modList->setLocalModList(localMods);
  63. }
  64. bool CModManager::addError(QString modname, QString message)
  65. {
  66. recentErrors.push_back(QString("%1: %2").arg(modname).arg(message));
  67. return false;
  68. }
  69. QStringList CModManager::getErrors()
  70. {
  71. QStringList ret = recentErrors;
  72. recentErrors.clear();
  73. return ret;
  74. }
  75. bool CModManager::installMod(QString modname, QString archivePath)
  76. {
  77. return canInstallMod(modname) && doInstallMod(modname, archivePath);
  78. }
  79. bool CModManager::uninstallMod(QString modname)
  80. {
  81. return canUninstallMod(modname) && doUninstallMod(modname);
  82. }
  83. bool CModManager::enableMod(QString modname)
  84. {
  85. return canEnableMod(modname) && doEnableMod(modname, true);
  86. }
  87. bool CModManager::disableMod(QString modname)
  88. {
  89. return canDisableMod(modname) && doEnableMod(modname, false);
  90. }
  91. bool CModManager::canInstallMod(QString modname)
  92. {
  93. auto mod = modList->getMod(modname);
  94. if (mod.isInstalled())
  95. return addError(modname, "Mod is already installed");
  96. if (!mod.isAvailable())
  97. return addError(modname, "Mod is not available");
  98. return true;
  99. }
  100. bool CModManager::canUninstallMod(QString modname)
  101. {
  102. auto mod = modList->getMod(modname);
  103. if (!mod.isInstalled())
  104. return addError(modname, "Mod is not installed");
  105. if (mod.isEnabled())
  106. return addError(modname, "Mod must be disabled first");
  107. return true;
  108. }
  109. bool CModManager::canEnableMod(QString modname)
  110. {
  111. auto mod = modList->getMod(modname);
  112. if (mod.isEnabled())
  113. return addError(modname, "Mod is already enabled");
  114. if (!mod.isInstalled())
  115. return addError(modname, "Mod must be installed first");
  116. for (auto modEntry : mod.getValue("depends").toStringList())
  117. {
  118. if (!modList->hasMod(modEntry)) // required mod is not available
  119. return addError(modname, QString("Required mod %1 is missing").arg(modEntry));
  120. if (!modList->getMod(modEntry).isEnabled())
  121. return addError(modname, QString("Required mod %1 is not enabled").arg(modEntry));
  122. }
  123. for (QString modEntry : modList->getModList())
  124. {
  125. auto mod = modList->getMod(modEntry);
  126. // "reverse conflict" - enabled mod has this one as conflict
  127. if (mod.isEnabled() && mod.getValue("conflicts").toStringList().contains(modname))
  128. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  129. }
  130. for (auto modEntry : mod.getValue("conflicts").toStringList())
  131. {
  132. if (modList->hasMod(modEntry) &&
  133. modList->getMod(modEntry).isEnabled()) // conflicting mod installed and enabled
  134. return addError(modname, QString("This mod conflicts with %1").arg(modEntry));
  135. }
  136. return true;
  137. }
  138. bool CModManager::canDisableMod(QString modname)
  139. {
  140. auto mod = modList->getMod(modname);
  141. if (mod.isDisabled())
  142. return addError(modname, "Mod is already disabled");
  143. if (!mod.isInstalled())
  144. return addError(modname, "Mod must be installed first");
  145. for (QString modEntry : modList->getModList())
  146. {
  147. auto current = modList->getMod(modEntry);
  148. if (current.getValue("depends").toStringList().contains(modname) &&
  149. current.isEnabled())
  150. return addError(modname, QString("This mod is needed to run %1").arg(modEntry));
  151. }
  152. return true;
  153. }
  154. bool CModManager::doEnableMod(QString mod, bool on)
  155. {
  156. QVariant value(on);
  157. QVariantMap list = modSettings["activeMods"].toMap();
  158. QVariantMap modData = list[mod].toMap();
  159. modData.insert("active", value);
  160. list.insert(mod, modData);
  161. modSettings.insert("activeMods", list);
  162. modList->setModSettings(modSettings["activeMods"]);
  163. JsonUtils::JsonToFile(settingsPath(), modSettings);
  164. return true;
  165. }
  166. bool CModManager::doInstallMod(QString modname, QString archivePath)
  167. {
  168. QString destDir = CLauncherDirs::get().modsPath() + "/";
  169. if (!QFile(archivePath).exists())
  170. return addError(modname, "Mod archive is missing");
  171. if (QDir(destDir + modname).exists()) // FIXME: recheck wog/vcmi data behavior - they have bits of data in our trunk
  172. return addError(modname, "Mod with such name is already installed");
  173. if (localMods.contains(modname))
  174. return addError(modname, "Mod with such name is already installed");
  175. QString modDirName = detectModArchive(archivePath, modname);
  176. if (!modDirName.size())
  177. return addError(modname, "Mod archive is invalid or corrupted");
  178. if (!ZipArchive::extract(archivePath.toUtf8().data(), destDir.toUtf8().data()))
  179. {
  180. QDir(destDir + modDirName).removeRecursively();
  181. return addError(modname, "Failed to extract mod data");
  182. }
  183. QVariantMap json = JsonUtils::JsonFromFile(destDir + modDirName + "/mod.json").toMap();
  184. localMods.insert(modname, json);
  185. modList->setLocalModList(localMods);
  186. return true;
  187. }
  188. bool CModManager::doUninstallMod(QString modname)
  189. {
  190. ResourceID resID(std::string("Mods/") + modname.toUtf8().data(), EResType::DIRECTORY);
  191. // Get location of the mod, in case-insensitive way
  192. QString modDir = QString::fromUtf8(CResourceHandler::get()->getResourceName(resID)->c_str());
  193. if (!QDir(modDir).exists())
  194. return addError(modname, "Data with this mod was not found");
  195. if (!localMods.contains(modname))
  196. return addError(modname, "Data with this mod was not found");
  197. if (!QDir(modDir).removeRecursively())
  198. return addError(modname, "Failed to delete mod data");
  199. localMods.remove(modname);
  200. modList->setLocalModList(localMods);
  201. return true;
  202. }