firstlaunch_moc.cpp 17 KB

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