firstlaunch_moc.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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/CGeneralTextHandler.h"
  17. #include "../../lib/Languages.h"
  18. #include "../../lib/VCMIDirs.h"
  19. #include "../../lib/filesystem/Filesystem.h"
  20. #include "../helper.h"
  21. #include "../languages.h"
  22. #ifdef ENABLE_INNOEXTRACT
  23. #include "cli/extract.hpp"
  24. #endif
  25. #ifdef VCMI_ANDROID
  26. #include <QAndroidJniObject>
  27. #include <QtAndroid>
  28. static FirstLaunchView * thiz;
  29. extern "C" JNIEXPORT void JNICALL Java_eu_vcmi_vcmi_NativeMethods_heroesDataUpdate(JNIEnv * env, jclass cls)
  30. {
  31. thiz->heroesDataUpdate();
  32. }
  33. #endif
  34. FirstLaunchView::FirstLaunchView(QWidget * parent)
  35. : QWidget(parent)
  36. , ui(new Ui::FirstLaunchView)
  37. {
  38. ui->setupUi(this);
  39. enterSetup();
  40. activateTabLanguage();
  41. ui->lineEditDataSystem->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().dataPaths().front())));
  42. ui->lineEditDataUser->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().userDataPath())));
  43. Helper::enableScrollBySwiping(ui->listWidgetLanguage);
  44. #ifdef VCMI_MOBILE
  45. // This directory is not accessible to players without rooting of their device
  46. ui->lineEditDataSystem->hide();
  47. #endif
  48. #ifndef ENABLE_INNOEXTRACT
  49. ui->pushButtonGogInstall->hide();
  50. ui->labelDataGogTitle->hide();
  51. ui->labelDataGogDescr->hide();
  52. #endif
  53. }
  54. void FirstLaunchView::on_buttonTabLanguage_clicked()
  55. {
  56. activateTabLanguage();
  57. }
  58. void FirstLaunchView::on_buttonTabHeroesData_clicked()
  59. {
  60. activateTabHeroesData();
  61. }
  62. void FirstLaunchView::on_buttonTabModPreset_clicked()
  63. {
  64. activateTabModPreset();
  65. }
  66. void FirstLaunchView::on_listWidgetLanguage_currentRowChanged(int currentRow)
  67. {
  68. languageSelected(ui->listWidgetLanguage->item(currentRow)->data(Qt::UserRole).toString());
  69. }
  70. void FirstLaunchView::changeEvent(QEvent * event)
  71. {
  72. if(event->type() == QEvent::LanguageChange)
  73. {
  74. ui->retranslateUi(this);
  75. Languages::fillLanguages(ui->listWidgetLanguage, false);
  76. }
  77. QWidget::changeEvent(event);
  78. }
  79. void FirstLaunchView::on_pushButtonLanguageNext_clicked()
  80. {
  81. activateTabHeroesData();
  82. }
  83. void FirstLaunchView::on_pushButtonDataNext_clicked()
  84. {
  85. activateTabModPreset();
  86. }
  87. void FirstLaunchView::on_pushButtonDataBack_clicked()
  88. {
  89. activateTabLanguage();
  90. }
  91. void FirstLaunchView::on_pushButtonDataSearch_clicked()
  92. {
  93. heroesDataUpdate();
  94. }
  95. void FirstLaunchView::on_pushButtonDataCopy_clicked()
  96. {
  97. #ifdef VCMI_ANDROID
  98. thiz = this;
  99. QtAndroid::androidActivity().callMethod<void>("copyHeroesData");
  100. #else
  101. // iOS can't display modal dialogs when called directly on button press
  102. // https://bugreports.qt.io/browse/QTBUG-98651
  103. QTimer::singleShot(0, this, [this]{ copyHeroesData(); });
  104. #endif
  105. }
  106. void FirstLaunchView::on_pushButtonGogInstall_clicked()
  107. {
  108. // iOS can't display modal dialogs when called directly on button press
  109. // https://bugreports.qt.io/browse/QTBUG-98651
  110. QTimer::singleShot(0, this, &FirstLaunchView::extractGogData);
  111. }
  112. void FirstLaunchView::enterSetup()
  113. {
  114. Languages::fillLanguages(ui->listWidgetLanguage, false);
  115. }
  116. void FirstLaunchView::setSetupProgress(int progress)
  117. {
  118. ui->buttonTabLanguage->setDisabled(progress < 1);
  119. ui->buttonTabHeroesData->setDisabled(progress < 2);
  120. ui->buttonTabModPreset->setDisabled(progress < 3);
  121. }
  122. void FirstLaunchView::activateTabLanguage()
  123. {
  124. setSetupProgress(1);
  125. ui->installerTabs->setCurrentIndex(0);
  126. ui->buttonTabLanguage->setChecked(true);
  127. ui->buttonTabHeroesData->setChecked(false);
  128. ui->buttonTabModPreset->setChecked(false);
  129. }
  130. void FirstLaunchView::activateTabHeroesData()
  131. {
  132. setSetupProgress(2);
  133. ui->installerTabs->setCurrentIndex(1);
  134. ui->buttonTabLanguage->setChecked(false);
  135. ui->buttonTabHeroesData->setChecked(true);
  136. ui->buttonTabModPreset->setChecked(false);
  137. if(heroesDataUpdate())
  138. {
  139. activateTabModPreset();
  140. return;
  141. }
  142. QString installPath = getHeroesInstallDir();
  143. if(!installPath.isEmpty())
  144. {
  145. auto reply = QMessageBox::question(this, tr("Heroes III installation found!"), tr("Copy data to VCMI folder?"), QMessageBox::Yes | QMessageBox::No);
  146. if(reply == QMessageBox::Yes)
  147. copyHeroesData(installPath);
  148. }
  149. }
  150. void FirstLaunchView::activateTabModPreset()
  151. {
  152. setSetupProgress(3);
  153. ui->installerTabs->setCurrentIndex(2);
  154. ui->buttonTabLanguage->setChecked(false);
  155. ui->buttonTabHeroesData->setChecked(false);
  156. ui->buttonTabModPreset->setChecked(true);
  157. modPresetUpdate();
  158. }
  159. void FirstLaunchView::exitSetup()
  160. {
  161. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  162. mainWindow->exitSetup();
  163. }
  164. // Tab Language
  165. void FirstLaunchView::languageSelected(const QString & selectedLanguage)
  166. {
  167. Settings node = settings.write["general"]["language"];
  168. node->String() = selectedLanguage.toStdString();
  169. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  170. mainWindow->updateTranslation();
  171. }
  172. bool FirstLaunchView::heroesDataUpdate()
  173. {
  174. bool detected = heroesDataDetect();
  175. if(detected)
  176. heroesDataDetected();
  177. else
  178. heroesDataMissing();
  179. return detected;
  180. }
  181. void FirstLaunchView::heroesDataMissing()
  182. {
  183. QPalette newPalette = palette();
  184. newPalette.setColor(QPalette::Base, QColor(200, 50, 50));
  185. ui->lineEditDataSystem->setPalette(newPalette);
  186. ui->lineEditDataUser->setPalette(newPalette);
  187. ui->labelDataManualTitle->setVisible(true);
  188. ui->labelDataManualDescr->setVisible(true);
  189. ui->pushButtonDataSearch->setVisible(true);
  190. #ifdef VCMI_ANDROID
  191. // selecting directory with ACTION_OPEN_DOCUMENT_TREE is available only since API level 21
  192. bool canUseDataCopy = QtAndroid::androidSdkVersion() >= 21;
  193. #else
  194. bool canUseDataCopy = true;
  195. #endif
  196. ui->labelDataCopyTitle->setVisible(canUseDataCopy);
  197. ui->labelDataCopyDescr->setVisible(canUseDataCopy);
  198. ui->pushButtonDataCopy->setVisible(canUseDataCopy);
  199. #ifdef ENABLE_INNOEXTRACT
  200. ui->pushButtonGogInstall->setVisible(true);
  201. ui->labelDataGogTitle->setVisible(true);
  202. ui->labelDataGogDescr->setVisible(true);
  203. #endif
  204. ui->labelDataFound->setVisible(false);
  205. ui->pushButtonDataNext->setEnabled(false);
  206. }
  207. void FirstLaunchView::heroesDataDetected()
  208. {
  209. QPalette newPalette = palette();
  210. newPalette.setColor(QPalette::Base, QColor(50, 200, 50));
  211. ui->lineEditDataSystem->setPalette(newPalette);
  212. ui->lineEditDataUser->setPalette(newPalette);
  213. ui->pushButtonDataSearch->setVisible(false);
  214. ui->pushButtonDataCopy->setVisible(false);
  215. ui->labelDataManualTitle->setVisible(false);
  216. ui->labelDataManualDescr->setVisible(false);
  217. ui->labelDataCopyTitle->setVisible(false);
  218. ui->labelDataCopyDescr->setVisible(false);
  219. #ifdef ENABLE_INNOEXTRACT
  220. ui->pushButtonGogInstall->setVisible(false);
  221. ui->labelDataGogTitle->setVisible(false);
  222. ui->labelDataGogDescr->setVisible(false);
  223. #endif
  224. ui->labelDataFound->setVisible(true);
  225. ui->pushButtonDataNext->setEnabled(true);
  226. CGeneralTextHandler::detectInstallParameters();
  227. }
  228. // Tab Heroes III Data
  229. bool FirstLaunchView::heroesDataDetect()
  230. {
  231. // user might have copied files to one of our data path.
  232. // perform full reinitialization of virtual filesystem
  233. CResourceHandler::destroy();
  234. CResourceHandler::initialize();
  235. CResourceHandler::load("config/filesystem.json");
  236. // use file from lod archive to check presence of H3 data. Very rough estimate, but will work in majority of cases
  237. bool heroesDataFoundROE = CResourceHandler::get()->existsResource(ResourcePath("DATA/GENRLTXT.TXT"));
  238. bool heroesDataFoundSOD = CResourceHandler::get()->existsResource(ResourcePath("DATA/TENTCOLR.TXT"));
  239. return heroesDataFoundROE && heroesDataFoundSOD;
  240. }
  241. QString FirstLaunchView::getHeroesInstallDir()
  242. {
  243. #ifdef VCMI_WINDOWS
  244. QString gogPath = QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\GOG.com\\Games\\1207658787", QSettings::NativeFormat).value("path").toString();
  245. if(!gogPath.isEmpty())
  246. return gogPath;
  247. QString cdPath = QSettings("HKEY_LOCAL_MACHINE\\SOFTWARE\\New World Computing\\Heroes of Might and Magic® III\\1.0", QSettings::NativeFormat).value("AppPath").toString();
  248. if(!cdPath.isEmpty())
  249. return cdPath;
  250. #endif
  251. return QString{};
  252. }
  253. void FirstLaunchView::extractGogData()
  254. {
  255. #ifdef ENABLE_INNOEXTRACT
  256. auto fileSelection = [this](QString type, QString filter, QString startPath = {}) {
  257. QString titleSel = tr("Select %1 file...", "param is file extension").arg(filter);
  258. QString titleErr = tr("You have to select %1 file!", "param is file extension").arg(filter);
  259. #if defined(VCMI_MOBILE)
  260. filter = tr("GOG file (*.*)");
  261. QMessageBox::information(this, tr("File selection"), titleSel);
  262. #endif
  263. QString file = QFileDialog::getOpenFileName(this, titleSel, startPath.isEmpty() ? QDir::homePath() : startPath, filter);
  264. if(file.isEmpty())
  265. return QString{};
  266. else if(!file.endsWith("." + type, Qt::CaseInsensitive))
  267. {
  268. QMessageBox::critical(this, tr("Invalid file selected"), titleErr);
  269. return QString{};
  270. }
  271. return file;
  272. };
  273. QString fileExe = fileSelection("exe", tr("GOG installer") + " (*.exe)");
  274. if(fileExe.isEmpty())
  275. return;
  276. QString fileBin = fileSelection("bin", tr("GOG data") + " (*.bin)", QFileInfo(fileExe).absolutePath());
  277. if(fileBin.isEmpty())
  278. return;
  279. ui->progressBarGog->setVisible(true);
  280. ui->pushButtonGogInstall->setVisible(false);
  281. setEnabled(false);
  282. QTimer::singleShot(100, this, [this, fileExe, fileBin](){ // background to make sure FileDialog is closed...
  283. QDir tempDir(pathToQString(VCMIDirs::get().userDataPath()));
  284. tempDir.mkdir("tmp");
  285. if(!tempDir.cd("tmp"))
  286. return; // should not happen - but avoid deleting wrong folder in any case
  287. QString tmpFileExe = tempDir.filePath("h3_gog.exe");
  288. QFile(fileExe).copy(tmpFileExe);
  289. QFile(fileBin).copy(tempDir.filePath("h3_gog-1.bin"));
  290. ::extract_options o;
  291. o.extract = true;
  292. // standard settings
  293. o.gog_galaxy = true;
  294. o.codepage = 0U;
  295. o.output_dir = tempDir.path().toStdString();
  296. o.extract_temp = true;
  297. o.extract_unknown = true;
  298. o.filenames.set_expand(true);
  299. o.preserve_file_times = true; // also correctly closes file -> without it: on Windows the files are not written completly
  300. process_file(tmpFileExe.toStdString(), o, [this](float progress) {
  301. ui->progressBarGog->setValue(progress * 100);
  302. qApp->processEvents();
  303. });
  304. ui->progressBarGog->setVisible(false);
  305. ui->pushButtonGogInstall->setVisible(true);
  306. setEnabled(true);
  307. QStringList dirData = tempDir.entryList({"data"}, QDir::Filter::Dirs);
  308. if(dirData.empty() || QDir(tempDir.filePath(dirData.front())).entryList({"*.lod"}, QDir::Filter::Files).empty())
  309. {
  310. QMessageBox::critical(this, tr("No Heroes III data!"), tr("Selected files do not contain Heroes III data!"), QMessageBox::Ok, QMessageBox::Ok);
  311. tempDir.removeRecursively();
  312. return;
  313. }
  314. copyHeroesData(tempDir.path(), true);
  315. tempDir.removeRecursively();
  316. });
  317. #endif
  318. }
  319. void FirstLaunchView::copyHeroesData(const QString & path, bool move)
  320. {
  321. QDir sourceRoot{path};
  322. if(path.isEmpty())
  323. sourceRoot.setPath(QFileDialog::getExistingDirectory(this, {}, {}, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
  324. if(!sourceRoot.exists())
  325. return;
  326. if (sourceRoot.dirName().compare("data", Qt::CaseInsensitive) == 0)
  327. {
  328. // We got Data folder. Possibly user selected "Data" folder of Heroes III install. Check whether valid data might exist 1 level above
  329. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  330. if (dirData.empty())
  331. {
  332. // This is "Data" folder without any "Data" folders inside. Try to check for data 1 level above
  333. sourceRoot.cdUp();
  334. }
  335. }
  336. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  337. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  338. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  339. const auto noDataMessage = tr("Failed to detect valid Heroes III data in chosen directory.\nPlease select directory with installed Heroes III data.");
  340. if(dirData.empty())
  341. {
  342. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  343. return;
  344. }
  345. QDir sourceData = sourceRoot.filePath(dirData.front());
  346. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  347. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  348. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  349. if(sodFiles.empty())
  350. {
  351. if (roeFiles.empty())
  352. {
  353. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  354. QMessageBox::critical(this, tr("Heroes III data not found!"), noDataMessage);
  355. return;
  356. }
  357. if (!hdFiles.empty())
  358. {
  359. // HD Edition contains only RoE data so we can't use even unmodified files from it
  360. QMessageBox::critical(this, tr("Heroes III data not found!"), tr("Heroes III: HD Edition files are not supported by VCMI.\nPlease select directory with Heroes III: Complete Edition or Heroes III: Shadow of Death."));
  361. return;
  362. }
  363. // RoE or some other unsupported edition. Demo version?
  364. QMessageBox::critical(this, tr("Heroes III data not found!"), tr("Unknown or unsupported Heroes III version found.\nPlease select directory with Heroes III: Complete Edition or Heroes III: Shadow of Death."));
  365. return;
  366. }
  367. QStringList copyDirectories;
  368. copyDirectories += dirData.front();
  369. if (!dirMaps.empty())
  370. copyDirectories += dirMaps.front();
  371. if (!dirMp3.empty())
  372. copyDirectories += dirMp3.front();
  373. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  374. for(const QString & dirName : copyDirectories)
  375. {
  376. QDir sourceDir = sourceRoot.filePath(dirName);
  377. QDir targetDir = targetRoot.filePath(dirName);
  378. if(!targetRoot.exists(dirName))
  379. targetRoot.mkdir(dirName);
  380. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  381. {
  382. QFile sourceFile(sourceDir.filePath(filename));
  383. if(move)
  384. sourceFile.rename(targetDir.filePath(filename));
  385. else
  386. sourceFile.copy(targetDir.filePath(filename));
  387. }
  388. }
  389. heroesDataUpdate();
  390. }
  391. // Tab Mod Preset
  392. void FirstLaunchView::modPresetUpdate()
  393. {
  394. bool translationExists = !findTranslationModName().isEmpty();
  395. ui->labelPresetLanguageDescr->setVisible(translationExists);
  396. ui->buttonPresetLanguage->setVisible(translationExists);
  397. ui->buttonPresetLanguage->setVisible(checkCanInstallTranslation());
  398. ui->buttonPresetExtras->setVisible(checkCanInstallExtras());
  399. ui->buttonPresetHota->setVisible(checkCanInstallHota());
  400. ui->buttonPresetWog->setVisible(checkCanInstallWog());
  401. ui->labelPresetLanguageDescr->setVisible(checkCanInstallTranslation());
  402. ui->labelPresetExtrasDescr->setVisible(checkCanInstallExtras());
  403. ui->labelPresetHotaDescr->setVisible(checkCanInstallHota());
  404. ui->labelPresetWogDescr->setVisible(checkCanInstallWog());
  405. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  406. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  407. exitSetup();
  408. }
  409. QString FirstLaunchView::findTranslationModName()
  410. {
  411. if (!getModView())
  412. return QString();
  413. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  414. QString installedlanguage = QString::fromStdString(settings["session"]["language"].String());
  415. if (preferredlanguage == installedlanguage)
  416. return QString();
  417. return getModView()->getTranslationModName(preferredlanguage);
  418. }
  419. bool FirstLaunchView::checkCanInstallTranslation()
  420. {
  421. QString modName = findTranslationModName();
  422. if(modName.isEmpty())
  423. return false;
  424. return checkCanInstallMod(modName);
  425. }
  426. bool FirstLaunchView::checkCanInstallWog()
  427. {
  428. return checkCanInstallMod("wake-of-gods");
  429. }
  430. bool FirstLaunchView::checkCanInstallHota()
  431. {
  432. return checkCanInstallMod("hota");
  433. }
  434. bool FirstLaunchView::checkCanInstallExtras()
  435. {
  436. return checkCanInstallMod("vcmi-extras");
  437. }
  438. CModListView * FirstLaunchView::getModView()
  439. {
  440. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  441. assert(mainWindow);
  442. if (!mainWindow)
  443. return nullptr;
  444. return mainWindow->getModView();
  445. }
  446. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  447. {
  448. return getModView() && getModView()->isModAvailable(modID);
  449. }
  450. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  451. {
  452. activateTabHeroesData();
  453. }
  454. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  455. {
  456. QStringList modsToInstall;
  457. if (ui->buttonPresetLanguage->isChecked() && checkCanInstallTranslation())
  458. modsToInstall.push_back(findTranslationModName());
  459. if (ui->buttonPresetExtras->isChecked() && checkCanInstallExtras())
  460. modsToInstall.push_back("vcmi-extras");
  461. if (ui->buttonPresetWog->isChecked() && checkCanInstallWog())
  462. modsToInstall.push_back("wake-of-gods");
  463. if (ui->buttonPresetHota->isChecked() && checkCanInstallHota())
  464. modsToInstall.push_back("hota");
  465. exitSetup();
  466. for (auto const & modName : modsToInstall)
  467. getModView()->doInstallMod(modName);
  468. }
  469. void FirstLaunchView::on_pushButtonDiscord_clicked()
  470. {
  471. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  472. }
  473. void FirstLaunchView::on_pushButtonSlack_clicked()
  474. {
  475. QDesktopServices::openUrl(QUrl("https://slack.vcmi.eu/"));
  476. }
  477. void FirstLaunchView::on_pushButtonGithub_clicked()
  478. {
  479. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  480. }