diff --git a/src/d_main.c b/src/d_main.c index 6029c648..d05e0a59 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -845,6 +845,24 @@ static inline void D_CleanFile(char **filearray) // Identify the SRB2 version, and IWAD file to use. // ========================================================================== +static boolean AddIWAD(char *srb2wad1, char *srb2wad2) +{ + if (srb2wad2 != NULL && FIL_ReadFileOK(srb2wad2)) + { + D_AddFile(srb2wad2, startupwadfiles); + return true; + } + else if (srb2wad1 != NULL && FIL_ReadFileOK(srb2wad1)) + { + D_AddFile(srb2wad1, startupwadfiles); + return true; + } + else + { + return false; + } +} + static void IdentifyVersion(void) { char *srb2wad1, *srb2wad2; @@ -896,12 +914,17 @@ static void IdentifyVersion(void) configfile[sizeof configfile - 1] = '\0'; // Load the IWAD - if (srb2wad2 != NULL && FIL_ReadFileOK(srb2wad2)) - D_AddFile(srb2wad2, startupwadfiles); - else if (srb2wad1 != NULL && FIL_ReadFileOK(srb2wad1)) - D_AddFile(srb2wad1, startupwadfiles); + if (AddIWAD(srb2wad1, srb2wad2)) + { + I_SaveCurrentWadDirectory(); + } else - I_Error("SRB2.SRB/SRB2.WAD not found! Expected in %s, ss files: %s or %s\n", srb2waddir, srb2wad1, srb2wad2); + { + if (!( I_UseSavedWadDirectory() && AddIWAD(srb2wad1, srb2wad2) )) + { + I_Error("SRB2.SRB/SRB2.WAD not found! Expected in %s, ss files: %s or %s\n", srb2waddir, srb2wad1, srb2wad2); + } + } if (srb2wad1) free(srb2wad1); diff --git a/src/i_system.h b/src/i_system.h index 3e589c69..2f4b1b5c 100644 --- a/src/i_system.h +++ b/src/i_system.h @@ -318,6 +318,15 @@ 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_system.c b/src/sdl/i_system.c index 1154e332..d37e688a 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -34,6 +34,7 @@ #ifdef _WIN32 #define RPC_NO_WINDOWS_H #include +#include #include "../doomtype.h" typedef BOOL (WINAPI *p_GetDiskFreeSpaceExA)(LPCSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER); typedef BOOL (WINAPI *p_IsProcessorFeaturePresent) (DWORD); @@ -3758,6 +3759,66 @@ 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 + char path[MAX_PATH]; + FILE * file = openAppDataFile("lastwaddir", "r"); + if (file != NULL) + { + if (fgets(path, sizeof path, file) != NULL) + { + I_OutputMsg( + "Going to the last known directory with srb2.srb: %s\n", + path); + ok = SetCurrentDirectoryA(path); + } + fclose(file); + } +#endif + return ok; +} + const char *I_LocateWad(void) { const char *waddir;