firstlaunch_moc.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. #include "progressoverlay.h"
  25. // Create and show overlay immediately
  26. static ProgressOverlay* createOverlay(QWidget *parent, const QString &title, bool indeterminate = true)
  27. {
  28. auto *overlay = new ProgressOverlay(parent, 50);
  29. overlay->setTitle(title);
  30. overlay->setIndeterminate(indeterminate);
  31. overlay->show();
  32. qApp->processEvents(); // paint before heavy work
  33. return overlay;
  34. }
  35. FirstLaunchView::FirstLaunchView(QWidget * parent)
  36. : QWidget(parent)
  37. , ui(std::make_unique<Ui::FirstLaunchView>())
  38. {
  39. ui->setupUi(this);
  40. enterSetup();
  41. activateTabLanguage();
  42. ui->lineEditDataSystem->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().dataPaths().front())));
  43. ui->lineEditDataUser->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().userDataPath())));
  44. Helper::enableScrollBySwiping(ui->listWidgetLanguage);
  45. #ifdef VCMI_MOBILE
  46. // This directory is not accessible to players without rooting of their device
  47. ui->lineEditDataSystem->hide();
  48. #endif
  49. #ifndef ENABLE_INNOEXTRACT
  50. ui->pushButtonGogInstall->hide();
  51. ui->labelDataGogTitle->hide();
  52. ui->labelDataGogDescr->hide();
  53. #endif
  54. }
  55. FirstLaunchView::~FirstLaunchView() = default;
  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. // iOS can't display modal dialogs when called directly on button press
  100. // https://bugreports.qt.io/browse/QTBUG-98651
  101. MessageBoxCustom::showDialog(this, [this]{
  102. Helper::nativeFolderPicker(this, [this](const QString &picked){
  103. if(!picked.isEmpty())
  104. copyHeroesData(picked, false);
  105. });
  106. });
  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, false);
  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. const bool canUseDataCopy = Helper::canUseFolderPicker();
  193. ui->labelDataCopyTitle->setVisible(canUseDataCopy);
  194. ui->labelDataCopyDescr->setVisible(canUseDataCopy);
  195. ui->pushButtonDataCopy->setVisible(canUseDataCopy);
  196. #ifdef ENABLE_INNOEXTRACT
  197. ui->pushButtonGogInstall->setVisible(true);
  198. ui->labelDataGogTitle->setVisible(true);
  199. ui->labelDataGogDescr->setVisible(true);
  200. #endif
  201. ui->labelDataFound->setVisible(false);
  202. ui->pushButtonDataNext->setEnabled(false);
  203. }
  204. void FirstLaunchView::heroesDataDetected()
  205. {
  206. QPalette newPalette = palette();
  207. newPalette.setColor(QPalette::Base, QColor(50, 200, 50));
  208. ui->lineEditDataSystem->setPalette(newPalette);
  209. ui->lineEditDataUser->setPalette(newPalette);
  210. ui->pushButtonDataSearch->setVisible(false);
  211. ui->pushButtonDataCopy->setVisible(false);
  212. ui->labelDataManualTitle->setVisible(false);
  213. ui->labelDataManualDescr->setVisible(false);
  214. ui->labelDataCopyTitle->setVisible(false);
  215. ui->labelDataCopyDescr->setVisible(false);
  216. #ifdef ENABLE_INNOEXTRACT
  217. ui->pushButtonGogInstall->setVisible(false);
  218. ui->labelDataGogTitle->setVisible(false);
  219. ui->labelDataGogDescr->setVisible(false);
  220. #endif
  221. ui->labelDataFound->setVisible(true);
  222. ui->pushButtonDataNext->setEnabled(true);
  223. CGeneralTextHandler::detectInstallParameters();
  224. }
  225. // Tab Heroes III Data
  226. bool FirstLaunchView::heroesDataDetect()
  227. {
  228. // user might have copied files to one of our data path.
  229. // perform full reinitialization of virtual filesystem
  230. CResourceHandler::destroy();
  231. CResourceHandler::initialize();
  232. CResourceHandler::load("config/filesystem.json");
  233. // use file from lod archive to check presence of H3 data. Very rough estimate, but will work in majority of cases
  234. bool heroesDataFoundROE = CResourceHandler::get()->existsResource(ResourcePath("DATA/GENRLTXT.TXT"));
  235. bool heroesDataFoundSOD = CResourceHandler::get()->existsResource(ResourcePath("DATA/TENTCOLR.TXT"));
  236. return heroesDataFoundROE && heroesDataFoundSOD;
  237. }
  238. QString FirstLaunchView::getHeroesInstallDir()
  239. {
  240. #ifdef VCMI_WINDOWS
  241. QVector<QPair<QString, QString>> regKeys = {
  242. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\GOG.com\\Games\\1207658787", "path" }, // Gog on x86 system
  243. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\GOG.com\\Games\\1207658787", "path" }, // Gog on x64 system
  244. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\New World Computing\\Heroes of Might and Magic® III\\1.0", "AppPath" }, // H3 Complete on x86 system
  245. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\New World Computing\\Heroes of Might and Magic® III\\1.0", "AppPath" }, // H3 Complete on x64 system
  246. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\New World Computing\\Heroes of Might and Magic III\\1.0", "AppPath" }, // some localized H3 on x86 system
  247. { "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\New World Computing\\Heroes of Might and Magic III\\1.0", "AppPath" }, // some localized H3 on x64 system
  248. };
  249. for(auto & regKey : regKeys)
  250. {
  251. QString path = QSettings(regKey.first, QSettings::NativeFormat).value(regKey.second).toString();
  252. if(!path.isEmpty())
  253. return path;
  254. }
  255. #endif
  256. return QString{};
  257. }
  258. static QString defaultStartDirForOpen()
  259. {
  260. #if defined(VCMI_MOBILE)
  261. const QStandardPaths::StandardLocation mobilePrefs[] = {
  262. QStandardPaths::DocumentsLocation,
  263. QStandardPaths::HomeLocation
  264. };
  265. for(auto location : mobilePrefs)
  266. {
  267. for(const QString &path : QStandardPaths::standardLocations(location))
  268. if(QDir(path).exists() && !path.isEmpty())
  269. return path;
  270. }
  271. return QDir::homePath();
  272. #else
  273. // Desktop: prefer Downloads, then Home, then Desktop
  274. const QStandardPaths::StandardLocation desktopPrefs[] = {
  275. QStandardPaths::DownloadLocation,
  276. QStandardPaths::HomeLocation,
  277. QStandardPaths::DesktopLocation
  278. };
  279. for(auto location : desktopPrefs)
  280. {
  281. for(const QString &path : QStandardPaths::standardLocations(location))
  282. if(QDir(path).exists() && !path.isEmpty())
  283. return path;
  284. }
  285. return QDir::homePath();
  286. #endif
  287. }
  288. QString FirstLaunchView::checkFileMagic(const QString &filename, const QString &filter, const QByteArray &magic, const QString &ext, bool &openFailed) const
  289. {
  290. QFile file(filename);
  291. if(!file.open(QIODevice::ReadOnly))
  292. {
  293. if(openFailed)
  294. {
  295. return tr("Failed to open file: %1").arg(file.errorString());
  296. }
  297. else
  298. {
  299. // Some systems can't access selected file for read, but can copy it, postpone fail fast for next run
  300. logGlobal->warn("checkMagic: open failed for '%s': %s", filename.toStdString(), file.errorString().toStdString());
  301. openFailed = true;
  302. return {};
  303. }
  304. }
  305. QFileInfo fileInfo(filename);
  306. quint64 fileSize = fileInfo.size();
  307. QString realFilename = Helper::getRealPath(filename);
  308. logGlobal->info("Checking %s with size: %llu", realFilename.toStdString(), fileSize);
  309. #if defined(VCMI_MOBILE)
  310. if(!realFilename.endsWith(ext, Qt::CaseInsensitive))
  311. return tr("You need to select a %1 file!", "param is file extension").arg(ext);
  312. #endif
  313. if(realFilename.endsWith(".exe", Qt::CaseInsensitive))
  314. {
  315. if(fileSize > 1500000) // 1.5MB
  316. {
  317. logGlobal->info("Unknown installer selected: %s", filename.toStdString());
  318. return tr("Unknown installer selected.\nYou need to select the offline GOG installer.");
  319. }
  320. const QByteArray data = file.peek(fileSize);
  321. constexpr std::u16string_view galaxyID = u"GOG Galaxy";
  322. const auto galaxyIDBytes = reinterpret_cast<const char*>(galaxyID.data());
  323. const auto magicId = QByteArray::fromRawData(galaxyIDBytes, galaxyID.size() * sizeof(decltype(galaxyID)::value_type));
  324. if(data.contains(magicId))
  325. {
  326. logGlobal->info("GOG Galaxy detected! Aborting...");
  327. return tr("You selected a GOG Galaxy installer. This file does not contain the game. Please download the offline backup game installer instead.");
  328. }
  329. }
  330. const QByteArray magicFile = file.peek(magic.length());
  331. if(!magicFile.startsWith(magic))
  332. return tr("You need to select a %1 file!", "param is file extension").arg(filter);
  333. return {};
  334. }
  335. void FirstLaunchView::extractGogData()
  336. {
  337. #ifdef ENABLE_INNOEXTRACT
  338. auto fileSelection = [this](const QString &title, QString filter, const QString &startPath = {}) {
  339. #if defined(VCMI_MOBILE)
  340. filter = tr("GOG file (*.*)");
  341. QMessageBox::information(this, tr("File selection"), title);
  342. #endif
  343. QString file = QFileDialog::getOpenFileName(this, title, startPath.isEmpty() ? defaultStartDirForOpen() : startPath, filter);
  344. if(file.isEmpty())
  345. return QString{};
  346. return file;
  347. };
  348. needPostCopyCheckExe = false;
  349. needPostCopyCheckBin = false;
  350. QString filterExe = tr("GOG installer") + " (*.exe)";
  351. QString titleExe = tr("Select the offline GOG installer (.exe)");
  352. QString fileExe = fileSelection(titleExe, filterExe);
  353. if(fileExe.isEmpty())
  354. return;
  355. QString errorText = checkFileMagic(fileExe, filterExe, QByteArray{"MZP"}, "EXE", needPostCopyCheckExe);
  356. if(!errorText.isEmpty())
  357. {
  358. QMessageBox::critical(this, tr("Invalid file selected"), errorText);
  359. return;
  360. }
  361. QFileInfo exeInfo(fileExe);
  362. QString expectedBinName = exeInfo.completeBaseName() + "-1.bin";
  363. QString filterBin = tr("GOG data") + " (*.bin)";
  364. QString titleBin = tr("Select the offline GOG installer data file: %1", "param is file name").arg(expectedBinName);
  365. // Try to access BIN based on selected EXE
  366. QString fileBinCandidate = exeInfo.absoluteDir().filePath(expectedBinName);
  367. bool haveCandidate = false;
  368. QFile file(fileBinCandidate);
  369. if(file.open(QIODevice::ReadOnly))
  370. {
  371. haveCandidate = true;
  372. file.close();
  373. }
  374. QString fileBin = haveCandidate ? fileBinCandidate : fileSelection(titleBin, filterBin, exeInfo.absolutePath());
  375. if(fileBin.isEmpty())
  376. return;
  377. errorText = checkFileMagic(fileBin, filterBin, QByteArray{"idska32"}, "BIN", needPostCopyCheckBin);
  378. if(!errorText.isEmpty())
  379. {
  380. QMessageBox::critical(this, tr("Invalid data file"), errorText);
  381. return;
  382. }
  383. QTimer::singleShot(100, this, [this, fileBin, fileExe](){ // background to make sure FileDialog is closed...
  384. extractGogDataAsync(fileBin, fileExe);
  385. setEnabled(true);
  386. heroesDataUpdate();
  387. });
  388. #endif
  389. }
  390. bool FirstLaunchView::performCopyFlow(const QString& path, ProgressOverlay* overlay, bool removeSource)
  391. {
  392. // 1) Scan -> "Source \t Target \t Name"
  393. overlay->setIndeterminate(true);
  394. const QStringList items = Helper::findFilesForCopy(path);
  395. if(items.isEmpty())
  396. {
  397. QMessageBox::critical(this, tr("Heroes III data not found!"), tr("Failed to detect valid Heroes III data in chosen directory.\nPlease select the directory with installed Heroes III data."));
  398. return false;
  399. }
  400. // 2) Validate signature
  401. // TODO: Find proper way for pure SoD check in import or way to block pure RoE / AB
  402. // Or prepare RoE / AB Ban mod and allow VCMI to go with any H3 version
  403. auto validate = [](const QStringList &items)->QString {
  404. bool anyLOD=false;
  405. bool anySOD=false;
  406. bool anyHD=false;
  407. for(const QString &line : items)
  408. {
  409. const auto part = line.split('\t');
  410. if(part[1].compare("Data", Qt::CaseInsensitive) != 0)
  411. continue;
  412. const QString &name = part[2];
  413. if(name.endsWith(".lod", Qt::CaseInsensitive))
  414. {
  415. anyLOD = true;
  416. if(name.startsWith("H3ab", Qt::CaseInsensitive))
  417. anySOD = true;
  418. }
  419. if(name.endsWith(".pak", Qt::CaseInsensitive))
  420. anyHD = true;
  421. }
  422. if(anySOD) return {};
  423. if(!anyLOD)
  424. return tr("Failed to detect valid Heroes III data in chosen directory.\nPlease select the directory with installed Heroes III data.");
  425. if(anyHD)
  426. return 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.");
  427. return tr("Unknown or unsupported Heroes III version found.\nPlease select the directory with Heroes III: Complete Edition or Heroes III: Shadow of Death.");
  428. };
  429. const QString err = validate(items);
  430. if(!err.isEmpty())
  431. {
  432. QMessageBox::critical(this, tr("Heroes III data not found!"), err);
  433. return false;
  434. }
  435. // 3) Plan destination, create target dirs on demand
  436. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  437. QSet<QString> created;
  438. struct CopyItem { QString source, destination; };
  439. QVector<CopyItem> plan;
  440. plan.reserve(items.size());
  441. for(const QString &line : items)
  442. {
  443. const auto part = line.split('\t');
  444. const QString &source = part[0];
  445. const QString &target = part[1]; // Data / Maps / Mp3
  446. const QString &file = part[2];
  447. if(!created.contains(target))
  448. {
  449. QDir{}.mkpath(targetRoot.filePath(target));
  450. created.insert(target);
  451. }
  452. const QDir destinationDir = targetRoot.filePath(target);
  453. plan.push_back({ source, destinationDir.filePath(file) });
  454. }
  455. // 4) Copy with progress
  456. overlay->setTitle(tr("Importing Heroes III data..."));
  457. overlay->setIndeterminate(false);
  458. overlay->setRange(plan.size());
  459. for(int i = 0; i < plan.size(); ++i)
  460. {
  461. overlay->setFileName(QFileInfo(plan[i].destination).fileName());
  462. overlay->setValue(i + 1);
  463. qApp->processEvents();
  464. if(QFile::exists(plan[i].destination))
  465. QFile::remove(plan[i].destination);
  466. Helper::performNativeCopy(plan[i].source, plan[i].destination);
  467. logGlobal->info("Copying '%s' -> '%s'", plan[i].source.toStdString(), plan[i].destination.toStdString());
  468. }
  469. // 5) Optional cleanup
  470. if(removeSource)
  471. QDir(path).removeRecursively();
  472. return true;
  473. }
  474. void FirstLaunchView::extractGogDataAsync(QString filePathBin, QString filePathExe)
  475. {
  476. logGlobal->info("Extracting gog data from '%s' and '%s'", filePathBin.toStdString(), filePathExe.toStdString());
  477. #ifdef ENABLE_INNOEXTRACT
  478. // Defer heavy work to next event-loop tick to ensure overlay is painted
  479. QTimer::singleShot(0, this, [this, filePathBin, filePathExe]()
  480. {
  481. QScopedPointer<ProgressOverlay> overlay(createOverlay(this, tr("Preparing installer..."), true));
  482. overlay->setFileName(QFileInfo(filePathExe).fileName());
  483. overlay->raise();
  484. qApp->processEvents();
  485. // "Goole TV Tick" without this was never displayed "Preparing installer" on screen
  486. QEventLoop ev;
  487. QTimer::singleShot(0, &ev, &QEventLoop::quit);
  488. ev.exec();
  489. // 1) Prepare temp dir
  490. QDir tempDir(pathToQString(VCMIDirs::get().userDataPath()));
  491. if(tempDir.cd("tmp"))
  492. {
  493. logGlobal->info("Cleaning up old temp data");
  494. tempDir.removeRecursively(); // remove if already exists (e.g. previous crash)
  495. tempDir.cdUp();
  496. }
  497. tempDir.mkdir("tmp");
  498. if(!tempDir.cd("tmp"))
  499. {
  500. return; // should not happen - but avoid deleting wrong folder in any case
  501. }
  502. logGlobal->info("Using '%s' as temporary directory", tempDir.path().toStdString());
  503. const QString tmpFileExe = tempDir.filePath("h3_gog.exe");
  504. const QString tmpFileBin = tempDir.filePath("h3_gog-1.bin");
  505. // 2) Copy selected files into tmp
  506. logGlobal->info("Performing native copy...");
  507. Helper::performNativeCopy(filePathExe, tmpFileExe);
  508. if(needPostCopyCheckExe)
  509. {
  510. const QString err = checkFileMagic(tmpFileExe, tr("GOG installer") + " (*.exe)", QByteArray{"MZP"}, "EXE", needPostCopyCheckExe);
  511. if(!err.isEmpty())
  512. {
  513. QMessageBox::critical(this, tr("Invalid file selected"), err);
  514. tempDir.removeRecursively();
  515. return;
  516. }
  517. }
  518. Helper::performNativeCopy(filePathBin, tmpFileBin);
  519. if(needPostCopyCheckBin)
  520. {
  521. const QString err = checkFileMagic(tmpFileBin, tr("GOG data") + " (*.bin)", QByteArray{"idska32"}, "BIN", needPostCopyCheckBin);
  522. if(!err.isEmpty())
  523. {
  524. QMessageBox::critical(this, tr("Invalid data file"), err);
  525. tempDir.removeRecursively();
  526. return;
  527. }
  528. }
  529. logGlobal->info("Native copy completed");
  530. // 3) Extract
  531. overlay->setTitle(tr("Extracting installer..."));
  532. overlay->setIndeterminate(false);
  533. overlay->setRange(100);
  534. overlay->setValue(0);
  535. logGlobal->info("Performing extraction using innoextract...");
  536. QString errorText;
  537. errorText = Innoextract::extract(tmpFileExe, tempDir.path(), [overlayPtr = overlay.data()](float progress) {
  538. overlayPtr->setValue(static_cast<int>(progress * 100));
  539. qApp->processEvents();
  540. });
  541. logGlobal->info("Extraction done!");
  542. // 4) Post-extract verification and error reporting
  543. QString hashError;
  544. if(!errorText.isEmpty())
  545. hashError = Innoextract::getHashError(tmpFileExe, tmpFileBin, filePathExe, filePathBin);
  546. QStringList dirData = tempDir.entryList({"data"}, QDir::Filter::Dirs);
  547. if(!errorText.isEmpty() || dirData.empty() || QDir(tempDir.filePath(dirData.front())).entryList({"*.lod"}, QDir::Filter::Files).empty())
  548. {
  549. if(!errorText.isEmpty())
  550. {
  551. logGlobal->error("GOG installer extraction failure! Reason: %s", errorText.toStdString());
  552. QMessageBox::critical(this, tr("Extracting error!"), errorText, QMessageBox::Ok, QMessageBox::Ok);
  553. if(!hashError.isEmpty())
  554. {
  555. logGlobal->error("Hash error: %s", hashError.toStdString());
  556. QMessageBox::critical(this, tr("Hash error!"), hashError, QMessageBox::Ok, QMessageBox::Ok);
  557. }
  558. }
  559. else
  560. QMessageBox::critical(this, tr("No Heroes III data!"), tr("Selected files do not contain Heroes III data!"), QMessageBox::Ok, QMessageBox::Ok);
  561. tempDir.removeRecursively();
  562. return;
  563. }
  564. logGlobal->info("Importing Heroes III data...");
  565. // 5) Reuse overlay for copy phase
  566. overlay->setTitle(tr("Importing Heroes III data..."));
  567. overlay->setFileName({});
  568. overlay->setRange(100); // performCopyFlow will reset to plan size internally
  569. overlay->setValue(0);
  570. if(performCopyFlow(tempDir.path(), overlay.data(), true))
  571. if(heroesDataUpdate())
  572. activateTabModPreset();
  573. });
  574. #endif
  575. }
  576. void FirstLaunchView::copyHeroesData(const QString &path, bool removeSource)
  577. {
  578. QPointer<ProgressOverlay> overlay = createOverlay(this, tr("Scanning selected folder..."), true);
  579. overlay->raise();
  580. auto work = [this, path, removeSource, overlay]() {
  581. if(performCopyFlow(path, overlay, removeSource))
  582. if(heroesDataUpdate())
  583. activateTabModPreset();
  584. overlay->deleteLater();
  585. };
  586. #ifdef VCMI_IOS
  587. // iOS needs to make synchronous call for the SelectDirectory object to be still alive
  588. // as it calls stopAccessingSecurityScopedResource on the user selected directory URL upon destruction
  589. qApp->processEvents();
  590. work();
  591. #else
  592. QTimer::singleShot(0, this, work);
  593. #endif
  594. }
  595. // Tab Mod Preset
  596. void FirstLaunchView::modPresetUpdate()
  597. {
  598. bool translationExists = !findTranslationModName().isEmpty();
  599. ui->labelPresetLanguageDescr->setVisible(translationExists);
  600. ui->buttonPresetLanguage->setVisible(translationExists);
  601. bool canTrans = checkCanInstallTranslation();
  602. bool canExtras = checkCanInstallExtras();
  603. bool canDemo = checkCanInstallDemo();
  604. bool canHota = checkCanInstallHota();
  605. bool canWog = checkCanInstallWog();
  606. bool canTow = checkCanInstallTow();
  607. bool canFod = checkCanInstallFod();
  608. ui->buttonPresetLanguage->setVisible(canTrans);
  609. ui->buttonPresetExtras->setVisible(canExtras);
  610. ui->buttonPresetDemo->setVisible(canDemo);
  611. ui->buttonPresetHota->setVisible(canHota);
  612. ui->buttonPresetWog->setVisible(canWog);
  613. ui->buttonPresetTow->setVisible(canTow);
  614. ui->buttonPresetFod->setVisible(canFod);
  615. ui->labelPresetLanguageDescr->setVisible(canTrans);
  616. ui->labelPresetExtrasDescr->setVisible(canExtras);
  617. ui->labelPresetDemoDescr->setVisible(canDemo);
  618. ui->labelPresetHotaDescr->setVisible(canHota);
  619. ui->labelPresetWogDescr->setVisible(canWog);
  620. ui->labelPresetTowDescr->setVisible(canTow);
  621. ui->labelPresetFodDescr->setVisible(canFod);
  622. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  623. if(!canTrans && !canExtras && !canDemo && !canHota && !canWog && !canTow && !canFod)
  624. exitSetup(false);
  625. }
  626. QString FirstLaunchView::findTranslationModName()
  627. {
  628. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  629. auto status = mainWindow->getTranslationStatus();
  630. if(status == ETranslationStatus::ACTIVE || status == ETranslationStatus::NOT_AVAILABLE)
  631. return QString();
  632. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  633. return getModView()->getTranslationModName(preferredlanguage);
  634. }
  635. bool FirstLaunchView::checkCanInstallTranslation()
  636. {
  637. QString modName = findTranslationModName();
  638. if(modName.isEmpty())
  639. return false;
  640. return checkCanInstallMod(modName);
  641. }
  642. bool FirstLaunchView::checkCanInstallExtras()
  643. {
  644. return checkCanInstallMod("vcmi-extras");
  645. }
  646. bool FirstLaunchView::checkCanInstallDemo()
  647. {
  648. if(!checkCanInstallMod("demo-support"))
  649. return false;
  650. QDir userRoot = pathToQString(VCMIDirs::get().userDataPath());
  651. QDir dataDir(userRoot.filePath(QStringLiteral("Data")));
  652. QDir mapsDir(userRoot.filePath(QStringLiteral("Maps")));
  653. bool hasDemoMap = false;
  654. QStringList mapFiles = mapsDir.entryList(QDir::Files | QDir::Readable);
  655. for(const QString &name : mapFiles)
  656. if(name.compare(QStringLiteral("h3demo.h3m"), Qt::CaseInsensitive) == 0)
  657. {
  658. hasDemoMap = true;
  659. break;
  660. }
  661. QStringList files = dataDir.entryList(QDir::Files | QDir::Readable);
  662. for(const QString &name : files)
  663. {
  664. if(name.compare(QStringLiteral("H3ab_spr.lod"), Qt::CaseInsensitive) == 0)
  665. {
  666. QFileInfo lodInfo(dataDir.filePath(name));
  667. quint64 fileSize = static_cast<quint64>(lodInfo.size());
  668. logGlobal->trace("H3ab_spr.lod size: %llu", fileSize);
  669. if(fileSize < 8000000 && hasDemoMap) // 8 MB + Demo map = Merged Windows and MacOS Demo
  670. return true;
  671. }
  672. }
  673. return false;
  674. }
  675. bool FirstLaunchView::checkCanInstallHota()
  676. {
  677. return checkCanInstallMod("hota");
  678. }
  679. bool FirstLaunchView::checkCanInstallWog()
  680. {
  681. return checkCanInstallMod("wake-of-gods");
  682. }
  683. bool FirstLaunchView::checkCanInstallTow()
  684. {
  685. return checkCanInstallMod("tides-of-war");
  686. }
  687. bool FirstLaunchView::checkCanInstallFod()
  688. {
  689. return checkCanInstallMod("fallen-of-the-depth");
  690. }
  691. CModListView * FirstLaunchView::getModView()
  692. {
  693. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  694. assert(mainWindow);
  695. if(!mainWindow)
  696. return nullptr;
  697. return mainWindow->getModView();
  698. }
  699. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  700. {
  701. return getModView() && getModView()->isModAvailable(modID);
  702. }
  703. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  704. {
  705. activateTabHeroesData();
  706. }
  707. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  708. {
  709. QStringList modsToInstall;
  710. if(ui->buttonPresetLanguage->isChecked() && checkCanInstallTranslation())
  711. modsToInstall.push_back(findTranslationModName());
  712. if(ui->buttonPresetExtras->isChecked() && checkCanInstallExtras())
  713. modsToInstall.push_back("vcmi-extras");
  714. if(ui->buttonPresetDemo->isChecked() && checkCanInstallDemo())
  715. modsToInstall.push_back("demo-support");
  716. if(ui->buttonPresetWog->isChecked() && checkCanInstallWog())
  717. modsToInstall.push_back("wake-of-gods");
  718. if(ui->buttonPresetHota->isChecked() && checkCanInstallHota())
  719. modsToInstall.push_back("hota");
  720. if(ui->buttonPresetTow->isChecked() && checkCanInstallTow())
  721. modsToInstall.push_back("tides-of-war");
  722. if(ui->buttonPresetFod->isChecked() && checkCanInstallFod())
  723. modsToInstall.push_back("fallen-of-the-depth");
  724. bool goToMods = !modsToInstall.empty();
  725. exitSetup(goToMods);
  726. for(auto const & modName : modsToInstall)
  727. getModView()->doInstallMod(modName);
  728. }
  729. void FirstLaunchView::on_pushButtonDiscord_clicked()
  730. {
  731. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  732. }
  733. void FirstLaunchView::on_pushButtonGithub_clicked()
  734. {
  735. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  736. }