firstlaunch_moc.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. auto fileSelection = [this](QString type, QString filter, QString startPath = {}) {
  246. QString titleSel = tr("Select %1 file...", "param is file extension").arg(filter);
  247. QString titleErr = tr("You have to select %1 file!", "param is file extension").arg(filter);
  248. #if defined(VCMI_MOBILE)
  249. filter = "GOG file (*.*)";
  250. QMessageBox::information(this, tr("File selection"), titleSel);
  251. #endif
  252. QString file = QFileDialog::getOpenFileName(this, titleSel, startPath.isEmpty() ? QDir::homePath() : startPath, filter);
  253. if(file.isEmpty())
  254. return QString{};
  255. else if(!file.endsWith("." + type, Qt::CaseInsensitive))
  256. {
  257. QMessageBox::critical(this, tr("Invalid file selected"), titleErr);
  258. return QString{};
  259. }
  260. return file;
  261. };
  262. QString fileExe = fileSelection("exe", tr("GOG installer") + " (*.exe)");
  263. if(fileExe.isEmpty())
  264. return;
  265. QString fileBin = fileSelection("bin", tr("GOG data") + " (*.bin)", QFileInfo(fileExe).absolutePath());
  266. if(fileBin.isEmpty())
  267. return;
  268. ui->pushButtonGogInstall->setText(tr("Installing... Please wait!"));
  269. QPalette pal = ui->pushButtonGogInstall->palette();
  270. pal.setColor(QPalette::Button, QColor(Qt::yellow));
  271. ui->pushButtonGogInstall->setAutoFillBackground(true);
  272. ui->pushButtonGogInstall->setPalette(pal);
  273. ui->pushButtonGogInstall->update();
  274. ui->pushButtonGogInstall->repaint();
  275. QTimer::singleShot(100, this, [this, fileExe, fileBin](){ // background to make sure FileDialog is closed...
  276. QTemporaryDir dir;
  277. if(dir.isValid()) {
  278. QDir tempDir{dir.path()};
  279. QString tmpFileExe = dir.filePath("h3_gog.exe");
  280. QFile(fileExe).copy(tmpFileExe);
  281. QFile(fileBin).copy(dir.filePath("h3_gog-1.bin"));
  282. ::extract_options o;
  283. o.extract = true;
  284. // standard settings
  285. o.gog_galaxy = true;
  286. o.codepage = 0U;
  287. o.output_dir = dir.path().toStdString();
  288. o.extract_temp = true;
  289. o.extract_unknown = true;
  290. o.filenames.set_expand(true);
  291. o.preserve_file_times = true; // also correctly closes file -> without it: on Windows the files are not written completly
  292. process_file(tmpFileExe.toStdString(), o);
  293. QStringList dirData = tempDir.entryList({"data"}, QDir::Filter::Dirs);
  294. if(dirData.empty() || QDir(tempDir.filePath(dirData.front())).entryList({"*.lod"}, QDir::Filter::Files).empty())
  295. {
  296. QMessageBox::critical(this, tr("No Heroes III data!"), tr("Selected files do not contain Heroes III data!"), QMessageBox::Ok, QMessageBox::Ok);
  297. ui->pushButtonGogInstall->setText(tr("Install GOG files"));
  298. return;
  299. }
  300. copyHeroesData(dir.path(), true);
  301. }
  302. });
  303. #endif
  304. }
  305. void FirstLaunchView::copyHeroesData(const QString & path, bool move)
  306. {
  307. QDir sourceRoot = QDir(path);
  308. if(path.isEmpty())
  309. sourceRoot.setPath(QFileDialog::getExistingDirectory(this, {}, {}, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
  310. if(!sourceRoot.exists())
  311. return;
  312. if (sourceRoot.dirName().compare("data", Qt::CaseInsensitive) == 0)
  313. {
  314. // We got Data folder. Possibly user selected "Data" folder of Heroes III install. Check whether valid data might exist 1 level above
  315. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  316. if (dirData.empty())
  317. {
  318. // This is "Data" folder without any "Data" folders inside. Try to check for data 1 level above
  319. sourceRoot.cdUp();
  320. }
  321. }
  322. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  323. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  324. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  325. if(dirData.empty())
  326. {
  327. 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."));
  328. return;
  329. }
  330. QDir sourceData = sourceRoot.filePath(dirData.front());
  331. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  332. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  333. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  334. if(sodFiles.empty())
  335. {
  336. if (roeFiles.empty())
  337. {
  338. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  339. 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."));
  340. return;
  341. }
  342. if (!hdFiles.empty())
  343. {
  344. // HD Edition contains only RoE data so we can't use even unmodified files from it
  345. 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."));
  346. return;
  347. }
  348. // RoE or some other unsupported edition. Demo version?
  349. 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."));
  350. return;
  351. }
  352. QStringList copyDirectories;
  353. copyDirectories += dirData.front();
  354. if (!dirMaps.empty())
  355. copyDirectories += dirMaps.front();
  356. if (!dirMp3.empty())
  357. copyDirectories += dirMp3.front();
  358. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  359. for(const QString & dirName : copyDirectories)
  360. {
  361. QDir sourceDir = sourceRoot.filePath(dirName);
  362. QDir targetDir = targetRoot.filePath(dirName);
  363. if(!targetRoot.exists(dirName))
  364. targetRoot.mkdir(dirName);
  365. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  366. {
  367. QFile sourceFile(sourceDir.filePath(filename));
  368. if(move)
  369. sourceFile.rename(targetDir.filePath(filename));
  370. else
  371. sourceFile.copy(targetDir.filePath(filename));
  372. }
  373. }
  374. heroesDataUpdate();
  375. }
  376. // Tab Mod Preset
  377. void FirstLaunchView::modPresetUpdate()
  378. {
  379. bool translationExists = !findTranslationModName().isEmpty();
  380. ui->labelPresetLanguage->setVisible(translationExists);
  381. ui->labelPresetLanguageDescr->setVisible(translationExists);
  382. ui->checkBoxPresetLanguage->setVisible(translationExists);
  383. ui->checkBoxPresetLanguage->setEnabled(checkCanInstallTranslation());
  384. ui->checkBoxPresetExtras->setEnabled(checkCanInstallExtras());
  385. ui->checkBoxPresetHota->setEnabled(checkCanInstallHota());
  386. ui->checkBoxPresetWog->setEnabled(checkCanInstallWog());
  387. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  388. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  389. exitSetup();
  390. }
  391. QString FirstLaunchView::findTranslationModName()
  392. {
  393. if (!getModView())
  394. return QString();
  395. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  396. QString installedlanguage = QString::fromStdString(settings["session"]["language"].String());
  397. if (preferredlanguage == installedlanguage)
  398. return QString();
  399. return getModView()->getTranslationModName(preferredlanguage);
  400. }
  401. bool FirstLaunchView::checkCanInstallTranslation()
  402. {
  403. QString modName = findTranslationModName();
  404. if(modName.isEmpty())
  405. return false;
  406. return checkCanInstallMod(modName);
  407. }
  408. bool FirstLaunchView::checkCanInstallWog()
  409. {
  410. return checkCanInstallMod("wake-of-gods");
  411. }
  412. bool FirstLaunchView::checkCanInstallHota()
  413. {
  414. return checkCanInstallMod("hota");
  415. }
  416. bool FirstLaunchView::checkCanInstallExtras()
  417. {
  418. return checkCanInstallMod("vcmi-extras");
  419. }
  420. CModListView * FirstLaunchView::getModView()
  421. {
  422. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  423. assert(mainWindow);
  424. if (!mainWindow)
  425. return nullptr;
  426. return mainWindow->getModView();
  427. }
  428. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  429. {
  430. return getModView() && getModView()->isModAvailable(modID);
  431. }
  432. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  433. {
  434. activateTabHeroesData();
  435. }
  436. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  437. {
  438. QStringList modsToInstall;
  439. if (ui->checkBoxPresetLanguage->isChecked() && checkCanInstallTranslation())
  440. modsToInstall.push_back(findTranslationModName());
  441. if (ui->checkBoxPresetExtras->isChecked() && checkCanInstallExtras())
  442. modsToInstall.push_back("vcmi-extras");
  443. if (ui->checkBoxPresetWog->isChecked() && checkCanInstallWog())
  444. modsToInstall.push_back("wake-of-gods");
  445. if (ui->checkBoxPresetHota->isChecked() && checkCanInstallHota())
  446. modsToInstall.push_back("hota");
  447. exitSetup();
  448. for (auto const & modName : modsToInstall)
  449. getModView()->doInstallMod(modName);
  450. }
  451. void FirstLaunchView::on_pushButtonDiscord_clicked()
  452. {
  453. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  454. }
  455. void FirstLaunchView::on_pushButtonSlack_clicked()
  456. {
  457. QDesktopServices::openUrl(QUrl("https://slack.vcmi.eu/"));
  458. }
  459. void FirstLaunchView::on_pushButtonGithub_clicked()
  460. {
  461. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  462. }