From ff379e7c0c19c6f2578cf7fcf83c53af632669e7 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 7 Oct 2019 01:24:51 +0200 Subject: [PATCH] replaced all 'exit's with an ExitEvent exception The main exits are initiated from code that cannot filter this back to D_DoomMain easily so the exception is the only way to get there. The 3 main points of exit are: * quit/exit CCMD * quitting the menu through ST_Endoom * receiving a quit message on the main window. --- src/console/c_cmds.cpp | 4 ++-- src/d_iwad.cpp | 3 ++- src/d_main.cpp | 7 ++++++- src/posix/cocoa/st_start.mm | 3 ++- src/posix/sdl/i_input.cpp | 3 ++- src/posix/sdl/st_start.cpp | 2 +- src/win32/i_input.cpp | 2 +- src/win32/st_start.cpp | 2 +- 8 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/console/c_cmds.cpp b/src/console/c_cmds.cpp index e99ff662c13..8d030eceda5 100644 --- a/src/console/c_cmds.cpp +++ b/src/console/c_cmds.cpp @@ -101,12 +101,12 @@ bool CheckCheatmode (bool printmsg) CCMD (quit) { - if (!insave) exit(0); + if (!insave) throw CExitEvent(0); } CCMD (exit) { - if (!insave) exit(0); + if (!insave) throw CExitEvent(0); } /* diff --git a/src/d_iwad.cpp b/src/d_iwad.cpp index b421b1b353b..b1a63da18fc 100644 --- a/src/d_iwad.cpp +++ b/src/d_iwad.cpp @@ -726,7 +726,7 @@ int FIWadManager::IdentifyVersion (TArray &wadfiles, const char *iwad, } else { - exit(0); + return -1; } havepicked = true; } @@ -793,6 +793,7 @@ int FIWadManager::IdentifyVersion (TArray &wadfiles, const char *iwad, const FIWADInfo *FIWadManager::FindIWAD(TArray &wadfiles, const char *iwad, const char *basewad, const char *optionalwad) { int iwadType = IdentifyVersion(wadfiles, iwad, basewad, optionalwad); + if (iwadType == -1) return nullptr; //gameiwad = iwadType; const FIWADInfo *iwad_info = &mIWadInfos[iwadType]; if (DoomStartupInfo.Name.IsEmpty()) DoomStartupInfo.Name = iwad_info->Name; diff --git a/src/d_main.cpp b/src/d_main.cpp index 2beb7201d29..d8ff73e0bb2 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -2459,6 +2459,7 @@ static int D_DoomMain_Internal (void) iwad_man = new FIWadManager(basewad, optionalwad); } const FIWADInfo *iwad_info = iwad_man->FindIWAD(allwads, iwad, basewad, optionalwad); + if (!iwad_info) return 0; // user exited the selection popup via cancel button. gameinfo.gametype = iwad_info->gametype; gameinfo.flags = iwad_info->flags; gameinfo.ConfigName = iwad_info->Configname; @@ -2847,7 +2848,11 @@ int D_DoomMain() { ret = D_DoomMain_Internal(); } - catch (std::exception &error) + catch (const CExitEvent &exit) // This is a regular exit initiated from deeply nested code. + { + ret = exit.Reason(); + } + catch (const std::exception &error) { I_ShowFatalError(error.what()); ret = -1; diff --git a/src/posix/cocoa/st_start.mm b/src/posix/cocoa/st_start.mm index e85a61f999f..609bb737695 100644 --- a/src/posix/cocoa/st_start.mm +++ b/src/posix/cocoa/st_start.mm @@ -39,6 +39,7 @@ #include "doomtype.h" #include "st_console.h" #include "st_start.h" +#include "doomerrors.h" FStartupScreen *StartScreen; @@ -174,5 +175,5 @@ void ST_Endoom() extern void I_ShutdownJoysticks(); I_ShutdownJoysticks(); - exit(0); + throw CExitEvent(0); } diff --git a/src/posix/sdl/i_input.cpp b/src/posix/sdl/i_input.cpp index 040abe736e0..77e6f1860b5 100644 --- a/src/posix/sdl/i_input.cpp +++ b/src/posix/sdl/i_input.cpp @@ -47,6 +47,7 @@ #include "g_game.h" #include "g_levellocals.h" #include "utf8.h" +#include "doomerrors.h" static void I_CheckGUICapture (); @@ -297,7 +298,7 @@ void MessagePump (const SDL_Event &sev) switch (sev.type) { case SDL_QUIT: - exit(0); + throw CExitEvent(0); case SDL_WINDOWEVENT: extern void ProcessSDLWindowEvent(const SDL_WindowEvent &); diff --git a/src/posix/sdl/st_start.cpp b/src/posix/sdl/st_start.cpp index fa8ebe209f3..59d04a99c43 100644 --- a/src/posix/sdl/st_start.cpp +++ b/src/posix/sdl/st_start.cpp @@ -350,5 +350,5 @@ bool FTTYStartupScreen::NetLoop(bool (*timer_callback)(void *), void *userdata) void ST_Endoom() { I_ShutdownJoysticks(); - exit(0); + throw CExitEvent(0); } diff --git a/src/win32/i_input.cpp b/src/win32/i_input.cpp index 1e596b39b6d..99dc9f293ce 100644 --- a/src/win32/i_input.cpp +++ b/src/win32/i_input.cpp @@ -739,7 +739,7 @@ void I_GetEvent () while (PeekMessage (&mess, NULL, 0, 0, PM_REMOVE)) { if (mess.message == WM_QUIT) - exit(mess.wParam); + throw CExitEvent(mess.wParam); if (GUICapture) { diff --git a/src/win32/st_start.cpp b/src/win32/st_start.cpp index 32b75db477d..3e9a96047a0 100644 --- a/src/win32/st_start.cpp +++ b/src/win32/st_start.cpp @@ -611,7 +611,7 @@ int RunEndoom() void ST_Endoom() { int code = RunEndoom(); - exit(code); + throw CExitEvent(code); }