firstlaunch_moc.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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://wiki.vcmi.eu/Using_vcmibuilder");
  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. setupPlatformSettings();
  88. Languages::fillLanguages(ui->listWidgetLanguage, false);
  89. }
  90. void FirstLaunchView::setSetupProgress(int progress)
  91. {
  92. int value = std::max(progress, ui->setupProgressBar->value());
  93. ui->setupProgressBar->setValue(value);
  94. ui->buttonTabLanguage->setDisabled(value < 1);
  95. ui->buttonTabHeroesData->setDisabled(value < 2);
  96. ui->buttonTabModPreset->setDisabled(value < 3);
  97. }
  98. void FirstLaunchView::activateTabLanguage()
  99. {
  100. setSetupProgress(1);
  101. ui->installerTabs->setCurrentIndex(0);
  102. ui->buttonTabLanguage->setChecked(true);
  103. ui->buttonTabHeroesData->setChecked(false);
  104. ui->buttonTabModPreset->setChecked(false);
  105. }
  106. void FirstLaunchView::activateTabHeroesData()
  107. {
  108. setSetupProgress(2);
  109. ui->installerTabs->setCurrentIndex(1);
  110. ui->buttonTabLanguage->setChecked(false);
  111. ui->buttonTabHeroesData->setChecked(true);
  112. ui->buttonTabModPreset->setChecked(false);
  113. if(!hasVCMIBuilderScript)
  114. {
  115. ui->pushButtonDataHelp->hide();
  116. ui->labelDataHelp->hide();
  117. }
  118. heroesDataUpdate();
  119. }
  120. void FirstLaunchView::activateTabModPreset()
  121. {
  122. setSetupProgress(3);
  123. ui->installerTabs->setCurrentIndex(2);
  124. ui->buttonTabLanguage->setChecked(false);
  125. ui->buttonTabHeroesData->setChecked(false);
  126. ui->buttonTabModPreset->setChecked(true);
  127. modPresetUpdate();
  128. }
  129. void FirstLaunchView::exitSetup()
  130. {
  131. if(auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow()))
  132. mainWindow->exitSetup();
  133. }
  134. // Initial platform-dependend settings
  135. void FirstLaunchView::setupPlatformSettings()
  136. {
  137. #if defined(VCMI_MOBILE)
  138. bool touchscreenMode = true;
  139. #else
  140. bool touchscreenMode = false;
  141. #endif
  142. Settings node = settings.write["battle"]["touchscreenMode"];
  143. node->Bool() = touchscreenMode;
  144. }
  145. // Tab Language
  146. void FirstLaunchView::languageSelected(const QString & selectedLanguage)
  147. {
  148. Settings node = settings.write["general"]["language"];
  149. node->String() = selectedLanguage.toStdString();
  150. if(auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow()))
  151. mainWindow->updateTranslation();
  152. }
  153. void FirstLaunchView::heroesDataUpdate()
  154. {
  155. if(heroesDataDetect())
  156. heroesDataDetected();
  157. else
  158. heroesDataMissing();
  159. }
  160. void FirstLaunchView::heroesDataMissing()
  161. {
  162. QPalette newPalette = palette();
  163. newPalette.setColor(QPalette::Base, QColor(200, 50, 50));
  164. ui->lineEditDataSystem->setPalette(newPalette);
  165. ui->lineEditDataUser->setPalette(newPalette);
  166. ui->pushButtonDataSearch->setVisible(true);
  167. ui->pushButtonDataCopy->setVisible(true);
  168. ui->labelDataSearch->setVisible(true);
  169. ui->labelDataCopy->setVisible(true);
  170. ui->labelDataFound->setVisible(false);
  171. if(hasVCMIBuilderScript)
  172. {
  173. ui->pushButtonDataHelp->setVisible(true);
  174. ui->labelDataHelp->setVisible(true);
  175. }
  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. if(hasVCMIBuilderScript)
  188. {
  189. ui->pushButtonDataHelp->setVisible(false);
  190. ui->labelDataHelp->setVisible(false);
  191. }
  192. ui->labelDataFound->setVisible(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(ResourceID("DATA/GENRLTXT.TXT"));
  205. bool heroesDataFoundSOD = CResourceHandler::get()->existsResource(ResourceID("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. ui->pushButtonDataNext->setEnabled(success);
  216. }
  217. void FirstLaunchView::forceHeroesLanguage(const QString & language)
  218. {
  219. Settings node = settings.write["general"]["gameDataLanguage"];
  220. node->String() = language.toStdString();
  221. }
  222. void FirstLaunchView::copyHeroesData()
  223. {
  224. QDir sourceRoot = QFileDialog::getExistingDirectory(this, "", "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  225. if(!sourceRoot.exists())
  226. return;
  227. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  228. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  229. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  230. if(dirData.empty())
  231. {
  232. 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.");
  233. return;
  234. }
  235. QDir sourceData = sourceRoot.filePath(dirData.front());
  236. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  237. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  238. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  239. if(sodFiles.empty())
  240. {
  241. if (roeFiles.empty())
  242. {
  243. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  244. 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.");
  245. return;
  246. }
  247. if (!hdFiles.empty())
  248. {
  249. // HD Edition contains only RoE data so we can't use even unmodified files from it
  250. 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.");
  251. return;
  252. }
  253. // RoE or some other unsupported edition. Demo version?
  254. 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.");
  255. return;
  256. }
  257. QStringList copyDirectories;
  258. copyDirectories += dirData.front();
  259. if (!dirMaps.empty())
  260. copyDirectories += dirMaps.front();
  261. if (!dirMp3.empty())
  262. copyDirectories += dirMp3.front();
  263. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  264. for(const QString & dirName : copyDirectories)
  265. {
  266. QDir sourceDir = sourceRoot.filePath(dirName);
  267. QDir targetDir = targetRoot.filePath(dirName);
  268. if(!targetRoot.exists(dirName))
  269. targetRoot.mkdir(dirName);
  270. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  271. {
  272. QFile sourceFile(sourceDir.filePath(filename));
  273. sourceFile.copy(targetDir.filePath(filename));
  274. }
  275. }
  276. heroesDataUpdate();
  277. }
  278. // Tab Mod Preset
  279. void FirstLaunchView::modPresetUpdate()
  280. {
  281. bool translationExists = !findTranslationModName().isEmpty();
  282. ui->labelPresetLanguage->setVisible(translationExists);
  283. ui->labelPresetLanguageDescr->setVisible(translationExists);
  284. ui->checkBoxPresetLanguage->setVisible(translationExists);
  285. ui->checkBoxPresetLanguage->setEnabled(checkCanInstallTranslation());
  286. ui->checkBoxPresetExtras->setEnabled(checkCanInstallExtras());
  287. ui->checkBoxPresetHota->setEnabled(checkCanInstallHota());
  288. ui->checkBoxPresetWog->setEnabled(checkCanInstallWog());
  289. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  290. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  291. exitSetup();
  292. }
  293. QString FirstLaunchView::findTranslationModName()
  294. {
  295. if (!getModView())
  296. return QString();
  297. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  298. QString installedlanguage = QString::fromStdString(settings["session"]["language"].String());
  299. if (preferredlanguage == installedlanguage)
  300. return QString();
  301. return getModView()->getTranslationModName(preferredlanguage);
  302. }
  303. bool FirstLaunchView::checkCanInstallTranslation()
  304. {
  305. QString modName = findTranslationModName();
  306. if(modName.isEmpty())
  307. return false;
  308. return checkCanInstallMod(modName);
  309. }
  310. bool FirstLaunchView::checkCanInstallWog()
  311. {
  312. return checkCanInstallMod("wake-of-gods");
  313. }
  314. bool FirstLaunchView::checkCanInstallHota()
  315. {
  316. return checkCanInstallMod("hota");
  317. }
  318. bool FirstLaunchView::checkCanInstallExtras()
  319. {
  320. return checkCanInstallMod("vcmi-extras");
  321. }
  322. CModListView * FirstLaunchView::getModView()
  323. {
  324. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  325. assert(mainWindow);
  326. if (!mainWindow)
  327. return nullptr;
  328. return mainWindow->getModView();
  329. }
  330. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  331. {
  332. return getModView() && getModView()->isModAvailable(modID);
  333. }
  334. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  335. {
  336. activateTabHeroesData();
  337. }
  338. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  339. {
  340. QStringList modsToInstall;
  341. if (ui->checkBoxPresetLanguage->isChecked() && checkCanInstallTranslation())
  342. modsToInstall.push_back(findTranslationModName());
  343. if (ui->checkBoxPresetExtras->isChecked() && checkCanInstallExtras())
  344. modsToInstall.push_back("vcmi-extras");
  345. if (ui->checkBoxPresetWog->isChecked() && checkCanInstallWog())
  346. modsToInstall.push_back("wake-of-gods");
  347. if (ui->checkBoxPresetHota->isChecked() && checkCanInstallHota())
  348. modsToInstall.push_back("hota");
  349. exitSetup();
  350. for (auto const & modName : modsToInstall)
  351. getModView()->doInstallMod(modName);
  352. }
  353. void FirstLaunchView::on_pushButtonDiscord_clicked()
  354. {
  355. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  356. }
  357. void FirstLaunchView::on_pushButtonSlack_clicked()
  358. {
  359. QDesktopServices::openUrl(QUrl("https://slack.vcmi.eu/"));
  360. }
  361. void FirstLaunchView::on_pushButtonGithub_clicked()
  362. {
  363. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  364. }