From 22e34d48c9950931b90985eed6f0078d26c79a38 Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Thu, 29 Jun 2017 09:38:07 -0700 Subject: [PATCH] Clean up a little, add gitignore --- .gitignore | 1 + src/discord-rpc.cpp | 37 ++++++++++++++++++++++++------------- src/discord-rpc.h | 6 +++--- src/simple.cpp | 1 + 4 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0155cb --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build*/ diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index e5f09ab..2f35bc7 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -1,18 +1,29 @@ #include "discord-rpc.h" -#include #include -#include // todo: think about making per-platform versions of this whole file, or wrapping the platform specific parts -- win32 for first version + +// I just want the basics +#define WIN32_LEAN_AND_MEAN +#define NOMCX +#define NOSERVICE +#define NOIME #include +namespace { + +struct RpcMessageFrame { + uint32_t length; + char message[64 * 1024 - sizeof(uint32_t)]; +}; + static const wchar_t* PipeName = L"\\\\.\\pipe\\DiscordRpcServer"; -static HANDLE PipeHandle{INVALID_HANDLE_VALUE}; +static HANDLE PipeHandle{ INVALID_HANDLE_VALUE }; static char ApplicationId[64]{}; static DiscordEventHandlers Handlers{}; -static char MessageBuffer[64 * 1024]; +static RpcMessageFrame Frame; // if only there was a standard library function for this size_t StringCopy(char* dest, const char* src, size_t maxBytes) { @@ -52,19 +63,19 @@ void EscapeString(char*& dest, const char* src) *dest++ = '\\'; *dest++ = 'b'; break; - case '\f': // Form feed(ascii code 0C) + case '\f': *dest++ = '\\'; *dest++ = 'f'; break; - case '\n': // New line + case '\n': *dest++ = '\\'; *dest++ = 'n'; break; - case '\r': // Carriage return + case '\r': *dest++ = '\\'; *dest++ = 'r'; break; - case '\t': // Tab + case '\t': *dest++ = '\\'; *dest++ = 't'; break; @@ -239,6 +250,8 @@ void CloseConnection() } } +} // anonymous namespace + void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers) { StringCopy(ApplicationId, applicationId, sizeof(ApplicationId)); @@ -273,14 +286,12 @@ void Discord_UpdatePresence(const DiscordRichPresence* presence) } } - int32_t* totalLength = (int32_t*)MessageBuffer; - char* jsonStart = MessageBuffer + sizeof(int32_t); - char* jsonWrite = jsonStart; + char* jsonWrite = Frame.message; JsonWriteRichPresenceObj(jsonWrite, presence); - *totalLength = sizeof(int32_t) + (jsonWrite - jsonStart); - BOOL success = WriteFile(PipeHandle, MessageBuffer, *totalLength, nullptr, nullptr); + Frame.length = sizeof(uint32_t) + (jsonWrite - Frame.message); + BOOL success = WriteFile(PipeHandle, &Frame, Frame.length, nullptr, nullptr); if (!success) { CloseConnection(); diff --git a/src/discord-rpc.h b/src/discord-rpc.h index 19219c3..21dcff4 100644 --- a/src/discord-rpc.h +++ b/src/discord-rpc.h @@ -1,11 +1,11 @@ #pragma once -#include +#include struct DiscordRichPresence { const char* state; const char* details; - time_t startTimestamp; - time_t endTimestamp; + int64_t startTimestamp; + int64_t endTimestamp; const char* largeImageKey; const char* largeImageText; const char* smallImageKey; diff --git a/src/simple.cpp b/src/simple.cpp index 905c41a..676f359 100644 --- a/src/simple.cpp +++ b/src/simple.cpp @@ -1,4 +1,5 @@ #include +#include #include "discord-rpc.h"