firstlaunch_moc.cpp 21 KB

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