From feb18208cbb6fbe0c380d96713af2278aa4119cb Mon Sep 17 00:00:00 2001 From: fickleheart Date: Sat, 21 Mar 2020 01:36:39 -0500 Subject: [PATCH 1/4] Add support for srb2:// URL handler (server links) --- src/d_main.c | 27 +++++++++++++++++++++++++-- src/i_tcp.c | 9 ++++++--- src/m_argv.c | 18 ++++++++++++++++++ src/m_argv.h | 3 +++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 3840e77a0..d954fa4d1 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -889,6 +889,29 @@ static void IdentifyVersion(void) char *srb2wad; const char *srb2waddir = NULL; + // URL handlers are opened by web browsers (at least Firefox) from the browser's working directory, not the game's stored directory, + // so chdir to that directory unless overridden. + if (M_GetUrlProtocolArg() != NULL && !M_CheckParm("-nochdir")) + { + size_t i; + + CONS_Printf("srb2:// connect links load game files from the SRB2 application's stored directory. Switching to "); + strlcpy(srb2path, myargv[0], sizeof(srb2path)); + + // Get just the directory, minus the EXE name + for (i = strlen(srb2path)-1; i > 0; i--) + { + if (srb2path[i] == '/' || srb2path[i] == '\\') + { + srb2path[i] = '\0'; + break; + } + } + + CONS_Printf("%s\n", srb2path); + chdir(srb2path); + } + #if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) || defined (HAVE_SDL) // change to the directory where 'srb2.pk3' is found srb2waddir = I_LocateWad(); @@ -1152,7 +1175,7 @@ void D_SRB2Main(void) // add any files specified on the command line with -file wadfile // to the wad list - if (!(M_CheckParm("-connect") && !M_CheckParm("-server"))) + if (!((M_GetUrlProtocolArg() || M_CheckParm("-connect")) && !M_CheckParm("-server"))) { if (M_CheckParm("-file")) { @@ -1187,7 +1210,7 @@ void D_SRB2Main(void) M_InitMenuPresTables(); // init title screen display params - if (M_CheckParm("-connect")) + if (M_GetUrlProtocolArg() || M_CheckParm("-connect")) F_InitMenuPresValues(); //---------------------------------------------------- READY TIME diff --git a/src/i_tcp.c b/src/i_tcp.c index 34cad1765..373ea1bd0 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -1423,6 +1423,7 @@ static void SOCK_ClearBans(void) boolean I_InitTcpNetwork(void) { char serverhostname[255]; + const char *urlparam = NULL; boolean ret = false; // initilize the OS's TCP/IP stack if (!I_InitTcpDriver()) @@ -1476,10 +1477,12 @@ boolean I_InitTcpNetwork(void) ret = true; } - else if (M_CheckParm("-connect")) + else if ((urlparam = M_GetUrlProtocolArg()) != NULL || M_CheckParm("-connect")) { - if (M_IsNextParm()) - strcpy(serverhostname, M_GetNextParm()); + if (urlparam != NULL) + strlcpy(serverhostname, urlparam, sizeof(serverhostname)); + else if (M_IsNextParm()) + strlcpy(serverhostname, M_GetNextParm(), sizeof(serverhostname)); else serverhostname[0] = 0; // assuming server in the LAN, use broadcast to detect it diff --git a/src/m_argv.c b/src/m_argv.c index acb74cff4..d7d1f4463 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -34,6 +34,24 @@ boolean myargmalloc = false; */ static INT32 found; +/** \brief Parses a server URL (such as srb2://127.0.0.1) as may be passed to the game via a web browser, etc. + + \return the contents of the URL after the protocol (a server to join), or NULL if not found +*/ +const char *M_GetUrlProtocolArg(void) +{ + INT32 i; + + for (i = 1; i < myargc; i++) + { + if (!strnicmp(myargv[i], "srb2://", 7)) + { + return &myargv[i][7]; + } + } + + return NULL; +} /** \brief The M_CheckParm function diff --git a/src/m_argv.h b/src/m_argv.h index ca97d9b12..92770f4e9 100644 --- a/src/m_argv.h +++ b/src/m_argv.h @@ -21,6 +21,9 @@ extern INT32 myargc; extern char **myargv; extern boolean myargmalloc; +// Looks for an srb2:// (or similar) URL passed in as an argument and returns the IP to connect to if found. +const char *M_GetUrlProtocolArg(void); + // Returns the position of the given parameter in the arg list (0 if not found). INT32 M_CheckParm(const char *check); From f32ab5918ec39181e238ebf5333499718f73e4b5 Mon Sep 17 00:00:00 2001 From: fickleheart Date: Sat, 21 Mar 2020 07:47:29 -0500 Subject: [PATCH 2/4] Define SERVER_URL_PROTOCOL const for easy reconfiguration --- src/d_main.c | 2 +- src/doomdef.h | 3 +++ src/m_argv.c | 5 +++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index d954fa4d1..ec63242b8 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -895,7 +895,7 @@ static void IdentifyVersion(void) { size_t i; - CONS_Printf("srb2:// connect links load game files from the SRB2 application's stored directory. Switching to "); + CONS_Printf("%s connect links load game files from the SRB2 application's stored directory. Switching to ", SERVER_URL_PROTOCOL); strlcpy(srb2path, myargv[0], sizeof(srb2path)); // Get just the directory, minus the EXE name diff --git a/src/doomdef.h b/src/doomdef.h index 71c885019..5a5c933e3 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -150,6 +150,9 @@ extern char logfilename[1024]; // Otherwise we can't force updates! #endif +/* A custom URL protocol for server links. */ +#define SERVER_URL_PROTOCOL "srb2://" + // Does this version require an added patch file? // Comment or uncomment this as necessary. #define USE_PATCH_DTA diff --git a/src/m_argv.c b/src/m_argv.c index d7d1f4463..7d43d96bc 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -41,12 +41,13 @@ static INT32 found; const char *M_GetUrlProtocolArg(void) { INT32 i; + const size_t len = strlen(SERVER_URL_PROTOCOL); for (i = 1; i < myargc; i++) { - if (!strnicmp(myargv[i], "srb2://", 7)) + if (strlen(myargv[i]) > len && !strnicmp(myargv[i], SERVER_URL_PROTOCOL, len)) { - return &myargv[i][7]; + return &myargv[i][len]; } } From 6845aca6e5cf2292b5586dbf01ea0be80c9225af Mon Sep 17 00:00:00 2001 From: fickleheart Date: Thu, 9 Apr 2020 20:52:23 -0500 Subject: [PATCH 3/4] Make chdir usage consistent with other source usage --- src/d_main.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/d_main.c b/src/d_main.c index 8519e6281..80f075cb1 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -909,7 +909,13 @@ static void IdentifyVersion(void) } CONS_Printf("%s\n", srb2path); - chdir(srb2path); + +#if defined (_WIN32) + SetCurrentDirectoryA(srb2path); +#else + if (chdir(srb2path) == -1) + I_OutputMsg("Couldn't change working directory\n"); +#endif } #if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) || defined (HAVE_SDL) From be07a23e5265aceea62f18acce09c1e7cdda31af Mon Sep 17 00:00:00 2001 From: fickleheart Date: Thu, 9 Apr 2020 20:56:27 -0500 Subject: [PATCH 4/4] Make a separate function instead of clogging up IdentifyVersion --- src/d_main.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 80f075cb1..a0672bb4e 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -880,15 +880,10 @@ static inline void D_CleanFile(void) } } -// ========================================================================== -// Identify the SRB2 version, and IWAD file to use. -// ========================================================================== - -static void IdentifyVersion(void) +///\brief Checks if a netgame URL is being handled, and changes working directory to the EXE's if so. +/// Done because browsers (at least, Firefox on Windows) launch the game from the browser's directory, which causes problems. +static void ChangeDirForUrlHandler(void) { - char *srb2wad; - const char *srb2waddir = NULL; - // URL handlers are opened by web browsers (at least Firefox) from the browser's working directory, not the game's stored directory, // so chdir to that directory unless overridden. if (M_GetUrlProtocolArg() != NULL && !M_CheckParm("-nochdir")) @@ -917,6 +912,16 @@ static void IdentifyVersion(void) I_OutputMsg("Couldn't change working directory\n"); #endif } +} + +// ========================================================================== +// Identify the SRB2 version, and IWAD file to use. +// ========================================================================== + +static void IdentifyVersion(void) +{ + char *srb2wad; + const char *srb2waddir = NULL; #if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) || defined (HAVE_SDL) // change to the directory where 'srb2.pk3' is found @@ -1097,6 +1102,9 @@ void D_SRB2Main(void) // Test Dehacked lists DEH_Check(); + // Netgame URL special case: change working dir to EXE folder. + ChangeDirForUrlHandler(); + // identify the main IWAD file to use IdentifyVersion();