firstlaunch_moc.cpp 16 KB

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