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);