main.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <QApplication>
  14. #include <QProcess>
  15. #include <QMessageBox>
  16. #include "../lib/VCMIDirs.h"
  17. // Conan workaround https://github.com/conan-io/conan-center-index/issues/13332
  18. #ifdef VCMI_IOS
  19. #if __has_include("QIOSIntegrationPlugin.h")
  20. #include "QIOSIntegrationPlugin.h"
  21. #endif
  22. int argcForClient;
  23. char ** argvForClient;
  24. #endif
  25. int main(int argc, char * argv[])
  26. {
  27. int result;
  28. #ifdef VCMI_IOS
  29. {
  30. #endif
  31. QApplication vcmilauncher(argc, argv);
  32. MainWindow mainWindow;
  33. mainWindow.show();
  34. result = vcmilauncher.exec();
  35. #ifdef VCMI_IOS
  36. }
  37. if (result == 0)
  38. launchGame(argcForClient, argvForClient);
  39. #endif
  40. return result;
  41. }
  42. void startGame(const QStringList & args)
  43. {
  44. logGlobal->warn("Starting game with the arguments: %s", args.join(" ").toStdString());
  45. #ifdef Q_OS_IOS
  46. static const char clientName[] = "vcmiclient";
  47. argcForClient = args.size() + 1; //first argument is omitted
  48. argvForClient = new char*[argcForClient];
  49. argvForClient[0] = new char[strlen(clientName)+1];
  50. strcpy(argvForClient[0], clientName);
  51. for(int i = 1; i < argcForClient; ++i)
  52. {
  53. std::string s = args.at(i - 1).toStdString();
  54. argvForClient[i] = new char[s.size() + 1];
  55. strcpy(argvForClient[i], s.c_str());
  56. }
  57. qApp->quit();
  58. #else
  59. startExecutable(pathToQString(VCMIDirs::get().clientPath()), args);
  60. #endif
  61. }
  62. #ifndef Q_OS_IOS
  63. void startExecutable(QString name, const QStringList & args)
  64. {
  65. QProcess process;
  66. // Start the executable
  67. if(process.startDetached(name, args))
  68. {
  69. qApp->quit();
  70. }
  71. else
  72. {
  73. QMessageBox::critical(qApp->activeWindow(),
  74. "Error starting executable",
  75. "Failed to start " + name + "\n"
  76. "Reason: " + process.errorString(),
  77. QMessageBox::Ok,
  78. QMessageBox::Ok);
  79. }
  80. }
  81. #endif