diff --git a/src/Makefile b/src/Makefile index 4fae86e1c..28a675b7c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -537,6 +537,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 b7a3b148d..d19e93a33 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -77,6 +77,7 @@ int snprintf(char *str, size_t n, const char *fmt, ...); #include "r_fps.h" // Frame interpolation/uncapped #include "keys.h" #include "filesrch.h" // refreshdirmenu +#include "d_protocol.h" #ifdef CMAKECONFIG #include "config.h" @@ -1225,6 +1226,8 @@ void D_SRB2Main(void) #endif } + D_SetupProtocol(); + // rand() needs seeded regardless of password srand((unsigned int)time(NULL)); @@ -1534,7 +1537,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 @@ -1595,6 +1598,36 @@ void D_SRB2Main(void) return; } +#ifdef HAVE_CURL + if (M_CheckProtoParam("replay")) + { + const char *replayurl = M_GetProtoParam(); + char *replayname = strrchr(replayurl, '/'); + + if (!replayname || !replayurl) + I_Error("REPLAY: Invalid URL."); + +#define REPLAYDIR "/DownloadedReplays/" + I_mkdir(va("%s%s", srb2home, REPLAYDIR), 0755); + 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; + } +#endif + /*if (M_CheckParm("-ultimatemode")) { autostart = true; 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 new file mode 100644 index 000000000..c485d47eb --- /dev/null +++ b/src/d_protocol.c @@ -0,0 +1,274 @@ +// 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 +#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" +#include "d_main.h" +#include "d_clisrv.h" +#include "d_netfil.h" +#include "d_protocol.h" +#include "i_system.h" + +// PROTOS +#if defined (__WIN32) +static HKEY OpenKey(HKEY hRootKey, const char* strKey); +static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data); +#endif + +static char *GetExePath(void) +{ + static char buf[PATH_MAX]; +#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; +} + +static boolean 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; + boolean newfolder = 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 + newfolder = true; + I_mkdir(applicationsfolder, 0755); + } + + desktopfile = fopen(va("%s/.local/share/applications/srb2kart-handler.desktop", homedir), "w"); + if (!desktopfile) + I_Error("Unable to register protocols. Error creating .desktop file."); + + fprintf(desktopfile, + "[Desktop Entry]\n" + "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); + + fclose(desktopfile); + + mimefile = fopen(va("%s/.local/share/applications/mimeinfo.cache", homedir), "a+"); + if (!mimefile) + 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) + { + 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; +} + +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) +{ + 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("Unable to register protocols. Could not create Registry Key."); + + return hKey; +} + +static boolean SetStringValue(HKEY hRegistryKey, const char *valueName, const char *data) +{ + I_Assert(hRegistryKey != NULL); + + return (RegSetValueExA(hRegistryKey, + valueName, + 0, + REG_SZ, + (const BYTE *)data, + strlen(data) + 1) == ERROR_SUCCESS); +} +#endif + +void D_SetupProtocol(void) +{ + FILE *fp = NULL; + 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(protocolfile, "a+"); + result = fgets(buffer, PATH_MAX, fp); + if (result) + { + if (strcmp(buffer, "no") == 0) + return; + 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 + { + 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) +{ + FILE *fp = NULL; + CURL *curl; + CURLcode cc; + curl = curl_easy_init(); + if (!curl) + I_Error("Unable to download 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, curlwrite_data); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp); + + cc = curl_easy_perform(curl); + if (cc != CURLE_OK) + I_Error("Unable to download replay. URL gave response code %u.", cc); + + curl_easy_cleanup(curl); + fclose(fp); +} +#endif diff --git a/src/d_protocol.h b/src/d_protocol.h new file mode 100644 index 000000000..43220fb95 --- /dev/null +++ b/src/d_protocol.h @@ -0,0 +1,18 @@ +// 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_SetupProtocol(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/i_tcp.c b/src/i_tcp.c index 433770d24..ac4f2b627 100644 --- a/src/i_tcp.c +++ b/src/i_tcp.c @@ -182,6 +182,7 @@ static UINT8 UPNP_support = TRUE; #include "i_tcp.h" #include "m_argv.h" #include "stun.h" +#include "d_main.h" #include "z_zone.h" #include "doomstat.h" @@ -1872,6 +1873,32 @@ boolean I_InitTcpNetwork(void) hardware_MAXPACKETLENGTH = MAXPACKETLENGTH; } } + if (M_CheckProtoParam("ip")) + { + COM_ImmedExecute(va("exec \"%s"PATHSEP"kartexec.cfg\" -noerror\n", srb2home)); + + strcpy(serverhostname, M_GetProtoParam()); + + // 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..723c4c203 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -59,6 +59,51 @@ INT32 M_CheckParm(const char *check) return 0; } +/** \brief Check params for URL protocol + + \return number (1 to argc-1) or 0 if not present +*/ +INT32 M_CheckProtoParam(const char *check) +{ + INT32 i; + + for (i = 1; i < myargc; i++) + { + if (strncmp(myargv[i], "srb2kart://", 11) == 0) + { + if (strncmp(myargv[i]+11, check, strlen(check))==0) + { + found = i; + return i; + } + } + } + found = 0; + return 0; +} + +/** \brief Gets the string after URL protocol + * + \return string after protocol parameter or NULL if not found +*/ +const char *M_GetProtoParam(void) +{ + INT32 i; + static char *string; + + for (i = 1; i < myargc; i++) + { + if (strncmp(myargv[i], "srb2kart://", 11) == 0) + { + string = strchr(myargv[i], '/'); + string = strchr(string+1, '/'); + string = strchr(string+1, '/')+1; + 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..f31a531d0 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_CheckProtoParam(const char *check); + +// Gets the string after srb2kart:// +const char *M_GetProtoParam(void); + // Pushes all parameters beginning with a +, ex: +map map01 void M_PushSpecialParameters(void); diff --git a/src/m_menu.c b/src/m_menu.c index 505b85f9f..983bfde04 100644 --- a/src/m_menu.c +++ b/src/m_menu.c @@ -70,6 +70,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) @@ -316,6 +319,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 @@ -333,6 +337,8 @@ static void M_Addons(INT32 choice); static void M_AddonsOptions(INT32 choice); static patch_t *addonsp[NUM_EXT+5]; +static void M_DeleteProtocol(void); + // Bird menu_t OP_BirdDef; menu_t OP_TiltDef; @@ -1388,12 +1394,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 }; @@ -1456,6 +1463,12 @@ enum op_addons_folder = 2, }; +static menuitem_t OP_ProtocolMenu[] = +{ + {IT_STRING | IT_CALL, NULL, "Register protocol", D_CreateProtocol, 10}, + {IT_STRING | IT_CALL, NULL, "\x85" "Disable and delete protocols", M_DeleteProtocol, 20}, +}; + #ifdef HAVE_DISCORDRPC static menuitem_t OP_DiscordOptionsMenu[] = { @@ -2203,6 +2216,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 @@ -10861,6 +10875,14 @@ static void M_ScreenshotOptions(INT32 choice) M_SetupNextMenu(&OP_ScreenshotOptionsDef); } +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 // =============