firstlaunch_moc.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. heroesDataUpdate();
  297. });
  298. #endif
  299. }
  300. void FirstLaunchView::extractGogDataAsync(QString filePathBin, QString filePathExe)
  301. {
  302. logGlobal->info("Extracting gog data from '%s' and '%s'", filePathBin.toStdString(), filePathExe.toStdString());
  303. #ifdef ENABLE_INNOEXTRACT
  304. auto checkMagic = [](QString filename, QString filter, QByteArray magic)
  305. {
  306. logGlobal->info("Checking file %s", filename.toStdString());
  307. QFile tmpFile(filename);
  308. if(!tmpFile.open(QIODevice::ReadOnly))
  309. {
  310. logGlobal->info("File cannot be opened: %s", tmpFile.errorString().toStdString());
  311. return tr("Failed to open file: %1").arg(tmpFile.errorString());
  312. }
  313. QByteArray magicFile = tmpFile.read(magic.length());
  314. if(!magicFile.startsWith(magic))
  315. {
  316. logGlobal->info("Invalid file selected: %s", filter.toStdString());
  317. return tr("You have to select %1 file!", "param is file extension").arg(filter);
  318. }
  319. logGlobal->info("Checking file %s", filename.toStdString());
  320. return QString();
  321. };
  322. QString filterBin = tr("GOG data") + " (*.bin)";
  323. QString filterExe = tr("GOG installer") + " (*.exe)";
  324. QDir tempDir(pathToQString(VCMIDirs::get().userDataPath()));
  325. if(tempDir.cd("tmp"))
  326. {
  327. logGlobal->info("Cleaning up old data");
  328. tempDir.removeRecursively(); // remove if already exists (e.g. previous crash)
  329. tempDir.cdUp();
  330. }
  331. tempDir.mkdir("tmp");
  332. if(!tempDir.cd("tmp"))
  333. return; // should not happen - but avoid deleting wrong folder in any case
  334. logGlobal->info("Using '%s' as temporary directory", tempDir.path().toStdString());
  335. QString tmpFileExe = tempDir.filePath("h3_gog.exe");
  336. QString tmpFileBin = tempDir.filePath("h3_gog-1.bin");
  337. logGlobal->info("Performing native copy...");
  338. Helper::performNativeCopy(filePathExe, tmpFileExe);
  339. Helper::performNativeCopy(filePathBin, tmpFileBin);
  340. logGlobal->info("Native copy completed");
  341. QString errorText{};
  342. if (errorText.isEmpty())
  343. errorText = checkMagic(tmpFileBin, filterBin, QByteArray{"idska32"});
  344. if (errorText.isEmpty())
  345. errorText = checkMagic(tmpFileExe, filterExe, QByteArray{"MZ"});
  346. logGlobal->info("Installing exe '%s' ('%s')", tmpFileExe.toStdString(), filePathExe.toStdString());
  347. logGlobal->info("Installing bin '%s' ('%s')", tmpFileBin.toStdString(), filePathBin.toStdString());
  348. auto isGogGalaxyExe = [](QString fileToTest) {
  349. QFile file(fileToTest);
  350. quint64 fileSize = file.size();
  351. if(fileSize > 10 * 1024 * 1024)
  352. return false; // avoid to load big files; galaxy exe is smaller...
  353. if(!file.open(QIODevice::ReadOnly))
  354. return false;
  355. QByteArray data = file.readAll();
  356. const QByteArray magicId{reinterpret_cast<const char*>(u"GOG Galaxy"), 20};
  357. return data.contains(magicId);
  358. };
  359. if(errorText.isEmpty())
  360. {
  361. if(isGogGalaxyExe(tmpFileExe))
  362. {
  363. logGlobal->info("Gog Galaxy detected! Aborting...");
  364. errorText = tr("You've provided a GOG Galaxy installer! This file doesn't contain the game. Please download the offline backup game installer!");
  365. }
  366. }
  367. if(errorText.isEmpty())
  368. {
  369. logGlobal->info("Performing extraction using innoextract...");
  370. errorText = Innoextract::extract(tmpFileExe, tempDir.path(), [this](float progress) {
  371. ui->progressBarGog->setValue(progress * 100);
  372. qApp->processEvents();
  373. });
  374. logGlobal->info("Extraction done!");
  375. }
  376. QString hashError;
  377. if(!errorText.isEmpty())
  378. hashError = Innoextract::getHashError(tmpFileExe, tmpFileBin, filePathExe, filePathBin);
  379. QStringList dirData = tempDir.entryList({"data"}, QDir::Filter::Dirs);
  380. if(!errorText.isEmpty() || dirData.empty() || QDir(tempDir.filePath(dirData.front())).entryList({"*.lod"}, QDir::Filter::Files).empty())
  381. {
  382. if(!errorText.isEmpty())
  383. {
  384. logGlobal->error("Gog installer extraction failure! Reason: %s", errorText.toStdString());
  385. QMessageBox::critical(this, tr("Extracting error!"), errorText, QMessageBox::Ok, QMessageBox::Ok);
  386. if(!hashError.isEmpty())
  387. {
  388. logGlobal->error("Hash error: %s", hashError.toStdString());
  389. QMessageBox::critical(this, tr("Hash error!"), hashError, QMessageBox::Ok, QMessageBox::Ok);
  390. }
  391. }
  392. else
  393. QMessageBox::critical(this, tr("No Heroes III data!"), tr("Selected files do not contain Heroes III data!"), QMessageBox::Ok, QMessageBox::Ok);
  394. tempDir.removeRecursively();
  395. return;
  396. }
  397. logGlobal->info("Copying provided game files...");
  398. copyHeroesData(tempDir.path(), true);
  399. tempDir.removeRecursively();
  400. #endif
  401. }
  402. void FirstLaunchView::copyHeroesData(const QString & path, bool move)
  403. {
  404. QDir sourceRoot{path};
  405. #ifdef VCMI_IOS
  406. // TODO: Qt 6.5 can select directories https://codereview.qt-project.org/c/qt/qtbase/+/446449
  407. SelectDirectory iosDirectorySelector;
  408. if(path.isEmpty())
  409. sourceRoot.setPath(iosDirectorySelector.getExistingDirectory());
  410. #else
  411. if(path.isEmpty())
  412. sourceRoot.setPath(QFileDialog::getExistingDirectory(this, {}, {}, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
  413. #endif
  414. if(!sourceRoot.exists())
  415. return;
  416. if (sourceRoot.dirName().compare("data", Qt::CaseInsensitive) == 0)
  417. {
  418. // We got Data folder. Possibly user selected "Data" folder of Heroes III install. Check whether valid data might exist 1 level above
  419. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  420. if (dirData.empty())
  421. {
  422. // This is "Data" folder without any "Data" folders inside. Try to check for data 1 level above
  423. sourceRoot.cdUp();
  424. }
  425. }
  426. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  427. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  428. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  429. const auto noDataMessage = tr("Failed to detect valid Heroes III data in chosen directory.\nPlease select the directory with installed Heroes III data.");
  430. if(dirData.empty())
  431. {
  432. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  433. return;
  434. }
  435. QDir sourceData = sourceRoot.filePath(dirData.front());
  436. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  437. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  438. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  439. if(sodFiles.empty())
  440. {
  441. if (roeFiles.empty())
  442. {
  443. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  444. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  445. return;
  446. }
  447. if (!hdFiles.empty())
  448. {
  449. // HD Edition contains only RoE data so we can't use even unmodified files from it
  450. 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."));
  451. return;
  452. }
  453. // RoE or some other unsupported edition. Demo version?
  454. 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."));
  455. return;
  456. }
  457. QStringList copyDirectories;
  458. copyDirectories += dirData.front();
  459. if (!dirMaps.empty())
  460. copyDirectories += dirMaps.front();
  461. if (!dirMp3.empty())
  462. copyDirectories += dirMp3.front();
  463. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  464. for(const QString & dirName : copyDirectories)
  465. {
  466. QDir sourceDir = sourceRoot.filePath(dirName);
  467. QDir targetDir = targetRoot.filePath(dirName);
  468. if(!targetRoot.exists(dirName))
  469. targetRoot.mkdir(dirName);
  470. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  471. {
  472. QFile sourceFile(sourceDir.filePath(filename));
  473. if(move)
  474. sourceFile.rename(targetDir.filePath(filename));
  475. else
  476. sourceFile.copy(targetDir.filePath(filename));
  477. }
  478. }
  479. heroesDataUpdate();
  480. }
  481. // Tab Mod Preset
  482. void FirstLaunchView::modPresetUpdate()
  483. {
  484. bool translationExists = !findTranslationModName().isEmpty();
  485. ui->labelPresetLanguageDescr->setVisible(translationExists);
  486. ui->buttonPresetLanguage->setVisible(translationExists);
  487. ui->buttonPresetLanguage->setVisible(checkCanInstallTranslation());
  488. ui->buttonPresetExtras->setVisible(checkCanInstallExtras());
  489. ui->buttonPresetHota->setVisible(checkCanInstallHota());
  490. ui->buttonPresetWog->setVisible(checkCanInstallWog());
  491. ui->labelPresetLanguageDescr->setVisible(checkCanInstallTranslation());
  492. ui->labelPresetExtrasDescr->setVisible(checkCanInstallExtras());
  493. ui->labelPresetHotaDescr->setVisible(checkCanInstallHota());
  494. ui->labelPresetWogDescr->setVisible(checkCanInstallWog());
  495. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  496. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  497. exitSetup(false);
  498. }
  499. QString FirstLaunchView::findTranslationModName()
  500. {
  501. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  502. auto status = mainWindow->getTranslationStatus();
  503. if (status == ETranslationStatus::ACTIVE || status == ETranslationStatus::NOT_AVAILABLE)
  504. return QString();
  505. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  506. return getModView()->getTranslationModName(preferredlanguage);
  507. }
  508. bool FirstLaunchView::checkCanInstallTranslation()
  509. {
  510. QString modName = findTranslationModName();
  511. if(modName.isEmpty())
  512. return false;
  513. return checkCanInstallMod(modName);
  514. }
  515. bool FirstLaunchView::checkCanInstallWog()
  516. {
  517. return checkCanInstallMod("wake-of-gods");
  518. }
  519. bool FirstLaunchView::checkCanInstallHota()
  520. {
  521. return checkCanInstallMod("hota");
  522. }
  523. bool FirstLaunchView::checkCanInstallExtras()
  524. {
  525. return checkCanInstallMod("vcmi-extras");
  526. }
  527. CModListView * FirstLaunchView::getModView()
  528. {
  529. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  530. assert(mainWindow);
  531. if (!mainWindow)
  532. return nullptr;
  533. return mainWindow->getModView();
  534. }
  535. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  536. {
  537. return getModView() && getModView()->isModAvailable(modID);
  538. }
  539. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  540. {
  541. activateTabHeroesData();
  542. }
  543. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  544. {
  545. QStringList modsToInstall;
  546. if (ui->buttonPresetLanguage->isChecked() && checkCanInstallTranslation())
  547. modsToInstall.push_back(findTranslationModName());
  548. if (ui->buttonPresetExtras->isChecked() && checkCanInstallExtras())
  549. modsToInstall.push_back("vcmi-extras");
  550. if (ui->buttonPresetWog->isChecked() && checkCanInstallWog())
  551. modsToInstall.push_back("wake-of-gods");
  552. if (ui->buttonPresetHota->isChecked() && checkCanInstallHota())
  553. modsToInstall.push_back("hota");
  554. bool goToMods = !modsToInstall.empty();
  555. exitSetup(goToMods);
  556. for (auto const & modName : modsToInstall)
  557. getModView()->doInstallMod(modName);
  558. }
  559. void FirstLaunchView::on_pushButtonDiscord_clicked()
  560. {
  561. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  562. }
  563. void FirstLaunchView::on_pushButtonGithub_clicked()
  564. {
  565. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  566. }