From 9657bb35c04ab31a0975d37207c44f433555af0d Mon Sep 17 00:00:00 2001 From: Fafabis Date: Wed, 21 Apr 2021 16:52:44 -0300 Subject: [PATCH 01/16] WIP?: Implement URL protocols. --- src/Makefile | 1 + src/d_main.c | 32 ++++++++ src/d_protocol.c | 209 +++++++++++++++++++++++++++++++++++++++++++++++ src/d_protocol.h | 13 +++ src/i_tcp.c | 24 ++++++ src/m_argv.c | 60 ++++++++++++++ src/m_argv.h | 6 ++ 7 files changed, 345 insertions(+) create mode 100644 src/d_protocol.c create mode 100644 src/d_protocol.h diff --git a/src/Makefile b/src/Makefile index da96f0cf1..f614a084a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -533,6 +533,7 @@ OBJS:=$(i_main_o) \ $(OBJDIR)/comptime.o \ $(OBJDIR)/string.o \ $(OBJDIR)/d_main.o \ + $(OBJDIR)/d_protocol.o \ $(OBJDIR)/d_clisrv.o \ $(OBJDIR)/d_net.o \ $(OBJDIR)/d_netfil.o \ diff --git a/src/d_main.c b/src/d_main.c index 2c02565dc..3dfc1fcbe 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -75,6 +75,7 @@ int snprintf(char *str, size_t n, const char *fmt, ...); #include "fastcmp.h" #include "keys.h" #include "filesrch.h" // refreshdirmenu +#include "d_protocol.h" #ifdef CMAKECONFIG #include "config.h" @@ -1131,6 +1132,8 @@ void D_SRB2Main(void) #endif } + D_ShowProtoWindow(); // ask the user if they want to have cool url protocols + // rand() needs seeded regardless of password srand((unsigned int)time(NULL)); @@ -1504,6 +1507,35 @@ void D_SRB2Main(void) return; } + if (M_CheckProtoParm("replay")) + { + const char *replayurl = M_GetProtoParm(); + char *replayname = strrchr(replayurl, '/'); + + if (!replayurl || replayurl) + I_Error("REPLAY: Invalid URL."); + +#define REPLAYDIR "/DownloadedReplays/" + I_mkdir(va("%s%s", srb2home, REPLAYDIR), 0700); + CONS_Printf("bruh %s", va("%s%s%s", srb2home, REPLAYDIR, replayname)); + + // check if file already exists + if (access(va("%s%s%s", srb2home, REPLAYDIR, replayurl), F_OK )!=0) { + D_DownloadReplay(replayurl, va("%s%s%s", srb2home, REPLAYDIR, replayname)); + } + + // add .lmp to identify the EXTERNAL demo file + // it is NOT possible to play an internal demo using -playdemo, + // rather push a playdemo command.. to do. + + G_DeferedPlayDemo(va("%s%s", REPLAYDIR, replayname)); +#undef REPLAYDIR + + G_SetGamestate(GS_NULL); + wipegamestate = GS_NULL; + return; + } + /*if (M_CheckParm("-ultimatemode")) { autostart = true; diff --git a/src/d_protocol.c b/src/d_protocol.c new file mode 100644 index 000000000..209612960 --- /dev/null +++ b/src/d_protocol.c @@ -0,0 +1,209 @@ +// SONIC ROBO BLAST 2 KART +//----------------------------------------------------------------------------- +// Copyright (C) 2021 by "Fafabis". +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file d_protocol.c +/// \brief srb2kart:// protocol stuff + +#include "doomdef.h" + +#include +#include "m_argv.h" +#include "d_main.h" +#include "d_clisrv.h" +#include "d_protocol.h" + +#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) +#include +#include +#include +#include +#else +#include "SDL.h" +#include +#include +#include +#endif + +// PROTOS +#ifdef __MINGW32__ +static HKEY OpenKey(HKEY hRootKey, const char* strKey); +static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data); +#endif + +static char *exe_name(void) +{ + static char buf[PATH_MAX]; +#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) + static char *p = NULL; + FILE *fp = NULL; + + if(!(fp = fopen("/proc/self/maps", "r"))) + return NULL; + + while(fgets(buf, PATH_MAX, fp)) + fclose(fp); + + *(p = strchr(buf, '\n')) = '\0'; + while(*p != ' ') + p--; + return p+1; +#else + GetModuleFileName(NULL, buf, PATH_MAX); + return buf; + #endif +} + +#ifdef __MINGW32__ +static HKEY OpenKey(HKEY hRootKey, const char* strKey) +{ + HKEY hKey; + LONG nError = RegOpenKeyEx(hRootKey, strKey, 0, KEY_ALL_ACCESS, &hKey); + + if (nError==ERROR_FILE_NOT_FOUND) + { + CONS_Printf("PROTOCOL: Creating registry key: %s\n", strKey); + nError = RegCreateKeyEx(hRootKey, strKey, 0, NULL, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL); + } + + if (nError) + I_Error("PROTOCOL: Could not create Registry Key.\nMaybe you forgot to run as an administrator?"); + + return hKey; +} + +static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data) +{ + I_Assert(hRegistryKey != NULL); + + return (RegSetValueExA(hRegistryKey, + valueName, + 0, + REG_SZ, + (LPBYTE)data, + strlen(data) + 1) == ERROR_SUCCESS); +} +#endif + +void D_ShowProtoWindow(void) { + char buffer[255]; + const char *exe_path = exe_name(); + FILE *fp = NULL; +#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) + FILE *desktopfile; + FILE *mimefile; + struct passwd *pw = getpwuid(getuid()); + const char *homedir = pw->pw_dir; + boolean alreadyexists = false; +#endif + if (dedicated) + return; + + fp = fopen(va("%s/protocol.txt", srb2home), "a+"); + + while (fgets(buffer, 255, fp)) + if (strcmp(buffer, "no") == 0) + return; + + if (strcmp(buffer, "yes") != 0) { + const SDL_MessageBoxButtonData buttons[] = { + { /* .flags, .buttonid, .text */ 0, 0, "Yes" }, + { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" }, + { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" }, + }; + const SDL_MessageBoxData messageboxdata = { + SDL_MESSAGEBOX_INFORMATION, + NULL, + "Register SRB2Kart Protocols", + "Would you like to register the srb2kart:// protocol?\n" + "This will allow you to connect to servers and load replays directly from the browser\n\n" +#if defined (__MINGW32__) + "This will require administrator permissions.\n" + "(if you aren't running the game as an administrator click in \"Cancel\" and run the game as administrator)." +#endif + "", + SDL_arraysize(buttons), + buttons, + NULL + }; + + int buttonid; + if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) { + I_Error("Error displaying message box."); + } + if (buttonid == 0) { +#if defined (__MINGW32__) + HKEY hKey = OpenKey(HKEY_CLASSES_ROOT,"srb2kart"); + SetStringValue(hKey, "URL Protocol", ""); + RegCloseKey(hKey); + hKey = OpenKey(HKEY_CLASSES_ROOT,"srb2kart\\shell\\open\\command"); + SetStringValue(hKey, "", va("\"%s\" \"%%1\"", exe_path)); + RegCloseKey(hKey); +#elif (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) + + desktopfile = fopen(va("%s/.local/share/applications/srb2kart.desktop", homedir), "w"); + + fprintf(desktopfile, + "[Desktop Entry]\n" + "Type=Application\n" + "Name=SRB2Kart Scheme Handler\n" + "Exec=bash -c '%s %%u'\n" + "StartupNotify=false\n" + "Terminal=false\n" + "MimeType=x-scheme-handler/srb2kart;\n" + , exe_path); + + fclose(desktopfile); + + mimefile = fopen(va("%s/.local/share/applications/mimeinfo.cache", homedir), "a+"); + + while (fgets(buffer, 255, mimefile)) + if (strncmp(buffer, "x-scheme-handler/srb2kart=srb2kart.desktop;", 43)==0) + { + alreadyexists = true; + break; + } + + if (!alreadyexists) + fprintf(mimefile, "x-scheme-handler/srb2kart=srb2kart.desktop;\n"); + +#endif + fprintf(fp, "yes"); + } + else + fprintf(fp, "no"); + } + fclose(fp); +} + +static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { + size_t written = fwrite(ptr, size, nmemb, stream); + return written; +} + +void D_DownloadReplay(const char *url, const char *path) { + FILE *fp = NULL; + CURL *curl; + CURLcode cc; + curl = curl_easy_init(); + + if (!curl) { + I_Error("REPLAY: Error initializing CURL."); + + } + fp = fopen(path, "wb"); + CONS_Printf("REPLAY: URL: %s\n", url); + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + cc = curl_easy_perform(curl); + if (cc != CURLE_OK) + I_Error("REPLAY: URL gave response code %u.", cc); + + curl_easy_cleanup(curl); + fclose(fp); +} diff --git a/src/d_protocol.h b/src/d_protocol.h new file mode 100644 index 000000000..085516637 --- /dev/null +++ b/src/d_protocol.h @@ -0,0 +1,13 @@ +// SONIC ROBO BLAST 2 KART +//----------------------------------------------------------------------------- +// Copyright (C) 2021 by "Fafabis". +// +// This program is free software distributed under the +// terms of the GNU General Public License, version 2. +// See the 'LICENSE' file for more details. +//----------------------------------------------------------------------------- +/// \file d_protocol.h +/// \brief srb2kart:// protocol stuff + +void D_ShowProtoWindow(void); +void D_DownloadReplay(const char *url, const char *path); diff --git a/src/i_tcp.c b/src/i_tcp.c index ba973494f..92eb44527 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -1564,6 +1564,30 @@ boolean I_InitTcpNetwork(void) hardware_MAXPACKETLENGTH = MAXPACKETLENGTH; } } + if (M_CheckProtoParm("ip")) + { + strcpy(serverhostname, M_GetProtoParm()); + + // server address only in ip + if (serverhostname[0]) + { + CONS_Printf("SERVERNAME: %s\n", serverhostname); + COM_BufAddText("connect \""); + COM_BufAddText(serverhostname); + COM_BufAddText("\"\n"); + + // probably modem + hardware_MAXPACKETLENGTH = INETPACKETLENGTH; + } + else + { + // so we're on a LAN + COM_BufAddText("connect any\n"); + + net_bandwidth = 800000; + hardware_MAXPACKETLENGTH = MAXPACKETLENGTH; + } + } I_NetOpenSocket = SOCK_OpenSocket; I_Ban = SOCK_Ban; diff --git a/src/m_argv.c b/src/m_argv.c index e1046f8a7..be951f9e7 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -59,6 +59,66 @@ INT32 M_CheckParm(const char *check) return 0; } +// Check params for srb2kart:// protocol +INT32 M_CheckProtoParm(const char *check) +{ + INT32 i; + + for (i = 1; i < myargc; i++) + { + if (strncmp(myargv[i], "srb2kart:/", 10) == 0) + { + UINT8 skip = 10; + if (myargv[i][10] == '/') + skip++; + if (strncmp(myargv[i]+skip, check, strlen(check))==0) + { + found = i; + return i; + } + } + } + found = 0; + return 0; +} + +// Gets the string after srb2kart:// +const char *M_GetProtoParm(void) +{ + INT32 i; + char buffer[255]; + static char string[255]; + char *token = NULL; + + for (i = 1; i < myargc; i++) + { + if (strncmp(myargv[i], "srb2kart:/", 10) == 0) + { + INT32 len = 0; + strcpy(buffer, myargv[i]); + token = strtok(buffer, "/"); + for (i = 0; token; i++) + { + if (i == 2) { + strcpy(string, token); + strcat(string, "/"); + } + else if (i > 2) { + strcat(string, token); + strcat(string, "/"); + } + + token = strtok(NULL, "/"); + } + len = strlen(string); + if (string[len-1] == '/') + string[len-1] = '\0'; + return string; + } + } + return NULL; +} + /** \brief the M_IsNextParm \return true if there is an available next parameter diff --git a/src/m_argv.h b/src/m_argv.h index d654dcc19..97740d085 100644 --- a/src/m_argv.h +++ b/src/m_argv.h @@ -24,6 +24,12 @@ extern boolean myargmalloc; // Returns the position of the given parameter in the arg list (0 if not found). INT32 M_CheckParm(const char *check); +// Check params for srb2kart:// protocol +INT32 M_CheckProtoParm(const char *check); + +// Gets the string after srb2kart:// +const char *M_GetProtoParm(void); + // Pushes all parameters beginning with a +, ex: +map map01 void M_PushSpecialParameters(void); From 139c548a7db93e32fafdbc8fb9736a3eb20b902d Mon Sep 17 00:00:00 2001 From: Fafabis Date: Wed, 21 Apr 2021 18:20:43 -0300 Subject: [PATCH 02/16] Fixes --- src/d_main.c | 2 +- src/d_protocol.c | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 3dfc1fcbe..6848a9d01 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1512,7 +1512,7 @@ void D_SRB2Main(void) const char *replayurl = M_GetProtoParm(); char *replayname = strrchr(replayurl, '/'); - if (!replayurl || replayurl) + if (!replayurl || !replayurl) I_Error("REPLAY: Invalid URL."); #define REPLAYDIR "/DownloadedReplays/" diff --git a/src/d_protocol.c b/src/d_protocol.c index 209612960..63dbbcace 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -17,7 +17,7 @@ #include "d_clisrv.h" #include "d_protocol.h" -#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) +#if defined (__unix__) || defined (UNIXCOMMON) #include #include #include @@ -30,7 +30,7 @@ #endif // PROTOS -#ifdef __MINGW32__ +#ifdef __WIN32 static HKEY OpenKey(HKEY hRootKey, const char* strKey); static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data); #endif @@ -38,7 +38,7 @@ static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const ch static char *exe_name(void) { static char buf[PATH_MAX]; -#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) +#if defined (__unix__) || defined (UNIXCOMMON) static char *p = NULL; FILE *fp = NULL; @@ -52,13 +52,13 @@ static char *exe_name(void) while(*p != ' ') p--; return p+1; -#else +#elif defined (__WIN32) GetModuleFileName(NULL, buf, PATH_MAX); return buf; #endif } -#ifdef __MINGW32__ +#ifdef __WIN32 static HKEY OpenKey(HKEY hRootKey, const char* strKey) { HKEY hKey; @@ -93,7 +93,7 @@ void D_ShowProtoWindow(void) { char buffer[255]; const char *exe_path = exe_name(); FILE *fp = NULL; -#if (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) +#if defined (__unix__) || defined (UNIXCOMMON) FILE *desktopfile; FILE *mimefile; struct passwd *pw = getpwuid(getuid()); @@ -121,7 +121,7 @@ void D_ShowProtoWindow(void) { "Register SRB2Kart Protocols", "Would you like to register the srb2kart:// protocol?\n" "This will allow you to connect to servers and load replays directly from the browser\n\n" -#if defined (__MINGW32__) +#if defined (__WIN32) "This will require administrator permissions.\n" "(if you aren't running the game as an administrator click in \"Cancel\" and run the game as administrator)." #endif @@ -136,14 +136,14 @@ void D_ShowProtoWindow(void) { I_Error("Error displaying message box."); } if (buttonid == 0) { -#if defined (__MINGW32__) +#if defined (__WIN32) HKEY hKey = OpenKey(HKEY_CLASSES_ROOT,"srb2kart"); SetStringValue(hKey, "URL Protocol", ""); RegCloseKey(hKey); hKey = OpenKey(HKEY_CLASSES_ROOT,"srb2kart\\shell\\open\\command"); SetStringValue(hKey, "", va("\"%s\" \"%%1\"", exe_path)); RegCloseKey(hKey); -#elif (defined (__unix__) && !defined (MSDOS)) || defined (UNIXCOMMON) +#elif defined (__unix__) || defined (UNIXCOMMON) desktopfile = fopen(va("%s/.local/share/applications/srb2kart.desktop", homedir), "w"); From 6c1a42f05fc931d83062212b44e74a5351a4e8bc Mon Sep 17 00:00:00 2001 From: Fafabis Date: Thu, 22 Apr 2021 11:15:29 -0300 Subject: [PATCH 03/16] Adjust coding style and "optimize" M_GetProtoParm --- src/d_main.c | 7 +++--- src/d_netfil.h | 1 + src/d_protocol.c | 61 +++++++++++++++++++++++++++++++----------------- src/d_protocol.h | 4 +++- src/m_argv.c | 35 ++++++--------------------- 5 files changed, 54 insertions(+), 54 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 6848a9d01..c3ca15de0 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1132,7 +1132,7 @@ void D_SRB2Main(void) #endif } - D_ShowProtoWindow(); // ask the user if they want to have cool url protocols + D_SetupProtocol(); // ask the user if they want to have cool url protocols // rand() needs seeded regardless of password srand((unsigned int)time(NULL)); @@ -1507,6 +1507,7 @@ void D_SRB2Main(void) return; } +#ifdef HAVE_CURL if (M_CheckProtoParm("replay")) { const char *replayurl = M_GetProtoParm(); @@ -1520,9 +1521,8 @@ void D_SRB2Main(void) CONS_Printf("bruh %s", va("%s%s%s", srb2home, REPLAYDIR, replayname)); // check if file already exists - if (access(va("%s%s%s", srb2home, REPLAYDIR, replayurl), F_OK )!=0) { + if (access(va("%s%s%s", srb2home, REPLAYDIR, replayurl), F_OK )!=0) D_DownloadReplay(replayurl, va("%s%s%s", srb2home, REPLAYDIR, replayname)); - } // add .lmp to identify the EXTERNAL demo file // it is NOT possible to play an internal demo using -playdemo, @@ -1535,6 +1535,7 @@ void D_SRB2Main(void) wipegamestate = GS_NULL; return; } +#endif /*if (M_CheckParm("-ultimatemode")) { diff --git a/src/d_netfil.h b/src/d_netfil.h index 1ab50398a..bcf1f6670 100644 --- a/src/d_netfil.h +++ b/src/d_netfil.h @@ -109,6 +109,7 @@ size_t nameonlylength(const char *s); void CURLPrepareFile(const char* url, int dfilenum); void CURLGetFile(void); HTTP_login * CURLGetLogin (const char *url, HTTP_login ***return_prev_next); +size_t curlwrite_data(void *ptr, size_t size, size_t nmemb, FILE *stream); #endif #endif // __D_NETFIL__ diff --git a/src/d_protocol.c b/src/d_protocol.c index 63dbbcace..d674bb945 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -11,10 +11,10 @@ #include "doomdef.h" -#include #include "m_argv.h" #include "d_main.h" #include "d_clisrv.h" +#include "d_netfil.h" #include "d_protocol.h" #if defined (__unix__) || defined (UNIXCOMMON) @@ -26,11 +26,16 @@ #include "SDL.h" #include #include +#endif + #include + +#ifdef HAVE_CURL +#include #endif // PROTOS -#ifdef __WIN32 +#if defined (__WIN32) static HKEY OpenKey(HKEY hRootKey, const char* strKey); static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data); #endif @@ -58,7 +63,7 @@ static char *exe_name(void) #endif } -#ifdef __WIN32 +#if defined (__WIN32) static HKEY OpenKey(HKEY hRootKey, const char* strKey) { HKEY hKey; @@ -89,7 +94,8 @@ static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const ch } #endif -void D_ShowProtoWindow(void) { +void D_SetupProtocol(void) +{ char buffer[255]; const char *exe_path = exe_name(); FILE *fp = NULL; @@ -98,23 +104,31 @@ void D_ShowProtoWindow(void) { FILE *mimefile; struct passwd *pw = getpwuid(getuid()); const char *homedir = pw->pw_dir; - boolean alreadyexists = false; #endif if (dedicated) return; fp = fopen(va("%s/protocol.txt", srb2home), "a+"); - while (fgets(buffer, 255, fp)) if (strcmp(buffer, "no") == 0) return; - if (strcmp(buffer, "yes") != 0) { + if (strcmp(buffer, "yes") != 0) + { +#if defined (__unix__) || defined (UNIXCOMMON) const SDL_MessageBoxButtonData buttons[] = { { /* .flags, .buttonid, .text */ 0, 0, "Yes" }, { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" }, { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" }, }; +#elif defined (__WIN32) + // Reversed on windows + const SDL_MessageBoxButtonData buttons[] = { + { /* .flags, .buttonid, .text */ 0, 2, "Yes" }, + { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" }, + { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "Cancel" }, + }; +#endif const SDL_MessageBoxData messageboxdata = { SDL_MESSAGEBOX_INFORMATION, NULL, @@ -132,10 +146,12 @@ void D_ShowProtoWindow(void) { }; int buttonid; - if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) { + if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) + { I_Error("Error displaying message box."); } - if (buttonid == 0) { + if (buttonid == 0) + { #if defined (__WIN32) HKEY hKey = OpenKey(HKEY_CLASSES_ROOT,"srb2kart"); SetStringValue(hKey, "URL Protocol", ""); @@ -144,8 +160,11 @@ void D_ShowProtoWindow(void) { SetStringValue(hKey, "", va("\"%s\" \"%%1\"", exe_path)); RegCloseKey(hKey); #elif defined (__unix__) || defined (UNIXCOMMON) + boolean alreadyexists = false; desktopfile = fopen(va("%s/.local/share/applications/srb2kart.desktop", homedir), "w"); + if (!desktopfile) + I_Error("PROTOCOL: Error creating .desktop file."); fprintf(desktopfile, "[Desktop Entry]\n" @@ -154,12 +173,13 @@ void D_ShowProtoWindow(void) { "Exec=bash -c '%s %%u'\n" "StartupNotify=false\n" "Terminal=false\n" - "MimeType=x-scheme-handler/srb2kart;\n" - , exe_path); - + "MimeType=x-scheme-handler/srb2kart;\n", + exe_path); fclose(desktopfile); mimefile = fopen(va("%s/.local/share/applications/mimeinfo.cache", homedir), "a+"); + if (!mimefile) + I_Error("PROTOCOL: Error opening mime file."); while (fgets(buffer, 255, mimefile)) if (strncmp(buffer, "x-scheme-handler/srb2kart=srb2kart.desktop;", 43)==0) @@ -180,26 +200,22 @@ void D_ShowProtoWindow(void) { fclose(fp); } -static size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) { - size_t written = fwrite(ptr, size, nmemb, stream); - return written; -} - -void D_DownloadReplay(const char *url, const char *path) { +#ifdef HAVE_CURL +void D_DownloadReplay(const char *url, const char *path) +{ FILE *fp = NULL; CURL *curl; CURLcode cc; curl = curl_easy_init(); - - if (!curl) { + if (!curl) I_Error("REPLAY: Error initializing CURL."); - } fp = fopen(path, "wb"); CONS_Printf("REPLAY: URL: %s\n", url); curl_easy_setopt(curl, CURLOPT_URL, url); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curlwrite_data); curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + cc = curl_easy_perform(curl); if (cc != CURLE_OK) I_Error("REPLAY: URL gave response code %u.", cc); @@ -207,3 +223,4 @@ void D_DownloadReplay(const char *url, const char *path) { curl_easy_cleanup(curl); fclose(fp); } +#endif diff --git a/src/d_protocol.h b/src/d_protocol.h index 085516637..382368299 100644 --- a/src/d_protocol.h +++ b/src/d_protocol.h @@ -9,5 +9,7 @@ /// \file d_protocol.h /// \brief srb2kart:// protocol stuff -void D_ShowProtoWindow(void); +void D_SetupProtocol(void); +#ifdef HAVE_CURL void D_DownloadReplay(const char *url, const char *path); +#endif diff --git a/src/m_argv.c b/src/m_argv.c index be951f9e7..e3d37c464 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -66,12 +66,9 @@ INT32 M_CheckProtoParm(const char *check) for (i = 1; i < myargc; i++) { - if (strncmp(myargv[i], "srb2kart:/", 10) == 0) + if (strncmp(myargv[i], "srb2kart://", 11) == 0) { - UINT8 skip = 10; - if (myargv[i][10] == '/') - skip++; - if (strncmp(myargv[i]+skip, check, strlen(check))==0) + if (strncmp(myargv[i]+11, check, strlen(check))==0) { found = i; return i; @@ -86,33 +83,15 @@ INT32 M_CheckProtoParm(const char *check) const char *M_GetProtoParm(void) { INT32 i; - char buffer[255]; - static char string[255]; - char *token = NULL; + static char *string; for (i = 1; i < myargc; i++) { - if (strncmp(myargv[i], "srb2kart:/", 10) == 0) + if (strncmp(myargv[i], "srb2kart://", 11) == 0) { - INT32 len = 0; - strcpy(buffer, myargv[i]); - token = strtok(buffer, "/"); - for (i = 0; token; i++) - { - if (i == 2) { - strcpy(string, token); - strcat(string, "/"); - } - else if (i > 2) { - strcat(string, token); - strcat(string, "/"); - } - - token = strtok(NULL, "/"); - } - len = strlen(string); - if (string[len-1] == '/') - string[len-1] = '\0'; + string = strchr(myargv[i], '/'); + string = strchr(string+1, '/'); + string = strchr(string+1, '/')+1; return string; } } From 76f11e69d6a96a0ce989211bc82f190facdd3905 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Sun, 2 May 2021 13:08:46 -0300 Subject: [PATCH 04/16] Reverse --- src/d_protocol.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index d674bb945..179f08d35 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -124,9 +124,9 @@ void D_SetupProtocol(void) #elif defined (__WIN32) // Reversed on windows const SDL_MessageBoxButtonData buttons[] = { - { /* .flags, .buttonid, .text */ 0, 2, "Yes" }, + { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" }, { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" }, - { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 0, "Cancel" }, + { /* .flags, .buttonid, .text */ 0, 0, "Yes" }, }; #endif const SDL_MessageBoxData messageboxdata = { From 381d48c6c9620ed8add4a52eed2bbe325af40082 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Tue, 4 May 2021 12:27:53 -0300 Subject: [PATCH 05/16] No admin Do not require administrator permissions on windows and automatically re-register if path has changed --- src/d_protocol.c | 187 +++++++++++++++++++++++++---------------------- src/m_argv.c | 10 ++- 2 files changed, 106 insertions(+), 91 deletions(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index 179f08d35..ee9e91021 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -9,6 +9,20 @@ /// \file d_protocol.c /// \brief srb2kart:// protocol stuff +#include +#include +#include +#if defined (__unix__) || defined (UNIXCOMMON) +#include +#include +#elif defined (__WIN32) +#include +#endif + +#ifdef HAVE_CURL +#include +#endif + #include "doomdef.h" #include "m_argv.h" @@ -16,45 +30,31 @@ #include "d_clisrv.h" #include "d_netfil.h" #include "d_protocol.h" +#include "i_system.h" -#if defined (__unix__) || defined (UNIXCOMMON) -#include -#include -#include -#include -#else #include "SDL.h" -#include -#include -#endif - -#include - -#ifdef HAVE_CURL -#include -#endif // PROTOS #if defined (__WIN32) static HKEY OpenKey(HKEY hRootKey, const char* strKey); -static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data); +static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data); #endif -static char *exe_name(void) +static char *GetExePath(void) { static char buf[PATH_MAX]; #if defined (__unix__) || defined (UNIXCOMMON) static char *p = NULL; FILE *fp = NULL; - if(!(fp = fopen("/proc/self/maps", "r"))) + if (!(fp = fopen("/proc/self/maps", "r"))) return NULL; - while(fgets(buf, PATH_MAX, fp)) + while (fgets(buf, PATH_MAX, fp)) fclose(fp); *(p = strchr(buf, '\n')) = '\0'; - while(*p != ' ') + while (*p != ' ') p--; return p+1; #elif defined (__WIN32) @@ -63,6 +63,55 @@ static char *exe_name(void) #endif } +static void RegisterProtocols(const char *path) +{ +#if defined (__WIN32) + HKEY hKey = OpenKey(HKEY_CURRENT_USER,"Software\\Classes\\srb2kart"); + SetStringValue(hKey, "URL Protocol", ""); + RegCloseKey(hKey); + hKey = OpenKey(HKEY_CURRENT_USER,"Software\\Classes\\srb2kart\\shell\\open\\command"); + SetStringValue(hKey, "", va("\"%s\" \"%%1\"", path)); + RegCloseKey(hKey); +#elif defined (__unix__) || defined (UNIXCOMMON) + FILE *desktopfile = NULL; + FILE *mimefile = NULL; + struct passwd *pw = getpwuid(getuid()); + const char *homedir = pw->pw_dir; + char buffer[255]; + boolean alreadyexists = false; + + desktopfile = fopen(va("%s/.local/share/applications/srb2kart-handler.desktop", homedir), "w"); + if (!desktopfile) + I_Error("PROTOCOL: Error creating .desktop file."); + + fprintf(desktopfile, + "[Desktop Entry]\n" + "Type=Application\n" + "Name=SRB2Kart Scheme Handler\n" + "Exec=bash -c '%s %%u'\n" + "StartupNotify=false\n" + "MimeType=x-scheme-handler/srb2kart;\n", + path); + + fclose(desktopfile); + + mimefile = fopen(va("%s/.local/share/applications/mimeinfo.cache", homedir), "a+"); + if (!mimefile) + I_Error("PROTOCOL: Error opening mime file."); + + while (fgets(buffer, 255, mimefile)) + if (strncmp(buffer, "x-scheme-handler/srb2kart=srb2kart-handler.desktop;", 43)==0) + { + alreadyexists = true; + break; + } + + if (!alreadyexists) + fprintf(mimefile, "x-scheme-handler/srb2kart=srb2kart-handler.desktop;\n"); + +#endif +} + #if defined (__WIN32) static HKEY OpenKey(HKEY hRootKey, const char* strKey) { @@ -76,7 +125,7 @@ static HKEY OpenKey(HKEY hRootKey, const char* strKey) } if (nError) - I_Error("PROTOCOL: Could not create Registry Key.\nMaybe you forgot to run as an administrator?"); + I_Error("PROTOCOL: Could not create Registry Key."); return hKey; } @@ -96,49 +145,47 @@ static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const ch void D_SetupProtocol(void) { - char buffer[255]; - const char *exe_path = exe_name(); FILE *fp = NULL; -#if defined (__unix__) || defined (UNIXCOMMON) - FILE *desktopfile; - FILE *mimefile; - struct passwd *pw = getpwuid(getuid()); - const char *homedir = pw->pw_dir; -#endif + char *result = NULL; + char buffer[PATH_MAX]; + const char *exe_path = GetExePath(); + const char *protocolfile = va("%s/protocol.txt", srb2home); + if (dedicated) return; - fp = fopen(va("%s/protocol.txt", srb2home), "a+"); - while (fgets(buffer, 255, fp)) + fp = fopen(protocolfile, "a+"); + result = fgets(buffer, PATH_MAX, fp); + if (result) + { if (strcmp(buffer, "no") == 0) return; - - if (strcmp(buffer, "yes") != 0) + else if (strcmp(buffer, exe_path) != 0) + { + // overwrite + fp = fopen(protocolfile, "w"); + RegisterProtocols(exe_path); + fprintf(fp, "%s", exe_path); + return; + } + } + else // probably first time { + const SDL_MessageBoxButtonData buttons[] = { #if defined (__unix__) || defined (UNIXCOMMON) - const SDL_MessageBoxButtonData buttons[] = { { /* .flags, .buttonid, .text */ 0, 0, "Yes" }, - { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" }, - { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" }, - }; + { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" } #elif defined (__WIN32) - // Reversed on windows - const SDL_MessageBoxButtonData buttons[] = { - { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 2, "Cancel" }, { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" }, - { /* .flags, .buttonid, .text */ 0, 0, "Yes" }, - }; + { /* .flags, .buttonid, .text */ 0, 0, "Yes" } #endif + }; const SDL_MessageBoxData messageboxdata = { SDL_MESSAGEBOX_INFORMATION, NULL, "Register SRB2Kart Protocols", "Would you like to register the srb2kart:// protocol?\n" - "This will allow you to connect to servers and load replays directly from the browser\n\n" -#if defined (__WIN32) - "This will require administrator permissions.\n" - "(if you aren't running the game as an administrator click in \"Cancel\" and run the game as administrator)." -#endif + "This will allow you to connect to servers and load replays directly from the browser" "", SDL_arraysize(buttons), buttons, @@ -150,51 +197,13 @@ void D_SetupProtocol(void) { I_Error("Error displaying message box."); } + if (buttonid == 0) { -#if defined (__WIN32) - HKEY hKey = OpenKey(HKEY_CLASSES_ROOT,"srb2kart"); - SetStringValue(hKey, "URL Protocol", ""); - RegCloseKey(hKey); - hKey = OpenKey(HKEY_CLASSES_ROOT,"srb2kart\\shell\\open\\command"); - SetStringValue(hKey, "", va("\"%s\" \"%%1\"", exe_path)); - RegCloseKey(hKey); -#elif defined (__unix__) || defined (UNIXCOMMON) - boolean alreadyexists = false; - - desktopfile = fopen(va("%s/.local/share/applications/srb2kart.desktop", homedir), "w"); - if (!desktopfile) - I_Error("PROTOCOL: Error creating .desktop file."); - - fprintf(desktopfile, - "[Desktop Entry]\n" - "Type=Application\n" - "Name=SRB2Kart Scheme Handler\n" - "Exec=bash -c '%s %%u'\n" - "StartupNotify=false\n" - "Terminal=false\n" - "MimeType=x-scheme-handler/srb2kart;\n", - exe_path); - fclose(desktopfile); - - mimefile = fopen(va("%s/.local/share/applications/mimeinfo.cache", homedir), "a+"); - if (!mimefile) - I_Error("PROTOCOL: Error opening mime file."); - - while (fgets(buffer, 255, mimefile)) - if (strncmp(buffer, "x-scheme-handler/srb2kart=srb2kart.desktop;", 43)==0) - { - alreadyexists = true; - break; - } - - if (!alreadyexists) - fprintf(mimefile, "x-scheme-handler/srb2kart=srb2kart.desktop;\n"); - -#endif - fprintf(fp, "yes"); + RegisterProtocols(exe_path); + fprintf(fp, "%s", exe_path); } - else + else fprintf(fp, "no"); } fclose(fp); diff --git a/src/m_argv.c b/src/m_argv.c index e3d37c464..b42ddc7b1 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -59,7 +59,10 @@ INT32 M_CheckParm(const char *check) return 0; } -// Check params for srb2kart:// protocol +/** \brief Check params for URL protocol + + \return number (1 to argc-1) or 0 if not present +*/ INT32 M_CheckProtoParm(const char *check) { INT32 i; @@ -79,7 +82,10 @@ INT32 M_CheckProtoParm(const char *check) return 0; } -// Gets the string after srb2kart:// +/** \brief Gets the string after URL protocol + * + \return string after protocol parameter or NULL if not found +*/ const char *M_GetProtoParm(void) { INT32 i; From 14c17c90cd10523290f17a3f98801d47cb308ca5 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Tue, 4 May 2021 17:23:38 -0300 Subject: [PATCH 06/16] Better method of getting exe paths in Linux and FreeBSD --- src/d_protocol.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index ee9e91021..f9330aabe 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -43,24 +43,16 @@ static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const ch static char *GetExePath(void) { static char buf[PATH_MAX]; -#if defined (__unix__) || defined (UNIXCOMMON) - static char *p = NULL; - FILE *fp = NULL; - - if (!(fp = fopen("/proc/self/maps", "r"))) - return NULL; - - while (fgets(buf, PATH_MAX, fp)) - fclose(fp); - - *(p = strchr(buf, '\n')) = '\0'; - while (*p != ' ') - p--; - return p+1; +#if defined (__linux__) + if (readlink("/proc/self/exe", buf, PATH_MAX) < 0) + I_Error("PROTOCOL: Error reading /proc/"); +#elif defined (__FreeBSD__) + if (readlink("/proc/curproc/file", buf, PATH_MAX) < 0) + I_Error("PROTOCOL: Error reading /proc/"); #elif defined (__WIN32) GetModuleFileName(NULL, buf, PATH_MAX); +#endif return buf; - #endif } static void RegisterProtocols(const char *path) From bff1f574235e4b4f1e74acbc2bec6eac0f162408 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Tue, 4 May 2021 17:24:25 -0300 Subject: [PATCH 07/16] Fix compiler warning when compiling on windows --- src/d_protocol.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index f9330aabe..c439dd38c 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -130,7 +130,7 @@ static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const ch valueName, 0, REG_SZ, - (LPBYTE)data, + (const BYTE *)data, strlen(data) + 1) == ERROR_SUCCESS); } #endif From 994dfa91f2a90ac807012f91334376f140f1c489 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Wed, 5 May 2021 23:41:49 -0300 Subject: [PATCH 08/16] *ProtoParm -> *ProtoParam --- src/d_main.c | 4 ++-- src/i_tcp.c | 4 ++-- src/m_argv.c | 4 ++-- src/m_argv.h | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index c3ca15de0..1077b556a 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1508,9 +1508,9 @@ void D_SRB2Main(void) } #ifdef HAVE_CURL - if (M_CheckProtoParm("replay")) + if (M_CheckProtoParam("replay")) { - const char *replayurl = M_GetProtoParm(); + const char *replayurl = M_GetProtoParam(); char *replayname = strrchr(replayurl, '/'); if (!replayurl || !replayurl) diff --git a/src/i_tcp.c b/src/i_tcp.c index 92eb44527..cd2560ad6 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -1564,9 +1564,9 @@ boolean I_InitTcpNetwork(void) hardware_MAXPACKETLENGTH = MAXPACKETLENGTH; } } - if (M_CheckProtoParm("ip")) + if (M_CheckProtoParam("ip")) { - strcpy(serverhostname, M_GetProtoParm()); + strcpy(serverhostname, M_GetProtoParam()); // server address only in ip if (serverhostname[0]) diff --git a/src/m_argv.c b/src/m_argv.c index b42ddc7b1..723c4c203 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -63,7 +63,7 @@ INT32 M_CheckParm(const char *check) \return number (1 to argc-1) or 0 if not present */ -INT32 M_CheckProtoParm(const char *check) +INT32 M_CheckProtoParam(const char *check) { INT32 i; @@ -86,7 +86,7 @@ INT32 M_CheckProtoParm(const char *check) * \return string after protocol parameter or NULL if not found */ -const char *M_GetProtoParm(void) +const char *M_GetProtoParam(void) { INT32 i; static char *string; diff --git a/src/m_argv.h b/src/m_argv.h index 97740d085..f31a531d0 100644 --- a/src/m_argv.h +++ b/src/m_argv.h @@ -25,10 +25,10 @@ extern boolean myargmalloc; INT32 M_CheckParm(const char *check); // Check params for srb2kart:// protocol -INT32 M_CheckProtoParm(const char *check); +INT32 M_CheckProtoParam(const char *check); // Gets the string after srb2kart:// -const char *M_GetProtoParm(void); +const char *M_GetProtoParam(void); // Pushes all parameters beginning with a +, ex: +map map01 void M_PushSpecialParameters(void); From 23632606b372246b41634cf86ca1d97af6459c26 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Wed, 5 May 2021 23:43:30 -0300 Subject: [PATCH 09/16] bruh --- src/d_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d_main.c b/src/d_main.c index 1077b556a..bdd0ee865 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1513,7 +1513,7 @@ void D_SRB2Main(void) const char *replayurl = M_GetProtoParam(); char *replayname = strrchr(replayurl, '/'); - if (!replayurl || !replayurl) + if (!replayname || !replayurl) I_Error("REPLAY: Invalid URL."); #define REPLAYDIR "/DownloadedReplays/" From cbea79960b71cf5397493912ce8af7335082e49e Mon Sep 17 00:00:00 2001 From: Fafabis Date: Wed, 5 May 2021 23:52:04 -0300 Subject: [PATCH 10/16] Normal mkdir perms --- src/d_main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/d_main.c b/src/d_main.c index bdd0ee865..24e43a321 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1517,7 +1517,7 @@ void D_SRB2Main(void) I_Error("REPLAY: Invalid URL."); #define REPLAYDIR "/DownloadedReplays/" - I_mkdir(va("%s%s", srb2home, REPLAYDIR), 0700); + I_mkdir(va("%s%s", srb2home, REPLAYDIR), 0755); CONS_Printf("bruh %s", va("%s%s%s", srb2home, REPLAYDIR, replayname)); // check if file already exists From 74938281edf8c67d611e65a873b34c399925491d Mon Sep 17 00:00:00 2001 From: Fafabis Date: Thu, 6 May 2021 10:50:51 -0300 Subject: [PATCH 11/16] Protocol user interface "refactor" No longer ask to register protocols in startup, instead, automatically register protocols and give the user a option to disable it in the menu. Detect if ".local/user/applications" exists and if mime database tools are present on unix platforms to create it. --- src/d_protocol.c | 113 +++++++++++++++++++++++++++++++---------------- src/d_protocol.h | 3 ++ src/m_menu.c | 34 ++++++++++++-- 3 files changed, 110 insertions(+), 40 deletions(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index c439dd38c..1d9269d5d 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -55,7 +55,36 @@ static char *GetExePath(void) return buf; } -static void RegisterProtocols(const char *path) +static void DeleteProtocols(void) +{ +#if defined (__WIN32) + + HKEY hKey; + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open\\command"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open\\command")); + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open")); + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell")); + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("URL Protocol")); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart")); +#elif defined (__unix__) || defined (UNIXCOMMON) + struct passwd *pw = getpwuid(getuid()); + const char *homedir = pw->pw_dir; + remove(va("%s/.local/share/applications/srb2kart-handler.desktop", homedir)); +#endif +} + +static boolean RegisterProtocols(const char *path) { #if defined (__WIN32) HKEY hKey = OpenKey(HKEY_CURRENT_USER,"Software\\Classes\\srb2kart"); @@ -71,10 +100,30 @@ static void RegisterProtocols(const char *path) const char *homedir = pw->pw_dir; char buffer[255]; boolean alreadyexists = false; + struct stat sb; + char *applicationsfolder = va("%s/.local/share/applications/", homedir); + + if (system("which update-desktop-database > /dev/null 2>&1")) + { + // command not found, probably doesn't have freedesktop, so let's ignore + CONS_Alert(CONS_ERROR, "Unable to register protocols. Your system doesn't seem to have freedesktop.\n"); + return false; + } + + if (stat(applicationsfolder, &sb) == -1) + { + // location doesn't exist, so let's actually create it + I_mkdir(applicationsfolder, 0755); + if (system(va("update-desktop-database %s", applicationsfolder)) != 1) + { + CONS_Alert(CONS_ERROR, "Unable to register protocols. Could not run call to run update-desktop-database.\n"); + return false; + } + } desktopfile = fopen(va("%s/.local/share/applications/srb2kart-handler.desktop", homedir), "w"); if (!desktopfile) - I_Error("PROTOCOL: Error creating .desktop file."); + I_Error("Unable to register protocols. Error creating .desktop file."); fprintf(desktopfile, "[Desktop Entry]\n" @@ -89,7 +138,7 @@ static void RegisterProtocols(const char *path) mimefile = fopen(va("%s/.local/share/applications/mimeinfo.cache", homedir), "a+"); if (!mimefile) - I_Error("PROTOCOL: Error opening mime file."); + I_Error("Unable to register protocols. Error opening mime file."); while (fgets(buffer, 255, mimefile)) if (strncmp(buffer, "x-scheme-handler/srb2kart=srb2kart-handler.desktop;", 43)==0) @@ -102,6 +151,7 @@ static void RegisterProtocols(const char *path) fprintf(mimefile, "x-scheme-handler/srb2kart=srb2kart-handler.desktop;\n"); #endif + return true; } #if defined (__WIN32) @@ -117,7 +167,7 @@ static HKEY OpenKey(HKEY hRootKey, const char* strKey) } if (nError) - I_Error("PROTOCOL: Could not create Registry Key."); + I_Error("Unable to register protocols. Could not create Registry Key."); return hKey; } @@ -163,44 +213,33 @@ void D_SetupProtocol(void) } else // probably first time { - const SDL_MessageBoxButtonData buttons[] = { -#if defined (__unix__) || defined (UNIXCOMMON) - { /* .flags, .buttonid, .text */ 0, 0, "Yes" }, - { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" } -#elif defined (__WIN32) - { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 1, "No" }, - { /* .flags, .buttonid, .text */ 0, 0, "Yes" } -#endif - }; - const SDL_MessageBoxData messageboxdata = { - SDL_MESSAGEBOX_INFORMATION, - NULL, - "Register SRB2Kart Protocols", - "Would you like to register the srb2kart:// protocol?\n" - "This will allow you to connect to servers and load replays directly from the browser" - "", - SDL_arraysize(buttons), - buttons, - NULL - }; - - int buttonid; - if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) - { - I_Error("Error displaying message box."); - } - - if (buttonid == 0) - { - RegisterProtocols(exe_path); + if (RegisterProtocols(exe_path)) fprintf(fp, "%s", exe_path); - } else fprintf(fp, "no"); } fclose(fp); } +void D_DeleteProtocol(void) +{ + FILE *fp = NULL; + DeleteProtocols(); + fp = fopen(va("%s/protocol.txt", srb2home), "w"); + fprintf(fp, "no"); + fclose(fp); +} + +void D_CreateProtocol(void) +{ + FILE *fp = NULL; + const char *exe_path = GetExePath(); + fp = fopen(va("%s/protocol.txt", srb2home), "w"); + RegisterProtocols(exe_path); + fprintf(fp, "%s", exe_path); + fclose(fp); +} + #ifdef HAVE_CURL void D_DownloadReplay(const char *url, const char *path) { @@ -209,7 +248,7 @@ void D_DownloadReplay(const char *url, const char *path) CURLcode cc; curl = curl_easy_init(); if (!curl) - I_Error("REPLAY: Error initializing CURL."); + I_Error("Unable to download replay. Error initializing CURL."); fp = fopen(path, "wb"); CONS_Printf("REPLAY: URL: %s\n", url); @@ -219,7 +258,7 @@ void D_DownloadReplay(const char *url, const char *path) cc = curl_easy_perform(curl); if (cc != CURLE_OK) - I_Error("REPLAY: URL gave response code %u.", cc); + I_Error("Unable to download replay. URL gave response code %u.", cc); curl_easy_cleanup(curl); fclose(fp); diff --git a/src/d_protocol.h b/src/d_protocol.h index 382368299..07d9fee73 100644 --- a/src/d_protocol.h +++ b/src/d_protocol.h @@ -10,6 +10,9 @@ /// \brief srb2kart:// protocol stuff void D_SetupProtocol(void); + +void D_DeleteProtocol(void); +void D_CreateProtocol(void); #ifdef HAVE_CURL void D_DownloadReplay(const char *url, const char *path); #endif diff --git a/src/m_menu.c b/src/m_menu.c index 15a619871..5628cee79 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -68,6 +68,9 @@ // And just some randomness for the exits. #include "m_random.h" +// protocol handling +#include "d_protocol.h" + #if defined(HAVE_SDL) #include "SDL.h" #if SDL_VERSION_ATLEAST(2,0,0) @@ -305,6 +308,7 @@ menu_t OP_SoundOptionsDef; //Misc menu_t OP_DataOptionsDef, OP_ScreenshotOptionsDef, OP_EraseDataDef; +menu_t OP_ProtocolDef; #ifdef HAVE_DISCORDRPC menu_t OP_DiscordOptionsDef; #endif @@ -322,6 +326,9 @@ static void M_Addons(INT32 choice); static void M_AddonsOptions(INT32 choice); static patch_t *addonsp[NUM_EXT+5]; +static void M_RegisterProtocol(void); +static void M_DeleteProtocol(void); + #define numaddonsshown 4 // Replay hut @@ -1351,12 +1358,13 @@ static menuitem_t OP_DataOptionsMenu[] = {IT_STRING | IT_CALL, NULL, "Screenshot Options...", M_ScreenshotOptions, 10}, {IT_STRING | IT_CALL, NULL, "Addon Options...", M_AddonsOptions, 20}, {IT_STRING | IT_SUBMENU, NULL, "Replay Options...", &MISC_ReplayOptionsDef, 30}, + {IT_STRING | IT_SUBMENU, NULL, "Protocol options...", &OP_ProtocolDef, 40}, #ifdef HAVE_DISCORDRPC - {IT_STRING | IT_SUBMENU, NULL, "Discord Options...", &OP_DiscordOptionsDef, 40}, + {IT_STRING | IT_SUBMENU, NULL, "Discord Options...", &OP_DiscordOptionsDef, 50}, - {IT_STRING | IT_SUBMENU, NULL, "Erase Data...", &OP_EraseDataDef, 60}, + {IT_STRING | IT_SUBMENU, NULL, "Erase Data...", &OP_EraseDataDef, 70}, #else - {IT_STRING | IT_SUBMENU, NULL, "Erase Data...", &OP_EraseDataDef, 50}, + {IT_STRING | IT_SUBMENU, NULL, "Erase Data...", &OP_EraseDataDef, 60}, #endif }; @@ -1419,6 +1427,12 @@ enum op_addons_folder = 2, }; +static menuitem_t OP_ProtocolMenu[] = +{ + {IT_STRING | IT_CALL, NULL, "Register protocol", M_RegisterProtocol, 10}, + {IT_STRING | IT_CALL, NULL, "\x85" "Disable and delete protocols", M_DeleteProtocol, 20}, +}; + #ifdef HAVE_DISCORDRPC static menuitem_t OP_DiscordOptionsMenu[] = { @@ -2100,6 +2114,7 @@ menu_t OP_OpenGLColorDef = menu_t OP_DataOptionsDef = DEFAULTMENUSTYLE("M_DATA", OP_DataOptionsMenu, &OP_MainDef, 60, 30); menu_t OP_ScreenshotOptionsDef = DEFAULTMENUSTYLE("M_SCSHOT", OP_ScreenshotOptionsMenu, &OP_DataOptionsDef, 30, 30); menu_t OP_AddonsOptionsDef = DEFAULTMENUSTYLE("M_ADDONS", OP_AddonsOptionsMenu, &OP_DataOptionsDef, 30, 30); +menu_t OP_ProtocolDef = DEFAULTMENUSTYLE(NULL, OP_ProtocolMenu, &OP_DataOptionsDef, 30, 30); #ifdef HAVE_DISCORDRPC menu_t OP_DiscordOptionsDef = DEFAULTMENUSTYLE(NULL, OP_DiscordOptionsMenu, &OP_DataOptionsDef, 30, 30); #endif @@ -9880,6 +9895,19 @@ static void M_ScreenshotOptions(INT32 choice) M_SetupNextMenu(&OP_ScreenshotOptionsDef); } +static void M_RegisterProtocol(void) +{ + D_CreateProtocol(); +} + +static void M_DeleteProtocol(void) +{ + M_StartMessage("Are you sure you want to disable and delete protocol registers?\n" + "Protocols can be registered again with the Register Protocol option in this menu\n\n" + "(Press 'Y' to confirm)\n", + D_DeleteProtocol, MM_YESNO); +} + // ============= // JOYSTICK MENU // ============= From 46c2964213503047b50b619bfcedf1a5cab98a4e Mon Sep 17 00:00:00 2001 From: Fafabis Date: Thu, 6 May 2021 10:53:59 -0300 Subject: [PATCH 12/16] Remove unnecessary things --- src/d_main.c | 2 +- src/d_protocol.c | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/d_main.c b/src/d_main.c index 24e43a321..e7633bf57 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1132,7 +1132,7 @@ void D_SRB2Main(void) #endif } - D_SetupProtocol(); // ask the user if they want to have cool url protocols + D_SetupProtocol(); // rand() needs seeded regardless of password srand((unsigned int)time(NULL)); diff --git a/src/d_protocol.c b/src/d_protocol.c index 1d9269d5d..76c7a46e3 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -32,8 +32,6 @@ #include "d_protocol.h" #include "i_system.h" -#include "SDL.h" - // PROTOS #if defined (__WIN32) static HKEY OpenKey(HKEY hRootKey, const char* strKey); From 2f457584d84eb2a538ca267a81e0be3d46aca4af Mon Sep 17 00:00:00 2001 From: Fafabis Date: Thu, 6 May 2021 11:50:55 -0300 Subject: [PATCH 13/16] Properly update update-desktop-database --- src/d_protocol.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index 76c7a46e3..bd6d799c5 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -10,6 +10,7 @@ /// \brief srb2kart:// protocol stuff #include +#include #include #include #if defined (__unix__) || defined (UNIXCOMMON) @@ -98,6 +99,7 @@ static boolean RegisterProtocols(const char *path) const char *homedir = pw->pw_dir; char buffer[255]; boolean alreadyexists = false; + boolean newfolder = false; struct stat sb; char *applicationsfolder = va("%s/.local/share/applications/", homedir); @@ -111,12 +113,8 @@ static boolean RegisterProtocols(const char *path) if (stat(applicationsfolder, &sb) == -1) { // location doesn't exist, so let's actually create it + newfolder = true; I_mkdir(applicationsfolder, 0755); - if (system(va("update-desktop-database %s", applicationsfolder)) != 1) - { - CONS_Alert(CONS_ERROR, "Unable to register protocols. Could not run call to run update-desktop-database.\n"); - return false; - } } desktopfile = fopen(va("%s/.local/share/applications/srb2kart-handler.desktop", homedir), "w"); @@ -138,15 +136,26 @@ static boolean RegisterProtocols(const char *path) if (!mimefile) I_Error("Unable to register protocols. Error opening mime file."); - while (fgets(buffer, 255, mimefile)) + while (fgets(buffer, 255, mimefile)) + { if (strncmp(buffer, "x-scheme-handler/srb2kart=srb2kart-handler.desktop;", 43)==0) { alreadyexists = true; break; } + } if (!alreadyexists) fprintf(mimefile, "x-scheme-handler/srb2kart=srb2kart-handler.desktop;\n"); + + if (newfolder) + { + if (system(va("update-desktop-database %s/.local/share/applications/", homedir)) == -1) + { + CONS_Alert(CONS_ERROR, "Unable to register protocols. Could not run call to run update-desktop-database sucessfully.\n"); + return false; + } + } #endif return true; From def20e607b0cf8dba1425cf1df14459b4ed7b494 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Thu, 6 May 2021 12:35:44 -0300 Subject: [PATCH 14/16] Cleanup --- src/d_protocol.c | 58 ++++++++++++++++++++++++------------------------ src/d_protocol.h | 2 +- src/m_menu.c | 8 +------ 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/d_protocol.c b/src/d_protocol.c index bd6d799c5..3aa5c1717 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -54,35 +54,6 @@ static char *GetExePath(void) return buf; } -static void DeleteProtocols(void) -{ -#if defined (__WIN32) - - HKEY hKey; - RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open\\command"), 0, KEY_ALL_ACCESS, &hKey); - RegDeleteValue(hKey, _T("")); - RegCloseKey(hKey); - RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open\\command")); - RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open"), 0, KEY_ALL_ACCESS, &hKey); - RegDeleteValue(hKey, _T("")); - RegCloseKey(hKey); - RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open")); - RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell"), 0, KEY_ALL_ACCESS, &hKey); - RegDeleteValue(hKey, _T("")); - RegCloseKey(hKey); - RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell")); - RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart"), 0, KEY_ALL_ACCESS, &hKey); - RegDeleteValue(hKey, _T("URL Protocol")); - RegDeleteValue(hKey, _T("")); - RegCloseKey(hKey); - RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart")); -#elif defined (__unix__) || defined (UNIXCOMMON) - struct passwd *pw = getpwuid(getuid()); - const char *homedir = pw->pw_dir; - remove(va("%s/.local/share/applications/srb2kart-handler.desktop", homedir)); -#endif -} - static boolean RegisterProtocols(const char *path) { #if defined (__WIN32) @@ -161,6 +132,35 @@ static boolean RegisterProtocols(const char *path) return true; } +static void DeleteProtocols(void) +{ +#if defined (__WIN32) + + HKEY hKey; + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open\\command"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open\\command")); + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell\\open")); + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart\\shell")); + RegOpenKeyEx(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart"), 0, KEY_ALL_ACCESS, &hKey); + RegDeleteValue(hKey, _T("URL Protocol")); + RegDeleteValue(hKey, _T("")); + RegCloseKey(hKey); + RegDeleteKey(HKEY_CURRENT_USER, _T("Software\\Classes\\srb2kart")); +#elif defined (__unix__) || defined (UNIXCOMMON) + struct passwd *pw = getpwuid(getuid()); + const char *homedir = pw->pw_dir; + remove(va("%s/.local/share/applications/srb2kart-handler.desktop", homedir)); +#endif +} + #if defined (__WIN32) static HKEY OpenKey(HKEY hRootKey, const char* strKey) { diff --git a/src/d_protocol.h b/src/d_protocol.h index 07d9fee73..43220fb95 100644 --- a/src/d_protocol.h +++ b/src/d_protocol.h @@ -11,8 +11,8 @@ void D_SetupProtocol(void); -void D_DeleteProtocol(void); void D_CreateProtocol(void); +void D_DeleteProtocol(void); #ifdef HAVE_CURL void D_DownloadReplay(const char *url, const char *path); #endif diff --git a/src/m_menu.c b/src/m_menu.c index 5628cee79..5fe8dde41 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -326,7 +326,6 @@ static void M_Addons(INT32 choice); static void M_AddonsOptions(INT32 choice); static patch_t *addonsp[NUM_EXT+5]; -static void M_RegisterProtocol(void); static void M_DeleteProtocol(void); #define numaddonsshown 4 @@ -1429,7 +1428,7 @@ enum static menuitem_t OP_ProtocolMenu[] = { - {IT_STRING | IT_CALL, NULL, "Register protocol", M_RegisterProtocol, 10}, + {IT_STRING | IT_CALL, NULL, "Register protocol", D_CreateProtocol, 10}, {IT_STRING | IT_CALL, NULL, "\x85" "Disable and delete protocols", M_DeleteProtocol, 20}, }; @@ -9895,11 +9894,6 @@ static void M_ScreenshotOptions(INT32 choice) M_SetupNextMenu(&OP_ScreenshotOptionsDef); } -static void M_RegisterProtocol(void) -{ - D_CreateProtocol(); -} - static void M_DeleteProtocol(void) { M_StartMessage("Are you sure you want to disable and delete protocol registers?\n" From 6c1a5236decaaca399081270d877201c0856ccef Mon Sep 17 00:00:00 2001 From: Fafabis Date: Thu, 20 May 2021 10:03:38 -0300 Subject: [PATCH 15/16] Force to execute kartexec.cfg --- src/d_main.c | 2 +- src/i_tcp.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/d_main.c b/src/d_main.c index e7633bf57..dcffc49f0 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -1446,7 +1446,7 @@ void D_SRB2Main(void) if (D_CheckNetGame()) autostart = true; - if (splitscreen && !M_CheckParm("-connect")) // Make sure multiplayer & autostart is set if you have splitscreen, even after D_CheckNetGame + if (splitscreen && !M_CheckParm("-connect") && !M_CheckProtoParam("ip")) // Make sure multiplayer & autostart is set if you have splitscreen, even after D_CheckNetGame multiplayer = autostart = true; // check for a driver that wants intermission stats diff --git a/src/i_tcp.c b/src/i_tcp.c index cd2560ad6..144ddf3a6 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -183,6 +183,7 @@ static UINT8 UPNP_support = TRUE; #include "i_tcp.h" #include "m_argv.h" #include "stun.h" +#include "d_main.h" #include "doomstat.h" @@ -1566,6 +1567,8 @@ boolean I_InitTcpNetwork(void) } if (M_CheckProtoParam("ip")) { + COM_ImmedExecute(va("exec \"%s"PATHSEP"kartexec.cfg\" -noerror\n", srb2home)); + strcpy(serverhostname, M_GetProtoParam()); // server address only in ip From 37b742683f3c43395f1bfa0f1650245e0f1533a7 Mon Sep 17 00:00:00 2001 From: Fafabis Date: Thu, 20 May 2021 10:05:36 -0300 Subject: [PATCH 16/16] Add NoDisplay to .desktop file --- src/d_protocol.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/d_protocol.c b/src/d_protocol.c index 3aa5c1717..c485d47eb 100644 --- a/src/d_protocol.c +++ b/src/d_protocol.c @@ -97,6 +97,7 @@ static boolean RegisterProtocols(const char *path) "Type=Application\n" "Name=SRB2Kart Scheme Handler\n" "Exec=bash -c '%s %%u'\n" + "NoDisplay=true\n" "StartupNotify=false\n" "MimeType=x-scheme-handler/srb2kart;\n", path);