Merge branch 'netgame-urls' into 'master'

srb2:// server URLs

See merge request STJr/SRB2!858
This commit is contained in:
James R 2020-04-18 20:11:00 -04:00
commit 40ec8c4344
5 changed files with 70 additions and 5 deletions

View file

@ -880,6 +880,40 @@ static inline void D_CleanFile(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)
{
// 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("%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
for (i = strlen(srb2path)-1; i > 0; i--)
{
if (srb2path[i] == '/' || srb2path[i] == '\\')
{
srb2path[i] = '\0';
break;
}
}
CONS_Printf("%s\n", srb2path);
#if defined (_WIN32)
SetCurrentDirectoryA(srb2path);
#else
if (chdir(srb2path) == -1)
I_OutputMsg("Couldn't change working directory\n");
#endif
}
}
// ========================================================================== // ==========================================================================
// Identify the SRB2 version, and IWAD file to use. // Identify the SRB2 version, and IWAD file to use.
// ========================================================================== // ==========================================================================
@ -1068,6 +1102,9 @@ void D_SRB2Main(void)
// Test Dehacked lists // Test Dehacked lists
DEH_Check(); DEH_Check();
// Netgame URL special case: change working dir to EXE folder.
ChangeDirForUrlHandler();
// identify the main IWAD file to use // identify the main IWAD file to use
IdentifyVersion(); IdentifyVersion();
@ -1158,7 +1195,7 @@ void D_SRB2Main(void)
// add any files specified on the command line with -file wadfile // add any files specified on the command line with -file wadfile
// to the wad list // to the wad list
if (!(M_CheckParm("-connect") && !M_CheckParm("-server"))) if (!((M_GetUrlProtocolArg() || M_CheckParm("-connect")) && !M_CheckParm("-server")))
{ {
if (M_CheckParm("-file")) if (M_CheckParm("-file"))
{ {
@ -1190,7 +1227,7 @@ void D_SRB2Main(void)
M_InitMenuPresTables(); M_InitMenuPresTables();
// init title screen display params // init title screen display params
if (M_CheckParm("-connect")) if (M_GetUrlProtocolArg() || M_CheckParm("-connect"))
F_InitMenuPresValues(); F_InitMenuPresValues();
//---------------------------------------------------- READY TIME //---------------------------------------------------- READY TIME

View file

@ -150,6 +150,9 @@ extern char logfilename[1024];
// Otherwise we can't force updates! // Otherwise we can't force updates!
#endif #endif
/* A custom URL protocol for server links. */
#define SERVER_URL_PROTOCOL "srb2://"
// Does this version require an added patch file? // Does this version require an added patch file?
// Comment or uncomment this as necessary. // Comment or uncomment this as necessary.
#define USE_PATCH_DTA #define USE_PATCH_DTA

View file

@ -1423,6 +1423,7 @@ static void SOCK_ClearBans(void)
boolean I_InitTcpNetwork(void) boolean I_InitTcpNetwork(void)
{ {
char serverhostname[255]; char serverhostname[255];
const char *urlparam = NULL;
boolean ret = false; boolean ret = false;
// initilize the OS's TCP/IP stack // initilize the OS's TCP/IP stack
if (!I_InitTcpDriver()) if (!I_InitTcpDriver())
@ -1476,10 +1477,12 @@ boolean I_InitTcpNetwork(void)
ret = true; ret = true;
} }
else if (M_CheckParm("-connect")) else if ((urlparam = M_GetUrlProtocolArg()) != NULL || M_CheckParm("-connect"))
{ {
if (M_IsNextParm()) if (urlparam != NULL)
strcpy(serverhostname, M_GetNextParm()); strlcpy(serverhostname, urlparam, sizeof(serverhostname));
else if (M_IsNextParm())
strlcpy(serverhostname, M_GetNextParm(), sizeof(serverhostname));
else else
serverhostname[0] = 0; // assuming server in the LAN, use broadcast to detect it serverhostname[0] = 0; // assuming server in the LAN, use broadcast to detect it

View file

@ -34,6 +34,25 @@ boolean myargmalloc = false;
*/ */
static INT32 found; 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;
const size_t len = strlen(SERVER_URL_PROTOCOL);
for (i = 1; i < myargc; i++)
{
if (strlen(myargv[i]) > len && !strnicmp(myargv[i], SERVER_URL_PROTOCOL, len))
{
return &myargv[i][len];
}
}
return NULL;
}
/** \brief The M_CheckParm function /** \brief The M_CheckParm function

View file

@ -21,6 +21,9 @@ extern INT32 myargc;
extern char **myargv; extern char **myargv;
extern boolean myargmalloc; 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). // Returns the position of the given parameter in the arg list (0 if not found).
INT32 M_CheckParm(const char *check); INT32 M_CheckParm(const char *check);