diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index 4487651..580bce5 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -12,6 +12,8 @@ #include #endif +#include "rapidjson/internal/itoa.h" + constexpr size_t MaxMessageSize = 16 * 1024; constexpr size_t MessageQueueSize = 8; @@ -33,6 +35,8 @@ static std::atomic_uint SendQueueNextSend{0}; static std::atomic_uint SendQueuePendingSends{0}; static Backoff ReconnectTimeMs(500, 60 * 1000); static auto NextConnect{std::chrono::system_clock::now()}; +static int Pid = 0; +static int Nonce = 1; #ifndef DISCORD_DISABLE_IO_THREAD static std::atomic_bool KeepRunning{ true }; @@ -110,6 +114,8 @@ void SignalIOActivity() extern "C" void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers) { + Pid = GetProcessId(); + if (handlers) { Handlers = *handlers; } @@ -153,7 +159,9 @@ extern "C" void Discord_UpdatePresence(const DiscordRichPresence* presence) { auto qmessage = SendQueueGetNextAddMessage(); if (qmessage) { - qmessage->length = JsonWriteRichPresenceObj(qmessage->buffer, sizeof(qmessage->buffer), presence); + char nonce[32]{}; + rapidjson::internal::i32toa(Nonce++, nonce); + qmessage->length = JsonWriteRichPresenceObj(qmessage->buffer, sizeof(qmessage->buffer), nonce, Pid, presence); SendQueueCommitMessage(); SignalIOActivity(); } diff --git a/src/serialization.cpp b/src/serialization.cpp index 6ff01b7..50bb3a2 100644 --- a/src/serialization.cpp +++ b/src/serialization.cpp @@ -93,19 +93,36 @@ void WriteOptionalString(JsonWriter& w, T& k, const char* value) { } } -size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, const DiscordRichPresence* presence) +void JsonWriteCommandStart(JsonWriter& writer, const char* nonce, const char* cmd) +{ + writer.StartObject(); + + WriteKey(writer, "nonce"); + writer.String(nonce); + + WriteKey(writer, "cmd"); + writer.String(cmd); + + WriteKey(writer, "args"); + writer.StartObject(); +} + +void JsonWriteCommandEnd(JsonWriter& writer) +{ + writer.EndObject(); // args + writer.EndObject(); // top level +} + +size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, char* nonce, int pid, const DiscordRichPresence* presence) { DirectStringBuffer sb(dest, maxLen); WriterAllocator wa; JsonWriter writer(sb, &wa, WriterNestingLevels); - // const args = {pid, activity}; - // this.socket.write(encode(OPCODES.FRAME, { nonce: uuid(), cmd : 'SET_ACTIVITY', args }) + JsonWriteCommandStart(writer, nonce, "SET_ACTIVITY"); - writer.StartObject(); - - WriteKey(writer, "args"); - writer.StartObject(); + WriteKey(writer, "pid"); + writer.Int(pid); WriteKey(writer, "activity"); writer.StartObject(); @@ -177,9 +194,7 @@ size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, const DiscordRichPres writer.EndObject(); // activity - writer.EndObject(); // args - - writer.EndObject(); // top level + JsonWriteCommandEnd(writer); return sb.GetSize(); } diff --git a/src/serialization.h b/src/serialization.h index 73d0bf9..59ce11b 100644 --- a/src/serialization.h +++ b/src/serialization.h @@ -17,7 +17,8 @@ inline size_t StringCopy(char (&dest)[Len], const char* src) { return copied - 1; } -struct DiscordRichPresence; - -size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, const DiscordRichPresence* presence); size_t JsonWriteHandshakeObj(char* dest, size_t maxLen, int version, const char* applicationId); + +struct DiscordRichPresence; +size_t JsonWriteRichPresenceObj(char* dest, size_t maxLen, char* nonce, int pid, const DiscordRichPresence* presence); +