main.cpp 2.2 KB

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