firstlaunch_moc.cpp 16 KB

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