firstlaunch_moc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. ui->pushButtonDataNext->setEnabled(false);
  172. if(hasVCMIBuilderScript)
  173. {
  174. ui->pushButtonDataHelp->setVisible(true);
  175. ui->labelDataHelp->setVisible(true);
  176. }
  177. }
  178. void FirstLaunchView::heroesDataDetected()
  179. {
  180. QPalette newPalette = palette();
  181. newPalette.setColor(QPalette::Base, QColor(50, 200, 50));
  182. ui->lineEditDataSystem->setPalette(newPalette);
  183. ui->lineEditDataUser->setPalette(newPalette);
  184. ui->pushButtonDataSearch->setVisible(false);
  185. ui->pushButtonDataCopy->setVisible(false);
  186. ui->labelDataSearch->setVisible(false);
  187. ui->labelDataCopy->setVisible(false);
  188. if(hasVCMIBuilderScript)
  189. {
  190. ui->pushButtonDataHelp->setVisible(false);
  191. ui->labelDataHelp->setVisible(false);
  192. }
  193. ui->labelDataFound->setVisible(true);
  194. ui->pushButtonDataNext->setEnabled(true);
  195. heroesLanguageUpdate();
  196. }
  197. // Tab Heroes III Data
  198. bool FirstLaunchView::heroesDataDetect()
  199. {
  200. // user might have copied files to one of our data path.
  201. // perform full reinitialization of virtual filesystem
  202. CResourceHandler::destroy();
  203. CResourceHandler::initialize();
  204. CResourceHandler::load("config/filesystem.json");
  205. // use file from lod archive to check presence of H3 data. Very rough estimate, but will work in majority of cases
  206. bool heroesDataFoundROE = CResourceHandler::get()->existsResource(ResourceID("DATA/GENRLTXT.TXT"));
  207. bool heroesDataFoundSOD = CResourceHandler::get()->existsResource(ResourceID("DATA/TENTCOLR.TXT"));
  208. return heroesDataFoundROE && heroesDataFoundSOD;
  209. }
  210. void FirstLaunchView::heroesLanguageUpdate()
  211. {
  212. Languages::fillLanguages(ui->comboBoxLanguage, true);
  213. QString language = Languages::getHeroesDataLanguage();
  214. bool success = !language.isEmpty();
  215. ui->labelDataFailure->setVisible(!success);
  216. ui->labelDataSuccess->setVisible(success);
  217. }
  218. void FirstLaunchView::forceHeroesLanguage(const QString & language)
  219. {
  220. Settings node = settings.write["general"]["gameDataLanguage"];
  221. node->String() = language.toStdString();
  222. }
  223. void FirstLaunchView::copyHeroesData()
  224. {
  225. QDir sourceRoot = QFileDialog::getExistingDirectory(this, "", "", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
  226. if(!sourceRoot.exists())
  227. return;
  228. if (sourceRoot.dirName().compare("data", Qt::CaseInsensitive) == 0)
  229. {
  230. // We got Data folder. Possibly user selected "Data" folder of Heroes III install. Check whether valid data might exist 1 level above
  231. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  232. if (dirData.empty())
  233. {
  234. // This is "Data" folder without any "Data" folders inside. Try to check for data 1 level above
  235. sourceRoot.cdUp();
  236. }
  237. }
  238. QStringList dirData = sourceRoot.entryList({"data"}, QDir::Filter::Dirs);
  239. QStringList dirMaps = sourceRoot.entryList({"maps"}, QDir::Filter::Dirs);
  240. QStringList dirMp3 = sourceRoot.entryList({"mp3"}, QDir::Filter::Dirs);
  241. if(dirData.empty())
  242. {
  243. 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.");
  244. return;
  245. }
  246. QDir sourceData = sourceRoot.filePath(dirData.front());
  247. QStringList roeFiles = sourceData.entryList({"*.lod"}, QDir::Filter::Files);
  248. QStringList sodFiles = sourceData.entryList({"H3ab*.lod"}, QDir::Filter::Files);
  249. QStringList hdFiles = sourceData.entryList({"*.pak"}, QDir::Filter::Files);
  250. if(sodFiles.empty())
  251. {
  252. if (roeFiles.empty())
  253. {
  254. // Directory structure is correct (Data/Maps/Mp3) but no .lod archives that should be present in any install
  255. 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.");
  256. return;
  257. }
  258. if (!hdFiles.empty())
  259. {
  260. // HD Edition contains only RoE data so we can't use even unmodified files from it
  261. 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.");
  262. return;
  263. }
  264. // RoE or some other unsupported edition. Demo version?
  265. 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.");
  266. return;
  267. }
  268. QStringList copyDirectories;
  269. copyDirectories += dirData.front();
  270. if (!dirMaps.empty())
  271. copyDirectories += dirMaps.front();
  272. if (!dirMp3.empty())
  273. copyDirectories += dirMp3.front();
  274. QDir targetRoot = pathToQString(VCMIDirs::get().userDataPath());
  275. for(const QString & dirName : copyDirectories)
  276. {
  277. QDir sourceDir = sourceRoot.filePath(dirName);
  278. QDir targetDir = targetRoot.filePath(dirName);
  279. if(!targetRoot.exists(dirName))
  280. targetRoot.mkdir(dirName);
  281. for(const QString & filename : sourceDir.entryList(QDir::Filter::Files))
  282. {
  283. QFile sourceFile(sourceDir.filePath(filename));
  284. sourceFile.copy(targetDir.filePath(filename));
  285. }
  286. }
  287. heroesDataUpdate();
  288. }
  289. // Tab Mod Preset
  290. void FirstLaunchView::modPresetUpdate()
  291. {
  292. bool translationExists = !findTranslationModName().isEmpty();
  293. ui->labelPresetLanguage->setVisible(translationExists);
  294. ui->labelPresetLanguageDescr->setVisible(translationExists);
  295. ui->checkBoxPresetLanguage->setVisible(translationExists);
  296. ui->checkBoxPresetLanguage->setEnabled(checkCanInstallTranslation());
  297. ui->checkBoxPresetExtras->setEnabled(checkCanInstallExtras());
  298. ui->checkBoxPresetHota->setEnabled(checkCanInstallHota());
  299. ui->checkBoxPresetWog->setEnabled(checkCanInstallWog());
  300. // we can't install anything - either repository checkout is off or all recommended mods are already installed
  301. if (!checkCanInstallTranslation() && !checkCanInstallExtras() && !checkCanInstallHota() && !checkCanInstallWog())
  302. exitSetup();
  303. }
  304. QString FirstLaunchView::findTranslationModName()
  305. {
  306. if (!getModView())
  307. return QString();
  308. QString preferredlanguage = QString::fromStdString(settings["general"]["language"].String());
  309. QString installedlanguage = QString::fromStdString(settings["session"]["language"].String());
  310. if (preferredlanguage == installedlanguage)
  311. return QString();
  312. return getModView()->getTranslationModName(preferredlanguage);
  313. }
  314. bool FirstLaunchView::checkCanInstallTranslation()
  315. {
  316. QString modName = findTranslationModName();
  317. if(modName.isEmpty())
  318. return false;
  319. return checkCanInstallMod(modName);
  320. }
  321. bool FirstLaunchView::checkCanInstallWog()
  322. {
  323. return checkCanInstallMod("wake-of-gods");
  324. }
  325. bool FirstLaunchView::checkCanInstallHota()
  326. {
  327. return checkCanInstallMod("hota");
  328. }
  329. bool FirstLaunchView::checkCanInstallExtras()
  330. {
  331. return checkCanInstallMod("vcmi-extras");
  332. }
  333. CModListView * FirstLaunchView::getModView()
  334. {
  335. auto * mainWindow = dynamic_cast<MainWindow *>(qApp->activeWindow());
  336. assert(mainWindow);
  337. if (!mainWindow)
  338. return nullptr;
  339. return mainWindow->getModView();
  340. }
  341. bool FirstLaunchView::checkCanInstallMod(const QString & modID)
  342. {
  343. return getModView() && getModView()->isModAvailable(modID);
  344. }
  345. void FirstLaunchView::on_pushButtonPresetBack_clicked()
  346. {
  347. activateTabHeroesData();
  348. }
  349. void FirstLaunchView::on_pushButtonPresetNext_clicked()
  350. {
  351. QStringList modsToInstall;
  352. if (ui->checkBoxPresetLanguage->isChecked() && checkCanInstallTranslation())
  353. modsToInstall.push_back(findTranslationModName());
  354. if (ui->checkBoxPresetExtras->isChecked() && checkCanInstallExtras())
  355. modsToInstall.push_back("vcmi-extras");
  356. if (ui->checkBoxPresetWog->isChecked() && checkCanInstallWog())
  357. modsToInstall.push_back("wake-of-gods");
  358. if (ui->checkBoxPresetHota->isChecked() && checkCanInstallHota())
  359. modsToInstall.push_back("hota");
  360. exitSetup();
  361. for (auto const & modName : modsToInstall)
  362. getModView()->doInstallMod(modName);
  363. }
  364. void FirstLaunchView::on_pushButtonDiscord_clicked()
  365. {
  366. QDesktopServices::openUrl(QUrl("https://discord.gg/chBT42V"));
  367. }
  368. void FirstLaunchView::on_pushButtonSlack_clicked()
  369. {
  370. QDesktopServices::openUrl(QUrl("https://slack.vcmi.eu/"));
  371. }
  372. void FirstLaunchView::on_pushButtonGithub_clicked()
  373. {
  374. QDesktopServices::openUrl(QUrl("https://github.com/vcmi/vcmi"));
  375. }