main.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. void startEditor(const QStringList & args)
  63. {
  64. #ifdef ENABLE_EDITOR
  65. startExecutable(pathToQString(VCMIDirs::get().mapEditorPath()), args);
  66. #endif
  67. }
  68. #ifndef Q_OS_IOS
  69. void startExecutable(QString name, const QStringList & args)
  70. {
  71. QProcess process;
  72. // Start the executable
  73. if(process.startDetached(name, args))
  74. {
  75. qApp->quit();
  76. }
  77. else
  78. {
  79. QMessageBox::critical(qApp->activeWindow(),
  80. "Error starting executable",
  81. "Failed to start " + name + "\n"
  82. "Reason: " + process.errorString(),
  83. QMessageBox::Ok,
  84. QMessageBox::Ok);
  85. }
  86. }
  87. #endif