Clean up a little, add gitignore
This commit is contained in:
parent
1a83c2872c
commit
22e34d48c9
4 changed files with 29 additions and 16 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/build*/
|
|
@ -1,18 +1,29 @@
|
||||||
#include "discord-rpc.h"
|
#include "discord-rpc.h"
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
// todo: think about making per-platform versions of this whole file, or wrapping the platform specific parts -- win32 for first version
|
// 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 <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
struct RpcMessageFrame {
|
||||||
|
uint32_t length;
|
||||||
|
char message[64 * 1024 - sizeof(uint32_t)];
|
||||||
|
};
|
||||||
|
|
||||||
static const wchar_t* PipeName = L"\\\\.\\pipe\\DiscordRpcServer";
|
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 char ApplicationId[64]{};
|
||||||
static DiscordEventHandlers Handlers{};
|
static DiscordEventHandlers Handlers{};
|
||||||
static char MessageBuffer[64 * 1024];
|
static RpcMessageFrame Frame;
|
||||||
|
|
||||||
// if only there was a standard library function for this
|
// if only there was a standard library function for this
|
||||||
size_t StringCopy(char* dest, const char* src, size_t maxBytes) {
|
size_t StringCopy(char* dest, const char* src, size_t maxBytes) {
|
||||||
|
@ -52,19 +63,19 @@ void EscapeString(char*& dest, const char* src)
|
||||||
*dest++ = '\\';
|
*dest++ = '\\';
|
||||||
*dest++ = 'b';
|
*dest++ = 'b';
|
||||||
break;
|
break;
|
||||||
case '\f': // Form feed(ascii code 0C)
|
case '\f':
|
||||||
*dest++ = '\\';
|
*dest++ = '\\';
|
||||||
*dest++ = 'f';
|
*dest++ = 'f';
|
||||||
break;
|
break;
|
||||||
case '\n': // New line
|
case '\n':
|
||||||
*dest++ = '\\';
|
*dest++ = '\\';
|
||||||
*dest++ = 'n';
|
*dest++ = 'n';
|
||||||
break;
|
break;
|
||||||
case '\r': // Carriage return
|
case '\r':
|
||||||
*dest++ = '\\';
|
*dest++ = '\\';
|
||||||
*dest++ = 'r';
|
*dest++ = 'r';
|
||||||
break;
|
break;
|
||||||
case '\t': // Tab
|
case '\t':
|
||||||
*dest++ = '\\';
|
*dest++ = '\\';
|
||||||
*dest++ = 't';
|
*dest++ = 't';
|
||||||
break;
|
break;
|
||||||
|
@ -239,6 +250,8 @@ void CloseConnection()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // anonymous namespace
|
||||||
|
|
||||||
void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers)
|
void Discord_Initialize(const char* applicationId, DiscordEventHandlers* handlers)
|
||||||
{
|
{
|
||||||
StringCopy(ApplicationId, applicationId, sizeof(ApplicationId));
|
StringCopy(ApplicationId, applicationId, sizeof(ApplicationId));
|
||||||
|
@ -273,14 +286,12 @@ void Discord_UpdatePresence(const DiscordRichPresence* presence)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t* totalLength = (int32_t*)MessageBuffer;
|
char* jsonWrite = Frame.message;
|
||||||
char* jsonStart = MessageBuffer + sizeof(int32_t);
|
|
||||||
char* jsonWrite = jsonStart;
|
|
||||||
|
|
||||||
JsonWriteRichPresenceObj(jsonWrite, presence);
|
JsonWriteRichPresenceObj(jsonWrite, presence);
|
||||||
|
|
||||||
*totalLength = sizeof(int32_t) + (jsonWrite - jsonStart);
|
Frame.length = sizeof(uint32_t) + (jsonWrite - Frame.message);
|
||||||
BOOL success = WriteFile(PipeHandle, MessageBuffer, *totalLength, nullptr, nullptr);
|
BOOL success = WriteFile(PipeHandle, &Frame, Frame.length, nullptr, nullptr);
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
CloseConnection();
|
CloseConnection();
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <time.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
struct DiscordRichPresence {
|
struct DiscordRichPresence {
|
||||||
const char* state;
|
const char* state;
|
||||||
const char* details;
|
const char* details;
|
||||||
time_t startTimestamp;
|
int64_t startTimestamp;
|
||||||
time_t endTimestamp;
|
int64_t endTimestamp;
|
||||||
const char* largeImageKey;
|
const char* largeImageKey;
|
||||||
const char* largeImageText;
|
const char* largeImageText;
|
||||||
const char* smallImageKey;
|
const char* smallImageKey;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#include "discord-rpc.h"
|
#include "discord-rpc.h"
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue