firstlaunch_moc.cpp 16 KB

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