CMT.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * CMT.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 "CMT.h"
  12. #include "CGameInfo.h"
  13. #include "mainmenu/CMainMenu.h"
  14. #include "media/CMusicHandler.h"
  15. #include "media/CSoundHandler.h"
  16. #include "media/CVideoHandler.h"
  17. #include "gui/CursorHandler.h"
  18. #include "CPlayerInterface.h"
  19. #include "gui/CGuiHandler.h"
  20. #include "gui/WindowHandler.h"
  21. #include "CServerHandler.h"
  22. #include "windows/CMessage.h"
  23. #include "windows/InfoWindows.h"
  24. #include "render/IScreenHandler.h"
  25. #include "render/Graphics.h"
  26. #include "../lib/texts/CGeneralTextHandler.h"
  27. #include "../lib/VCMI_Lib.h"
  28. #include "../lib/logging/CBasicLogConfigurator.h"
  29. #include <SDL_main.h>
  30. #include <SDL.h>
  31. #ifdef VCMI_ANDROID
  32. #include "../lib/CAndroidVMHelper.h"
  33. #include <SDL_system.h>
  34. #endif
  35. #if __MINGW32__
  36. #undef main
  37. #endif
  38. extern std::atomic<bool> headlessQuit;
  39. extern std::optional<std::string> criticalInitializationError;
  40. extern CBasicLogConfigurator *logConfig;
  41. [[noreturn]] static void quitApplicationImmediately(int error_code)
  42. {
  43. // Perform quick exit without executing static destructors and let OS cleanup anything that we did not
  44. // We generally don't care about them and this leads to numerous issues, e.g.
  45. // destruction of locked mutexes (fails an assertion), even in third-party libraries (as well as native libs on Android)
  46. // Android - std::quick_exit is available only starting from API level 21
  47. // Mingw, macOS and iOS - std::quick_exit is unavailable (at least in current version of CI)
  48. #if (defined(__ANDROID_API__) && __ANDROID_API__ < 21) || (defined(__MINGW32__)) || defined(VCMI_APPLE)
  49. ::exit(error_code);
  50. #else
  51. std::quick_exit(error_code);
  52. #endif
  53. }
  54. [[noreturn]] void quitApplication()
  55. {
  56. CSH->endNetwork();
  57. if(!settings["session"]["headless"].Bool())
  58. {
  59. if(CSH->client)
  60. CSH->endGameplay();
  61. GH.windows().clear();
  62. }
  63. vstd::clear_pointer(CSH);
  64. CMM.reset();
  65. if(!settings["session"]["headless"].Bool())
  66. {
  67. // cleanup, mostly to remove false leaks from analyzer
  68. if(CCS)
  69. {
  70. delete CCS->consoleh;
  71. delete CCS->curh;
  72. delete CCS->videoh;
  73. delete CCS->musich;
  74. delete CCS->soundh;
  75. vstd::clear_pointer(CCS);
  76. }
  77. CMessage::dispose();
  78. vstd::clear_pointer(graphics);
  79. }
  80. vstd::clear_pointer(VLC);
  81. // sometimes leads to a hang. TODO: investigate
  82. //vstd::clear_pointer(console);// should be removed after everything else since used by logging
  83. if(!settings["session"]["headless"].Bool())
  84. GH.screenHandler().close();
  85. if(logConfig != nullptr)
  86. {
  87. logConfig->deconfigure();
  88. delete logConfig;
  89. logConfig = nullptr;
  90. }
  91. std::cout << "Ending...\n";
  92. quitApplicationImmediately(0);
  93. }
  94. void handleQuit(bool ask)
  95. {
  96. if(!ask)
  97. {
  98. if(settings["session"]["headless"].Bool())
  99. {
  100. headlessQuit = true;
  101. }
  102. else
  103. {
  104. quitApplication();
  105. }
  106. return;
  107. }
  108. // FIXME: avoids crash if player attempts to close game while opening is still playing
  109. // use cursor handler as indicator that loading is not done yet
  110. // proper solution would be to abort init thread (or wait for it to finish)
  111. if (!CCS->curh)
  112. {
  113. quitApplicationImmediately(0);
  114. }
  115. if (LOCPLINT)
  116. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[69], quitApplication, nullptr);
  117. else
  118. CInfoWindow::showYesNoDialog(CGI->generaltexth->allTexts[69], {}, quitApplication, {}, PlayerColor(1));
  119. }
  120. void handleFatalError(const std::string & message, bool terminate)
  121. {
  122. logGlobal->error("FATAL ERROR ENCOUNTERED, VCMI WILL NOW TERMINATE");
  123. logGlobal->error("Reason: %s", message);
  124. std::string messageToShow = "Fatal error! " + message;
  125. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error!", messageToShow.c_str(), nullptr);
  126. if (terminate)
  127. throw std::runtime_error(message);
  128. else
  129. quitApplicationImmediately(1);
  130. }