From 3387170d6281f97fedc467555bdeb0e5d0408592 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Wed, 20 May 2020 15:43:01 +0200 Subject: [PATCH] (Discord) Cleanups - get rid of discord_register.h header --- deps/discord-rpc/include/discord_register.h | 14 -- deps/discord-rpc/include/discord_rpc.h | 6 +- deps/discord-rpc/src/backoff.h | 55 +++---- deps/discord-rpc/src/connection.h | 19 +-- deps/discord-rpc/src/discord_register_linux.c | 10 +- deps/discord-rpc/src/discord_register_osx.m | 76 +++++---- deps/discord-rpc/src/discord_register_win.c | 150 +++++++++--------- deps/discord-rpc/src/discord_rpc.cpp | 13 +- network/discord.c | 13 +- 9 files changed, 180 insertions(+), 176 deletions(-) delete mode 100644 deps/discord-rpc/include/discord_register.h diff --git a/deps/discord-rpc/include/discord_register.h b/deps/discord-rpc/include/discord_register.h deleted file mode 100644 index 3bd96b1f48..0000000000 --- a/deps/discord-rpc/include/discord_register.h +++ /dev/null @@ -1,14 +0,0 @@ -#pragma once - -#define DISCORD_EXPORT - -#ifdef __cplusplus -extern "C" { -#endif - -DISCORD_EXPORT void Discord_Register(const char* applicationId, const char* command); -DISCORD_EXPORT void Discord_RegisterSteamGame(const char* applicationId, const char* steamId); - -#ifdef __cplusplus -} -#endif diff --git a/deps/discord-rpc/include/discord_rpc.h b/deps/discord-rpc/include/discord_rpc.h index 05c562911f..f66eca87f1 100644 --- a/deps/discord-rpc/include/discord_rpc.h +++ b/deps/discord-rpc/include/discord_rpc.h @@ -53,9 +53,9 @@ typedef struct DiscordEventHandlers #define DISCORD_REPLY_IGNORE 2 DISCORD_EXPORT void Discord_Initialize(const char* applicationId, - DiscordEventHandlers* handlers, - int autoRegister, - const char* optionalSteamId); + DiscordEventHandlers* handlers, + int autoRegister, + const char* optionalSteamId); DISCORD_EXPORT void Discord_Shutdown(void); /* checks for incoming messages, dispatches callbacks */ diff --git a/deps/discord-rpc/src/backoff.h b/deps/discord-rpc/src/backoff.h index a3e736fb7b..db89a50893 100644 --- a/deps/discord-rpc/src/backoff.h +++ b/deps/discord-rpc/src/backoff.h @@ -5,36 +5,37 @@ #include #include -struct Backoff { - int64_t minAmount; - int64_t maxAmount; - int64_t current; - int fails; - std::mt19937_64 randGenerator; - std::uniform_real_distribution<> randDistribution; +struct Backoff +{ + int64_t minAmount; + int64_t maxAmount; + int64_t current; + int fails; + std::mt19937_64 randGenerator; + std::uniform_real_distribution<> randDistribution; - double rand01() { return randDistribution(randGenerator); } + double rand01() { return randDistribution(randGenerator); } - Backoff(int64_t min, int64_t max) + Backoff(int64_t min, int64_t max) : minAmount(min) - , maxAmount(max) - , current(min) - , fails(0) - , randGenerator((uint64_t)time(0)) - { - } + , maxAmount(max) + , current(min) + , fails(0) + , randGenerator((uint64_t)time(0)) + { + } - void reset() - { - fails = 0; - current = minAmount; - } + void reset() + { + fails = 0; + current = minAmount; + } - int64_t nextDelay() - { - ++fails; - int64_t delay = (int64_t)((double)current * 2.0 * rand01()); - current = std::min(current + delay, maxAmount); - return current; - } + int64_t nextDelay() + { + ++fails; + int64_t delay = (int64_t)((double)current * 2.0 * rand01()); + current = std::min(current + delay, maxAmount); + return current; + } }; diff --git a/deps/discord-rpc/src/connection.h b/deps/discord-rpc/src/connection.h index d3fa231d5e..4502fa2dd4 100644 --- a/deps/discord-rpc/src/connection.h +++ b/deps/discord-rpc/src/connection.h @@ -6,14 +6,15 @@ #include /* not really connectiony, but need per-platform */ -int GetProcessId(); +int GetProcessId(void); -struct BaseConnection { - static BaseConnection* Create(); - static void Destroy(BaseConnection*&); - bool isOpen{false}; - bool Open(); - bool Close(); - bool Write(const void* data, size_t length); - bool Read(void* data, size_t length); +struct BaseConnection +{ + static BaseConnection* Create(); + static void Destroy(BaseConnection*&); + bool isOpen{false}; + bool Open(); + bool Close(); + bool Write(const void* data, size_t length); + bool Read(void* data, size_t length); }; diff --git a/deps/discord-rpc/src/discord_register_linux.c b/deps/discord-rpc/src/discord_register_linux.c index 8eec618cb3..7fc42ebd0d 100644 --- a/deps/discord-rpc/src/discord_register_linux.c +++ b/deps/discord-rpc/src/discord_register_linux.c @@ -1,5 +1,3 @@ -#include "discord_rpc.h" -#include "discord_register.h" #include #include @@ -13,9 +11,11 @@ #include #include +#include + /* we want to register games so we can run them from * Discord client as discord-:// */ -void Discord_Register(const char* applicationId, const char* command) +void Discord_Register(const char *applicationId, const char *command) { FILE* fp; int fileLen; @@ -82,9 +82,7 @@ void Discord_Register(const char* applicationId, const char* command) fprintf(stderr, "Failed to register mime handler\n"); } -void Discord_RegisterSteamGame( - const char* applicationId, - const char* steamId) +void Discord_RegisterSteamGame(const char *applicationId, const char *steamId) { char command[256]; snprintf(command, sizeof(command), "xdg-open steam://rungameid/%s", steamId); diff --git a/deps/discord-rpc/src/discord_register_osx.m b/deps/discord-rpc/src/discord_register_osx.m index 4975565296..0973e15108 100644 --- a/deps/discord-rpc/src/discord_register_osx.m +++ b/deps/discord-rpc/src/discord_register_osx.m @@ -3,8 +3,6 @@ #import -#include "../include/discord_register.h" - static void RegisterCommand(const char* applicationId, const char* command) { /* There does not appear to be a way to register arbitrary commands on OSX, so instead we'll save the command @@ -31,54 +29,52 @@ static void RegisterCommand(const char* applicationId, const char* command) static void RegisterURL(const char* applicationId) { - char url[256]; - snprintf(url, sizeof(url), "discord-%s", applicationId); - CFStringRef cfURL = CFStringCreateWithCString(NULL, url, kCFStringEncodingUTF8); - NSString* myBundleId = [[NSBundle mainBundle] bundleIdentifier]; + char url[256]; + snprintf(url, sizeof(url), "discord-%s", applicationId); + CFStringRef cfURL = CFStringCreateWithCString(NULL, url, kCFStringEncodingUTF8); + NSString* myBundleId = [[NSBundle mainBundle] bundleIdentifier]; - if (!myBundleId) - { - fprintf(stderr, "No bundle id found\n"); - return; - } + if (!myBundleId) + { + fprintf(stderr, "No bundle id found\n"); + return; + } - NSURL* myURL = [[NSBundle mainBundle] bundleURL]; - if (!myURL) - { - fprintf(stderr, "No bundle url found\n"); - return; - } + NSURL* myURL = [[NSBundle mainBundle] bundleURL]; + if (!myURL) + { + fprintf(stderr, "No bundle url found\n"); + return; + } - OSStatus status = LSSetDefaultHandlerForURLScheme(cfURL, (__bridge CFStringRef)myBundleId); - if (status != noErr) - { - fprintf(stderr, "Error in LSSetDefaultHandlerForURLScheme: %d\n", (int)status); - return; - } + OSStatus status = LSSetDefaultHandlerForURLScheme(cfURL, (__bridge CFStringRef)myBundleId); + if (status != noErr) + { + fprintf(stderr, "Error in LSSetDefaultHandlerForURLScheme: %d\n", (int)status); + return; + } - status = LSRegisterURL((__bridge CFURLRef)myURL, true); - if (status != noErr) - { - fprintf(stderr, "Error in LSRegisterURL: %d\n", (int)status); - } + status = LSRegisterURL((__bridge CFURLRef)myURL, true); + if (status != noErr) + fprintf(stderr, "Error in LSRegisterURL: %d\n", (int)status); } void Discord_Register(const char* applicationId, const char* command) { - if (command) - RegisterCommand(applicationId, command); - else - { - /* RAII Lite */ - @autoreleasepool { - RegisterURL(applicationId); - } - } + if (command) + RegisterCommand(applicationId, command); + else + { + /* RAII Lite */ + @autoreleasepool { + RegisterURL(applicationId); + } + } } void Discord_RegisterSteamGame(const char* applicationId, const char* steamId) { - char command[256]; - snprintf(command, sizeof(command), "steam://rungameid/%s", steamId); - Discord_Register(applicationId, command); + char command[256]; + snprintf(command, sizeof(command), "steam://rungameid/%s", steamId); + Discord_Register(applicationId, command); } diff --git a/deps/discord-rpc/src/discord_register_win.c b/deps/discord-rpc/src/discord_register_win.c index 1a03603b80..992ff58ce3 100644 --- a/deps/discord-rpc/src/discord_register_win.c +++ b/deps/discord-rpc/src/discord_register_win.c @@ -1,5 +1,4 @@ #include "discord_rpc.h" -#include "discord_register.h" #define WIN32_LEAN_AND_MEAN #define NOMCX @@ -53,99 +52,99 @@ static LSTATUS regset(HKEY hkey, const void* data, DWORD len) { - HKEY htkey = hkey, hsubkey = NULL; - LSTATUS ret; - if (subkey && subkey[0]) - { - if ((ret = RegCreateKeyExW(hkey, subkey, 0, 0, 0, KEY_ALL_ACCESS, 0, &hsubkey, 0)) != + LSTATUS ret; + HKEY htkey = hkey, hsubkey = NULL; + if (subkey && subkey[0]) + { + if ((ret = RegCreateKeyExW(hkey, subkey, 0, 0, 0, KEY_ALL_ACCESS, 0, &hsubkey, 0)) != ERROR_SUCCESS) - return ret; - htkey = hsubkey; - } - ret = RegSetValueExW(htkey, name, 0, type, (const BYTE*)data, len); - if (hsubkey && hsubkey != hkey) - RegCloseKey(hsubkey); - return ret; + return ret; + htkey = hsubkey; + } + ret = RegSetValueExW(htkey, name, 0, type, (const BYTE*)data, len); + if (hsubkey && hsubkey != hkey) + RegCloseKey(hsubkey); + return ret; } static void Discord_RegisterW( const wchar_t* applicationId, const wchar_t* command) { - /* https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx - * we want to register games so we can run them as discord-:// - * Update the HKEY_CURRENT_USER, because it doesn't seem to require special permissions. */ + /* https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx + * we want to register games so we can run them as discord-:// + * Update the HKEY_CURRENT_USER, because it doesn't seem to require special permissions. */ - DWORD len; - LSTATUS result; - wchar_t urlProtocol = 0; - wchar_t keyName[256]; - wchar_t protocolName[64]; - wchar_t protocolDescription[128]; - wchar_t exeFilePath[MAX_PATH]; - DWORD exeLen = GetModuleFileNameW(NULL, exeFilePath, MAX_PATH); - wchar_t openCommand[1024]; + DWORD len; + LSTATUS result; + wchar_t urlProtocol = 0; + wchar_t keyName[256]; + wchar_t protocolName[64]; + wchar_t protocolDescription[128]; + wchar_t exeFilePath[MAX_PATH]; + DWORD exeLen = GetModuleFileNameW(NULL, exeFilePath, MAX_PATH); + wchar_t openCommand[1024]; - if (command && command[0]) - StringCbPrintfW(openCommand, sizeof(openCommand), L"%S", command); - else - StringCbPrintfW(openCommand, sizeof(openCommand), L"%S", exeFilePath); + if (command && command[0]) + StringCbPrintfW(openCommand, sizeof(openCommand), L"%S", command); + else + StringCbPrintfW(openCommand, sizeof(openCommand), L"%S", exeFilePath); - StringCbPrintfW(protocolName, sizeof(protocolName), - L"discord-%S", applicationId); - StringCbPrintfW( - protocolDescription, sizeof(protocolDescription), - L"URL:Run game %S protocol", applicationId); - StringCbPrintfW(keyName, sizeof(keyName), L"Software\\Classes\\%S", protocolName); - HKEY key; - LSTATUS status = + StringCbPrintfW(protocolName, sizeof(protocolName), + L"discord-%S", applicationId); + StringCbPrintfW( + protocolDescription, sizeof(protocolDescription), + L"URL:Run game %S protocol", applicationId); + StringCbPrintfW(keyName, sizeof(keyName), L"Software\\Classes\\%S", protocolName); + HKEY key; + LSTATUS status = RegCreateKeyExW(HKEY_CURRENT_USER, keyName, 0, NULL, 0, KEY_WRITE, NULL, &key, NULL); - if (status != ERROR_SUCCESS) - { - fprintf(stderr, "Error creating key\n"); - return; - } - len = (DWORD)lstrlenW(protocolDescription) + 1; - result = + if (status != ERROR_SUCCESS) + { + fprintf(stderr, "Error creating key\n"); + return; + } + len = (DWORD)lstrlenW(protocolDescription) + 1; + result = RegSetKeyValueW(key, NULL, NULL, REG_SZ, protocolDescription, len * sizeof(wchar_t)); - if (FAILED(result)) { - fprintf(stderr, "Error writing description\n"); - } + if (FAILED(result)) + fprintf(stderr, "Error writing description\n"); - len = (DWORD)lstrlenW(protocolDescription) + 1; - result = RegSetKeyValueW(key, NULL, L"URL Protocol", REG_SZ, &urlProtocol, sizeof(wchar_t)); - if (FAILED(result)) - fprintf(stderr, "Error writing description\n"); + len = (DWORD)lstrlenW(protocolDescription) + 1; + result = RegSetKeyValueW(key, NULL, L"URL Protocol", REG_SZ, &urlProtocol, sizeof(wchar_t)); + if (FAILED(result)) + fprintf(stderr, "Error writing description\n"); - result = RegSetKeyValueW( - key, L"DefaultIcon", NULL, REG_SZ, exeFilePath, (exeLen + 1) * sizeof(wchar_t)); - if (FAILED(result)) - fprintf(stderr, "Error writing icon\n"); + result = RegSetKeyValueW( + key, L"DefaultIcon", NULL, REG_SZ, exeFilePath, (exeLen + 1) * sizeof(wchar_t)); + if (FAILED(result)) + fprintf(stderr, "Error writing icon\n"); - len = (DWORD)lstrlenW(openCommand) + 1; - result = RegSetKeyValueW( - key, L"shell\\open\\command", NULL, REG_SZ, openCommand, len * sizeof(wchar_t)); - if (FAILED(result)) - fprintf(stderr, "Error writing command\n"); - RegCloseKey(key); + len = (DWORD)lstrlenW(openCommand) + 1; + result = RegSetKeyValueW( + key, L"shell\\open\\command", NULL, REG_SZ, openCommand, len * sizeof(wchar_t)); + if (FAILED(result)) + fprintf(stderr, "Error writing command\n"); + RegCloseKey(key); } void Discord_Register(const char* applicationId, const char* command) { - wchar_t openCommand[1024]; - const wchar_t* wcommand = NULL; - wchar_t appId[32]; + wchar_t appId[32]; + wchar_t openCommand[1024]; + const wchar_t* wcommand = NULL; - MultiByteToWideChar(CP_UTF8, 0, applicationId, -1, appId, 32); - if (command && command[0]) - { - const int commandBufferLen = - sizeof(openCommand) / sizeof(*openCommand); - MultiByteToWideChar(CP_UTF8, 0, command, -1, - openCommand, commandBufferLen); - wcommand = openCommand; - } + MultiByteToWideChar(CP_UTF8, 0, applicationId, -1, appId, 32); - Discord_RegisterW(appId, wcommand); + if (command && command[0]) + { + const int commandBufferLen = + sizeof(openCommand) / sizeof(*openCommand); + MultiByteToWideChar(CP_UTF8, 0, command, -1, + openCommand, commandBufferLen); + wcommand = openCommand; + } + + Discord_RegisterW(appId, wcommand); } void Discord_RegisterSteamGame( @@ -172,7 +171,8 @@ void Discord_RegisterSteamGame( status = RegQueryValueExW(key, L"SteamExe", NULL, NULL, (BYTE*)steamPath, &pathBytes); RegCloseKey(key); - if (status != ERROR_SUCCESS || pathBytes < 1) { + if (status != ERROR_SUCCESS || pathBytes < 1) + { fprintf(stderr, "Error reading SteamExe key\n"); return; } diff --git a/deps/discord-rpc/src/discord_rpc.cpp b/deps/discord-rpc/src/discord_rpc.cpp index f430746e83..41a9161db4 100644 --- a/deps/discord-rpc/src/discord_rpc.cpp +++ b/deps/discord-rpc/src/discord_rpc.cpp @@ -1,7 +1,6 @@ #include "discord_rpc.h" #include "backoff.h" -#include "discord_register.h" #include "msg_queue.h" #include "rpc_connection.h" #include "serialization.h" @@ -15,6 +14,18 @@ #include #endif +/* Forward declarations */ +#ifdef __cplusplus +extern "C" { +#endif + +void Discord_Register(const char *a, const char *b); +void Discord_RegisterSteamGame(const char *a, const char *b); + +#ifdef __cplusplus +} +#endif + constexpr size_t MaxMessageSize{16 * 1024}; constexpr size_t MessageQueueSize{8}; constexpr size_t JoinQueueSize{8}; diff --git a/network/discord.c b/network/discord.c index 66a839f7ef..480efd1f7c 100644 --- a/network/discord.c +++ b/network/discord.c @@ -23,7 +23,6 @@ #include #include "discord.h" -#include "discord_register.h" #include "../deps/discord-rpc/include/discord_rpc.h" @@ -90,6 +89,18 @@ static discord_state_t discord_st; #define CDN_URL "https://cdn.discordapp.com/avatars" +/* Forward declarations */ +#ifdef __cplusplus +extern "C" { +#endif + +void Discord_Register(const char *a, const char *b); +void Discord_RegisterSteamGame(const char *a, const char *b); + +#ifdef __cplusplus +} +#endif + static discord_state_t *discord_get_ptr(void) { return &discord_st;