firstlaunch_moc.cpp 16 KB

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