From 608cc66d789aa4e03263ba26c559a4cc0660236c Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 21 May 2025 12:14:45 +0200 Subject: [PATCH 1/3] try to fix curl crashing if you dont have write access to download folder --- src/d_clisrv.c | 17 ++++++++--------- src/d_clisrv.h | 3 +++ src/d_netfil.c | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/d_clisrv.c b/src/d_clisrv.c index 02721559d..9625934fc 100644 --- a/src/d_clisrv.c +++ b/src/d_clisrv.c @@ -2290,7 +2290,7 @@ static void M_ConfirmConnect(event_t *ev) #endif } -static void AbortConnection(void) +void CL_AbortConnection(void) { #ifdef HAVE_CURL CURLAbortFile(); @@ -2303,7 +2303,6 @@ static void AbortConnection(void) cl_mode = CL_ABORTED; } - static boolean CL_FinishedFileList(void) { INT32 i; @@ -2316,7 +2315,7 @@ static boolean CL_FinishedFileList(void) } else if (i == 3) // too many files { - AbortConnection(); + CL_AbortConnection(); M_StartMessage(M_GetText( "You have too many WAD files loaded\n" "to add ones the server is using.\n" @@ -2327,7 +2326,7 @@ static boolean CL_FinishedFileList(void) } else if (i == 2) // cannot join for some reason { - AbortConnection(); + CL_AbortConnection(); M_StartMessage(M_GetText( "You have the wrong addons loaded.\n\n" "To play on this server, restart\n" @@ -2364,7 +2363,7 @@ static boolean CL_FinishedFileList(void) { if (!CL_CheckDownloadable()) // nope! { - AbortConnection(); + CL_AbortConnection(); M_StartMessage(M_GetText( "An error occured when trying to\n" "download missing addons.\n" @@ -2629,7 +2628,7 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic { CONS_Printf(M_GetText("Legacy downloader request packet failed.\n")); CONS_Printf(M_GetText("Network game synchronization aborted.\n")); - AbortConnection(); + CL_AbortConnection(); M_StartMessage(M_GetText( "The direct download encountered an error.\n" "See the logfile for more info.\n" @@ -2656,7 +2655,7 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic { CONS_Printf("%d minute wait time exceeded.\n", cv_connectawaittime.value); CONS_Printf(M_GetText("Network game synchronization aborted.\n")); - AbortConnection(); + CL_AbortConnection(); M_StartMessage(va( "%d minute wait time exceeded.\n" "You may retry connection.\n" @@ -2727,7 +2726,7 @@ static boolean CL_ServerConnectionTicker(const char *tmpsave, tic_t *oldtic, tic if (!modeattacking && (key == KEY_ESCAPE || key == KEY_JOY1+1 || cl_mode == CL_ABORTED)) { CONS_Printf(M_GetText("Network game synchronization aborted.\n")); - AbortConnection(); + CL_AbortConnection(); return false; } @@ -3955,7 +3954,7 @@ static void Got_KickCmd(UINT8 **p, INT32 playernum) #ifdef DUMPCONSISTENCY if (msg == KICK_MSG_CON_FAIL) SV_SavedGame(); #endif - AbortConnection(); + CL_AbortConnection(); if (msg == KICK_MSG_CON_FAIL) M_StartMessage(M_GetText("Server closed connection\n(Synch failure)\nPress ESC\n"), NULL, MM_NOTHING); diff --git a/src/d_clisrv.h b/src/d_clisrv.h index 65884362b..6202c8c52 100644 --- a/src/d_clisrv.h +++ b/src/d_clisrv.h @@ -635,6 +635,9 @@ void CL_RemovePlayer(INT32 playernum, INT32 reason); void CL_QueryServerList(msg_server_t *list); void CL_UpdateServerList(void); void CL_TimeoutServerList(void); + +void CL_AbortConnection(void); + // Is there a game running boolean Playing(void); diff --git a/src/d_netfil.c b/src/d_netfil.c index a71df9a71..0650f3c34 100644 --- a/src/d_netfil.c +++ b/src/d_netfil.c @@ -1306,6 +1306,21 @@ void CURLPrepareFile(const char* url, int dfilenum) strcatbf(curl_curfile->filename, downloaddir, "/"); curl_curfile->file = fopen(curl_curfile->filename, "wb"); + + if (!curl_curfile->file) + { + CONS_Alert(CONS_ERROR, "Couldnt open %s for writing!\nAborting file download.\nCheck if you have write access to your download folder!\n", curl_curfile->filename); + CL_AbortConnection(); + M_StartMessage(M_GetText( + "An error occured when trying to\n" + "download missing addons.\n" + "See the console or log file\n" + "for additional details.\n\n" + "Press ESC\n" + ), NULL, MM_NOTHING); + return; + } + curl_easy_setopt(http_handle, CURLOPT_WRITEDATA, curl_curfile->file); curl_easy_setopt(http_handle, CURLOPT_WRITEFUNCTION, curlwrite_data); curl_easy_setopt(http_handle, CURLOPT_NOPROGRESS, 0L); @@ -1366,6 +1381,7 @@ void CURLGetFile(void) CONS_Alert(CONS_WARNING, "curl_multi_wait() failed, code %d.\n", mc); continue; } + curl_curfile->currentsize = curl_dlnow; curl_curfile->totalsize = curl_dltotal; } From b574ed0b1b403531a37163abe9eaf97c09358c6e Mon Sep 17 00:00:00 2001 From: Alug Date: Thu, 22 May 2025 23:17:38 +0200 Subject: [PATCH 2/3] allow capping the game fps seperately for when its unfocused --- src/m_menu.c | 9 ++++++--- src/r_fps.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/r_fps.h | 2 +- src/r_main.c | 1 + 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/m_menu.c b/src/m_menu.c index 535f9162f..2fb38a4d5 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -1715,9 +1715,11 @@ static menuitem_t OP_FocusOptionsMenu[] = {IT_STRING|IT_CVAR, NULL, "Pause Game While Unfocused", &cv_pauseifunfocused, 60}, - {IT_STRING|IT_CVAR, NULL, "Show \"FOCUS LOST\"", &cv_showfocuslost, 80}, + {IT_STRING|IT_CVAR, NULL, "Background FPS Cap", &cv_fpscapbg, 80}, - {IT_STRING|IT_CVAR, NULL, "Always Grab Mouse While Focused", &cv_alwaysgrabmouse, 100}, + {IT_STRING|IT_CVAR, NULL, "Show \"FOCUS LOST\"", &cv_showfocuslost, 100}, + + {IT_STRING|IT_CVAR, NULL, "Always Grab Mouse While Focused", &cv_alwaysgrabmouse, 120}, }; static const char* OP_FocusOptionsTooltips[] = @@ -1726,6 +1728,7 @@ static const char* OP_FocusOptionsTooltips[] = "Should music play while the game is unfocused?", "Should soundeffects play while the game is unfocused?", "Should the game pause while the game is unfocused?", + "Set manual framerate cap while the game is unfocused.", "Should the FOCUS LOST window appear\n while the game is unfocused?", "Should the mouse cursor be grabbed and hidden\n while the game is in focus?", }; @@ -3157,7 +3160,7 @@ menu_t OP_ChatOptionsDef = DEFAULTMENUSTYLE("M_HUD", OP_ChatOptionsMenu, &OP_HUD menu_t OP_SoundAdvancedDef = DEFAULTSCROLLSTYLE("M_SOUND", OP_SoundAdvancedMenu, &OP_SoundOptionsDef, 30, 30); -menu_t OP_FocusOptionsDef = DEFAULTMENUSTYLE(NULL, OP_FocusOptionsMenu, &OP_MainDef, 25, 30); +menu_t OP_FocusOptionsDef = DEFAULTMENUSTYLE(NULL, OP_FocusOptionsMenu, &OP_MainDef, 17, 30); menu_t OP_GameOptionsDef = DEFAULTMENUSTYLE("M_GAME", OP_GameOptionsMenu, &OP_MainDef, 30, 20); menu_t OP_ServerOptionsDef = DEFAULTMENUSTYLE("M_SERVER", OP_ServerOptionsMenu, &OP_MainDef, 24, 20); diff --git a/src/r_fps.c b/src/r_fps.c index a427d823e..4938988dc 100644 --- a/src/r_fps.c +++ b/src/r_fps.c @@ -41,13 +41,49 @@ static CV_PossibleValue_t fpscap_cons_t[] = { {0, "Match refresh rate"}, {0, NULL} }; + consvar_t cv_fpscap = {"fpscap", "Match refresh rate", CV_SAVE, fpscap_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; +consvar_t cv_fpscapbg = {"fpscapbackground", "Match refresh rate", CV_SAVE, fpscap_cons_t, NULL, 0, NULL, NULL, 0, 0, NULL}; consvar_t cv_precipinterp = {"precipinterpolation", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL}; ps_metric_t ps_interp_frac = {0}; ps_metric_t ps_interp_lag = {0}; +static boolean R_UseBackgroundFramerateCap(void) +{ + if (!window_notinfocus) + return false; + + // if foreground is unlimited or matched to refresh rate but bg is limited + if (cv_fpscap.value <= 0 && cv_fpscapbg.value >= 0) + return true; + + // use the lower value + if ((cv_fpscap.value > 0 && cv_fpscapbg.value > 0) + && (cv_fpscapbg.value < cv_fpscap.value)) + return true; + + return false; +} + +static UINT32 R_GetBackgroundFramerateCap(void) +{ + if (cv_fpscapbg.value == 0) + { + // 0: Match refresh rate + return I_GetRefreshRate(); + } + + if (cv_fpscapbg.value < 0) + { + // -1: Unlimited + return 0; + } + + return cv_fpscapbg.value; +} + UINT32 R_GetFramerateCap(void) { if (rendermode == render_none) @@ -57,6 +93,11 @@ UINT32 R_GetFramerateCap(void) return TICRATE; } + if (R_UseBackgroundFramerateCap()) + { + return R_GetBackgroundFramerateCap(); + } + if (cv_fpscap.value == 0) { // 0: Match refresh rate diff --git a/src/r_fps.h b/src/r_fps.h index 2ca6f1bf3..7ed55c848 100644 --- a/src/r_fps.h +++ b/src/r_fps.h @@ -20,7 +20,7 @@ #include "r_state.h" #include "m_perfstats.h" -extern consvar_t cv_fpscap; +extern consvar_t cv_fpscap, cv_fpscapbg; extern consvar_t cv_precipinterp; extern ps_metric_t ps_interp_frac; diff --git a/src/r_main.c b/src/r_main.c index b1b707d1d..5a7d77acd 100644 --- a/src/r_main.c +++ b/src/r_main.c @@ -1576,5 +1576,6 @@ void R_RegisterEngineStuff(void) // Frame interpolation/uncapped CV_RegisterVar(&cv_fpscap); + CV_RegisterVar(&cv_fpscapbg); CV_RegisterVar(&cv_precipinterp); } From 7e5ae71714fd3233696f376db87b681116e0d4d7 Mon Sep 17 00:00:00 2001 From: Indev Date: Fri, 23 May 2025 15:14:36 +0300 Subject: [PATCH 3/3] Do backups for custom configs too --- src/m_misc.c | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/src/m_misc.c b/src/m_misc.c index 6b9699243..c44086f4c 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -549,6 +549,28 @@ void M_FirstLoadConfig(void) CV_ToggleExecVersion(false); } +static boolean M_BackupConfig(const char *filename) +{ + char backupfile[MAX_WADPATH+4]; + + snprintf(backupfile, sizeof backupfile, "%s.bak", filename); + backupfile[sizeof backupfile - 1] = '\0'; + + FILE *config = fopen(filename, "r"); + + if (config != NULL) + { + fclose(config); + if (FIL_CopyFile(filename, backupfile) == false) + { + CONS_Alert(CONS_WARNING,"Failed to create a backup of the configuration file. Will not attempt to write to file\n"); + return false; + } + } + + return true; +} + /** Saves the game configuration. * * \sa Command_SaveConfig_f @@ -557,28 +579,11 @@ void M_SaveConfig(const char *filename) { FILE *f; char *filepath; - char backupfile[MAX_WADPATH+4]; // make sure not to write back the config until it's been correctly loaded if (!loaded_config) return; - // Create backup of the config file - snprintf(backupfile, sizeof backupfile, "%s.bak", configfile); - backupfile[sizeof backupfile - 1] = '\0'; - - FILE *config = fopen(configfile, "r"); - - if (config != NULL) - { - fclose(config); - if (FIL_CopyFile(configfile, backupfile) == false) - { - CONS_Alert(CONS_WARNING,"Failed to create a backup of the configuration file. Will not attempt to write to file\n"); - return; - } - } - // can change the file name if (filename) { @@ -595,6 +600,10 @@ void M_SaveConfig(const char *filename) else filepath = Z_StrDup(filename); + // If failed to backup, do not proceed + if (!M_BackupConfig(filepath)) + return; + f = fopen(filepath, "w"); // change it only if valid if (f) @@ -613,6 +622,10 @@ void M_SaveConfig(const char *filename) return; } + // If failed to backup, do not proceed + if (!M_BackupConfig(configfile)) + return; + f = fopen(configfile, "w"); if (!f) {