Skip to content

Commit

Permalink
Adding user object to READY event (#159)
Browse files Browse the repository at this point in the history
* Pass the READY event data down in onConnect

* Changes made for UE4 and Unity wrappers

* Changing object name from joinRequest to DiscordUser
  • Loading branch information
msciotti authored Apr 16, 2018
1 parent 2ce9fe0 commit 64027b3
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 56 deletions.
10 changes: 5 additions & 5 deletions examples/button-clicker/Assets/DiscordController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class DiscordJoinEvent : UnityEngine.Events.UnityEvent<string> { }
public class DiscordSpectateEvent : UnityEngine.Events.UnityEvent<string> { }

[System.Serializable]
public class DiscordJoinRequestEvent : UnityEngine.Events.UnityEvent<DiscordRpc.JoinRequest> { }
public class DiscordJoinRequestEvent : UnityEngine.Events.UnityEvent<DiscordRpc.DiscordUser> { }

public class DiscordController : MonoBehaviour
{
Expand All @@ -16,7 +16,7 @@ public class DiscordController : MonoBehaviour
public string optionalSteamId;
public int callbackCalls;
public int clickCounter;
public DiscordRpc.JoinRequest joinRequest;
public DiscordRpc.DiscordUser joinRequest;
public UnityEngine.Events.UnityEvent onConnect;
public UnityEngine.Events.UnityEvent onDisconnect;
public UnityEngine.Events.UnityEvent hasResponded;
Expand Down Expand Up @@ -50,10 +50,10 @@ public void RequestRespondNo()
hasResponded.Invoke();
}

public void ReadyCallback()
public void ReadyCallback(ref DiscordRpc.DiscordUser connectedUser)
{
++callbackCalls;
Debug.Log("Discord: ready");
Debug.Log(string.Format("Discord: connected to {0}#{1}: {2}", connectedUser.username, connectedUser.discriminator, connectedUser.userId));
onConnect.Invoke();
}

Expand Down Expand Up @@ -84,7 +84,7 @@ public void SpectateCallback(string secret)
onSpectate.Invoke(secret);
}

public void RequestCallback(ref DiscordRpc.JoinRequest request)
public void RequestCallback(ref DiscordRpc.DiscordUser request)
{
++callbackCalls;
Debug.Log(string.Format("Discord: join request {0}#{1}: {2}", request.username, request.discriminator, request.userId));
Expand Down
6 changes: 3 additions & 3 deletions examples/button-clicker/Assets/DiscordRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public class DiscordRpc
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ReadyCallback();
public delegate void ReadyCallback(ref DiscordUser connectedUser);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void DisconnectedCallback(int errorCode, string message);
Expand All @@ -21,7 +21,7 @@ public class DiscordRpc
public delegate void SpectateCallback(string secret);

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void RequestCallback(ref JoinRequest request);
public delegate void RequestCallback(ref DiscordUser request);

public struct EventHandlers
{
Expand Down Expand Up @@ -54,7 +54,7 @@ public struct RichPresenceStruct
}

[Serializable]
public struct JoinRequest
public struct DiscordUser
{
public string userId;
public string username;
Expand Down
19 changes: 12 additions & 7 deletions examples/send-presence/send-presence.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ static void updateDiscordPresence()
discordPresence.spectateSecret = "look";
discordPresence.instance = 0;
Discord_UpdatePresence(&discordPresence);
} else {
}
else {
Discord_ClearPresence();
}
}

static void handleDiscordReady(void)
static void handleDiscordReady(const DiscordUser* connectedUser)
{
printf("\nDiscord: ready\n");
printf("\nDiscord: connected to user %s#%s - %s\n",
connectedUser->username,
connectedUser->discriminator,
connectedUser->userId);
}

static void handleDiscordDisconnected(int errcode, const char* message)
Expand All @@ -82,13 +86,13 @@ static void handleDiscordSpectate(const char* secret)
printf("\nDiscord: spectate (%s)\n", secret);
}

static void handleDiscordJoinRequest(const DiscordJoinRequest* request)
static void handleDiscordJoinRequest(const DiscordUser* request)
{
int response = -1;
char yn[4];
printf("\nDiscord: join request from %s - %s - %s\n",
printf("\nDiscord: join request from %s#%s - %s\n",
request->username,
request->avatar,
request->discriminator,
request->userId);
do {
printf("Accept? (y/n)");
Expand Down Expand Up @@ -152,7 +156,8 @@ static void gameLoop()
if (SendPresence) {
printf("Clearing presence information.\n");
SendPresence = 0;
} else {
}
else {
printf("Restoring presence information.\n");
SendPresence = 1;
}
Expand Down
Binary file modified examples/unrealstatus/Content/ShowTheUILevel.umap
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,22 @@ DEFINE_LOG_CATEGORY(Discord)

static UDiscordRpc* self = nullptr;

static void ReadyHandler()
static void ReadyHandler(const DiscordUser* connectedUser)
{
UE_LOG(Discord, Log, TEXT("Discord connected"));
FDiscordUserData ud;
ud.userId = ANSI_TO_TCHAR(connectedUser->userId);
ud.username = ANSI_TO_TCHAR(connectedUser->username);
ud.discriminator = ANSI_TO_TCHAR(connectedUser->discriminator);
ud.avatar = ANSI_TO_TCHAR(connectedUser->avatar);
UE_LOG(Discord,
Log,
TEXT("Discord connected to %s - %s#%s"),
*ud.userId,
*ud.username,
*ud.discriminator);
if (self) {
self->IsConnected = true;
self->OnConnected.Broadcast();
self->OnConnected.Broadcast(ud);
}
}

Expand Down Expand Up @@ -52,22 +62,27 @@ static void SpectateGameHandler(const char* spectateSecret)
}
}

static void JoinRequestHandler(const DiscordJoinRequest* request)
static void JoinRequestHandler(const DiscordUser* request)
{
FDiscordJoinRequestData jr;
jr.userId = ANSI_TO_TCHAR(request->userId);
jr.username = ANSI_TO_TCHAR(request->username);
jr.discriminator = ANSI_TO_TCHAR(request->discriminator);
jr.avatar = ANSI_TO_TCHAR(request->avatar);
UE_LOG(Discord, Log, TEXT("Discord join request from %s - %s#%s"), *jr.userId, *jr.username, *jr.discriminator);
FDiscordUserData ud;
ud.userId = ANSI_TO_TCHAR(request->userId);
ud.username = ANSI_TO_TCHAR(request->username);
ud.discriminator = ANSI_TO_TCHAR(request->discriminator);
ud.avatar = ANSI_TO_TCHAR(request->avatar);
UE_LOG(Discord,
Log,
TEXT("Discord join request from %s - %s#%s"),
*ud.userId,
*ud.username,
*ud.discriminator);
if (self) {
self->OnJoinRequest.Broadcast(jr);
self->OnJoinRequest.Broadcast(ud);
}
}

void UDiscordRpc::Initialize(const FString& applicationId,
bool autoRegister,
const FString& optionalSteamId)
bool autoRegister,
const FString& optionalSteamId)
{
self = this;
IsConnected = false;
Expand All @@ -87,7 +102,7 @@ void UDiscordRpc::Initialize(const FString& applicationId,
auto appId = StringCast<ANSICHAR>(*applicationId);
auto steamId = StringCast<ANSICHAR>(*optionalSteamId);
Discord_Initialize(
(const char*)appId.Get(), &handlers, autoRegister, (const char*)steamId.Get());
(const char*)appId.Get(), &handlers, autoRegister, (const char*)steamId.Get());
}

void UDiscordRpc::Shutdown()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Ask to join callback data
*/
USTRUCT(BlueprintType)
struct FDiscordJoinRequestData {
struct FDiscordUserData {
GENERATED_USTRUCT_BODY()

UPROPERTY(BlueprintReadOnly)
Expand All @@ -27,12 +27,12 @@ struct FDiscordJoinRequestData {

DECLARE_LOG_CATEGORY_EXTERN(Discord, Log, All);

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FDiscordConnected);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDiscordConnected, const FDiscordUserData&, joinRequest);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FDiscordDisconnected, int, errorCode, const FString&, errorMessage);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FDiscordErrored, int, errorCode, const FString&, errorMessage);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDiscordJoin, const FString&, joinSecret);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDiscordSpectate, const FString&, spectateSecret);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDiscordJoinRequest, const FDiscordJoinRequestData&, joinRequest);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FDiscordJoinRequest, const FDiscordUserData&, joinRequest);

// clang-format on

Expand Down
8 changes: 4 additions & 4 deletions include/discord_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,20 @@ typedef struct DiscordRichPresence {
int8_t instance;
} DiscordRichPresence;

typedef struct DiscordJoinRequest {
typedef struct DiscordUser {
const char* userId;
const char* username;
const char* discriminator;
const char* avatar;
} DiscordJoinRequest;
} DiscordUser;

typedef struct DiscordEventHandlers {
void (*ready)(void);
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 DiscordJoinRequest* request);
void (*joinRequest)(const DiscordUser* request);
} DiscordEventHandlers;

#define DISCORD_REPLY_NO 0
Expand Down
57 changes: 39 additions & 18 deletions src/discord_rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct QueuedMessage {
}
};

struct JoinRequest {
struct User {
// snowflake (64bit int), turned into a ascii decimal string, at most 20 chars +1 null
// terminator = 21
char userId[32];
Expand Down Expand Up @@ -64,7 +64,8 @@ static std::mutex PresenceMutex;
static std::mutex HandlerMutex;
static QueuedMessage QueuedPresence{};
static MsgQueue<QueuedMessage, MessageQueueSize> SendQueue;
static MsgQueue<JoinRequest, JoinQueueSize> JoinAskQueue;
static MsgQueue<User, JoinQueueSize> JoinAskQueue;
static User connectedUser;

// We want to auto connect, and retry on failure, but not as fast as possible. This does expoential
// backoff from 0.5 seconds to 1 minute
Expand Down Expand Up @@ -292,16 +293,34 @@ extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
}

Handlers = {};

}

if (Connection) {
return;
}

Connection = RpcConnection::Create(applicationId);
Connection->onConnect = []() {
Connection->onConnect = [](JsonDocument& readyMessage) {
Discord_UpdateHandlers(&QueuedHandlers);
auto data = GetObjMember(&readyMessage, "data");
auto user = GetObjMember(data, "user");
auto userId = GetStrMember(user, "id");
auto username = GetStrMember(user, "username");
auto avatar = GetStrMember(user, "avatar");
if (userId && username) {
StringCopy(connectedUser.userId, userId);
StringCopy(connectedUser.username, username);
auto discriminator = GetStrMember(user, "discriminator");
if (discriminator) {
StringCopy(connectedUser.discriminator, discriminator);
}
if (avatar) {
StringCopy(connectedUser.avatar, avatar);
}
else {
connectedUser.avatar[0] = 0;
}
}
WasJustConnected.exchange(true);
ReconnectTimeMs.reset();
};
Expand Down Expand Up @@ -336,7 +355,7 @@ extern "C" DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence*
{
std::lock_guard<std::mutex> guard(PresenceMutex);
QueuedPresence.length = JsonWriteRichPresenceObj(
QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence);
QueuedPresence.buffer, sizeof(QueuedPresence.buffer), Nonce++, Pid, presence);
}
SignalIOActivity();
}
Expand Down Expand Up @@ -385,7 +404,11 @@ extern "C" DISCORD_EXPORT void Discord_RunCallbacks(void)
if (WasJustConnected.exchange(false)) {
std::lock_guard<std::mutex> guard(HandlerMutex);
if (Handlers.ready) {
Handlers.ready();
DiscordUser du{connectedUser.userId,
connectedUser.username,
connectedUser.discriminator,
connectedUser.avatar};
Handlers.ready(&du);
}
}

Expand Down Expand Up @@ -420,8 +443,8 @@ extern "C" DISCORD_EXPORT void Discord_RunCallbacks(void)
{
std::lock_guard<std::mutex> guard(HandlerMutex);
if (Handlers.joinRequest) {
DiscordJoinRequest djr{req->userId, req->username, req->discriminator, req->avatar};
Handlers.joinRequest(&djr);
DiscordUser du{req->userId, req->username, req->discriminator, req->avatar};
Handlers.joinRequest(&du);
}
}
JoinAskQueue.CommitSend();
Expand All @@ -439,14 +462,13 @@ extern "C" DISCORD_EXPORT void Discord_RunCallbacks(void)
extern "C" DISCORD_EXPORT void Discord_UpdateHandlers(DiscordEventHandlers* newHandlers)
{
if (newHandlers) {

#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); \
}
#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); \
}

std::lock_guard<std::mutex> guard(HandlerMutex);
HANDLE_EVENT_REGISTRATION(joinGame, "ACTIVITY_JOIN")
Expand All @@ -457,8 +479,7 @@ extern "C" DISCORD_EXPORT void Discord_UpdateHandlers(DiscordEventHandlers* newH

Handlers = *newHandlers;
}
else
{
else {
std::lock_guard<std::mutex> guard(HandlerMutex);
Handlers = {};
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void RpcConnection::Open()
if (cmd && evt && !strcmp(cmd, "DISPATCH") && !strcmp(evt, "READY")) {
state = State::Connected;
if (onConnect) {
onConnect();
onConnect(message);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/rpc_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct RpcConnection {

BaseConnection* connection{nullptr};
State state{State::Disconnected};
void (*onConnect)(){nullptr};
void (*onConnect)(JsonDocument& message){nullptr};
void (*onDisconnect)(int errorCode, const char* message){nullptr};
char appId[64]{};
int lastErrorCode{0};
Expand Down

0 comments on commit 64027b3

Please sign in to comment.