firstlaunch_moc.cpp 17 KB

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