From e9110b660cbd21bd62f56e0ea422e47fbc4e38d5 Mon Sep 17 00:00:00 2001 From: Steel Titanium Date: Tue, 24 Mar 2020 15:36:44 -0700 Subject: [PATCH 01/16] Better directory structure (cherry picked from commit 8ee0a9309b289a1a3d1a7260a44a43c776180686) # Conflicts: # src/m_menu.c # src/m_menu.h # src/m_misc.h --- src/d_main.c | 6 +++++- src/d_netcmd.c | 2 ++ src/m_misc.c | 44 ++++++++++++++++++++++++++++++-------------- src/m_misc.h | 2 +- src/sdl/i_main.c | 36 ++++++++++++++++++++++++++++++------ 5 files changed, 68 insertions(+), 22 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 88c501345..b63f6ba42 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -152,6 +152,7 @@ char srb2path[256] = "."; #endif boolean usehome = true; const char *pandf = "%s" PATHSEP "%s"; +static char addonsdir[MAX_WADPATH]; // // EVENT HANDLING @@ -1090,7 +1091,6 @@ void D_SRB2Main(void) // can't use sprintf since there is %u in savegamename strcatbf(savegamename, srb2home, PATHSEP); - I_mkdir(srb2home, 0700); #else snprintf(srb2home, sizeof srb2home, "%s", userhome); snprintf(downloaddir, sizeof downloaddir, "%s", userhome); @@ -1111,6 +1111,10 @@ void D_SRB2Main(void) #endif } + // Create addons dir + snprintf(addonsdir, sizeof addonsdir, "%s%s%s", srb2home, PATHSEP, "addons"); + I_mkdir(addonsdir, 0755); + // rand() needs seeded regardless of password srand((unsigned int)time(NULL)); diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 88621ee66..f061e3090 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -790,6 +790,8 @@ void D_RegisterClientCommands(void) CV_RegisterVar(&cv_screenshot_option); CV_RegisterVar(&cv_screenshot_folder); CV_RegisterVar(&cv_moviemode); + CV_RegisterVar(&cv_movie_option); + CV_RegisterVar(&cv_movie_folder); // PNG variables CV_RegisterVar(&cv_zlib_level); CV_RegisterVar(&cv_zlib_memory); diff --git a/src/m_misc.c b/src/m_misc.c index f4a4ec291..9b1bff285 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -107,6 +107,9 @@ consvar_t cv_screenshot_folder = {"screenshot_folder", "", CV_SAVE, NULL, NULL, static CV_PossibleValue_t moviemode_cons_t[] = {{MM_GIF, "GIF"}, {MM_APNG, "aPNG"}, {MM_SCREENSHOT, "Screenshots"}, {0, NULL}}; consvar_t cv_moviemode = {"moviemode_mode", "GIF", CV_SAVE|CV_CALL, moviemode_cons_t, Moviemode_mode_Onchange, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_movie_option = {"movie_option", "Default", CV_SAVE|CV_CALL, screenshot_cons_t, Moviemode_option_Onchange, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_movie_folder = {"movie_folder", "", CV_SAVE, NULL, NULL, 0, NULL, NULL, 0, 0, NULL}; + static CV_PossibleValue_t zlib_mem_level_t[] = { {1, "(Min Memory) 1"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"}, @@ -1080,19 +1083,25 @@ static inline moviemode_t M_StartMovieGIF(const char *pathname) void M_StartMovie(void) { #if NUMSCREENS > 2 - const char *pathname = "."; + char pathname[MAX_WADPATH]; if (moviemode) return; - if (cv_screenshot_option.value == 0) - pathname = usehome ? srb2home : srb2path; - else if (cv_screenshot_option.value == 1) - pathname = srb2home; - else if (cv_screenshot_option.value == 2) - pathname = srb2path; - else if (cv_screenshot_option.value == 3 && *cv_screenshot_folder.string != '\0') - pathname = cv_screenshot_folder.string; + if (cv_movie_option.value == 0) + strcpy(pathname, usehome ? srb2home : srb2path); + else if (cv_movie_option.value == 1) + strcpy(pathname, srb2home); + else if (cv_movie_option.value == 2) + strcpy(pathname, srb2path); + else if (cv_movie_option.value == 3 && *cv_movie_folder.string != '\0') + strcpy(pathname, cv_screenshot_folder.string); + + if (cv_movie_option.value != 3) + { + strcat(pathname, PATHSEP"movies"PATHSEP); + I_mkdir(pathname, 0755); + } if (rendermode == render_none) I_Error("Can't make a movie without a render system\n"); @@ -1424,7 +1433,8 @@ void M_ScreenShot(void) void M_DoScreenShot(void) { #if NUMSCREENS > 2 - const char *freename = NULL, *pathname = "."; + const char *freename = NULL; + char pathname[MAX_WADPATH]; boolean ret = false; UINT8 *linear = NULL; @@ -1432,13 +1442,19 @@ void M_DoScreenShot(void) takescreenshot = false; if (cv_screenshot_option.value == 0) - pathname = usehome ? srb2home : srb2path; + strcpy(pathname, usehome ? srb2home : srb2path); else if (cv_screenshot_option.value == 1) - pathname = srb2home; + strcpy(pathname, srb2home); else if (cv_screenshot_option.value == 2) - pathname = srb2path; + strcpy(pathname, srb2path); else if (cv_screenshot_option.value == 3 && *cv_screenshot_folder.string != '\0') - pathname = cv_screenshot_folder.string; + strcpy(pathname, cv_screenshot_folder.string); + + if (cv_screenshot_option.value != 3) + { + strcat(pathname, PATHSEP"screenshots"PATHSEP); + I_mkdir(pathname, 0755); + } #ifdef USE_PNG if (rendermode != render_none) diff --git a/src/m_misc.h b/src/m_misc.h index 658028b44..990555971 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -30,7 +30,7 @@ typedef enum { extern moviemode_t moviemode; extern consvar_t cv_screenshot_option, cv_screenshot_folder; -extern consvar_t cv_moviemode; +extern consvar_t cv_moviemode, cv_movie_folder, cv_movie_option; extern consvar_t cv_zlib_memory, cv_zlib_level, cv_zlib_strategy, cv_zlib_window_bits; extern consvar_t cv_zlib_memorya, cv_zlib_levela, cv_zlib_strategya, cv_zlib_window_bitsa; extern consvar_t cv_apng_delay; diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 41a9d7cd6..5aea78210 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -26,6 +26,8 @@ #include #endif +#include "time.h" // For log timestamps + #ifdef HAVE_SDL #ifdef HAVE_TTF @@ -114,6 +116,7 @@ int main(int argc, char **argv) #endif { const char *logdir = NULL; + char logfile[MAX_WADPATH]; myargc = argc; myargv = argv; /// \todo pull out path to exe from this string @@ -125,15 +128,35 @@ int main(int argc, char **argv) #endif #endif - logdir = D_Home(); - #ifdef LOGMESSAGES + if (!M_CheckParm("-nolog")) + { + logdir = D_Home(); + + time_t my_time; + struct tm * timeinfo; + char buf[26]; + my_time = time(NULL); + timeinfo = localtime(&my_time); + + strftime(buf, 26, "%Y-%m-%d %H-%M-%S", timeinfo); + strcpy(logfile, va("log-%s.txt", buf)); + #ifdef DEFAULTDIR - if (logdir) - logstream = fopen(va("%s/"DEFAULTDIR"/log.txt",logdir), "wt"); - else + if (logdir) + { + // Create dirs here because D_SRB2Main() is too late. + I_mkdir(va("%s%s"DEFAULTDIR, logdir, PATHSEP), 0755); + I_mkdir(va("%s%s"DEFAULTDIR"%slogs",logdir, PATHSEP, PATHSEP), 0755); + logstream = fopen(va("%s%s"DEFAULTDIR"%slogs%s%s",logdir, PATHSEP, PATHSEP, PATHSEP, logfile), "wt"); + } + else #endif - logstream = fopen("./log.txt", "wt"); + { + I_mkdir("."PATHSEP"logs"PATHSEP, 0755); + logstream = fopen(va("."PATHSEP"logs"PATHSEP"%s", logfile), "wt"); + } + } #endif //I_OutputMsg("I_StartupSystem() ...\n"); @@ -161,6 +184,7 @@ int main(int argc, char **argv) CONS_Printf("Setting up SRB2Kart...\n"); D_SRB2Main(); CONS_Printf("Entering main game loop...\n"); + CONS_Printf("%s\n", logfile); // never return D_SRB2Loop(); From d6c8306858c656d0254effa14f430d077b2c4fc8 Mon Sep 17 00:00:00 2001 From: Steel Titanium Date: Sun, 8 Sep 2019 16:54:40 -0400 Subject: [PATCH 02/16] Fix this mistake (cherry picked from commit cc80cd77c531d23f88ae5f7ee417a453172fdc09) --- src/m_misc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index 9b1bff285..8c0070270 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -107,7 +107,7 @@ consvar_t cv_screenshot_folder = {"screenshot_folder", "", CV_SAVE, NULL, NULL, static CV_PossibleValue_t moviemode_cons_t[] = {{MM_GIF, "GIF"}, {MM_APNG, "aPNG"}, {MM_SCREENSHOT, "Screenshots"}, {0, NULL}}; consvar_t cv_moviemode = {"moviemode_mode", "GIF", CV_SAVE|CV_CALL, moviemode_cons_t, Moviemode_mode_Onchange, 0, NULL, NULL, 0, 0, NULL}; -consvar_t cv_movie_option = {"movie_option", "Default", CV_SAVE|CV_CALL, screenshot_cons_t, Moviemode_option_Onchange, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_movie_option = {"movie_option", "Default", CV_SAVE, screenshot_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_movie_folder = {"movie_folder", "", CV_SAVE, NULL, NULL, 0, NULL, NULL, 0, 0, NULL}; static CV_PossibleValue_t zlib_mem_level_t[] = { @@ -1095,7 +1095,7 @@ void M_StartMovie(void) else if (cv_movie_option.value == 2) strcpy(pathname, srb2path); else if (cv_movie_option.value == 3 && *cv_movie_folder.string != '\0') - strcpy(pathname, cv_screenshot_folder.string); + strcpy(pathname, cv_movie_folder.string); if (cv_movie_option.value != 3) { From 5653a7214192d37ff97145adcad12f5b315f355f Mon Sep 17 00:00:00 2001 From: toaster Date: Fri, 20 Sep 2019 17:22:09 +0100 Subject: [PATCH 03/16] * Resolve compiling issues with logmessages. * Improve logfile print. (I know Steel wanted it gone entirely, but I feel like it's relevant to have it in game..?) (cherry picked from commit 113568095a609c662e33e67f311c94e28be127d9) --- src/sdl/i_main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 5aea78210..10f14f36f 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -131,11 +131,12 @@ int main(int argc, char **argv) #ifdef LOGMESSAGES if (!M_CheckParm("-nolog")) { - logdir = D_Home(); - time_t my_time; struct tm * timeinfo; char buf[26]; + + logdir = D_Home(); + my_time = time(NULL); timeinfo = localtime(&my_time); @@ -183,8 +184,11 @@ int main(int argc, char **argv) // startup SRB2 CONS_Printf("Setting up SRB2Kart...\n"); D_SRB2Main(); +#ifdef LOGMESSAGES + if (!M_CheckParm("-nolog")) + CONS_Printf("Logfile: %s\n", logfile); +#endif CONS_Printf("Entering main game loop...\n"); - CONS_Printf("%s\n", logfile); // never return D_SRB2Loop(); From f400d33947b3d517a9265791c53da5138eea7f6a Mon Sep 17 00:00:00 2001 From: toaster Date: Fri, 20 Sep 2019 17:43:41 +0100 Subject: [PATCH 04/16] Mark new-style log names as loaded. (cherry picked from commit c36123aa5687c5151aad98fd1941412f7407ae52) --- src/filesrch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/filesrch.c b/src/filesrch.c index d132e9fb4..1be74f9b9 100644 --- a/src/filesrch.c +++ b/src/filesrch.c @@ -868,7 +868,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut) } else if (ext == EXT_TXT) { - if (!strcmp(dent->d_name, "log.txt") || !strcmp(dent->d_name, "errorlog.txt")) + if (!strncmp(dent->d_name, "log-", 4) || !strcmp(dent->d_name, "errorlog.txt")) ext |= EXT_LOADED; } From 6274b9fde11a7e985b85246cc9b66983798425c5 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 7 Oct 2019 17:55:31 -0700 Subject: [PATCH 05/16] ferror does not return errno, are you stupid? Use M_FileError to return the proper error description, or "end-of-file". (cherry picked from commit 2f521f2eb18a79fd7e0a6aca2f2b7b6113042d6c) --- src/d_netfil.c | 4 ++-- src/m_argv.c | 2 +- src/m_misc.c | 10 ++++++++++ src/m_misc.h | 2 ++ src/sdl12/sdl_sound.c | 2 +- src/w_wad.c | 12 ++++++------ 6 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/d_netfil.c b/src/d_netfil.c index 155700807..cac5a2d80 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -742,7 +742,7 @@ void SV_FileSendTicker(void) if (ram) M_Memcpy(p->data, &f->id.ram[transfer[i].position], size); else if (fread(p->data, 1, size, transfer[i].currentfile) != size) - I_Error("SV_FileSendTicker: can't read %s byte on %s at %d because %s", sizeu1(size), f->id.filename, transfer[i].position, strerror(ferror(transfer[i].currentfile))); + I_Error("SV_FileSendTicker: can't read %s byte on %s at %d because %s", sizeu1(size), f->id.filename, transfer[i].position, M_FileError(transfer[i].currentfile)); p->position = LONG(transfer[i].position); // Put flag so receiver knows the total size if (transfer[i].position + size == f->size) @@ -818,7 +818,7 @@ void Got_Filetxpak(void) // We can receive packet in the wrong order, anyway all os support gaped file fseek(file->file, pos, SEEK_SET); if (fwrite(netbuffer->u.filetxpak.data,size,1,file->file) != 1) - I_Error("Can't write to %s: %s\n",filename, strerror(ferror(file->file))); + I_Error("Can't write to %s: %s\n",filename, M_FileError(file->file)); file->currentsize += size; // Finished? diff --git a/src/m_argv.c b/src/m_argv.c index e8bfdd3db..a23f938a8 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -166,7 +166,7 @@ void M_FindResponseFile(void) if (!file) I_Error("No more free memory for the response file"); if (fread(file, size, 1, handle) != 1) - I_Error("Couldn't read response file because %s", strerror(ferror(handle))); + I_Error("Couldn't read response file because %s", M_FileError(handle)); fclose(handle); // keep all the command line arguments following @responsefile diff --git a/src/m_misc.c b/src/m_misc.c index 8c0070270..91f8f18c5 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2371,3 +2371,13 @@ void M_SetupMemcpy(void) M_Memcpy = cpu_cpy; #endif } + +/** Return the appropriate message for a file error or end of file. +*/ +const char *M_FileError(FILE *fp) +{ + if (ferror(fp)) + return strerror(errno); + else + return "end-of-file"; +} diff --git a/src/m_misc.h b/src/m_misc.h index 990555971..fb4012b8e 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -100,6 +100,8 @@ void strcatbf(char *s1, const char *s2, const char *s3); void M_SetupMemcpy(void); +const char *M_FileError(FILE *handle); + // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); diff --git a/src/sdl12/sdl_sound.c b/src/sdl12/sdl_sound.c index ed1afd8e2..1a7525fee 100644 --- a/src/sdl12/sdl_sound.c +++ b/src/sdl12/sdl_sound.c @@ -1435,7 +1435,7 @@ static boolean LoadSong(void *data, size_t lumplength, size_t selectpos) if (fwrite(data, lumplength, 1, midfile) == 0) { - CONS_Printf(M_GetText("Couldn't write music into file %s because %s\n"), tempname, strerror(ferror(midfile))); + CONS_Printf(M_GetText("Couldn't write music into file %s because %s\n"), tempname, M_FileError(midfile)); Z_Free(data); fclose(midfile); return false; diff --git a/src/w_wad.c b/src/w_wad.c index da82a276d..734308956 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -366,7 +366,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen // read the header if (fread(&header, 1, sizeof header, handle) < sizeof header) { - CONS_Alert(CONS_ERROR, M_GetText("Can't read wad header because %s\n"), strerror(ferror(handle))); + CONS_Alert(CONS_ERROR, M_GetText("Can't read wad header because %s\n"), M_FileError(handle)); return NULL; } @@ -389,7 +389,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen if (fseek(handle, header.infotableofs, SEEK_SET) == -1 || fread(fileinfo, 1, i, handle) < i) { - CONS_Alert(CONS_ERROR, M_GetText("Corrupt wadfile directory (%s)\n"), strerror(ferror(handle))); + CONS_Alert(CONS_ERROR, M_GetText("Corrupt wadfile directory (%s)\n"), M_FileError(handle)); free(fileinfov); return NULL; } @@ -410,7 +410,7 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen handle) < sizeof realsize) { I_Error("corrupt compressed file: %s; maybe %s", /// \todo Avoid the bailout? - filename, strerror(ferror(handle))); + filename, M_FileError(handle)); } realsize = LONG(realsize); if (realsize != 0) @@ -548,7 +548,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) fseek(handle, -4, SEEK_CUR); if (fread(&zend, 1, sizeof zend, handle) < sizeof zend) { - CONS_Alert(CONS_ERROR, "Corrupt central directory (%s)\n", strerror(ferror(handle))); + CONS_Alert(CONS_ERROR, "Corrupt central directory (%s)\n", M_FileError(handle)); return NULL; } numlumps = zend.entries; @@ -565,7 +565,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) if (fread(zentry, 1, sizeof(zentry_t), handle) < sizeof(zentry_t)) { - CONS_Alert(CONS_ERROR, "Failed to read central directory (%s)\n", strerror(ferror(handle))); + CONS_Alert(CONS_ERROR, "Failed to read central directory (%s)\n", M_FileError(handle)); Z_Free(lumpinfo); free(zentry); return NULL; @@ -585,7 +585,7 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) fullname = malloc(zentry->namelen + 1); if (fgets(fullname, zentry->namelen + 1, handle) != fullname) { - CONS_Alert(CONS_ERROR, "Unable to read lumpname (%s)\n", strerror(ferror(handle))); + CONS_Alert(CONS_ERROR, "Unable to read lumpname (%s)\n", M_FileError(handle)); Z_Free(lumpinfo); free(zentry); free(fullname); From c79ceca65e0dbb21c693bd573acc05d2908e2523 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 7 Oct 2019 18:10:33 -0700 Subject: [PATCH 06/16] Forgot includes (cherry picked from commit 92dd9d20663c8c601053354326fd9412694e3078) --- src/m_argv.c | 1 + src/m_misc.c | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/m_argv.c b/src/m_argv.c index a23f938a8..e1046f8a7 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -16,6 +16,7 @@ #include "doomdef.h" #include "command.h" #include "m_argv.h" +#include "m_misc.h" /** \brief number of arg */ diff --git a/src/m_misc.c b/src/m_misc.c index 91f8f18c5..a7bcd6ff4 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -23,6 +23,8 @@ #include #endif +#include + // Extended map support. #include From ccde660cdf19ed01eda5a49063fb629f061ed838 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 15:31:50 -0800 Subject: [PATCH 07/16] -logfile to let the user change the log file name (cherry picked from commit d9d13764e6840e036c095c6d87838a4dfede80f4) --- src/sdl/i_main.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 10f14f36f..61e15f2ba 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -133,15 +133,19 @@ int main(int argc, char **argv) { time_t my_time; struct tm * timeinfo; - char buf[26]; + const char *format; logdir = D_Home(); my_time = time(NULL); timeinfo = localtime(&my_time); - strftime(buf, 26, "%Y-%m-%d %H-%M-%S", timeinfo); - strcpy(logfile, va("log-%s.txt", buf)); + if (M_CheckParm("-logfile") && M_IsNextParm()) + format = M_GetNextParm(); + else + format = "log-%Y-%m-%d %H-%M-%S.txt"; + + strftime(logfile, sizeof logfile, format, timeinfo); #ifdef DEFAULTDIR if (logdir) From 599452af7beb4e14a4d1c57b7b1ddf59d82881db Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 15:34:27 -0800 Subject: [PATCH 08/16] Change default log filename to not use a space bleh (cherry picked from commit c285000c5668ab82545ec9ee4989aabf65e2585e) --- src/sdl/i_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 61e15f2ba..82367aaaa 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -143,7 +143,7 @@ int main(int argc, char **argv) if (M_CheckParm("-logfile") && M_IsNextParm()) format = M_GetNextParm(); else - format = "log-%Y-%m-%d %H-%M-%S.txt"; + format = "log-%Y-%m-%d_%H-%M-%S.txt"; strftime(logfile, sizeof logfile, format, timeinfo); From 45a2e22abc767b4e3076db09c256162dbb0629db Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 17:21:58 -0800 Subject: [PATCH 09/16] -logdir lets the user change the log directory (cherry picked from commit 457e986b7514c0e9fa921c1961344012443ac164) --- src/m_misc.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ src/m_misc.h | 4 +++ src/sdl/i_main.c | 34 ++++++++++++++++------ 3 files changed, 102 insertions(+), 9 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index a7bcd6ff4..2754bac74 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2383,3 +2383,76 @@ const char *M_FileError(FILE *fp) else return "end-of-file"; } + +/** Return the number of parts of this path. +*/ +int M_PathParts(const char *path) +{ + int n; + const char *p; + const char *t; + for (n = 0, p = path ;; ++n) + { + t = p; + if (( p = strchr(p, PATHSEP[0]) )) + p += strspn(p, PATHSEP); + else + { + if (*t)/* there is something after the final delimiter */ + n++; + break; + } + } + return n; +} + +/** Check whether a path is an absolute path. +*/ +boolean M_IsPathAbsolute(const char *path) +{ +#ifdef _WIN32 + return ( strncmp(&path[1], ":\\", 2) == 0 ); +#else + return ( path[0] == '/' ); +#endif +} + +/** I_mkdir for each part of the path. +*/ +void M_MkdirEach(const char *cpath, int start, int mode) +{ + char path[MAX_WADPATH]; + char *p; + char *t; + strlcpy(path, cpath, sizeof path); +#ifdef _WIN32 + if (strncmp(&path[1], ":\\", 2) == 0) + p = &path[3]; + else +#endif + p = path; + for (; start > 0; --start) + { + p += strspn(p, PATHSEP); + if (!( p = strchr(p, PATHSEP[0]) )) + return; + } + p += strspn(p, PATHSEP); + for (;;) + { + t = p; + if (( p = strchr(p, PATHSEP[0]) )) + { + *p = '\0'; + I_mkdir(path, mode); + *p = PATHSEP[0]; + p += strspn(p, PATHSEP); + } + else + { + if (*t) + I_mkdir(path, mode); + break; + } + } +} diff --git a/src/m_misc.h b/src/m_misc.h index fb4012b8e..0a82322b0 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -102,6 +102,10 @@ void M_SetupMemcpy(void); const char *M_FileError(FILE *handle); +int M_PathParts (const char *path); +boolean M_IsPathAbsolute (const char *path); +void M_MkdirEach (const char *path, int start, int mode); + // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 82367aaaa..e7f47bfae 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -20,6 +20,7 @@ #include "../doomdef.h" #include "../m_argv.h" #include "../d_main.h" +#include "../m_misc.h"/* path shit */ #include "../i_system.h" #ifdef __GNUC__ @@ -134,6 +135,8 @@ int main(int argc, char **argv) time_t my_time; struct tm * timeinfo; const char *format; + const char *reldir; + int left; logdir = D_Home(); @@ -145,24 +148,37 @@ int main(int argc, char **argv) else format = "log-%Y-%m-%d_%H-%M-%S.txt"; - strftime(logfile, sizeof logfile, format, timeinfo); + if (M_CheckParm("-logdir") && M_IsNextParm()) + reldir = M_GetNextParm(); + else + reldir = "logs"; + if (M_IsPathAbsolute(reldir)) + { + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP, reldir); + } + else #ifdef DEFAULTDIR if (logdir) { - // Create dirs here because D_SRB2Main() is too late. - I_mkdir(va("%s%s"DEFAULTDIR, logdir, PATHSEP), 0755); - I_mkdir(va("%s%s"DEFAULTDIR"%slogs",logdir, PATHSEP, PATHSEP), 0755); - logstream = fopen(va("%s%s"DEFAULTDIR"%slogs%s%s",logdir, PATHSEP, PATHSEP, PATHSEP, logfile), "wt"); + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); } else -#endif +#endif/*DEFAULTDIR*/ { - I_mkdir("."PATHSEP"logs"PATHSEP, 0755); - logstream = fopen(va("."PATHSEP"logs"PATHSEP"%s", logfile), "wt"); + left = snprintf(logfile, sizeof logfile, + "."PATHSEP"%s"PATHSEP, reldir); } +#endif/*LOGMESSAGES*/ + + M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); + + strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + + logstream = fopen(logfile, "wt"); } -#endif //I_OutputMsg("I_StartupSystem() ...\n"); I_StartupSystem(); From b4f93fea8e16f7695a4fb665cb3312487fe7205c Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 17:40:43 -0800 Subject: [PATCH 10/16] Let an asbolute path work with -logfile (cherry picked from commit 5fbe77cdda7ec52bc2ca10ab9253df8dc56d2317) --- src/m_misc.c | 18 ++++++++++++++- src/m_misc.h | 1 + src/sdl/i_main.c | 58 +++++++++++++++++++++++++++++++----------------- 3 files changed, 56 insertions(+), 21 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index 2754bac74..8363e9d17 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2419,11 +2419,15 @@ boolean M_IsPathAbsolute(const char *path) /** I_mkdir for each part of the path. */ -void M_MkdirEach(const char *cpath, int start, int mode) +void M_MkdirEachUntil(const char *cpath, int start, int end, int mode) { char path[MAX_WADPATH]; char *p; char *t; + + if (end > 0 && end <= start) + return; + strlcpy(path, cpath, sizeof path); #ifdef _WIN32 if (strncmp(&path[1], ":\\", 2) == 0) @@ -2431,6 +2435,10 @@ void M_MkdirEach(const char *cpath, int start, int mode) else #endif p = path; + + if (end > 0) + end -= start; + for (; start > 0; --start) { p += strspn(p, PATHSEP); @@ -2440,6 +2448,9 @@ void M_MkdirEach(const char *cpath, int start, int mode) p += strspn(p, PATHSEP); for (;;) { + if (end > 0 && !--end) + break; + t = p; if (( p = strchr(p, PATHSEP[0]) )) { @@ -2456,3 +2467,8 @@ void M_MkdirEach(const char *cpath, int start, int mode) } } } + +void M_MkdirEach(const char *path, int start, int mode) +{ + M_MkdirEachUntil(path, start, -1, mode); +} diff --git a/src/m_misc.h b/src/m_misc.h index 0a82322b0..9e43b20c9 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -105,6 +105,7 @@ const char *M_FileError(FILE *handle); int M_PathParts (const char *path); boolean M_IsPathAbsolute (const char *path); void M_MkdirEach (const char *path, int start, int mode); +void M_MkdirEachUntil (const char *path, int start, int end, int mode); // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index e7f47bfae..7059348b7 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -137,6 +137,7 @@ int main(int argc, char **argv) const char *format; const char *reldir; int left; + boolean fileabs; logdir = D_Home(); @@ -144,38 +145,55 @@ int main(int argc, char **argv) timeinfo = localtime(&my_time); if (M_CheckParm("-logfile") && M_IsNextParm()) + { format = M_GetNextParm(); + fileabs = M_IsPathAbsolute(format); + } else + { format = "log-%Y-%m-%d_%H-%M-%S.txt"; + fileabs = false; + } - if (M_CheckParm("-logdir") && M_IsNextParm()) - reldir = M_GetNextParm(); - else - reldir = "logs"; - - if (M_IsPathAbsolute(reldir)) + if (fileabs) { - left = snprintf(logfile, sizeof logfile, - "%s"PATHSEP, reldir); + strftime(logfile, sizeof logfile, format, timeinfo); + + M_MkdirEachUntil(logfile, + M_PathParts(logdir) - 1, + M_PathParts(logfile) - 1, 0755); } else + { + if (M_CheckParm("-logdir") && M_IsNextParm()) + reldir = M_GetNextParm(); + else + reldir = "logs"; + + if (M_IsPathAbsolute(reldir)) + { + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP, reldir); + } + else #ifdef DEFAULTDIR - if (logdir) - { - left = snprintf(logfile, sizeof logfile, - "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); - } - else + if (logdir) + { + left = snprintf(logfile, sizeof logfile, + "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); + } + else #endif/*DEFAULTDIR*/ - { - left = snprintf(logfile, sizeof logfile, - "."PATHSEP"%s"PATHSEP, reldir); - } + { + left = snprintf(logfile, sizeof logfile, + "."PATHSEP"%s"PATHSEP, reldir); + } #endif/*LOGMESSAGES*/ - M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); + M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); - strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + } logstream = fopen(logfile, "wt"); } From 294dfbacc5886dfa13ea708e932cedbd72b854f7 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 17:43:42 -0800 Subject: [PATCH 11/16] Create directories from -logfile too (cherry picked from commit 98cb238d36e0f85d9d4b4d2cff59c720e1630bdd) --- src/sdl/i_main.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 7059348b7..932861353 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -158,10 +158,6 @@ int main(int argc, char **argv) if (fileabs) { strftime(logfile, sizeof logfile, format, timeinfo); - - M_MkdirEachUntil(logfile, - M_PathParts(logdir) - 1, - M_PathParts(logfile) - 1, 0755); } else { @@ -190,11 +186,13 @@ int main(int argc, char **argv) } #endif/*LOGMESSAGES*/ - M_MkdirEach(logfile, M_PathParts(logdir) - 1, 0755); - strftime(&logfile[left], sizeof logfile - left, format, timeinfo); } + M_MkdirEachUntil(logfile, + M_PathParts(logdir) - 1, + M_PathParts(logfile) - 1, 0755); + logstream = fopen(logfile, "wt"); } From 10a9a05cd89ed234062b5464c41efc8b8c78ae79 Mon Sep 17 00:00:00 2001 From: James R Date: Mon, 23 Dec 2019 18:20:04 -0800 Subject: [PATCH 12/16] symlink latest-log.txt on nix, copy to the real log file everywhere else (cherry picked from commit 25525a6aae84581ac03ca3089950ad4ff25b8512) --- src/doomdef.h | 1 + src/sdl/i_main.c | 40 +++++++++++++++++++++++++++++----------- src/sdl/i_system.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 11 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index bb21e3d0f..a3e473acc 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -138,6 +138,7 @@ #ifdef LOGMESSAGES extern FILE *logstream; +extern char logfilename[1024]; #endif //#define DEVELOP // Disable this for release builds to remove excessive cheat commands and enable MD5 checking and stuff, all in one go. :3 diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 932861353..0550b8e02 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -23,10 +23,14 @@ #include "../m_misc.h"/* path shit */ #include "../i_system.h" -#ifdef __GNUC__ +#if defined (__GNUC__) || defined (__unix__) #include #endif +#ifdef __unix__ +#include +#endif + #include "time.h" // For log timestamps #ifdef HAVE_SDL @@ -48,6 +52,7 @@ extern int SDL_main(int argc, char *argv[]); #ifdef LOGMESSAGES FILE *logstream = NULL; +char logfilename[1024]; #endif #ifndef DOXYGEN @@ -117,7 +122,6 @@ int main(int argc, char **argv) #endif { const char *logdir = NULL; - char logfile[MAX_WADPATH]; myargc = argc; myargv = argv; /// \todo pull out path to exe from this string @@ -157,7 +161,7 @@ int main(int argc, char **argv) if (fileabs) { - strftime(logfile, sizeof logfile, format, timeinfo); + strftime(logfilename, sizeof logfilename, format, timeinfo); } else { @@ -168,32 +172,46 @@ int main(int argc, char **argv) if (M_IsPathAbsolute(reldir)) { - left = snprintf(logfile, sizeof logfile, + left = snprintf(logfilename, sizeof logfilename, "%s"PATHSEP, reldir); } else #ifdef DEFAULTDIR if (logdir) { - left = snprintf(logfile, sizeof logfile, + left = snprintf(logfilename, sizeof logfilename, "%s"PATHSEP DEFAULTDIR PATHSEP"%s"PATHSEP, logdir, reldir); } else #endif/*DEFAULTDIR*/ { - left = snprintf(logfile, sizeof logfile, + left = snprintf(logfilename, sizeof logfilename, "."PATHSEP"%s"PATHSEP, reldir); } #endif/*LOGMESSAGES*/ - strftime(&logfile[left], sizeof logfile - left, format, timeinfo); + strftime(&logfilename[left], sizeof logfilename - left, + format, timeinfo); } - M_MkdirEachUntil(logfile, + M_MkdirEachUntil(logfilename, M_PathParts(logdir) - 1, - M_PathParts(logfile) - 1, 0755); + M_PathParts(logfilename) - 1, 0755); - logstream = fopen(logfile, "wt"); +#ifdef __unix__ + logstream = fopen(logfilename, "w"); +#ifdef DEFAULTDIR + if (symlink(logfilename, + va("%s/"DEFAULTDIR"/latest-log.txt", logdir)) == -1) +#else + if (symlink(logfilename, va("%s/latest-log.txt", logdir)) == -1) +#endif/*DEFAULTDIR*/ + { + I_OutputMsg("Error symlinking latest-log.txt: %s\n", strerror(errno)); + } +#else/*__unix__*/ + logstream = fopen("latest-log.txt", "wt+"); +#endif/*__unix__*/ } //I_OutputMsg("I_StartupSystem() ...\n"); @@ -222,7 +240,7 @@ int main(int argc, char **argv) D_SRB2Main(); #ifdef LOGMESSAGES if (!M_CheckParm("-nolog")) - CONS_Printf("Logfile: %s\n", logfile); + CONS_Printf("Logfile: %s\n", logfilename); #endif CONS_Printf("Entering main game loop...\n"); // never return diff --git a/src/sdl/i_system.c b/src/sdl/i_system.c index f75a42117..2ccdec5c4 100644 --- a/src/sdl/i_system.c +++ b/src/sdl/i_system.c @@ -3268,6 +3268,48 @@ void I_RemoveExitFunc(void (*func)()) } } +#ifndef __unix__ +static void Shittycopyerror(const char *name) +{ + I_OutputMsg( + "Error copying log file: %s: %s\n", + name, + strerror(errno) + ); +} + +static void Shittylogcopy(void) +{ + char buf[8192]; + FILE *fp; + int n; + if (fseek(logstream, 0, SEEK_SET) == -1) + { + Shittycopyerror("fseek"); + } + else if (( fp = fopen(logfilename, "wt") )) + { + while (( n = fread(buf, 1, sizeof buf, logstream) )) + { + if (fwrite(buf, 1, n, fp) < n) + { + Shittycopyerror("fwrite"); + break; + } + } + if (ferror(logstream)) + { + Shittycopyerror("fread"); + } + fclose(fp); + } + else + { + Shittycopyerror(logfilename); + } +} +#endif/*__unix__*/ + // // Closes down everything. This includes restoring the initial // palette and video mode, and removing whatever mouse, keyboard, and @@ -3286,6 +3328,9 @@ void I_ShutdownSystem(void) if (logstream) { I_OutputMsg("I_ShutdownSystem(): end of logstream.\n"); +#ifndef __unix__ + Shittylogcopy(); +#endif fclose(logstream); logstream = NULL; } From d1de8cc2ebcc0dcd6bd2a0fc638a6928f6959ca5 Mon Sep 17 00:00:00 2001 From: James R Date: Tue, 24 Dec 2019 01:55:47 -0800 Subject: [PATCH 13/16] Overwrite an already existing symlink (cherry picked from commit f2c2836301d4dc59242d09f08a5473341c4d692c) --- src/sdl/i_main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 0550b8e02..f8dcc3c3b 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -142,6 +142,7 @@ int main(int argc, char **argv) const char *reldir; int left; boolean fileabs; + const char *link; logdir = D_Home(); @@ -201,11 +202,12 @@ int main(int argc, char **argv) #ifdef __unix__ logstream = fopen(logfilename, "w"); #ifdef DEFAULTDIR - if (symlink(logfilename, - va("%s/"DEFAULTDIR"/latest-log.txt", logdir)) == -1) + link = va("%s/"DEFAULTDIR"/latest-log.txt", logdir); #else - if (symlink(logfilename, va("%s/latest-log.txt", logdir)) == -1) + link = va("%s/latest-log.txt", logdir); #endif/*DEFAULTDIR*/ + unlink(link); + if (symlink(logfilename, link) == -1) { I_OutputMsg("Error symlinking latest-log.txt: %s\n", strerror(errno)); } From 7ef8e81713dc2f20b4855e1389d34772d45e5cdc Mon Sep 17 00:00:00 2001 From: James R Date: Tue, 24 Dec 2019 01:58:39 -0800 Subject: [PATCH 14/16] Correct usage of logdir (cherry picked from commit b7b4945c36fb95f406a365d64f109cfc83ad4d63) --- src/sdl/i_main.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index f8dcc3c3b..816142d72 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -202,10 +202,11 @@ int main(int argc, char **argv) #ifdef __unix__ logstream = fopen(logfilename, "w"); #ifdef DEFAULTDIR - link = va("%s/"DEFAULTDIR"/latest-log.txt", logdir); -#else - link = va("%s/latest-log.txt", logdir); + if (logdir) + link = va("%s/"DEFAULTDIR"/latest-log.txt", logdir); + else #endif/*DEFAULTDIR*/ + link = "latest-log.txt"; unlink(link); if (symlink(logfilename, link) == -1) { From f74adb0ad99d72ce710dd0d34da9f7231cacc4af Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sat, 28 Dec 2019 22:14:23 -0300 Subject: [PATCH 15/16] Fix logfiles crashing Windoze (cherry picked from commit 3bb7fd4cbf2bb92f5548cafad00b80cb366395a4) --- src/sdl/i_main.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 816142d72..9daca99fa 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -195,9 +195,12 @@ int main(int argc, char **argv) format, timeinfo); } - M_MkdirEachUntil(logfilename, - M_PathParts(logdir) - 1, - M_PathParts(logfilename) - 1, 0755); + if (logdir) + { + M_MkdirEachUntil(logfilename, + M_PathParts(logdir) - 1, + M_PathParts(logfilename) - 1, 0755); + } #ifdef __unix__ logstream = fopen(logfilename, "w"); From 21a3067936003c441460a3b721d838dae57c619f Mon Sep 17 00:00:00 2001 From: Jaime Passos Date: Sat, 28 Dec 2019 22:18:41 -0300 Subject: [PATCH 16/16] Fix M_PathParts instead (cherry picked from commit 244f0b228fefb69fb59c1f7f70d4815c0388a634) --- src/m_misc.c | 2 ++ src/sdl/i_main.c | 9 +++------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index 8363e9d17..f2f3bbdf5 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2391,6 +2391,8 @@ int M_PathParts(const char *path) int n; const char *p; const char *t; + if (path == NULL) + return 0; for (n = 0, p = path ;; ++n) { t = p; diff --git a/src/sdl/i_main.c b/src/sdl/i_main.c index 9daca99fa..816142d72 100644 --- a/src/sdl/i_main.c +++ b/src/sdl/i_main.c @@ -195,12 +195,9 @@ int main(int argc, char **argv) format, timeinfo); } - if (logdir) - { - M_MkdirEachUntil(logfilename, - M_PathParts(logdir) - 1, - M_PathParts(logfilename) - 1, 0755); - } + M_MkdirEachUntil(logfilename, + M_PathParts(logdir) - 1, + M_PathParts(logfilename) - 1, 0755); #ifdef __unix__ logstream = fopen(logfilename, "w");