From 3e6b45abe979f0ced01b660598fb4018683b9e1b Mon Sep 17 00:00:00 2001
From: James R <justsomejames2@gmail.com>
Date: Mon, 31 Aug 2020 01:23:56 -0700
Subject: [PATCH] Just SetCurrentDirectory to the exe's folder on windoze

---
 src/d_main.c       | 12 +---------
 src/i_system.h     |  9 -------
 src/sdl/i_main.c   | 18 ++++++++++++++
 src/sdl/i_system.c | 60 ----------------------------------------------
 4 files changed, 19 insertions(+), 80 deletions(-)

diff --git a/src/d_main.c b/src/d_main.c
index 4b7db8079..b241acf37 100644
--- a/src/d_main.c
+++ b/src/d_main.c
@@ -895,17 +895,7 @@ static void IdentifyVersion(void)
 	}
 
 	// Load the IWAD
-	if (AddIWAD())
-	{
-		I_SaveCurrentWadDirectory();
-	}
-	else
-	{
-		if (!( I_UseSavedWadDirectory() && AddIWAD() ))
-		{
-			I_Error("SRB2.SRB not found! Expected in %s\n", srb2waddir);
-		}
-	}
+	AddIWAD();
 
 	// will be overwritten in case of -cdrom or unix/win home
 	snprintf(configfile, sizeof configfile, "%s" PATHSEP CONFIGFILENAME, srb2waddir);
diff --git a/src/i_system.h b/src/i_system.h
index 2f4b1b5c8..3e589c69a 100644
--- a/src/i_system.h
+++ b/src/i_system.h
@@ -318,15 +318,6 @@ const CPUInfoFlags *I_CPUInfo(void);
 */
 const char *I_LocateWad(void);
 
-/**	\brief Save current wad directory to appdata
-*/
-void I_SaveCurrentWadDirectory(void);
-
-/**	\brief Change directory to last known directory saved in appdata
-		\return whether the directory could be saved
-*/
-boolean I_UseSavedWadDirectory(void);
-
 /**	\brief First Joystick's events
 */
 void I_GetJoystickEvents(void);
diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c
index 41874e9f7..a2ce653ec 100644
--- a/src/sdl/i_main.c
+++ b/src/sdl/i_main.c
@@ -97,6 +97,20 @@ static inline VOID MakeCodeWritable(VOID)
 #endif
 
 
+#ifdef _WIN32
+static void
+ChDirToExe (void)
+{
+	CHAR path[MAX_PATH];
+	if (GetModuleFileNameA(NULL, path, MAX_PATH) > 0)
+	{
+		strrchr(path, '\\')[0] = '\0';
+		SetCurrentDirectoryA(path);
+	}
+}
+#endif
+
+
 /**	\brief	The main function
 
 	\param	argc	number of arg
@@ -126,6 +140,10 @@ int main(int argc, char **argv)
 #endif
 #endif
 
+#ifdef _WIN32
+	ChDirToExe();
+#endif
+
 	logdir = D_Home();
 
 #ifdef LOGMESSAGES
diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c
index 4cdac36b9..51f708d0f 100644
--- a/src/sdl/i_system.c
+++ b/src/sdl/i_system.c
@@ -34,7 +34,6 @@
 #ifdef _WIN32
 #define RPC_NO_WINDOWS_H
 #include <windows.h>
-#include <shlobj.h>
 #include "../doomtype.h"
 typedef BOOL (WINAPI *p_GetDiskFreeSpaceExA)(LPCSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);
 typedef BOOL (WINAPI *p_IsProcessorFeaturePresent) (DWORD);
@@ -3766,65 +3765,6 @@ static const char *locateWad(void)
 	return NULL;
 }
 
-#ifdef _WIN32
-static FILE * openAppDataFile(const char *filename, const char *mode)
-{
-	FILE * file = NULL;
-	char   kdir[MAX_PATH];
-
-	if (SHGetFolderPathAndSubDirA(NULL, CSIDL_LOCAL_APPDATA|CSIDL_FLAG_CREATE,
-				NULL, 0, "SRB2Kart", kdir) == S_OK)
-	{
-		strcat(kdir, "\\");
-		strcat(kdir, filename);
-		file = fopen(kdir, mode);
-	}
-
-	return file;
-}
-#endif
-
-void I_SaveCurrentWadDirectory(void)
-{
-#ifdef _WIN32
-	char   path[MAX_PATH];
-	FILE * file = openAppDataFile("lastwaddir", "w");
-	if (file != NULL)
-	{
-		if (strcmp(srb2path, ".") == 0)
-		{
-			GetCurrentDirectoryA(sizeof path, path);
-			fputs(path, file);
-		}
-		else
-		{
-			fputs(srb2path, file);
-		}
-		fclose(file);
-	}
-#endif
-}
-
-boolean I_UseSavedWadDirectory(void)
-{
-	boolean ok = false;
-#ifdef _WIN32
-	FILE * file = openAppDataFile("lastwaddir", "r");
-	if (file != NULL)
-	{
-		if (fgets(srb2path, sizeof srb2path, file) != NULL)
-		{
-			I_OutputMsg(
-					"Going to the last known directory with srb2.srb: %s\n",
-					srb2path);
-			ok = SetCurrentDirectoryA(srb2path);
-		}
-		fclose(file);
-	}
-#endif
-	return ok;
-}
-
 const char *I_LocateWad(void)
 {
 	const char *waddir;
-- 
GitLab