firstlaunch_moc.cpp 17 KB

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