main.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * main.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 "main.h"
  12. #include "mainwindow_moc.h"
  13. #include "prepare.h"
  14. #include "../lib/VCMIDirs.h"
  15. #include <QApplication>
  16. #include <QNetworkProxy>
  17. #include <QNetworkProxyFactory>
  18. // Conan workaround https://github.com/conan-io/conan-center-index/issues/13332
  19. #ifdef VCMI_IOS
  20. # if __has_include("QIOSIntegrationPlugin.h")
  21. # include "QIOSIntegrationPlugin.h"
  22. # endif
  23. int argcForClient;
  24. char ** argvForClient;
  25. #elif defined(VCMI_ANDROID)
  26. # include <QAndroidJniObject>
  27. # include <QtAndroid>
  28. #else
  29. # include <QMessageBox>
  30. # include <QProcess>
  31. #endif // VCMI_IOS
  32. // android must export main explicitly to make it visible in the shared library
  33. #ifdef VCMI_ANDROID
  34. # define MAIN_EXPORT ELF_VISIBILITY
  35. #else
  36. # define MAIN_EXPORT
  37. #endif // VCMI_ANDROID
  38. int MAIN_EXPORT main(int argc, char * argv[])
  39. {
  40. int result;
  41. #ifdef VCMI_IOS
  42. {
  43. #endif
  44. #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
  45. QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
  46. #endif
  47. QApplication vcmilauncher(argc, argv);
  48. // use system proxy
  49. {
  50. QNetworkProxyFactory::setUseSystemConfiguration(true);
  51. const auto systemProxies = QNetworkProxyFactory::systemProxyForQuery();
  52. if(!systemProxies.isEmpty())
  53. QNetworkProxy::setApplicationProxy(systemProxies[0]);
  54. }
  55. launcher::prepare();
  56. MainWindow mainWindow;
  57. mainWindow.show();
  58. #ifdef VCMI_ANDROID
  59. // changing language causes window to increase size over the bounds, force it back to proper value
  60. // TODO: check in Qt 6 if the hack is still needed
  61. auto appWindow = vcmilauncher.focusWindow();
  62. auto resizeWindowToScreen = [appWindow]{
  63. appWindow->resize(appWindow->screen()->availableSize());
  64. };
  65. QObject::connect(appWindow, &QWindow::widthChanged, resizeWindowToScreen);
  66. QObject::connect(appWindow, &QWindow::heightChanged, resizeWindowToScreen);
  67. #endif
  68. result = vcmilauncher.exec();
  69. #ifdef VCMI_IOS
  70. }
  71. if (result == 0)
  72. launchGame(argcForClient, argvForClient);
  73. #endif
  74. return result;
  75. }
  76. void startGame(const QStringList & args)
  77. {
  78. logGlobal->warn("Starting game with the arguments: %s", args.join(" ").toStdString());
  79. #ifdef VCMI_IOS
  80. static const char clientName[] = "vcmiclient";
  81. argcForClient = args.size() + 1; //first argument is omitted
  82. argvForClient = new char*[argcForClient];
  83. argvForClient[0] = new char[strlen(clientName)+1];
  84. strcpy(argvForClient[0], clientName);
  85. for(int i = 1; i < argcForClient; ++i)
  86. {
  87. std::string s = args.at(i - 1).toStdString();
  88. argvForClient[i] = new char[s.size() + 1];
  89. strcpy(argvForClient[i], s.c_str());
  90. }
  91. qApp->quit();
  92. #elif defined(VCMI_ANDROID)
  93. QtAndroid::androidActivity().callMethod<void>("onLaunchGameBtnPressed");
  94. #else
  95. startExecutable(pathToQString(VCMIDirs::get().clientPath()), args);
  96. #endif
  97. }
  98. void startEditor(const QStringList & args)
  99. {
  100. #ifdef ENABLE_EDITOR
  101. startExecutable(pathToQString(VCMIDirs::get().mapEditorPath()), args);
  102. #endif
  103. }
  104. #ifndef VCMI_MOBILE
  105. void startExecutable(QString name, const QStringList & args)
  106. {
  107. QProcess process;
  108. auto showError = [&] {
  109. QMessageBox::critical(qApp->activeWindow(),
  110. QObject::tr("Error starting executable"),
  111. QObject::tr("Failed to start %1\nReason: %2").arg(name, process.errorString()));
  112. };
  113. #if defined(VCMI_MAC) || defined(VCMI_WINDOWS)
  114. if(process.startDetached(name, args))
  115. {
  116. qApp->quit();
  117. }
  118. else
  119. {
  120. showError();
  121. }
  122. #else // Linux
  123. // Start vcmiclient and vcmieditor with QProcess::start() instead of QProcess::startDetached()
  124. // since startDetached() results in a missing terminal prompt after quitting vcmiclient.
  125. // QProcess::start() causes the launcher window to freeze while the child process is running, so we hide it in
  126. // MainWindow::on_startGameButton_clicked() and MainWindow::on_startEditorButton_clicked()
  127. process.setProcessChannelMode(QProcess::ForwardedChannels);
  128. process.start(name, args);
  129. process.waitForFinished(-1);
  130. if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0) {
  131. showError();
  132. }
  133. qApp->quit();
  134. #endif
  135. }
  136. #endif