From 4505bfa4b82bd108c3da8cfa51e5cc623aa8983a Mon Sep 17 00:00:00 2001
From: Christoph Oelckers <coelckers@users.noreply.github.com>
Date: Tue, 10 Aug 2021 22:09:32 +0200
Subject: [PATCH] - delay the restart action from the error pane until after
 everything has been shut down.

This cannot be done from a place where the old instance still can write to the config file, which happens only in the shutdown process.
---
 src/common/platform/win32/i_main.cpp | 20 +++++++++++++++-----
 src/d_main.cpp                       |  4 ++++
 2 files changed, 19 insertions(+), 5 deletions(-)

diff --git a/src/common/platform/win32/i_main.cpp b/src/common/platform/win32/i_main.cpp
index 7207717ea7..bbada0fb26 100644
--- a/src/common/platform/win32/i_main.cpp
+++ b/src/common/platform/win32/i_main.cpp
@@ -545,6 +545,20 @@ LRESULT CALLBACK LConProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 //
 //==========================================================================
 
+bool restartrequest;
+
+void CheckForRestart()
+{
+	if (restartrequest)
+	{
+		HMODULE hModule = GetModuleHandleW(NULL);
+		WCHAR path[MAX_PATH];
+		GetModuleFileNameW(hModule, path, MAX_PATH);
+		ShellExecuteW(NULL, L"open", path, GetCommandLineW(), NULL, SW_SHOWNORMAL);
+	}
+	restartrequest = false;
+}
+
 INT_PTR CALLBACK ErrorPaneProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
 {
 	switch (msg)
@@ -559,11 +573,7 @@ INT_PTR CALLBACK ErrorPaneProc (HWND hDlg, UINT msg, WPARAM wParam, LPARAM lPara
 		{
 			if (LOWORD(wParam) == IDC_BUTTON1) // we pressed the restart button, so run GZDoom again
 			{
-				HMODULE hModule = GetModuleHandleW(NULL);
-				WCHAR path[MAX_PATH];
-				GetModuleFileNameW(hModule, path, MAX_PATH);
-
-				ShellExecuteW(NULL, L"open", path, GetCommandLineW(), NULL, SW_SHOWNORMAL);
+				restartrequest = true;
 			}
 			PostQuitMessage (0);
 			return TRUE;
diff --git a/src/d_main.cpp b/src/d_main.cpp
index 245922beb4..e29cca2db2 100644
--- a/src/d_main.cpp
+++ b/src/d_main.cpp
@@ -3716,6 +3716,10 @@ int GameMain()
 	DeleteStartupScreen();
 	delete Args;
 	Args = nullptr;
+#ifdef _WIN32
+	void CheckForRestart();
+	CheckForRestart();
+#endif
 	return ret;
 }