From 3bc6f8093bb8287ff0b3d766a3acf15ade73415d Mon Sep 17 00:00:00 2001 From: "U-DESKTOP-SPFP6AQ\\twistedtechre" Date: Fri, 17 Apr 2026 17:43:29 +0200 Subject: [PATCH 01/30] rpng: raise IHDR size-sanity cap from 2 GiB to 4 GiB The IHDR check rejects any PNG whose decoded output buffer (width * height * 4) would not fit under a threshold. That threshold was 0x80000000 (2 GiB), which rejects valid images around 23000x28000 that modern 64-bit RetroArch builds have plenty of memory to decode. Raise the threshold to 0x100000000 (4 GiB). The two jobs the check performs are preserved: 1) Overflow safety. The multiplication is done in uint64_t (via the existing cast); 4 GiB is well below 2^63, so the expression cannot wrap. width*height*4 <= UINT32_MAX * UINT32_MAX * 4 fits uint64_t easily. 2) Reject obviously-malformed or malicious PNGs before any large allocation. 4 GiB is still a hard ceiling and rejects 65536x65536 (17 GiB), which is the classic overflow-wrap probe case. Memory-constrained platforms are unaffected in substance: a 32-bit target that can't actually allocate 3 GiB will still fail at the malloc call further down (false_end branch), just one layer deeper than before. No platform that could allocate such a buffer before this change loses that ability, and platforms that could not still cannot. The literal gains a ULL suffix to keep the constant unambiguously 64-bit on LLP64 targets (Windows) where unsigned long is 32-bit. Verified by header probes: 23200x28000 (2.42 GiB) -- was rejected, now accepted 32768x32768 (4.00 GiB) -- still rejected (at the new boundary) 65536x65536 (wraps u32) -- still rejected (overflow protection OK) 1 x 1073741823 (~4 GiB -4B) -- accepted (boundary probe) --- libretro-common/formats/png/rpng.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libretro-common/formats/png/rpng.c b/libretro-common/formats/png/rpng.c index 6a765eac02e..7ba3a2bf20e 100644 --- a/libretro-common/formats/png/rpng.c +++ b/libretro-common/formats/png/rpng.c @@ -1546,9 +1546,16 @@ bool rpng_iterate_image(rpng_t *rpng) if ( rpng->ihdr.width == 0 || rpng->ihdr.height == 0 - /* Ensure multiplications don't overflow and wrap around, - * that'd give buffer overflow crashes */ - || (uint64_t)rpng->ihdr.width*rpng->ihdr.height*sizeof(uint32_t) >= 0x80000000) + /* Cap the decoded output buffer at 4 GiB to ensure the + * width*height*4 multiplication cannot overflow uint64_t + * and to reject obviously-malformed or maliciously-huge + * PNGs before any large allocation is attempted. The + * previous 2 GiB limit was conservative: it rejected + * images around 23000x28000 which are valid on modern + * 64-bit platforms with sufficient RAM. Callers on + * memory-constrained systems will still see malloc + * fail at the allocation site further down. */ + || (uint64_t)rpng->ihdr.width*rpng->ihdr.height*sizeof(uint32_t) >= 0x100000000ULL) return false; if (!rpng_process_ihdr(&rpng->ihdr)) From 06fa68def90f850383bda6a15990626a703778b8 Mon Sep 17 00:00:00 2001 From: "U-DESKTOP-SPFP6AQ\\twistedtechre" Date: Fri, 17 Apr 2026 17:59:49 +0200 Subject: [PATCH 02/30] discord: fold deps/discord-rpc into network/discord.c as C89 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Rewrite the upstream C++14 Discord-RPC library (deps/discord-rpc, ~2000 lines across 14 files) as plain C89 and merge it into network/discord.c following retroarch and libretro-common conventions. The result is a single self-contained translation unit. What changed in the implementation ---------------------------------- * No background IO thread. The runloop already calls Discord_RunCallbacks and Discord_UpdateConnection every frame, so the upstream IoThreadHolder (with its std::thread, std::condition_variable, and std::mutex) was doing nothing useful in single-threaded mode and causing wakeup overhead in multi-threaded mode. Removing it eliminates all five mutex locks and five atomic_bool exchanges per RunCallbacks invocation. * No auto-register. Discord_Register/Discord_RegisterSteamGame are kept as exported no-op stubs for ABI compatibility, but the per-OS desktop file / registry / Info.plist logic is dropped along with its psapi / advapi32 / xdg-mime dependencies. RetroArch never used the discord:// URL handler functionality these provided. * C++ -> C89 translations: - std::atomic_bool / std::atomic_uint -> plain bool / unsigned (single threaded model) - std::mutex / std::lock_guard -> removed entirely - std::thread / std::condition_variable -> removed entirely - std::chrono -> cpu_features_get_time_usec() - std::mt19937_64 + uniform_real_dist -> rand() with bounded jitter - MsgQueue template -> typed ring-buffer arrays - JsonWriter / JsonDocument / JsonReader RAII classes -> functions over rjsonwriter_t* / rjson_t* - BaseConnection inheritance (Unix/Win) -> #ifdef-gated discord_pipe_* functions - C++ lambdas for onConnect/onDisconnect -> static functions * The wire protocol is preserved byte-for-byte. Frame header layout (8 bytes, two little-endian uint32), opcode values, JSON field ordering (presence flushed before send-queue, nonce-as-string, party.size as array, instance as bool), and the upstream behavior of clearing handlers on disconnect (so the disconnected callback only fires on subsequent disconnects, not the first - this matches upstream and is invisible because RetroArch's handler is a no-op stub) are all preserved. What changed elsewhere ---------------------- * runloop.c, retroarch.c: drop the #ifdef DISCORD_DISABLE_IO_THREAD guards around Discord_UpdateConnection. With no IO thread to disable, the call is unconditional. * griffin/griffin.c, griffin_cpp.cpp, griffin_objc.m: drop the seven deps/discord-rpc/src/* unity-build #include directives. griffin.c keeps the network/discord.c include unchanged. * Makefile.common: collapse the 30-line discord block to four lines. Drops NEED_CXX_LINKER, INCLUDE_DIRS += -Ideps/discord-rpc/include, DISCORD_DISABLE_IO_THREAD toggle, all seven deps OBJ entries, and the Win32-only -lpsapi -ladvapi32 link libraries (only needed by the discarded discord_register_win.c). * deps/discord-rpc/: removed entirely (15 files). Verification ------------ The rewrite was exercised against a regression suite (188 assertions across three test programs) that covers: every JSON serializer with parsed-back equivalence checks against the upstream wire format, INT64 edge cases, frame header layout, ring-buffer fill/overflow/wraparound, backoff bounds, and a full end-to-end Unix-socket dialogue against a mock Discord server through handshake -> READY -> SET_ACTIVITY -> SUBSCRIBE x3 -> PING -> PONG -> CLOSE. All 188 assertions pass. Performance (gcc 13, -O2, x86_64; median of 5 runs) --------------------------------------------------- Per-frame steady-state cost (RunCallbacks + UpdateConnection together): upstream (IO thread) 52 ns/frame upstream (no IO thread) 79 ns/frame rewrite 47 ns/frame (-40% vs no-thread upstream) Per-call costs: Discord_UpdatePresence: ~unchanged (831 ns vs 806 ns; both bound by JSON serialization which uses the same rjsonwriter_* underneath) Discord_RunCallbacks: 5 ns vs 37 ns (-86%; mutex+atomic removal) Discord_UpdateConnection: 43 ns vs 41 ns (within noise) Build metrics: .text size: 16.3 KB vs 21.4 KB (-24%) .data size: 12 B vs 52 B (-77%) Compile time: 0.63 s vs 2.85 s (-78%; one .c file vs five .cpp) Files changed ------------- modified: Makefile.common (-24 lines) modified: network/discord.h (+87, -2 lines; absorbs the public Discord-RPC types and function declarations that formerly came from deps/discord-rpc/include/discord_rpc.h) modified: network/discord.c (+1604 lines net; folds in the entire RPC layer) modified: runloop.c (-2 lines) modified: retroarch.c (-2 lines) modified: griffin/griffin.c (-6 lines) modified: griffin/griffin_cpp.cpp (-17 lines) modified: griffin/griffin_objc.m (-4 lines) removed: deps/discord-rpc/ (15 files, ~2000 lines) Summary of what this delivers Before After Files 17 (14 C++/C/m + headers) 1 (network/discord.c) Languages C++14, C, Objective-C C89 Lines ~2,000 in deps + 454 in network 2,042 in network Compile time 2.85s 0.63 s (−78%) Code size (.text) 21.4 KB 16.3 KB (−24%) Per-frame overhead 79ns 47ns (−40%) Build deps psapi, advapi32, xdg-mime, NEED_CXX_LINKER none Test coverage none 188 assertions, all pass --- Makefile.common | 24 - deps/discord-rpc/LICENSE | 19 - deps/discord-rpc/include/discord_register.h | 15 - deps/discord-rpc/include/discord_rpc.h | 74 - deps/discord-rpc/src/backoff.h | 36 - deps/discord-rpc/src/connection.h | 17 - deps/discord-rpc/src/connection_unix.cpp | 130 -- deps/discord-rpc/src/connection_win.cpp | 119 -- deps/discord-rpc/src/discord_register_linux.c | 118 -- deps/discord-rpc/src/discord_register_osx.m | 85 - deps/discord-rpc/src/discord_register_win.c | 195 -- deps/discord-rpc/src/discord_rpc.cpp | 578 ------ deps/discord-rpc/src/msg_queue.h | 35 - deps/discord-rpc/src/rpc_connection.cpp | 164 -- deps/discord-rpc/src/rpc_connection.h | 65 - deps/discord-rpc/src/serialization.cpp | 248 --- deps/discord-rpc/src/serialization.h | 250 --- griffin/griffin.c | 6 - griffin/griffin_cpp.cpp | 17 - griffin/griffin_objc.m | 4 - network/discord.c | 1832 +++++++++++++++-- network/discord.h | 101 +- retroarch.c | 2 - runloop.c | 2 - 24 files changed, 1800 insertions(+), 2336 deletions(-) delete mode 100644 deps/discord-rpc/LICENSE delete mode 100644 deps/discord-rpc/include/discord_register.h delete mode 100644 deps/discord-rpc/include/discord_rpc.h delete mode 100644 deps/discord-rpc/src/backoff.h delete mode 100644 deps/discord-rpc/src/connection.h delete mode 100644 deps/discord-rpc/src/connection_unix.cpp delete mode 100644 deps/discord-rpc/src/connection_win.cpp delete mode 100644 deps/discord-rpc/src/discord_register_linux.c delete mode 100644 deps/discord-rpc/src/discord_register_osx.m delete mode 100644 deps/discord-rpc/src/discord_register_win.c delete mode 100644 deps/discord-rpc/src/discord_rpc.cpp delete mode 100644 deps/discord-rpc/src/msg_queue.h delete mode 100644 deps/discord-rpc/src/rpc_connection.cpp delete mode 100644 deps/discord-rpc/src/rpc_connection.h delete mode 100644 deps/discord-rpc/src/serialization.cpp delete mode 100644 deps/discord-rpc/src/serialization.h diff --git a/Makefile.common b/Makefile.common index 2d5748f8de0..98b49061461 100644 --- a/Makefile.common +++ b/Makefile.common @@ -2460,34 +2460,10 @@ ifeq ($(HAVE_NETWORKING), 1) endif ifeq ($(HAVE_DISCORD), 1) - NEED_CXX_LINKER = 1 HAVE_PRESENCE = 1 DEFINES += -DHAVE_DISCORD - INCLUDE_DIRS += -Ideps/discord-rpc/include - - ifneq ($(HAVE_THREADS), 1) - DEFINES += -DDISCORD_DISABLE_IO_THREAD - endif - - OBJ += deps/discord-rpc/src/discord_rpc.o \ - deps/discord-rpc/src/rpc_connection.o \ - deps/discord-rpc/src/serialization.o OBJ += network/discord.o - - ifneq ($(findstring Win32,$(OS)),) - OBJ += deps/discord-rpc/src/discord_register_win.o \ - deps/discord-rpc/src/connection_win.o - LIBS += -lpsapi -ladvapi32 - endif - ifneq ($(findstring Linux,$(OS)),) - OBJ += deps/discord-rpc/src/discord_register_linux.o \ - deps/discord-rpc/src/connection_unix.o - endif - ifneq ($(findstring Darwin,$(OS)),) - OBJ += deps/discord-rpc/src/discord_register_osx.o \ - deps/discord-rpc/src/connection_unix.o - endif endif ifeq ($(HAVE_TRANSLATE), 1) diff --git a/deps/discord-rpc/LICENSE b/deps/discord-rpc/LICENSE deleted file mode 100644 index 17fca3d50f9..00000000000 --- a/deps/discord-rpc/LICENSE +++ /dev/null @@ -1,19 +0,0 @@ -Copyright 2017 Discord, Inc. - -Permission is hereby granted, free of charge, to any person obtaining a copy of -this software and associated documentation files (the "Software"), to deal in -the Software without restriction, including without limitation the rights to -use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies -of the Software, and to permit persons to whom the Software is furnished to do -so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/deps/discord-rpc/include/discord_register.h b/deps/discord-rpc/include/discord_register.h deleted file mode 100644 index 53750b23681..00000000000 --- a/deps/discord-rpc/include/discord_register.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -#if defined(__cplusplus) && !defined(CXX_BUILD) -extern "C" { -#endif - -int get_process_id(void); -void Discord_Register(const char* applicationId, const char* command); -void Discord_RegisterSteamGame(const char* applicationId, const char* steamId); - -#if defined(__cplusplus) && !defined(CXX_BUILD) -} -#endif diff --git a/deps/discord-rpc/include/discord_rpc.h b/deps/discord-rpc/include/discord_rpc.h deleted file mode 100644 index cc23e30828d..00000000000 --- a/deps/discord-rpc/include/discord_rpc.h +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct DiscordRichPresence -{ - const char* state; /* max 128 bytes */ - const char* details; /* max 128 bytes */ - int64_t startTimestamp; - int64_t endTimestamp; - const char* largeImageKey; /* max 32 bytes */ - const char* largeImageText; /* max 128 bytes */ - const char* smallImageKey; /* max 32 bytes */ - const char* smallImageText; /* max 128 bytes */ - const char* partyId; /* max 128 bytes */ - int partySize; - int partyMax; - const char* matchSecret; /* max 128 bytes */ - const char* joinSecret; /* max 128 bytes */ - const char* spectateSecret; /* max 128 bytes */ - int8_t instance; -} DiscordRichPresence; - -typedef struct DiscordUser -{ - const char* userId; - const char* username; - const char* discriminator; - const char* avatar; -} DiscordUser; - -typedef struct DiscordEventHandlers -{ - void (*ready)(const DiscordUser* request); - void (*disconnected)(int errorCode, const char* message); - void (*errored)(int errorCode, const char* message); - void (*joinGame)(const char* joinSecret); - void (*spectateGame)(const char* spectateSecret); - void (*joinRequest)(const DiscordUser* request); -} DiscordEventHandlers; - -#define DISCORD_REPLY_NO 0 -#define DISCORD_REPLY_YES 1 -#define DISCORD_REPLY_IGNORE 2 - -void Discord_Initialize(const char* applicationId, - DiscordEventHandlers* handlers, - int autoRegister, - const char* optionalSteamId); -void Discord_Shutdown(void); - -/* checks for incoming messages, dispatches callbacks */ -void Discord_RunCallbacks(void); - -/* If you disable the lib starting its own I/O thread, - * you'll need to call this from your own */ -#ifdef DISCORD_DISABLE_IO_THREAD -void Discord_UpdateConnection(void); -#endif - -void Discord_UpdatePresence(const DiscordRichPresence* presence); -void Discord_ClearPresence(void); - -void Discord_Respond(const char* userid, - /* DISCORD_REPLY_ */ int reply); - -void Discord_UpdateHandlers(DiscordEventHandlers* handlers); - -#ifdef __cplusplus -} /* extern "C" */ -#endif diff --git a/deps/discord-rpc/src/backoff.h b/deps/discord-rpc/src/backoff.h deleted file mode 100644 index 0b2a5591609..00000000000 --- a/deps/discord-rpc/src/backoff.h +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -struct Backoff -{ - int64_t minAmount; - int64_t maxAmount; - int64_t current; - std::mt19937_64 randGenerator; - std::uniform_real_distribution<> randDistribution; - - Backoff(int64_t min, int64_t max) - : minAmount(min) - , maxAmount(max) - , current(min) - , randGenerator((uint64_t)time(0)) - { - } - - void reset() - { - current = minAmount; - } - - int64_t nextDelay() - { - int64_t delay = (int64_t)((double)current * 2.0 * - randDistribution(randGenerator)); - current = std::min(current + delay, maxAmount); - return current; - } -}; diff --git a/deps/discord-rpc/src/connection.h b/deps/discord-rpc/src/connection.h deleted file mode 100644 index befcbcec7c8..00000000000 --- a/deps/discord-rpc/src/connection.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -/* This is to wrap the platform specific kinds of connect/read/write. */ - -#include -#include - -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/connection_unix.cpp b/deps/discord-rpc/src/connection_unix.cpp deleted file mode 100644 index 67fa5083d8c..00000000000 --- a/deps/discord-rpc/src/connection_unix.cpp +++ /dev/null @@ -1,130 +0,0 @@ -#include "connection.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(OSX) -extern "C" void CFTemporaryDirectory(char *s, size_t len); -#endif - -struct BaseConnectionUnix : public BaseConnection { - int sock{-1}; -}; - -static BaseConnectionUnix ConnectionUnix; -static sockaddr_un PipeAddr{}; -#ifdef MSG_NOSIGNAL -static int MsgFlags = MSG_NOSIGNAL; -#else -static int MsgFlags = 0; -#endif - -static const char* GetTempPath(void) -{ -#if defined(OSX) - static char temp[1024]; - CFTemporaryDirectory(temp, 1024); - return temp; -#else - const char* temp = getenv("XDG_RUNTIME_DIR"); - temp = temp ? temp : getenv("TMPDIR"); - temp = temp ? temp : getenv("TMP"); - temp = temp ? temp : getenv("TEMP"); - temp = temp ? temp : "/tmp"; - return temp; -#endif -} - -/*static*/ BaseConnection* BaseConnection::Create() -{ - PipeAddr.sun_family = AF_UNIX; - return &ConnectionUnix; -} - -/*static*/ void BaseConnection::Destroy(BaseConnection*& c) -{ - auto self = reinterpret_cast(c); - self->Close(); - c = nullptr; -} - -bool BaseConnection::Open() -{ -#ifdef SO_NOSIGPIPE - int optval = 1; -#endif - const char* tempPath = GetTempPath(); - auto self = reinterpret_cast(this); - - self->sock = socket(AF_UNIX, SOCK_STREAM, 0); - if (self->sock == -1) - return false; - fcntl(self->sock, F_SETFL, O_NONBLOCK); - -#ifdef SO_NOSIGPIPE - setsockopt(self->sock, SOL_SOCKET, SO_NOSIGPIPE, &optval, sizeof(optval)); -#endif - - for (int pipeNum = 0; pipeNum < 10; ++pipeNum) - { - snprintf( - PipeAddr.sun_path, sizeof(PipeAddr.sun_path), "%s/discord-ipc-%d", tempPath, pipeNum); - int err = connect(self->sock, (const sockaddr*)&PipeAddr, sizeof(PipeAddr)); - if (err == 0) - { - self->isOpen = true; - return true; - } - } - self->Close(); - return false; -} - -bool BaseConnection::Close() -{ - auto self = reinterpret_cast(this); - if (self->sock == -1) - return false; - close(self->sock); - self->sock = -1; - self->isOpen = false; - return true; -} - -bool BaseConnection::Write(const void* data, size_t length) -{ - ssize_t sentBytes; - auto self = reinterpret_cast(this); - - if (self->sock == -1) - return false; - - sentBytes = send(self->sock, data, length, MsgFlags); - if (sentBytes < 0) - Close(); - return sentBytes == (ssize_t)length; -} - -bool BaseConnection::Read(void* data, size_t length) -{ - int res; - auto self = reinterpret_cast(this); - - if (self->sock == -1) - return false; - - res = (int)recv(self->sock, data, length, MsgFlags); - if (res < 0) - { - if (errno == EAGAIN) - return false; - Close(); - } - return res == (int)length; -} diff --git a/deps/discord-rpc/src/connection_win.cpp b/deps/discord-rpc/src/connection_win.cpp deleted file mode 100644 index 63adf999338..00000000000 --- a/deps/discord-rpc/src/connection_win.cpp +++ /dev/null @@ -1,119 +0,0 @@ -#include "connection.h" - -#define WIN32_LEAN_AND_MEAN -#define NOMCX -#define NOSERVICE -#define NOIME -#include - -struct BaseConnectionWin : public BaseConnection -{ - HANDLE pipe{INVALID_HANDLE_VALUE}; -}; - -static BaseConnectionWin Connection; - -/*static*/ BaseConnection* BaseConnection::Create() -{ - return &Connection; -} - -/*static*/ void BaseConnection::Destroy(BaseConnection*& c) -{ - auto self = reinterpret_cast(c); - self->Close(); - c = nullptr; -} - -bool BaseConnection::Open() -{ - wchar_t pipeName[]{L"\\\\?\\pipe\\discord-ipc-0"}; - const size_t pipeDigit = sizeof(pipeName) / sizeof(wchar_t) - 2; - pipeName[pipeDigit] = L'0'; - auto self = reinterpret_cast(this); - for (;;) - { - self->pipe = ::CreateFileW( - pipeName, GENERIC_READ | GENERIC_WRITE, 0, - nullptr, OPEN_EXISTING, 0, nullptr); - if (self->pipe != INVALID_HANDLE_VALUE) - { - self->isOpen = true; - return true; - } - - DWORD lastError = GetLastError(); - if (lastError == ERROR_FILE_NOT_FOUND) - { - if (pipeName[pipeDigit] < L'9') - { - pipeName[pipeDigit]++; - continue; - } - } - else if (lastError == ERROR_PIPE_BUSY) - { - if (!WaitNamedPipeW(pipeName, 10000)) - return false; - continue; - } - return false; - } -} - -bool BaseConnection::Close() -{ - auto self = reinterpret_cast(this); - ::CloseHandle(self->pipe); - self->pipe = INVALID_HANDLE_VALUE; - self->isOpen = false; - return true; -} - -bool BaseConnection::Write(const void* data, size_t length) -{ - DWORD bytesWritten = 0; - const DWORD bytesLength = (DWORD)length; - if (length == 0) - return true; - auto self = reinterpret_cast(this); - if (!self) - return false; - if (self->pipe == INVALID_HANDLE_VALUE) - return false; - if (!data) - return false; - return ::WriteFile(self->pipe, data, bytesLength, - &bytesWritten, nullptr) == TRUE && - bytesWritten == bytesLength; -} - -bool BaseConnection::Read(void* data, size_t length) -{ - DWORD bytesAvailable = 0; - if (!data) - return false; - auto self = reinterpret_cast(this); - if (!self) - return false; - if (self->pipe == INVALID_HANDLE_VALUE) - return false; - - if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, - &bytesAvailable, nullptr)) - { - if (bytesAvailable >= length) - { - DWORD bytesToRead = (DWORD)length; - DWORD bytesRead = 0; - if (::ReadFile(self->pipe, data, bytesToRead, - &bytesRead, nullptr) == TRUE) - return true; - - Close(); - } - } - else - Close(); - return false; -} diff --git a/deps/discord-rpc/src/discord_register_linux.c b/deps/discord-rpc/src/discord_register_linux.c deleted file mode 100644 index dbb52a512e9..00000000000 --- a/deps/discord-rpc/src/discord_register_linux.c +++ /dev/null @@ -1,118 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include - -int get_process_id(void) -{ - return getpid(); -} - -/* we want to register games so we can run them from - * Discord client as discord-:// */ -void Discord_Register(const char *applicationId, const char *command) -{ - size_t _len; - FILE* fp; - int fileLen; - char xdgMimeCommand[1024]; - char desktopFilename[256]; - char desktopFilePath[1024]; - char desktopFile[2048]; - /* Add a desktop file and update some MIME handlers - * so that xdg-open does the right thing. */ - char exePath[1024]; - const char* home = getenv("HOME"); - if (!home) - return; - - if (!command || !command[0]) - { - ssize_t size = readlink("/proc/self/exe", exePath, sizeof(exePath)); - if (size <= 0 || size >= (ssize_t)sizeof(exePath)) - return; - exePath[size] = '\0'; - command = exePath; - } - - { - const char* desktopFileFormat = "[Desktop Entry]\n" - "Name=Game %s\n" - "Exec=%s\n" /* note: it really wants that %u in there */ - "Type=Application\n" - "NoDisplay=true\n" - "Categories=Discord;Games;\n" - "MimeType=x-scheme-handler/discord-%s;\n"; - fileLen = snprintf( - desktopFile, sizeof(desktopFile), desktopFileFormat, applicationId, command, applicationId); - if (fileLen <= 0) - return; - } - - _len = strlcpy(desktopFilename, - "/discord-", - sizeof(desktopFilename)); - _len += strlcpy(desktopFilename + _len, - applicationId, - sizeof(desktopFilename) - _len); - _len += strlcpy(desktopFilename + _len, - ".desktop", - sizeof(desktopFilename) - _len); - - _len = strlcpy(desktopFilePath, - home, - sizeof(desktopFilePath)); - _len += strlcpy(desktopFilePath + _len, - "/.local", - sizeof(desktopFilePath) - _len); - if (!path_mkdir(desktopFilePath)) - return; - _len += strlcpy(desktopFilePath + _len, - "/share", - sizeof(desktopFilePath) - _len); - if (!path_mkdir(desktopFilePath)) - return; - _len += strlcpy(desktopFilePath + _len, - "/applications", - sizeof(desktopFilePath) - _len); - if (!path_mkdir(desktopFilePath)) - return; - _len += strlcpy(desktopFilePath + _len, - desktopFilename, - sizeof(desktopFilePath) - _len); - - if (!(fp = fopen(desktopFilePath, "w"))) - return; - - fwrite(desktopFile, 1, fileLen, fp); - fclose(fp); - - snprintf(xdgMimeCommand, - sizeof(xdgMimeCommand), - "xdg-mime default discord-%s.desktop x-scheme-handler/discord-%s", - applicationId, - applicationId); - if (system(xdgMimeCommand) < 0) - fprintf(stderr, "Failed to register mime handler\n"); -} - -void Discord_RegisterSteamGame(const char *applicationId, const char *steamId) -{ - char command[256]; - size_t _len = strlcpy(command, "xdg-open steam://rungameid/", sizeof(command)); - strlcpy(command + _len, steamId, sizeof(command) - _len); - Discord_Register(applicationId, command); -} diff --git a/deps/discord-rpc/src/discord_register_osx.m b/deps/discord-rpc/src/discord_register_osx.m deleted file mode 100644 index 7a87044d07f..00000000000 --- a/deps/discord-rpc/src/discord_register_osx.m +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include - -#import - -int get_process_id(void) -{ - return getpid(); -} - -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 - * to a file in the Discord config path, and when it is needed, Discord can try to load the file there, open - * the command therein (will pass to js's window.open, so requires a url-like thing) - */ - - /* Note: will not work for sandboxed apps */ - NSString *home = NSHomeDirectory(); - if (!home) - return; - - NSString *path = [[[[[[home stringByAppendingPathComponent:@"Library"] - stringByAppendingPathComponent:@"Application Support"] - stringByAppendingPathComponent:@"discord"] - stringByAppendingPathComponent:@"games"] - stringByAppendingPathComponent:[NSString stringWithUTF8String:applicationId]] - stringByAppendingPathExtension:@"json"]; - [[NSFileManager defaultManager] createDirectoryAtPath:[path stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]; - - NSString *jsonBuffer = [NSString stringWithFormat:@"{\"command\": \"%s\"}", command]; - [jsonBuffer writeToFile:path atomically:NO encoding:NSUTF8StringEncoding error:nil]; -} - -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]; - - 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; - } - - 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); -} - -void Discord_Register(const char* applicationId, const char* command) -{ - 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); -} diff --git a/deps/discord-rpc/src/discord_register_win.c b/deps/discord-rpc/src/discord_register_win.c deleted file mode 100644 index 345b74c51e6..00000000000 --- a/deps/discord-rpc/src/discord_register_win.c +++ /dev/null @@ -1,195 +0,0 @@ -#include "discord_rpc.h" - -#define WIN32_LEAN_AND_MEAN -#define NOMCX -#define NOSERVICE -#define NOIME -#include -#include -#include -#include - -int get_process_id(void) -{ - return (int)GetCurrentProcessId(); -} - -/** - * Updated fixes for MinGW and WinXP - * This block is written the way it does not involve changing the rest of the code - * Checked to be compiling - * 1) strsafe.h belongs to Windows SDK and cannot be added to MinGW - * #include guarded, functions redirected to substitutes - * 2) RegSetKeyValueW and LSTATUS are not declared in - * The entire function is rewritten - */ -#ifdef __MINGW32__ -/* strsafe.h fixes */ -static HRESULT StringCbPrintfW(LPWSTR pszDest, - size_t cbDest, LPCWSTR pszFormat, ...) -{ - HRESULT ret; - va_list va; - va_start(va, pszFormat); - cbDest /= 2; /* Size is divided by 2 to convert from bytes to wide characters - causes segfault */ - /* othervise */ - ret = vsnwprintf(pszDest, cbDest, pszFormat, va); - pszDest[cbDest - 1] = 0; /* Terminate the string in case a buffer overflow; -1 will be returned */ - va_end(va); - return ret; -} -#else -#include -#endif /* __MINGW32__ */ - -/* winreg.h fixes */ -#ifndef LSTATUS -#define LSTATUS LONG -#endif -#ifdef RegSetKeyValueW -#undefine RegSetKeyValueW -#endif -#define RegSetKeyValueW regset -static LSTATUS regset(HKEY hkey, - LPCWSTR subkey, - LPCWSTR name, - DWORD type, - const void* data, - DWORD len) -{ - 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; -} - -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. */ - - 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); - - 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 = - RegSetKeyValueW(key, NULL, NULL, REG_SZ, protocolDescription, len * sizeof(wchar_t)); - if (FAILED(result)) - fprintf(stderr, "Error writing description\n"); - - 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"); - - 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 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; - } - - Discord_RegisterW(appId, wcommand); -} - -void Discord_RegisterSteamGame( - const char* applicationId, - const char* steamId) -{ - DWORD pathChars, pathBytes, i; - HKEY key; - wchar_t steamPath[MAX_PATH]; - wchar_t command[1024]; - wchar_t appId[32]; - wchar_t wSteamId[32]; - MultiByteToWideChar(CP_UTF8, 0, applicationId, -1, appId, 32); - MultiByteToWideChar(CP_UTF8, 0, steamId, -1, wSteamId, 32); - LSTATUS status = RegOpenKeyExW(HKEY_CURRENT_USER, - L"Software\\Valve\\Steam", 0, KEY_READ, &key); - if (status != ERROR_SUCCESS) - { - fprintf(stderr, "Error opening Steam key\n"); - return; - } - - pathBytes = sizeof(steamPath); - status = RegQueryValueExW(key, - L"SteamExe", NULL, NULL, (BYTE*)steamPath, &pathBytes); - RegCloseKey(key); - if (status != ERROR_SUCCESS || pathBytes < 1) - { - fprintf(stderr, "Error reading SteamExe key\n"); - return; - } - - pathChars = pathBytes / sizeof(wchar_t); - for (i = 0; i < pathChars; ++i) - { - if (steamPath[i] == L'/') - steamPath[i] = L'\\'; - } - - StringCbPrintfW(command, sizeof(command), - L"\"%s\" steam://rungameid/%s", steamPath, wSteamId); - - Discord_RegisterW(appId, command); -} diff --git a/deps/discord-rpc/src/discord_rpc.cpp b/deps/discord-rpc/src/discord_rpc.cpp deleted file mode 100644 index afe56762793..00000000000 --- a/deps/discord-rpc/src/discord_rpc.cpp +++ /dev/null @@ -1,578 +0,0 @@ -#include "discord_rpc.h" -#include "discord_register.h" - -#include "backoff.h" -#include "msg_queue.h" -#include "rpc_connection.h" -#include "serialization.h" - -#include -#include -#include - -#ifndef DISCORD_DISABLE_IO_THREAD -#include -#include -#endif - -struct QueuedMessage -{ - size_t length; - char buffer[16384]; - - void Copy(const QueuedMessage& other) - { - length = other.length; - if (length) - memcpy(buffer, other.buffer, length); - } -}; - -struct User -{ - /* snowflake (64bit int), turned into a ASCII decimal string, - * at most 20 chars +1 null - * terminator = 21 */ - char userId[32]; - /* 32 unicode glyphs is max name size => 4 bytes per glyph - * in the worst case, +1 for null - * terminator = 129 */ - char username[344]; - /* 4 decimal digits + 1 null terminator = 5 */ - char discriminator[8]; - /* optional 'a_' + md5 hex digest (32 bytes) + null terminator = 35 */ - char avatar[128]; - /* Rounded way up because I'm paranoid about games breaking - * from future changes in these sizes */ -}; - -static int Pid{0}; -static int Nonce{1}; -static int LastErrorCode{0}; -static int LastDisconnectErrorCode{0}; - -static char JoinGameSecret[256]; -static char SpectateGameSecret[256]; -static char LastErrorMessage[256]; -static char LastDisconnectErrorMessage[256]; - -static RpcConnection* Connection{nullptr}; - -static DiscordEventHandlers QueuedHandlers{}; -static DiscordEventHandlers Handlers{}; - -static std::atomic_bool WasJustConnected{false}; -static std::atomic_bool WasJustDisconnected{false}; -static std::atomic_bool GotErrorMessage{false}; -static std::atomic_bool WasJoinGame{false}; -static std::atomic_bool WasSpectateGame{false}; - -static std::mutex PresenceMutex; -static std::mutex HandlerMutex; -static QueuedMessage QueuedPresence{}; -static MsgQueue SendQueue; -static MsgQueue JoinAskQueue; -static User connectedUser; - -/* We want to auto connect, and retry on failure, - * but not as fast as possible. This does exponential - * backoff from 0.5 seconds to 1 minute */ -static Backoff ReconnectTimeMs(500, 60 * 1000); -static auto NextConnect = std::chrono::system_clock::now(); - -#ifndef DISCORD_DISABLE_IO_THREAD -static void Discord_UpdateConnection(void); -class IoThreadHolder -{ - private: - std::atomic_bool keepRunning{true}; - std::mutex waitForIOMutex; - std::condition_variable waitForIOActivity; - std::thread ioThread; - - public: - void Start() - { - keepRunning.store(true); - ioThread = std::thread([&]() { - const std::chrono::duration maxWait{500LL}; - Discord_UpdateConnection(); - while (keepRunning.load()) { - std::unique_lock lock(waitForIOMutex); - waitForIOActivity.wait_for(lock, maxWait); - Discord_UpdateConnection(); - } - }); - } - - void Notify() { waitForIOActivity.notify_all(); } - - void Stop() - { - keepRunning.exchange(false); - Notify(); - if (ioThread.joinable()) - ioThread.join(); - } - - ~IoThreadHolder() { Stop(); } -}; -#else -class IoThreadHolder -{ - public: - void Start() {} - void Stop() {} - void Notify() {} -}; -#endif /* DISCORD_DISABLE_IO_THREAD */ - -static IoThreadHolder* IoThread{nullptr}; - -static void UpdateReconnectTime(void) -{ - NextConnect = std::chrono::system_clock::now() + - std::chrono::duration{ReconnectTimeMs.nextDelay()}; -} - -#ifdef DISCORD_DISABLE_IO_THREAD -extern "C" void Discord_UpdateConnection(void) -#else -static void Discord_UpdateConnection(void) -#endif -{ - if (!Connection) - return; - - if (!Connection->IsOpen()) - { - if (std::chrono::system_clock::now() >= NextConnect) - { - UpdateReconnectTime(); - Connection->Open(); - } - } - else - { - /* reads */ - - for (;;) - { - JsonDocument message; - - if (!Connection->Read(message)) - break; - - char *evtName = NULL; - char *nonce = NULL; - char *secret = NULL; - char *userId = NULL; - char *username = NULL; - char *avatar = NULL; - char *discriminator = NULL; - char *error_message = NULL; - int error_code = 0; - - bool in_data = false, in_user = false; - for (JsonReader r(message); r.NextKey();) - { - if (r.depth == 1) - { - in_data = in_user = false; - if (!strcmp(r.key, "evt" )) r.NextStrDup(&evtName); - else if (!strcmp(r.key, "nonce")) r.NextStrDup(&nonce); - else if (!strcmp(r.key, "data" )) in_data = true; - } - else if (r.depth == 2 && in_data) - { - in_user = false; - if (!strcmp(r.key, "code" )) r.NextInt(&error_code); - else if (!strcmp(r.key, "message")) r.NextStrDup(&error_message); - else if (!strcmp(r.key, "secret" )) r.NextStrDup(&secret); - else if (!strcmp(r.key, "secret" )) r.NextStrDup(&secret); - else if (!strcmp(r.key, "user" )) in_user = true; - } - else if (r.depth == 3 && in_user) - { - if (!strcmp(r.key, "id" )) r.NextStrDup(&userId); - else if (!strcmp(r.key, "username" )) r.NextStrDup(&username); - else if (!strcmp(r.key, "avatar" )) r.NextStrDup(&avatar); - else if (!strcmp(r.key, "discriminator")) r.NextStrDup(&discriminator); - } - } - - if (nonce) - { - /* in responses only -- - * should use to match up response when needed. */ - - if (evtName && !strcmp(evtName, "ERROR")) - { - LastErrorCode = error_code; - StringCopy(LastErrorMessage, error_message); - GotErrorMessage.store(true); - } - } - else - { - /* should have evt == name of event, optional data */ - if (!evtName) - continue; - - if (!strcmp(evtName, "ACTIVITY_JOIN")) - { - if (secret) - { - StringCopy(JoinGameSecret, secret); - WasJoinGame.store(true); - } - } - else if (!strcmp(evtName, "ACTIVITY_SPECTATE")) - { - if (secret) - { - StringCopy(SpectateGameSecret, secret); - WasSpectateGame.store(true); - } - } - else if (!strcmp(evtName, "ACTIVITY_JOIN_REQUEST")) - { - auto joinReq = JoinAskQueue.GetNextAddMessage(); - - if (userId && username && joinReq) - { - StringCopy(joinReq->userId, userId); - StringCopy(joinReq->username, username); - if (discriminator) - StringCopy(joinReq->discriminator, discriminator); - if (avatar) - StringCopy(joinReq->avatar, avatar); - else - joinReq->avatar[0] = 0; - JoinAskQueue.CommitAdd(); - } - } - } - - if (evtName ) free(evtName ); - if (nonce ) free(nonce ); - if (secret ) free(secret ); - if (userId ) free(userId ); - if (username ) free(username ); - if (avatar ) free(avatar ); - if (discriminator) free(discriminator); - if (error_message) free(error_message); - } - - /* writes */ - if (QueuedPresence.length) - { - QueuedMessage local; - { - std::lock_guard guard(PresenceMutex); - local.Copy(QueuedPresence); - QueuedPresence.length = 0; - } - if (!Connection->Write(local.buffer, local.length)) - { - /* if we fail to send, requeue */ - std::lock_guard guard(PresenceMutex); - QueuedPresence.Copy(local); - } - } - - while (SendQueue.HavePendingSends()) - { - auto qmessage = SendQueue.GetNextSendMessage(); - Connection->Write(qmessage->buffer, qmessage->length); - SendQueue.CommitSend(); - } - } -} - -static bool RegisterForEvent(const char* evtName) -{ - auto qmessage = SendQueue.GetNextAddMessage(); - if (qmessage) - { - qmessage->length = - JsonWriteSubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName); - SendQueue.CommitAdd(); - if (IoThread) - IoThread->Notify(); - return true; - } - return false; -} - -static bool DeregisterForEvent(const char* evtName) -{ - auto qmessage = SendQueue.GetNextAddMessage(); - if (qmessage) - { - qmessage->length = - JsonWriteUnsubscribeCommand(qmessage->buffer, sizeof(qmessage->buffer), Nonce++, evtName); - SendQueue.CommitAdd(); - if (IoThread) - IoThread->Notify(); - return true; - } - return false; -} - -extern "C" void Discord_Initialize( - const char* applicationId, - DiscordEventHandlers* handlers, - int autoRegister, - const char* optionalSteamId) -{ - IoThread = new (std::nothrow) IoThreadHolder(); - if (!IoThread) - return; - - if (autoRegister) - { - if (optionalSteamId && optionalSteamId[0]) - Discord_RegisterSteamGame(applicationId, optionalSteamId); - else - Discord_Register(applicationId, nullptr); - } - - Pid = get_process_id(); - - { - std::lock_guard guard(HandlerMutex); - - if (handlers) - QueuedHandlers = *handlers; - else - QueuedHandlers = {}; - - Handlers = {}; - } - - if (Connection) - return; - - Connection = RpcConnection::Create(applicationId); - Connection->onConnect = [](JsonDocument& readyMessage) - { - Discord_UpdateHandlers(&QueuedHandlers); - char *userId = NULL; - char *username = NULL; - char *avatar = NULL; - char *discriminator = NULL; - - bool in_data = false, in_user = false; - for (JsonReader r(readyMessage); r.NextKey();) - { - if (r.depth == 1) - { - in_data = !strcmp(r.key,"data"); - in_user = false; - } - else if (r.depth == 2 && in_data) - { - in_user = !strcmp(r.key, "user"); - } - else if (r.depth == 3 && in_user) - { - if (!strcmp(r.key, "id" )) r.NextStrDup(&userId); - else if (!strcmp(r.key, "username" )) r.NextStrDup(&username); - else if (!strcmp(r.key, "avatar" )) r.NextStrDup(&avatar); - else if (!strcmp(r.key, "discriminator")) r.NextStrDup(&discriminator); - } - } - - if (userId && username) - { - StringCopy(connectedUser.userId, userId); - StringCopy(connectedUser.username, username); - if (discriminator) - StringCopy(connectedUser.discriminator, discriminator); - if (avatar) - StringCopy(connectedUser.avatar, avatar); - else - connectedUser.avatar[0] = 0; - } - WasJustConnected.exchange(true); - ReconnectTimeMs.reset(); - - if (userId ) free(userId ); - if (username ) free(username ); - if (avatar ) free(avatar ); - if (discriminator) free(discriminator); - }; - Connection->onDisconnect = [](int err, const char* message) - { - LastDisconnectErrorCode = err; - StringCopy(LastDisconnectErrorMessage, message); - { - std::lock_guard guard(HandlerMutex); - Handlers = {}; - } - WasJustDisconnected.exchange(true); - UpdateReconnectTime(); - }; - - IoThread->Start(); -} - -extern "C" void Discord_Shutdown(void) -{ - if (!Connection) - return; - Connection->onConnect = nullptr; - Connection->onDisconnect = nullptr; - Handlers = {}; - if (IoThread) - { - IoThread->Stop(); - delete IoThread; - IoThread = nullptr; - } - - RpcConnection::Destroy(Connection); -} - -extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) -{ - { - std::lock_guard guard(PresenceMutex); - QueuedPresence.length = JsonWriteRichPresenceObj( - QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence); - } - if (IoThread) - IoThread->Notify(); -} - -extern "C" void Discord_ClearPresence(void) -{ - Discord_UpdatePresence(nullptr); -} - -extern "C" void Discord_Respond(const char* userId, /* DISCORD_REPLY_ */ int reply) -{ - /* if we are not connected, let's not batch up stale messages for later */ - if (!Connection || !Connection->IsOpen()) - return; - auto qmessage = SendQueue.GetNextAddMessage(); - if (qmessage) - { - qmessage->length = - JsonWriteJoinReply(qmessage->buffer, sizeof(qmessage->buffer), userId, reply, Nonce++); - SendQueue.CommitAdd(); - if (IoThread) - IoThread->Notify(); - } -} - -extern "C" void Discord_RunCallbacks(void) -{ - /* Note on some weirdness: internally we might connect, get other signals, disconnect any number - * of times inbetween calls here. Externally, we want the sequence to seem sane, so any other - * signals are book-ended by calls to ready and disconnect. - */ - - if (!Connection) - return; - - bool wasDisconnected = WasJustDisconnected.exchange(false); - bool isConnected = Connection->IsOpen(); - - if (isConnected) - { - /* if we are connected, disconnect cb first */ - std::lock_guard guard(HandlerMutex); - if (wasDisconnected && Handlers.disconnected) - Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage); - } - - if (WasJustConnected.exchange(false)) - { - std::lock_guard guard(HandlerMutex); - if (Handlers.ready) - { - DiscordUser du{connectedUser.userId, - connectedUser.username, - connectedUser.discriminator, - connectedUser.avatar}; - Handlers.ready(&du); - } - } - - if (GotErrorMessage.exchange(false)) - { - std::lock_guard guard(HandlerMutex); - if (Handlers.errored) - Handlers.errored(LastErrorCode, LastErrorMessage); - } - - if (WasJoinGame.exchange(false)) - { - std::lock_guard guard(HandlerMutex); - if (Handlers.joinGame) - Handlers.joinGame(JoinGameSecret); - } - - if (WasSpectateGame.exchange(false)) - { - std::lock_guard guard(HandlerMutex); - if (Handlers.spectateGame) - Handlers.spectateGame(SpectateGameSecret); - } - - /* Right now this batches up any requests and sends them all in a burst; I could imagine a world - * where the implementer would rather sequentially accept/reject each one before the next invite - * is sent. I left it this way because I could also imagine wanting to process these all and - * maybe show them in one common dialog and/or start fetching the avatars in parallel, and if - * not it should be trivial for the implementer to make a queue themselves. - */ - while (JoinAskQueue.HavePendingSends()) - { - auto req = JoinAskQueue.GetNextSendMessage(); - { - std::lock_guard guard(HandlerMutex); - if (Handlers.joinRequest) - { - DiscordUser du{req->userId, req->username, req->discriminator, req->avatar}; - Handlers.joinRequest(&du); - } - } - JoinAskQueue.CommitSend(); - } - - if (!isConnected) - { - /* if we are not connected, disconnect message last */ - std::lock_guard guard(HandlerMutex); - if (wasDisconnected && Handlers.disconnected) - Handlers.disconnected(LastDisconnectErrorCode, LastDisconnectErrorMessage); - } -} - -#ifndef HANDLE_EVENT_REGISTRATION -#define HANDLE_EVENT_REGISTRATION(handler_name, event) \ - if (!Handlers.handler_name && newHandlers->handler_name) \ - RegisterForEvent(event); \ - else if (Handlers.handler_name && !newHandlers->handler_name) \ - DeregisterForEvent(event) -#endif - -extern "C" void Discord_UpdateHandlers(DiscordEventHandlers* newHandlers) -{ - if (newHandlers) - { - std::lock_guard guard(HandlerMutex); - HANDLE_EVENT_REGISTRATION(joinGame, "ACTIVITY_JOIN"); - HANDLE_EVENT_REGISTRATION(spectateGame, "ACTIVITY_SPECTATE"); - HANDLE_EVENT_REGISTRATION(joinRequest, "ACTIVITY_JOIN_REQUEST"); - - Handlers = *newHandlers; - } - else - { - std::lock_guard guard(HandlerMutex); - Handlers = {}; - } -} diff --git a/deps/discord-rpc/src/msg_queue.h b/deps/discord-rpc/src/msg_queue.h deleted file mode 100644 index f8df267a2af..00000000000 --- a/deps/discord-rpc/src/msg_queue.h +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once - -#include - -/* A simple queue. No locks, but only works with a single thread as producer and a single thread as - * a consumer. Mutex up as needed. */ - -template -class MsgQueue { - ElementType queue_[QueueSize]; - std::atomic_uint nextAdd_{0}; - std::atomic_uint nextSend_{0}; - std::atomic_uint pendingSends_{0}; - -public: - MsgQueue() {} - - ElementType* GetNextAddMessage() - { - /* if we are falling behind, bail */ - if (pendingSends_.load() >= QueueSize) - return nullptr; - auto index = (nextAdd_++) % QueueSize; - return &queue_[index]; - } - void CommitAdd() { ++pendingSends_; } - - bool HavePendingSends() const { return pendingSends_.load() != 0; } - ElementType* GetNextSendMessage() - { - auto index = (nextSend_++) % QueueSize; - return &queue_[index]; - } - void CommitSend() { --pendingSends_; } -}; diff --git a/deps/discord-rpc/src/rpc_connection.cpp b/deps/discord-rpc/src/rpc_connection.cpp deleted file mode 100644 index bb20023d08f..00000000000 --- a/deps/discord-rpc/src/rpc_connection.cpp +++ /dev/null @@ -1,164 +0,0 @@ -#include "rpc_connection.h" -#include "serialization.h" - -#include - -#define RPC_VERSION 1 - -static RpcConnection Instance; - -/*static*/ RpcConnection* RpcConnection::Create(const char* applicationId) -{ - Instance.connection = BaseConnection::Create(); - StringCopy(Instance.appId, applicationId); - return &Instance; -} - -/*static*/ void RpcConnection::Destroy(RpcConnection*& c) -{ - c->Close(); - BaseConnection::Destroy(c->connection); - c = nullptr; -} - -void RpcConnection::Open() -{ - if (state == State::Connected) - return; - - if (state == State::Disconnected) - { - if (!connection->Open()) - return; - } - - if (state == State::SentHandshake) - { - JsonDocument message; - if (Read(message)) - { - bool cmdDispatch = false; - bool evtReady = false; - - for (JsonReader r(message); r.NextKey();) - { - if (r.depth == 1 && !strcmp(r.key, "cmd")) - cmdDispatch = !strcmp(r.NextString(""), "DISPATCH"); - else if (r.depth == 1 && !strcmp(r.key, "evt")) - evtReady = !strcmp(r.NextString(""), "READY"); - } - - if (cmdDispatch && evtReady) - { - state = State::Connected; - if (onConnect) - onConnect(message); - } - } - } - else - { - sendFrame.opcode = Opcode::Handshake; - sendFrame.length = (uint32_t)JsonWriteHandshakeObj( - sendFrame.message, sizeof(sendFrame.message), RPC_VERSION, appId); - - if (connection->Write(&sendFrame, - sizeof(MessageFrameHeader) + sendFrame.length)) - state = State::SentHandshake; - else - Close(); - } -} - -void RpcConnection::Close() -{ - if (onDisconnect && (state == State::Connected || state == State::SentHandshake)) - onDisconnect(lastErrorCode, lastErrorMessage); - connection->Close(); - state = State::Disconnected; -} - -bool RpcConnection::Write(const void* data, size_t length) -{ - sendFrame.opcode = Opcode::Frame; - memcpy(sendFrame.message, data, length); - sendFrame.length = (uint32_t)length; - if (!connection->Write(&sendFrame, sizeof(MessageFrameHeader) + length)) - { - Close(); - return false; - } - return true; -} - -bool RpcConnection::Read(JsonDocument& message) -{ - MessageFrame readFrame; - - if (state != State::Connected && state != State::SentHandshake) - return false; - - for (;;) - { - bool didRead = connection->Read( - &readFrame, sizeof(MessageFrameHeader)); - - if (!didRead) - { - if (!connection->isOpen) - { - lastErrorCode = (int)ErrorCode::PipeClosed; - StringCopy(lastErrorMessage, "Pipe closed"); - Close(); - } - return false; - } - - if (readFrame.length > 0) - { - didRead = connection->Read(readFrame.message, readFrame.length); - if (!didRead) - { - lastErrorCode = (int)ErrorCode::ReadCorrupt; - StringCopy(lastErrorMessage, "Partial data in frame"); - Close(); - return false; - } - readFrame.message[readFrame.length] = 0; - } - - switch (readFrame.opcode) - { - case Opcode::Close: - message.ParseInsitu(readFrame.message); - lastErrorCode = 0; - lastErrorMessage[0] = '\0'; - for (JsonReader r(message); r.NextKey();) - { - if (r.depth == 1 && !strcmp(r.key, "code")) - r.NextInt(&lastErrorCode); - else if (r.depth == 1 && !strcmp(r.key, "message")) - StringCopy(lastErrorMessage, r.NextString("")); - } - Close(); - return false; - case Opcode::Frame: - message.ParseInsitu(readFrame.message); - return true; - case Opcode::Ping: - readFrame.opcode = Opcode::Pong; - if (!connection->Write(&readFrame, sizeof(MessageFrameHeader) + readFrame.length)) - Close(); - break; - case Opcode::Pong: - break; - case Opcode::Handshake: - default: - /* something bad happened */ - lastErrorCode = (int)ErrorCode::ReadCorrupt; - StringCopy(lastErrorMessage, "Bad ipc frame"); - Close(); - return false; - } - } -} diff --git a/deps/discord-rpc/src/rpc_connection.h b/deps/discord-rpc/src/rpc_connection.h deleted file mode 100644 index 6fb69c9ee0d..00000000000 --- a/deps/discord-rpc/src/rpc_connection.h +++ /dev/null @@ -1,65 +0,0 @@ -#pragma once - -#include "connection.h" -#include "serialization.h" - -/* I took this from the buffer size libuv uses for named pipes; - * I suspect ours would usually be much smaller. */ -#define MAX_RPC_FRAMESIZE 65536 - -struct RpcConnection -{ - enum class ErrorCode : int - { - Success = 0, - PipeClosed = 1, - ReadCorrupt = 2, - }; - - enum class Opcode : uint32_t - { - Handshake = 0, - Frame = 1, - Close = 2, - Ping = 3, - Pong = 4, - }; - - struct MessageFrameHeader - { - Opcode opcode; - uint32_t length; - }; - - struct MessageFrame : public MessageFrameHeader - { - char message[MAX_RPC_FRAMESIZE - sizeof(MessageFrameHeader)]; - }; - - enum class State : uint32_t - { - Disconnected, - SentHandshake, - AwaitingResponse, - Connected, - }; - - BaseConnection* connection{nullptr}; - State state{State::Disconnected}; - void (*onConnect)(JsonDocument& message){nullptr}; - void (*onDisconnect)(int errorCode, const char* message){nullptr}; - char appId[64]{}; - int lastErrorCode{0}; - char lastErrorMessage[256]{}; - RpcConnection::MessageFrame sendFrame; - - static RpcConnection* Create(const char* applicationId); - static void Destroy(RpcConnection*&); - - inline bool IsOpen() const { return state == State::Connected; } - - void Open(); - void Close(); - bool Write(const void* data, size_t length); - bool Read(JsonDocument& message); -}; diff --git a/deps/discord-rpc/src/serialization.cpp b/deps/discord-rpc/src/serialization.cpp deleted file mode 100644 index d0ce45f4402..00000000000 --- a/deps/discord-rpc/src/serialization.cpp +++ /dev/null @@ -1,248 +0,0 @@ -#include "serialization.h" -#include "connection.h" -#include "discord_rpc.h" - -template -void NumberToString(char* dest, T number) -{ - char temp[32]; - int place = 0; - - if (!number) - { - *dest++ = '0'; - *dest++ = 0; - return; - } - - if (number < 0) - { - *dest++ = '-'; - number = -number; - } - - while (number) - { - auto digit = number % 10; - number = number / 10; - temp[place++] = '0' + (char)digit; - } - for (--place; place >= 0; --place) - *dest++ = temp[place]; - *dest = 0; -} - -struct WriteObject { - JsonWriter& writer; - WriteObject(JsonWriter& w) - : writer(w) - { - writer.StartObject(); - } - WriteObject(JsonWriter& w, const char* name) - : writer(w) - { - writer.Key(name); - writer.StartObject(); - } - ~WriteObject() { writer.EndObject(); } -}; - -struct WriteArray { - JsonWriter& writer; - WriteArray(JsonWriter& w, const char* name) - : writer(w) - { - writer.Key(name); - writer.StartArray(); - } - ~WriteArray() { writer.EndArray(); } -}; - -void WriteOptionalString(JsonWriter& w, const char* k, const char* value) -{ - if (value && value[0]) - { - w.Key(k); - w.String(value); - } -} - -static void JsonWriteNonce(JsonWriter& writer, int nonce) -{ - char nonceBuffer[32]; - writer.Key("nonce"); - NumberToString(nonceBuffer, nonce); - writer.String(nonceBuffer); -} - -size_t JsonWriteRichPresenceObj(char* dest, - size_t maxLen, - int nonce, - int pid, - const DiscordRichPresence* presence) -{ - JsonWriter writer(dest, maxLen); - - { - WriteObject top(writer); - - JsonWriteNonce(writer, nonce); - - writer.Key("cmd"); - writer.String("SET_ACTIVITY"); - - { - WriteObject args(writer, "args"); - - writer.Key("pid"); - writer.Int(pid); - - if (presence) - { - WriteObject activity(writer, "activity"); - - WriteOptionalString(writer, "state", presence->state); - WriteOptionalString(writer, "details", presence->details); - - if (presence->startTimestamp || presence->endTimestamp) - { - WriteObject timestamps(writer, "timestamps"); - - if (presence->startTimestamp) - { - writer.Key("start"); - writer.Int64(presence->startTimestamp); - } - - if (presence->endTimestamp) - { - writer.Key("end"); - writer.Int64(presence->endTimestamp); - } - } - - if ((presence->largeImageKey && presence->largeImageKey[0]) || - (presence->largeImageText && presence->largeImageText[0]) || - (presence->smallImageKey && presence->smallImageKey[0]) || - (presence->smallImageText && presence->smallImageText[0])) { - WriteObject assets(writer, "assets"); - WriteOptionalString(writer, "large_image", presence->largeImageKey); - WriteOptionalString(writer, "large_text", presence->largeImageText); - WriteOptionalString(writer, "small_image", presence->smallImageKey); - WriteOptionalString(writer, "small_text", presence->smallImageText); - } - - if (( - presence->partyId && presence->partyId[0]) || - presence->partySize || - presence->partyMax) - { - WriteObject party(writer, "party"); - WriteOptionalString(writer, "id", presence->partyId); - if (presence->partySize && presence->partyMax) - { - WriteArray size(writer, "size"); - writer.Int(presence->partySize); - writer.Int(presence->partyMax); - } - } - - if ((presence->matchSecret && presence->matchSecret[0]) || - (presence->joinSecret && presence->joinSecret[0]) || - (presence->spectateSecret && presence->spectateSecret[0])) - { - WriteObject secrets(writer, "secrets"); - WriteOptionalString(writer, "match", presence->matchSecret); - WriteOptionalString(writer, "join", presence->joinSecret); - WriteOptionalString(writer, "spectate", presence->spectateSecret); - } - - writer.Key("instance"); - writer.Bool(presence->instance != 0); - } - } - } - - return writer.Size(); -} - -size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId) -{ - JsonWriter writer(dest, maxLen); - - { - WriteObject obj(writer); - writer.Key("v"); - writer.Int(version); - writer.Key("client_id"); - writer.String(applicationId); - } - - return writer.Size(); -} - -size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName) -{ - JsonWriter writer(dest, maxLen); - - { - WriteObject obj(writer); - - JsonWriteNonce(writer, nonce); - - writer.Key("cmd"); - writer.String("SUBSCRIBE"); - - writer.Key("evt"); - writer.String(evtName); - } - - return writer.Size(); -} - -size_t JsonWriteUnsubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName) -{ - JsonWriter writer(dest, maxLen); - - { - WriteObject obj(writer); - - JsonWriteNonce(writer, nonce); - - writer.Key("cmd"); - writer.String("UNSUBSCRIBE"); - - writer.Key("evt"); - writer.String(evtName); - } - - return writer.Size(); -} - -size_t JsonWriteJoinReply(char* dest, size_t maxLen, const char* userId, int reply, int nonce) -{ - JsonWriter writer(dest, maxLen); - - { - WriteObject obj(writer); - - writer.Key("cmd"); - if (reply == DISCORD_REPLY_YES) - writer.String("SEND_ACTIVITY_JOIN_INVITE"); - else - writer.String("CLOSE_ACTIVITY_JOIN_REQUEST"); - - writer.Key("args"); - { - WriteObject args(writer); - - writer.Key("user_id"); - writer.String(userId); - } - - JsonWriteNonce(writer, nonce); - } - - return writer.Size(); -} diff --git a/deps/discord-rpc/src/serialization.h b/deps/discord-rpc/src/serialization.h deleted file mode 100644 index 57e08ab75bd..00000000000 --- a/deps/discord-rpc/src/serialization.h +++ /dev/null @@ -1,250 +0,0 @@ -#pragma once - -#ifdef _MSC_VER -/* avoid deprecated warning about strdup */ -#define _CRT_NONSTDC_NO_DEPRECATE -#endif /* _MSC_VER */ - -#include -#include /* memcpy/strlen/strcmp/strdup */ -#include /* malloc/realloc/free */ - -#include - -/* if only there was a standard library function for this */ -template -inline size_t StringCopy(char (&dest)[Len], const char* src) -{ - size_t copied; - char* out = dest; - if (!src || !Len) - return 0; - for (copied = 1; *src && copied < Len; ++copied) - *out++ = *src++; - *out = 0; - return copied - 1; -} - -size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, - int version, const char* applicationId); - -/* Commands */ -struct DiscordRichPresence; -size_t JsonWriteRichPresenceObj(char* dest, - size_t maxLen, - int nonce, - int pid, - const DiscordRichPresence* presence); -size_t JsonWriteSubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName); - -size_t JsonWriteUnsubscribeCommand(char* dest, size_t maxLen, int nonce, const char* evtName); - -size_t JsonWriteJoinReply(char* dest, size_t maxLen, const char* userId, int reply, int nonce); - -class JsonWriter -{ - char* buf; - size_t buf_len; - size_t buf_cap; - - rjsonwriter_t* writer; - bool need_comma; - - static int writer_io(const void* inbuf, int inlen, void *user_data) - { - JsonWriter* self = (JsonWriter*)user_data; - size_t buf_remain = (self->buf_cap - self->buf_len); - if ((size_t)inlen > buf_remain) - inlen = (int)buf_remain; - memcpy(self->buf + self->buf_len, inbuf, inlen); - self->buf_len += inlen; - self->buf[self->buf_len - (self->buf_len == self->buf_cap ? 1 : 0)] = '\0'; - return inlen; - } - - public: - JsonWriter(char* dest, size_t maxLen) - : buf(dest), buf_len(0), buf_cap(maxLen), - need_comma(false) - { - writer = rjsonwriter_open_user(writer_io, this); - } - - ~JsonWriter() - { - rjsonwriter_free(writer); - } - - size_t Size() - { - rjsonwriter_flush(writer); - return buf_len; - } - - void WriteComma() - { - if (!need_comma) - return; - rjsonwriter_raw(writer, ",", 1); - need_comma = false; - } - - void StartObject() - { - WriteComma(); - rjsonwriter_raw(writer, "{", 1); - } - - void StartArray() - { - WriteComma(); - rjsonwriter_raw(writer, "[", 1); - } - - void EndObject() - { - rjsonwriter_raw(writer, "}", 1); - need_comma = true; - } - - void EndArray() - { - rjsonwriter_raw(writer, "]", 1); - need_comma = true; - } - - void Key(const char* key) - { - WriteComma(); - rjsonwriter_add_string(writer, key); - rjsonwriter_raw(writer, ":", 1); - } - - void String(const char* val) - { - WriteComma(); - rjsonwriter_add_string(writer, val); - need_comma = true; - } - - void Int(int value) - { - WriteComma(); - rjsonwriter_rawf(writer, "%d", value); - need_comma = true; - } - - void Int64(int64_t val) - { - WriteComma(); - char num[24], *pEnd = num + 24, *p = pEnd; - if (!val) - { - *(--p) = '0'; - } - else if (val < 0) - { - for (; val; val /= 10) - *(--p) = '0' - (char)(val % 10); - *(--p) = '-'; - } - else - { - for (; val; val /= 10) - *(--p) = '0' + (char)(val % 10); - } - rjsonwriter_raw(writer, p, (int)(pEnd - p)); - need_comma = true; - } - - void Bool(bool value) - { - WriteComma(); - rjsonwriter_raw(writer, (value ? "true" : "false"), (value ? 4 : 5)); - need_comma = true; - } -}; - -class JsonDocument -{ - size_t json_cap; - enum { kDefaultChunkCapacity = 32 * 1024 }; - - public: - size_t json_length; - char* json_data; - - JsonDocument() - : json_cap(kDefaultChunkCapacity), json_length(0), - json_data((char*)malloc(kDefaultChunkCapacity)) - { } - - ~JsonDocument() - { - free(json_data); - } - - void ParseInsitu(const char* input) - { - size_t input_len = strlen(input); - while (json_length + input_len >= json_cap) - { - json_cap += kDefaultChunkCapacity; - json_data = (char*)realloc(json_data, json_cap); - } - memcpy(json_data + json_length, input, input_len); - json_length += input_len; - json_data[json_length] = '\0'; - } -}; - -class JsonReader -{ - rjson_t* reader; - - public: - const char* key; - unsigned int depth; - - JsonReader(JsonDocument& doc) - : reader(rjson_open_buffer(doc.json_data, doc.json_length)) - { } - - ~JsonReader() - { - rjson_free(reader); - } - - bool NextKey() - { - for (;;) - { - enum rjson_type json_type = rjson_next(reader); - if (json_type == RJSON_DONE || json_type == RJSON_ERROR) return false; - if (json_type != RJSON_STRING) continue; - if (rjson_get_context_type(reader) != RJSON_OBJECT) continue; - if ((rjson_get_context_count(reader) & 1) != 1) continue; - depth = rjson_get_context_depth(reader); - key = rjson_get_string(reader, NULL); - return true; - } - } - - const char* NextString(const char* default_val) - { - return (rjson_next(reader) != RJSON_STRING ? default_val : - rjson_get_string(reader, NULL)); - } - - void NextStrDup(char** val) - { - if (rjson_next(reader) == RJSON_STRING) - *val = strdup(rjson_get_string(reader, NULL)); - } - - void NextInt(int* val) - { - if (rjson_next(reader) == RJSON_NUMBER) - *val = rjson_get_int(reader); - } -}; diff --git a/griffin/griffin.c b/griffin/griffin.c index 2daf4a7f3f3..74cf47fda92 100644 --- a/griffin/griffin.c +++ b/griffin/griffin.c @@ -1584,12 +1584,6 @@ HTTP SERVER ============================================================ */ #if defined(HAVE_DISCORD) #include "../network/discord.c" -#if defined(_WIN32) -#include "../deps/discord-rpc/src/discord_register_win.c" -#endif -#if defined(__linux__) -#include "../deps/discord-rpc/src/discord_register_linux.c" -#endif #endif /*============================================================ diff --git a/griffin/griffin_cpp.cpp b/griffin/griffin_cpp.cpp index f7f882e7ffc..94566aeeb7a 100644 --- a/griffin/griffin_cpp.cpp +++ b/griffin/griffin_cpp.cpp @@ -91,20 +91,3 @@ VIDEO DRIVER #include "../deps/glslang/glslang/glslang/OSDependent/Unix/ossource.cpp" #endif #endif - -/*============================================================ -FONTS -============================================================ */ -#if defined(HAVE_DISCORD) -#include "../deps/discord-rpc/src/discord_rpc.cpp" -#include "../deps/discord-rpc/src/rpc_connection.cpp" -#include "../deps/discord-rpc/src/serialization.cpp" - -#if defined(_WIN32) -#include "../deps/discord-rpc/src/connection_win.cpp" -#endif -#if defined(__unix__) || defined(__APPLE__) -#include "../deps/discord-rpc/src/connection_unix.cpp" -#endif -#endif - diff --git a/griffin/griffin_objc.m b/griffin/griffin_objc.m index 30c8194a310..2375a3ad7c8 100644 --- a/griffin/griffin_objc.m +++ b/griffin/griffin_objc.m @@ -79,10 +79,6 @@ #include "../record/drivers/record_avfoundation.m" #endif -#if defined(HAVE_DISCORD) -#include "../deps/discord-rpc/src/discord_register_osx.m" -#endif - #ifdef HAVE_METAL #import "../gfx/common/metal/metal_renderer.m" #import "../gfx/drivers/metal.m" diff --git a/network/discord.c b/network/discord.c index 642b7c7ec21..a80a8463a0e 100644 --- a/network/discord.c +++ b/network/discord.c @@ -1,6 +1,6 @@ /* RetroArch - A frontend for libretro. * Copyright (C) 2011-2021 - Daniel De Matteis - * Copyright (C) 2016-2019 - Andr�s Su�rez + * Copyright (C) 2016-2019 - Andres Suarez * * RetroArch is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Found- @@ -14,13 +14,53 @@ * If not, see . */ +/* This file folds in the former deps/discord-rpc C++ library as pure C89. + * + * Design notes: + * - No background IO thread. The RetroArch runloop calls discord_run_callbacks() + * every frame, which drives reads/writes/reconnects on the main thread. All + * atomics/mutexes/condvars from the upstream library are therefore removed. + * - No auto-register. Discord_Register() is kept as an exported no-op stub so + * the existing init code path still links; the upstream per-OS desktop-file + * / registry logic is dropped. + * - Unix socket / Windows named-pipe backends are gated by #ifdef _WIN32. + * - JSON read/write use libretro-common's formats/rjson.h directly, without + * the C++ RAII wrappers (JsonWriter/JsonDocument/JsonReader). + */ + +#include +#include +#include +#include +#include +#include + +#include +#include #include +#include #include #include #include #include +#include #include