firstlaunch_moc.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /*
  2. * firstlaunch_moc.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 "firstlaunch_moc.h"
  12. #include "ui_firstlaunch_moc.h"
  13. #include "mainwindow_moc.h"
  14. #include "modManager/cmodlistview_moc.h"
  15. #include "../../lib/CConfigHandler.h"
  16. #include "../../lib/texts/CGeneralTextHandler.h"
  17. #include "../../lib/texts/Languages.h"
  18. #include "../../lib/VCMIDirs.h"
  19. #include "../../lib/filesystem/Filesystem.h"
  20. #include "../../vcmiqt/MessageBox.h"
  21. #include "../helper.h"
  22. #include "../languages.h"
  23. #include "../innoextract.h"
  24. #ifdef VCMI_IOS
  25. #include "ios/selectdirectory.h"
  26. #include "iOS_utils.h"
  27. #elif defined(VCMI_ANDROID)
  28. #include <QAndroidJniObject>
  29. #include <QtAndroid>
  30. static FirstLaunchView * thiz;
  31. extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_heroesDataUpdate(JNIEnv * env, jclass cls)
  32. {
  33. thiz->heroesDataUpdate();
  34. }
  35. #endif
  36. FirstLaunchView::FirstLaunchView(QWidget * parent)
  37. : QWidget(parent)
  38. , ui(new Ui::FirstLaunchView)
  39. {
  40. ui->setupUi(this);
  41. enterSetup();
  42. activateTabLanguage();
  43. ui->lineEditDataSystem->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().dataPaths().front())));
  44. ui->lineEditDataUser->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().userDataPath())));
  45. Helper::enableScrollBySwiping(ui->listWidgetLanguage);
  46. #ifdef VCMI_MOBILE
  47. // This directory is not accessible to players without rooting of their device
  48. ui->lineEditDataSystem->hide();
  49. #endif
  50. #ifndef ENABLE_INNOEXTRACT
  51. ui->pushButtonGogInstall->hide();
  52. ui->labelDataGogTitle->hide();
  53. ui->labelDataGogDescr->hide();
  54. #endif
  55. }
  56. void FirstLaunchView::on_buttonTabLanguage_clicked()
  57. {
  58. activateTabLanguage();
  59. }
  60. void FirstLaunchView::on_buttonTabHeroesData_clicked()
  61. {
  62. activateTabHeroesData();
  63. }
  64. void FirstLaunchView::on_buttonTabModPreset_clicked()
  65. {
  66. activateTabModPreset();
  67. }
  68. void FirstLaunchView::on_listWidgetLanguage_currentRowChanged(int currentRow)
  69. {
  70. languageSelected(ui->listWidgetLanguage->item(currentRow)->data(Qt::UserRole).toString());
  71. }
  72. void FirstLaunchView::changeEvent(QEvent * event)
  73. {
  74. if(event->type() == QEvent::LanguageChange)
  75. {
  76. ui->retranslateUi(this);
  77. Languages::fillLanguages(ui->listWidgetLanguage, false);
  78. }
  79. QWidget::changeEvent(event);
  80. }
  81. void FirstLaunchView::on_pushButtonLanguageNext_clicked()
  82. {
  83. activateTabHeroesData();
  84. }
  85. void FirstLaunchView::on_pushButtonDataNext_clicked()
  86. {
  87. activateTabModPreset();
  88. }
  89. void FirstLaunchView::on_pushButtonDataBack_clicked()
  90. {
  91. activateTabLanguage();
  92. }
  93. void FirstLaunchView::on_pushButtonDataSearch_clicked()
  94. {
  95. heroesDataUpdate();
  96. }
  97. void FirstLaunchView::on_pushButtonDataCopy_clicked()
  98. {
  99. #ifdef VCMI_ANDROID
  100. thiz = this;
  101. QtAndroid::androidActivity().callMethod<void>("copyHeroesData");
  102. #else
  103. // iOS can't display modal dialogs when called directly on button press
  104. // https://bugreports.qt.io/browse/QTBUG-98651
  105. MessageBoxCustom::showDialog(this, [this]{ copyHeroesData(); });
  106. #endif
  107. }
  108. void FirstLaunchView::on_pushButtonGogInstall_clicked()
  109. {
  110. // iOS can't display modal dialogs when called directly on button press
  111. // https://bugreports.qt.io/browse/QTBUG-98651
  112. MessageBoxCustom::showDialog(this, [this]{extractGogData();});
  113. }
  114. void FirstLaunchView::enterSetup()
  115. {
  116. Languages::fillLanguages(ui->listWidgetLanguage, false);
  117. }
  118. void FirstLaunchView::setSetupProgress(int progress)
  119. {
  120. ui->buttonTabLanguage->setDisabled(progress < 1);
  121. ui->buttonTabHeroesData->setDisabled(progress < 2);
  122. ui->buttonTabModPreset->setDisabled(progress < 3);
  123. }
  124. void FirstLaunchView::activateTabLanguage()
  125. {
  126. setSetupProgress(1);
  127. ui->installerTabs->setCurrentIndex(0);
  128. ui->buttonTabLanguage->setChecked(true);
  129. ui->buttonTabHeroesData->setChecked(false);
  130. ui->buttonTabModPreset->setChecked(false);
  131. }
  132. void FirstLaunchView::activateTabHeroesData()
  133. {
  134. setSetupProgress(2);
  135. ui->installerTabs->setCurrentIndex(1);
  136. ui->buttonTabLanguage->setChecked(false);
  137. ui->buttonTabHeroesData->setChecked(true);
  138. ui->buttonTabModPreset->setChecked(false);
  139. if(heroesDataUpdate())
  140. {
  141. activateTabModPreset();
  142. return;
  143. }
  144. QString installPath = getHeroesInstallDir();
  145. if(!installPath.isEmpty())
  146. {
  147. auto reply = QMessageBox::question(this, tr("Heroes III installation found!"), tr("Copy data to VCMI folder?"), QMessageBox::Yes | QMessageBox::No);
  148. if(reply == QMessageBox::Yes)
  149. copyHeroesData(installPath);
  150. }
  151. }
  152. void FirstLaunchView::activateTabModPreset()
  153. {
  154. setSetupProgress(3);
  155. ui->installerTabs->setCurrentIndex(2);
  156. ui->buttonTabLanguage->setChecked(false);
  157. ui->buttonTabHeroesData->setChecked(false);
  158. ui->buttonTabModPreset->setChecked(true);
  159. modPresetUpdate();
  160. }
  161. void FirstLaunchView::exitSetup(bool goToMods)
  162. {
  163. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  164. mainWindow->exitSetup(goToMods);
  165. }
  166. // Tab Language
  167. void FirstLaunchView::languageSelected(const QString & selectedLanguage)
  168. {
  169. Settings node = settings.write["general"]["language"];
  170. node->String() = selectedLanguage.toStdString();
  171. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  172. mainWindow->updateTranslation();
  173. }
  174. bool FirstLaunchView::heroesDataUpdate()
  175. {
  176. bool detected = heroesDataDetect();
  177. if(detected)
  178. heroesDataDetected();
  179. else
  180. heroesDataMissing();
  181. return detected;
  182. }
  183. void FirstLaunchView::heroesDataMissing()
  184. {
  185. QPalette newPalette = palette();
  186. newPalette.setColor(QPalette::Base, QColor(200, 50, 50));
  187. ui->lineEditDataSystem->setPalette(newPalette);
  188. ui->lineEditDataUser->setPalette(newPalette);
  189. ui->labelDataManualTitle->setVisible(true);
  190. ui->labelDataManualDescr->setVisible(true);
  191. ui->pushButtonDataSearch->setVisible(true);
  192. #ifdef VCMI_ANDROID
  193. // selecting directory with ACTION_OPEN_DOCUMENT_TREE is available only since API level 21
  194. const bool canUseDataCopy = QtAndroid::androidSdkVersion() >= 21;
  195. #elif defined(VCMI_IOS)
  196. // selecting directory through UIDocumentPickerViewController is available only since iOS 13
  197. const bool canUseDataCopy = iOS_utils::isOsVersionAtLeast(13);
  198. #else
  199. const bool canUseDataCopy = true;
  200. #endif
  201. ui->labelDataCopyTitle->setVisible(canUseDataCopy);
  202. ui->labelDataCopyDescr->setVisible(canUseDataCopy);
  203. ui->pushButtonDataCopy->setVisible(canUseDataCopy);
  204. #ifdef ENABLE_INNOEXTRACT
  205. ui->pushButtonGogInstall->setVisible(true);
  206. ui->labelDataGogTitle->setVisible(true);
  207. ui->labelDataGogDescr->setVisible(true);
  208. #endif
  209. ui->labelDataFound->setVisible(false);
  210. ui->pushButtonDataNext->setEnabled(false);
  211. }
  212. void FirstLaunchView::heroesDataDetected()
  213. {
  214. QPalette newPalette = palette();
  215. newPalette.setColor(QPalette::Base, QColor(50, 200, 50));
  216. ui->lineEditDataSystem->setPalette(newPalette);
  217. ui->lineEditDataUser->setPalette(newPalette);
  218. ui->pushButtonDataSearch->setVisible(false);
  219. ui->pushButtonDataCopy->setVisible(false);
  220. ui->labelDataManualTitle->setVisible(false);
  221. ui->labelDataManualDescr->setVisible(false);
  222. ui->labelDataCopyTitle->setVisible(false);
  223. ui->labelDataCopyDescr->setVisible(false);
  224. #ifdef ENABLE_INNOEXTRACT
  225. ui->pushButtonGogInstall->setVisible(false);
  226. ui->labelDataGogTitle->setVisible(false);
  227. ui->labelDataGogDescr->setVisible(false);
  228. #endif
  229. ui->labelDataFound->setVisible(true);
  230. ui->pushButtonDataNext->setEnabled(true);
  231. CGeneralTextHandler::detectInstallParameters();
  232. }
  233. // Tab Heroes III Data
  234. bool FirstLaunchView::heroesDataDetect()
  235. {
  236. // user might have copied files to one of our data path.
  237. // perform full reinitialization of virtual filesystem
  238. CResourceHandler::destroy();
  239. CResourceHandler::initialize();
  240. CResourceHandler::load("config/filesystem.json");
  241. // use file from lod archive to check presence of H3 data. Very rough estimate, but will work in majority of cases
  242. bool heroesDataFoundROE = CResourceHandler::get()->existsResource(ResourcePath("DATA/GENRLTXT.TXT"));
  243. bool heroesDataFoundSOD = CResourceHandler::get()->existsResource(ResourcePath("DATA/TENTCOLR.TXT"));
  244. return heroesDataFoundROE && heroesDataFoundSOD;
  245. }
  246. QString FirstLaunchView::getHeroesInstallDir()
  247. {
  248. #ifdef VCMI_WINDOWS
  249. QVector<QPair<QString, QString>> regKeys = {
  250. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\GOG.com\\Games\\1207658787", "path" }, // Gog on x86 system
  251. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\GOG.com\\Games\\1207658787", "path" }, // Gog on x64 system
  252. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\New World Computing\\Heroes of Might and Magic® III\\1.0", "AppPath" }, // H3 Complete on x86 system
  253. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\New World Computing\\Heroes of Might and Magic® III\\1.0", "AppPath" }, // H3 Complete on x64 system
  254. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\New World Computing\\Heroes of Might and Magic III\\1.0", "AppPath" }, // some localized H3 on x86 system
  255. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\New World Computing\\Heroes of Might and Magic III\\1.0", "AppPath" }, // some localized H3 on x64 system
  256. };
  257. for(auto & regKey : regKeys)
  258. {
  259. QString path = QSettings(regKey.first, QSettings::NativeFormat).value(regKey.second).toString();
  260. if(!path.isEmpty())
  261. return path;
  262. }
  263. #endif
  264. return QString{};
  265. }
  266. void FirstLaunchView::extractGogData()
  267. {
  268. #ifdef ENABLE_INNOEXTRACT
  269. auto fileSelection = [this](QString filter, QString startPath = {}) {
  270. QString titleSel = tr("Select %1 file...", "param is file extension").arg(filter);
  271. #if defined(VCMI_MOBILE)
  272. filter = tr("GOG file (*.*)");
  273. QMessageBox::information(this, tr("File selection"), titleSel);
  274. #endif
  275. QString file = QFileDialog::getOpenFileName(this, titleSel, startPath.isEmpty() ? QDir::homePath() : startPath, filter);
  276. if(file.isEmpty())
  277. return QString{};
  278. return file;
  279. };
  280. QString filterBin = tr("GOG data") + " (*.bin)";
  281. QString filterExe = tr("GOG installer") + " (*.exe)";
  282. QString fileBin = fileSelection(filterBin);
  283. if(fileBin.isEmpty())
  284. return;
  285. QString fileExe = fileSelection(filterExe, QFileInfo(fileBin).absolutePath());
  286. if(fileExe.isEmpty())
  287. return;
  288. ui->progressBarGog->setVisible(true);
  289. ui->pushButtonGogInstall->setVisible(false);
  290. setEnabled(false);
  291. QTimer::singleShot(100, this, [this, fileBin, fileExe](){ // background to make sure FileDialog is closed...
  292. extractGogDataAsync(fileBin, fileExe);
  293. ui->progressBarGog->setVisible(false);
  294. ui->pushButtonGogInstall->setVisible(true);
  295. setEnabled(true);
  296. });
  297. #endif
  298. }
  299. void FirstLaunchView::extractGogDataAsync(QString filePathBin, QString filePathExe)
  300. {
  301. logGlobal->info("Extracting gog data from '%s' and '%s'", filePathBin.toStdString(), filePathExe.toStdString());
  302. #ifdef ENABLE_INNOEXTRACT
  303. auto checkMagic = [](QString filename, QString filter, QByteArray magic)
  304. {
  305. logGlobal->info("Checking file %s", filename.toStdString());
  306. QFile tmpFile(filename);
  307. if(!tmpFile.open(QIODevice::ReadOnly))
  308. {
  309. logGlobal->info("File cannot be opened: %s", tmpFile.errorString().toStdString());
  310. return tr("Failed to open file: %1").arg(tmpFile.errorString());
  311. }
  312. QByteArray magicFile = tmpFile.read(magic.length());
  313. if(!magicFile.startsWith(magic))
  314. {
  315. logGlobal->info("Invalid file selected: %s", filter.toStdString());
  316. return tr("You have to select %1 file!", "param is file extension").arg(filter);
  317. }
  318. logGlobal->info("Checking file %s", filename.toStdString());
  319. return QString();
  320. };
  321. QString filterBin = tr("GOG data") + " (*.bin)";
  322. QString filterExe = tr("GOG installer") + " (*.exe)";
  323. QDir tempDir(pathToQString(VCMIDirs::get().userDataPath()));
  324. if(tempDir.cd("tmp"))
  325. {
  326. logGlobal->info("Cleaning up old data");
  327. tempDir.removeRecursively(); // remove if already exists (e.g. previous crash)
  328. tempDir.cdUp();
  329. }
  330. tempDir.mkdir("tmp");
  331. if(!tempDir.cd("tmp"))
  332. return; // should not happen - but avoid deleting wrong folder in any case
  333. logGlobal->info("Using '%s' as temporary directory", tempDir.path().toStdString());
  334. QString tmpFileExe = tempDir.filePath("h3_gog.exe");
  335. QString tmpFileBin = tempDir.filePath("h3_gog-1.bin");
  336. logGlobal->info("Performing native copy...");
  337. Helper::performNativeCopy(filePathExe, tmpFileExe);
  338. Helper::performNativeCopy(filePathBin, tmpFileBin);
  339. logGlobal->info("Native copy completed");
  340. QString errorText{};
  341. if (errorText.isEmpty())
  342. errorText = checkMagic(tmpFileBin, filterBin, QByteArray{"idska32"});
  343. if (errorText.isEmpty())
  344. errorText = checkMagic(tmpFileExe, filterExe, QByteArray{"MZ"});
  345. logGlobal->info("Installing exe '%s' ('%s')", tmpFileExe.toStdString(), filePathExe.toStdString());
  346. logGlobal->info("Installing bin '%s' ('%s')", tmpFileBin.toStdString(), filePathBin.toStdString());
  347. auto isGogGalaxyExe = [](QString fileToTest) {
  348. QFile file(fileToTest);
  349. quint64 fileSize = file.size();
  350. if(fileSize > 10 * 1024 * 1024)
  351. return false; // avoid to load big files; galaxy exe is smaller...
  352. if(!file.open(QIODevice::ReadOnly))
  353. return false;
  354. QByteArray data = file.readAll();
  355. const QByteArray magicId{reinterpret_cast<const char*>(u"GOG Galaxy"), 20};
  356. return data.contains(magicId);
  357. };
  358. if(errorText.isEmpty())
  359. {
  360. if(isGogGalaxyExe(tmpFileExe))
  361. {
  362. logGlobal->info("Gog Galaxy detected! Aborting...");
  363. errorText = tr("You've provided a GOG Galaxy installer! This file doesn't contain the game. Please download the offline backup game installer!");
  364. }
  365. }
  366. if(errorText.isEmpty())
  367. {
  368. logGlobal->info("Performing extraction using innoextract...");
  369. errorText = Innoextract::extract(tmpFileExe, tempDir.path(), [this](float progress) {
  370. ui->progressBarGog->setValue(progress * 100);
  371. qApp->processEvents();
  372. });
  373. logGlobal->info("Extraction done!");
  374. }
  375. QString hashError;
  376. if(!errorText.isEmpty())
  377. hashError = Innoextract::getHashError(tmpFileExe, tmpFileBin, filePathExe, filePathBin);
  378. QStringList dirData = tempDir.entryList({"data"}, QDir::Filter::Dirs);
  379. if(!errorText.isEmpty() || dirData.empty() || QDir(tempDir.filePath(dirData.front())).entryList({"*.lod"}, QDir::Filter::Files).empty())
  380. {
  381. if(!errorText.isEmpty())
  382. {
  383. logGlobal->error("Gog installer extraction failure! Reason: %s", errorText.toStdString());
  384. QMessageBox::critical(this, tr("Extracting error!"), errorText, QMessageBox::Ok, QMessageBox::Ok);
  385. if(!hashError.isEmpty())
  386. {
  387. logGlobal->error("Hash error: %s", hashError.toStdString());
  388. QMessageBox::critical(this, tr("Hash error!"), hashError, QMessageBox::Ok, QMessageBox::Ok);
  389. }
  390. }
  391. else
  392. QMessageBox::critical(this, tr("No Heroes III data!"), tr("Selected files do not contain Heroes III data!"), QMessageBox::Ok, QMessageBox::Ok);
  393. tempDir.removeRecursively();
  394. return;
  395. }
  396. logGlobal->info("Copying provided game files...");
  397. copyHeroesData(tempDir.path(), true);
  398. tempDir.removeRecursively();
  399. #endif
  400. }
  401. void FirstLaunchView::copyHeroesData(const QString & path, bool move)
  402. {
  403. QDir sourceRoot{path};
  404. #ifdef VCMI_IOS
  405. // TODO: Qt 6.5 can select directories https://codereview.qt-project.org/c/qt/qtbase/+/446449
  406. SelectDirectory iosDirectorySelector;
  407. if(path.isEmpty())
  408. sourceRoot.setPath(iosDirectorySelector.getExistingDirectory());
  409. #else
  410. if(path.isEmpty())
  411. sourceRoot.setPath(QFileDialog::getExistingDirectory(this, {}, {}, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
  412. #endif
  413. if(!sourceRoot.exists())
  414. return;
  415. if (sourceRoot.dirName().compare("data", Qt::CaseInsensitive) == 0)
  416. {
  417. // We got Data folder. Possibly user selected "Data" folder of Heroes III install. Check whether valid data might exist 1 level above
  418. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  419. if (dirData.empty())
  420. {
  421. // This is "Data" folder without any "Data" folders inside. Try to check for data 1 level above
  422. sourceRoot.cdUp();
  423. }
  424. }
  425. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  426. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  427. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  428. const auto noDataMessage = tr("Failed to detect valid Heroes III data in chosen directory.\nPlease select the directory with installed Heroes III data.");
  429. if(dirData.empty())
  430. {
  431. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  432. return;
  433. }
  434. QDir sourceData = sourceRoot.filePath(dirData.front());
  435. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  436. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  437. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  438. if(sodFiles.empty())
  439. {
  440. if (roeFiles.empty())
  441. {
  442. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  443. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  444. return;
  445. }
  446. if (!hdFiles.empty())
  447. {
  448. // HD Edition contains only RoE data so we can't use even unmodified files from it
  449. QMessageBox::critical(this, tr("Heroes III data not found!"), tr("Heroes III: HD Edition files are not supported by VCMI.\nPlease select the directory with Heroes III: Complete Edition or Heroes III: Shadow of Death."));
  450. return;
  451. }
  452. // RoE or some other unsupported edition. Demo version?
  453. QMessageBox::critical(this, tr("Heroes III data not found!"), tr("Unknown or unsupported Heroes III version found.\nPlease select the directory with Heroes III: Complete Edition or Heroes III: Shadow of Death."));
  454. return;
  455. }
  456. QStringList copyDirectories;
  457. copyDirectories += dirData.front();
  458. if (!dirMaps.empty())
  459. copyDirectories += dirMaps.front();
  460. if (!dirMp3.empty())
  461. copyDirectories += dirMp3.front();
  462. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  463. for(const QString & dirName : copyDirectories)
  464. {
  465. QDir sourceDir = sourceRoot.filePath(dirName);
  466. QDir targetDir = targetRoot.filePath(dirName);
  467. if(!targetRoot.exists(dirName))
  468. targetRoot.mkdir(dirName);
  469. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  470. {
  471. QFile sourceFile(sourceDir.filePath(filename));
  472. if(move)
  473. sourceFile.rename(targetDir.filePath(filename));
  474. else
  475. sourceFile.copy(targetDir.filePath(filename));
  476. }
  477. }
  478. heroesDataUpdate();
  479. }
  480. // Tab Mod Preset
  481. void FirstLaunchView::modPresetUpdate()
  482. {
  483. bool translationExists = !findTranslationModName().isEmpty();
  484. ui->labelPresetLanguageDescr->setVisible(translationExists);
  485. ui->buttonPresetLanguage->setVisible(translationExists);
  486. ui->buttonPresetLanguage->setVisible(checkCanInstallTranslation());
  487. ui->buttonPresetExtras->setVisible(checkCanInstallExtras());
  488. ui->buttonPresetHota->setVisible(checkCanInstallHota());
  489. ui->buttonPresetWog->setVisible(checkCanInstallWog());
  490. ui->labelPresetLanguageDescr->setVisible(checkCanInstallTranslation());
  491. ui->labelPresetExtrasDescr->setVisible(checkCanInstallExtras());
  492. ui->labelPresetHotaDescr->setVisible(checkCanInstallHota());
  493. ui->labelPresetWogDescr->setVisible(checkCanInstallWog());
  494. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  495. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  496. exitSetup(false);
  497. }
  498. QString FirstLaunchView::findTranslationModName()
  499. {
  500. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  501. auto status = mainWindow->getTranslationStatus();
  502. if (status == ETranslationStatus::ACTIVE || status == ETranslationStatus::NOT_AVAILABLE)
  503. return QString();
  504. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  505. return getModView()->getTranslationModName(preferredlanguage);
  506. }
  507. bool FirstLaunchView::checkCanInstallTranslation()
  508. {
  509. QString modName = findTranslationModName();
  510. if(modName.isEmpty())
  511. return false;
  512. return checkCanInstallMod(modName);
  513. }
  514. bool FirstLaunchView::checkCanInstallWog()
  515. {
  516. return checkCanInstallMod("wake-of-gods");
  517. }
  518. bool FirstLaunchView::checkCanInstallHota()
  519. {
  520. return checkCanInstallMod("hota");
  521. }
  522. bool FirstLaunchView::checkCanInstallExtras()
  523. {
  524. return checkCanInstallMod("vcmi-extras");
  525. }
  526. CModListView * FirstLaunchView::getModView()
  527. {
  528. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  529. assert(mainWindow);
  530. if (!mainWindow)
  531. return nullptr;
  532. return mainWindow->getModView();
  533. }
  534. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  535. {
  536. return getModView() && getModView()->isModAvailable(modID);
  537. }
  538. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  539. {
  540. activateTabHeroesData();
  541. }
  542. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  543. {
  544. QStringList modsToInstall;
  545. if (ui->buttonPresetLanguage->isChecked() && checkCanInstallTranslation())
  546. modsToInstall.push_back(findTranslationModName());
  547. if (ui->buttonPresetExtras->isChecked() && checkCanInstallExtras())
  548. modsToInstall.push_back("vcmi-extras");
  549. if (ui->buttonPresetWog->isChecked() && checkCanInstallWog())
  550. modsToInstall.push_back("wake-of-gods");
  551. if (ui->buttonPresetHota->isChecked() && checkCanInstallHota())
  552. modsToInstall.push_back("hota");
  553. bool goToMods = !modsToInstall.empty();
  554. exitSetup(goToMods);
  555. for (auto const & modName : modsToInstall)
  556. getModView()->doInstallMod(modName);
  557. }
  558. void FirstLaunchView::on_pushButtonDiscord_clicked()
  559. {
  560. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  561. }
  562. void FirstLaunchView::on_pushButtonGithub_clicked()
  563. {
  564. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  565. }