firstlaunch_moc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. FirstLaunchView::FirstLaunchView(QWidget * parent)
  22. : QWidget(parent)
  23. , ui(new Ui::FirstLaunchView)
  24. {
  25. ui->setupUi(this);
  26. enterSetup();
  27. activateTabLanguage();
  28. ui->lineEditDataSystem->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().dataPaths().front())));
  29. ui->lineEditDataUser->setText(pathToQString(boost::filesystem::absolute(VCMIDirs::get().userDataPath())));
  30. }
  31. void FirstLaunchView::on_buttonTabLanguage_clicked()
  32. {
  33. activateTabLanguage();
  34. }
  35. void FirstLaunchView::on_buttonTabHeroesData_clicked()
  36. {
  37. activateTabHeroesData();
  38. }
  39. void FirstLaunchView::on_buttonTabModPreset_clicked()
  40. {
  41. activateTabModPreset();
  42. }
  43. void FirstLaunchView::on_listWidgetLanguage_currentRowChanged(int currentRow)
  44. {
  45. languageSelected(ui->listWidgetLanguage->item(currentRow)->data(Qt::UserRole).toString());
  46. }
  47. void FirstLaunchView::changeEvent(QEvent * event)
  48. {
  49. if(event->type() == QEvent::LanguageChange)
  50. {
  51. ui->retranslateUi(this);
  52. Languages::fillLanguages(ui->listWidgetLanguage, false);
  53. }
  54. QWidget::changeEvent(event);
  55. }
  56. void FirstLaunchView::on_pushButtonLanguageNext_clicked()
  57. {
  58. activateTabHeroesData();
  59. }
  60. void FirstLaunchView::on_pushButtonDataNext_clicked()
  61. {
  62. activateTabModPreset();
  63. }
  64. void FirstLaunchView::on_pushButtonDataBack_clicked()
  65. {
  66. activateTabLanguage();
  67. }
  68. void FirstLaunchView::on_pushButtonDataSearch_clicked()
  69. {
  70. heroesDataUpdate();
  71. }
  72. void FirstLaunchView::on_pushButtonDataCopy_clicked()
  73. {
  74. copyHeroesData();
  75. }
  76. void FirstLaunchView::on_pushButtonDataHelp_clicked()
  77. {
  78. static const QUrl vcmibuilderWiki("https://github.com/vcmi/vcmi/blob/master/docs/players/Installation_Linux.md#install-data-using-vcmibuilder-script");
  79. QDesktopServices::openUrl(vcmibuilderWiki);
  80. }
  81. void FirstLaunchView::on_comboBoxLanguage_currentIndexChanged(int index)
  82. {
  83. forceHeroesLanguage(ui->comboBoxLanguage->itemData(index).toString());
  84. }
  85. void FirstLaunchView::enterSetup()
  86. {
  87. Languages::fillLanguages(ui->listWidgetLanguage, false);
  88. }
  89. void FirstLaunchView::setSetupProgress(int progress)
  90. {
  91. ui->buttonTabLanguage->setDisabled(progress < 1);
  92. ui->buttonTabHeroesData->setDisabled(progress < 2);
  93. ui->buttonTabModPreset->setDisabled(progress < 3);
  94. }
  95. void FirstLaunchView::activateTabLanguage()
  96. {
  97. setSetupProgress(1);
  98. ui->installerTabs->setCurrentIndex(0);
  99. ui->buttonTabLanguage->setChecked(true);
  100. ui->buttonTabHeroesData->setChecked(false);
  101. ui->buttonTabModPreset->setChecked(false);
  102. }
  103. void FirstLaunchView::activateTabHeroesData()
  104. {
  105. setSetupProgress(2);
  106. ui->installerTabs->setCurrentIndex(1);
  107. ui->buttonTabLanguage->setChecked(false);
  108. ui->buttonTabHeroesData->setChecked(true);
  109. ui->buttonTabModPreset->setChecked(false);
  110. if(!hasVCMIBuilderScript)
  111. {
  112. ui->pushButtonDataHelp->hide();
  113. ui->labelDataHelp->hide();
  114. }
  115. if(heroesDataUpdate())
  116. return;
  117. QString installPath = getHeroesInstallDir();
  118. if(!installPath.isEmpty())
  119. {
  120. auto reply = QMessageBox::question(this, tr("Heroes III installation found!"), tr("Copy data to VCMI folder?"), QMessageBox::Yes | QMessageBox::No);
  121. if(reply == QMessageBox::Yes)
  122. copyHeroesData(installPath);
  123. }
  124. }
  125. void FirstLaunchView::activateTabModPreset()
  126. {
  127. setSetupProgress(3);
  128. ui->installerTabs->setCurrentIndex(2);
  129. ui->buttonTabLanguage->setChecked(false);
  130. ui->buttonTabHeroesData->setChecked(false);
  131. ui->buttonTabModPreset->setChecked(true);
  132. modPresetUpdate();
  133. }
  134. void FirstLaunchView::exitSetup()
  135. {
  136. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  137. mainWindow->exitSetup();
  138. }
  139. // Tab Language
  140. void FirstLaunchView::languageSelected(const QString & selectedLanguage)
  141. {
  142. Settings node = settings.write["general"]["language"];
  143. node->String() = selectedLanguage.toStdString();
  144. if(auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow()))
  145. mainWindow->updateTranslation();
  146. }
  147. bool FirstLaunchView::heroesDataUpdate()
  148. {
  149. bool detected = heroesDataDetect();
  150. if(detected)
  151. heroesDataDetected();
  152. else
  153. heroesDataMissing();
  154. return detected;
  155. }
  156. void FirstLaunchView::heroesDataMissing()
  157. {
  158. QPalette newPalette = palette();
  159. newPalette.setColor(QPalette::Base, QColor(200, 50, 50));
  160. ui->lineEditDataSystem->setPalette(newPalette);
  161. ui->lineEditDataUser->setPalette(newPalette);
  162. ui->pushButtonDataSearch->setVisible(true);
  163. ui->pushButtonDataCopy->setVisible(true);
  164. ui->labelDataSearch->setVisible(true);
  165. ui->labelDataCopy->setVisible(true);
  166. ui->labelDataFound->setVisible(false);
  167. ui->pushButtonDataNext->setEnabled(false);
  168. if(hasVCMIBuilderScript)
  169. {
  170. ui->pushButtonDataHelp->setVisible(true);
  171. ui->labelDataHelp->setVisible(true);
  172. }
  173. }
  174. void FirstLaunchView::heroesDataDetected()
  175. {
  176. QPalette newPalette = palette();
  177. newPalette.setColor(QPalette::Base, QColor(50, 200, 50));
  178. ui->lineEditDataSystem->setPalette(newPalette);
  179. ui->lineEditDataUser->setPalette(newPalette);
  180. ui->pushButtonDataSearch->setVisible(false);
  181. ui->pushButtonDataCopy->setVisible(false);
  182. ui->labelDataSearch->setVisible(false);
  183. ui->labelDataCopy->setVisible(false);
  184. if(hasVCMIBuilderScript)
  185. {
  186. ui->pushButtonDataHelp->setVisible(false);
  187. ui->labelDataHelp->setVisible(false);
  188. }
  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::copyHeroesData(const QString & path)
  232. {
  233. QDir sourceRoot = QDir(path);
  234. if(path.isEmpty())
  235. sourceRoot.setPath(QFileDialog::getExistingDirectory(this, {}, {}, QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks));
  236. if(!sourceRoot.exists())
  237. return;
  238. if (sourceRoot.dirName().compare("data", Qt::CaseInsensitive) == 0)
  239. {
  240. // We got Data folder. Possibly user selected "Data" folder of Heroes III install. Check whether valid data might exist 1 level above
  241. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  242. if (dirData.empty())
  243. {
  244. // This is "Data" folder without any "Data" folders inside. Try to check for data 1 level above
  245. sourceRoot.cdUp();
  246. }
  247. }
  248. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  249. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  250. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  251. if(dirData.empty())
  252. {
  253. 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.");
  254. return;
  255. }
  256. QDir sourceData = sourceRoot.filePath(dirData.front());
  257. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  258. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  259. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  260. if(sodFiles.empty())
  261. {
  262. if (roeFiles.empty())
  263. {
  264. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  265. 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.");
  266. return;
  267. }
  268. if (!hdFiles.empty())
  269. {
  270. // HD Edition contains only RoE data so we can't use even unmodified files from it
  271. 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.");
  272. return;
  273. }
  274. // RoE or some other unsupported edition. Demo version?
  275. 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.");
  276. return;
  277. }
  278. QStringList copyDirectories;
  279. copyDirectories += dirData.front();
  280. if (!dirMaps.empty())
  281. copyDirectories += dirMaps.front();
  282. if (!dirMp3.empty())
  283. copyDirectories += dirMp3.front();
  284. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  285. for(const QString & dirName : copyDirectories)
  286. {
  287. QDir sourceDir = sourceRoot.filePath(dirName);
  288. QDir targetDir = targetRoot.filePath(dirName);
  289. if(!targetRoot.exists(dirName))
  290. targetRoot.mkdir(dirName);
  291. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  292. {
  293. QFile sourceFile(sourceDir.filePath(filename));
  294. sourceFile.copy(targetDir.filePath(filename));
  295. }
  296. }
  297. heroesDataUpdate();
  298. }
  299. // Tab Mod Preset
  300. void FirstLaunchView::modPresetUpdate()
  301. {
  302. bool translationExists = !findTranslationModName().isEmpty();
  303. ui->labelPresetLanguage->setVisible(translationExists);
  304. ui->labelPresetLanguageDescr->setVisible(translationExists);
  305. ui->checkBoxPresetLanguage->setVisible(translationExists);
  306. ui->checkBoxPresetLanguage->setEnabled(checkCanInstallTranslation());
  307. ui->checkBoxPresetExtras->setEnabled(checkCanInstallExtras());
  308. ui->checkBoxPresetHota->setEnabled(checkCanInstallHota());
  309. ui->checkBoxPresetWog->setEnabled(checkCanInstallWog());
  310. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  311. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  312. exitSetup();
  313. }
  314. QString FirstLaunchView::findTranslationModName()
  315. {
  316. if (!getModView())
  317. return QString();
  318. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  319. QString installedlanguage = QString::fromStdString(settings["session"]["language"].String());
  320. if (preferredlanguage == installedlanguage)
  321. return QString();
  322. return getModView()->getTranslationModName(preferredlanguage);
  323. }
  324. bool FirstLaunchView::checkCanInstallTranslation()
  325. {
  326. QString modName = findTranslationModName();
  327. if(modName.isEmpty())
  328. return false;
  329. return checkCanInstallMod(modName);
  330. }
  331. bool FirstLaunchView::checkCanInstallWog()
  332. {
  333. return checkCanInstallMod("wake-of-gods");
  334. }
  335. bool FirstLaunchView::checkCanInstallHota()
  336. {
  337. return checkCanInstallMod("hota");
  338. }
  339. bool FirstLaunchView::checkCanInstallExtras()
  340. {
  341. return checkCanInstallMod("vcmi-extras");
  342. }
  343. CModListView * FirstLaunchView::getModView()
  344. {
  345. auto * mainWindow = dynamic_cast<MainWindow *>(QApplication::activeWindow());
  346. assert(mainWindow);
  347. if (!mainWindow)
  348. return nullptr;
  349. return mainWindow->getModView();
  350. }
  351. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  352. {
  353. return getModView() && getModView()->isModAvailable(modID);
  354. }
  355. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  356. {
  357. activateTabHeroesData();
  358. }
  359. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  360. {
  361. QStringList modsToInstall;
  362. if (ui->checkBoxPresetLanguage->isChecked() && checkCanInstallTranslation())
  363. modsToInstall.push_back(findTranslationModName());
  364. if (ui->checkBoxPresetExtras->isChecked() && checkCanInstallExtras())
  365. modsToInstall.push_back("vcmi-extras");
  366. if (ui->checkBoxPresetWog->isChecked() && checkCanInstallWog())
  367. modsToInstall.push_back("wake-of-gods");
  368. if (ui->checkBoxPresetHota->isChecked() && checkCanInstallHota())
  369. modsToInstall.push_back("hota");
  370. exitSetup();
  371. for (auto const & modName : modsToInstall)
  372. getModView()->doInstallMod(modName);
  373. }
  374. void FirstLaunchView::on_pushButtonDiscord_clicked()
  375. {
  376. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  377. }
  378. void FirstLaunchView::on_pushButtonSlack_clicked()
  379. {
  380. QDesktopServices::openUrl(QUrl("https://slack.vcmi.eu/"));
  381. }
  382. void FirstLaunchView::on_pushButtonGithub_clicked()
  383. {
  384. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  385. }