shithub: pt2-clone

Download patch

ref: 060ef365fa2b0816f349f4b5616a5ec277929c1a
parent: 96b4a252f20aec887a58d07a5bcd34bb4365c1e0
author: Olav Sørensen <[email protected]>
date: Fri Apr 8 10:35:45 EDT 2022

Windows: Fix crash when double-clicking a .MOD while a PT2 clone instance is already running

Also do less unnecessary stuff on initialization before closing newly spawned instance.

--- a/src/pt2_header.h
+++ b/src/pt2_header.h
@@ -14,7 +14,7 @@
 #include "pt2_unicode.h"
 #include "pt2_palette.h"
 
-#define PROG_VER_STR "1.44"
+#define PROG_VER_STR "1.45"
 
 #ifdef _WIN32
 #define DIR_DELIMITER '\\'
--- a/src/pt2_main.c
+++ b/src/pt2_main.c
@@ -61,6 +61,7 @@
 static TCHAR sharedHwndName[] = TEXT("Local\\PT2CloneHwnd");
 static TCHAR sharedFileName[] = TEXT("Local\\PT2CloneFilename");
 static bool handleSingleInstancing(int32_t argc, char **argv);
+static void closeSingleInstancing(void);
 static void handleSysMsg(SDL_Event inputEvent);
 #endif
 
@@ -233,10 +234,7 @@
 	makeSureDirIsProgramDir();
 #endif
 
-	hpc_Init();
-	hpc_SetDurationInHz(&video.vblankHpc, VBLANK_HZ);
-
-	if (!initializeVars() || !initKaiserTable())
+	if (!initializeVars())
 	{
 		cleanUp();
 		SDL_Quit();
@@ -264,7 +262,10 @@
 	SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
 #endif
 
-	if (!setupAudio() || !unpackBMPs())
+	hpc_Init();
+	hpc_SetDurationInHz(&video.vblankHpc, VBLANK_HZ);
+
+	if (!initKaiserTable() || !setupAudio() || !unpackBMPs())
 	{
 		cleanUp();
 		SDL_Quit();
@@ -772,6 +773,7 @@
 		if (sharedMemBuf != NULL)
 		{
 			memcpy(&hWnd_to, sharedMemBuf, sizeof (HWND));
+
 			UnmapViewOfFile(sharedMemBuf);
 			sharedMemBuf = NULL;
 			CloseHandle(hMapFile);
@@ -784,11 +786,12 @@
 				if (sharedMemBuf != NULL)
 				{
 					strcpy((char *)sharedMemBuf, argv[1]);
+
 					UnmapViewOfFile(sharedMemBuf);
 					sharedMemBuf = NULL;
 
 					SendMessage(hWnd_to, SYSMSG_FILE_ARG, 0, 0);
-					SDL_Delay(80); // wait a bit to make sure first instance received msg
+					Sleep(80); // wait a bit to make sure first instance received msg
 
 					CloseHandle(hMapFile);
 					hMapFile = NULL;
@@ -809,31 +812,39 @@
 
 static void handleSysMsg(SDL_Event inputEvent)
 {
-	SDL_SysWMmsg *wmMsg;
+	if (inputEvent.type != SDL_SYSWMEVENT)
+		return;
 
-	if (inputEvent.type == SDL_SYSWMEVENT)
+	SDL_SysWMmsg *wmMsg = inputEvent.syswm.msg;
+	if (wmMsg->subsystem == SDL_SYSWM_WINDOWS && wmMsg->msg.win.msg == SYSMSG_FILE_ARG)
 	{
-		wmMsg = inputEvent.syswm.msg;
-		if (wmMsg->subsystem == SDL_SYSWM_WINDOWS && wmMsg->msg.win.msg == SYSMSG_FILE_ARG)
+		hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, sharedFileName);
+		if (hMapFile != NULL)
 		{
-			hMapFile = OpenFileMapping(FILE_MAP_READ, FALSE, sharedFileName);
-			if (hMapFile != NULL)
+			sharedMemBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, ARGV_SHARED_MEM_MAX_LEN);
+			if (sharedMemBuf != NULL)
 			{
-				sharedMemBuf = (LPTSTR)MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, ARGV_SHARED_MEM_MAX_LEN);
-				if (sharedMemBuf != NULL)
-				{
-					loadDroppedFile((char *)sharedMemBuf, (uint32_t)strlen(sharedMemBuf), true, true);
-					UnmapViewOfFile(sharedMemBuf);
-					sharedMemBuf = NULL;
-				}
+				loadDroppedFile((char *)sharedMemBuf, (uint32_t)strlen(sharedMemBuf), true, true);
 
-				CloseHandle(hMapFile);
-				hMapFile = NULL;
+				UnmapViewOfFile(sharedMemBuf);
+				sharedMemBuf = NULL;
 			}
+
+			CloseHandle(hMapFile);
+			hMapFile = NULL;
 		}
 	}
 }
 
+void closeSingleInstancing(void)
+{
+	if (oneInstHandle != NULL)
+	{
+		CloseHandle(oneInstHandle);
+		oneInstHandle = NULL;
+	}
+}
+
 static LONG WINAPI exceptionHandler(EXCEPTION_POINTERS *ptr)
 #else
 static void exceptionHandler(int32_t signal)
@@ -898,6 +909,10 @@
 #ifndef _DEBUG
 	UnhookWindowsHookEx(g_hKeyboardHook);
 #endif
-	if (oneInstHandle != NULL) CloseHandle(oneInstHandle);
+	if (oneInstHandle != NULL)
+	{
+		CloseHandle(oneInstHandle);
+		oneInstHandle = NULL;
+	}
 #endif
 }
--- a/src/pt2_replayer.c
+++ b/src/pt2_replayer.c
@@ -37,6 +37,9 @@
 
 void setReplayerPosToTrackerPos(void)
 {
+	if (song == NULL)
+		return;
+
 	modPattern = (int8_t)song->currPattern;
 	modOrder = song->currOrder;
 	song->row = song->currRow;