main.cpp 1.8 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 "main.h"
  11. #include "mainwindow_moc.h"
  12. #include <QApplication>
  13. #include <QProcess>
  14. #include <QMessageBox>
  15. #include "../lib/VCMIDirs.h"
  16. // Conan workaround https://github.com/conan-io/conan-center-index/issues/13332
  17. #ifdef VCMI_IOS
  18. #if __has_include("QIOSIntegrationPlugin.h")
  19. #include "QIOSIntegrationPlugin.h"
  20. #endif
  21. #endif
  22. int __argc;
  23. char ** __argv;
  24. int main(int argc, char * argv[])
  25. {
  26. int result;
  27. #ifdef VCMI_IOS
  28. __argc = argc;
  29. __argv = argv;
  30. {
  31. #endif
  32. QApplication vcmilauncher(argc, argv);
  33. MainWindow mainWindow;
  34. mainWindow.show();
  35. result = vcmilauncher.exec();
  36. #ifdef VCMI_IOS
  37. }
  38. if (result == 0)
  39. launchGame(__argc, __argv);
  40. #endif
  41. return result;
  42. }
  43. void startGame(const QStringList & args)
  44. {
  45. __argc = args.size() + 1; //first argument is omitted
  46. __argv = new char*[__argc];
  47. __argv[0] = new char[strlen("vcmi")];
  48. strcpy(__argv[0], "vcmi");
  49. for(int i = 1; i < __argc; ++i)
  50. {
  51. const char * s = args[i - 1].toLocal8Bit().constData();
  52. __argv[i] = new char[strlen(s)];
  53. strcpy(__argv[i], s);
  54. }
  55. #ifdef Q_OS_IOS
  56. logGlobal->warn("Starting game with the arguments: %s", args.join(" ").toStdString());
  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