Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
3bdb88d918 | |||
aa79c70bf9 | |||
a089aab53e | |||
dafd85c39f | |||
b1d6a7c0fc | |||
e4b3ef63b7 | |||
86ca320cb9 |
19
LICENSE
Normal file
19
LICENSE
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
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.
|
@ -1,6 +1,16 @@
|
|||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
public class DiscordController : MonoBehaviour {
|
[System.Serializable]
|
||||||
|
public class DiscordJoinEvent : UnityEngine.Events.UnityEvent<string> { }
|
||||||
|
|
||||||
|
[System.Serializable]
|
||||||
|
public class DiscordSpectateEvent : UnityEngine.Events.UnityEvent<string> { }
|
||||||
|
|
||||||
|
[System.Serializable]
|
||||||
|
public class DiscordJoinRequestEvent : UnityEngine.Events.UnityEvent<DiscordRpc.JoinRequest> { }
|
||||||
|
|
||||||
|
public class DiscordController : MonoBehaviour
|
||||||
|
{
|
||||||
public DiscordRpc.RichPresence presence;
|
public DiscordRpc.RichPresence presence;
|
||||||
public string applicationId;
|
public string applicationId;
|
||||||
public string optionalSteamId;
|
public string optionalSteamId;
|
||||||
@ -8,6 +18,9 @@ public class DiscordController : MonoBehaviour {
|
|||||||
public int clickCounter;
|
public int clickCounter;
|
||||||
public UnityEngine.Events.UnityEvent onConnect;
|
public UnityEngine.Events.UnityEvent onConnect;
|
||||||
public UnityEngine.Events.UnityEvent onDisconnect;
|
public UnityEngine.Events.UnityEvent onDisconnect;
|
||||||
|
public DiscordJoinEvent onJoin;
|
||||||
|
public DiscordJoinEvent onSpectate;
|
||||||
|
public DiscordJoinRequestEvent onJoinRequest;
|
||||||
|
|
||||||
DiscordRpc.EventHandlers handlers;
|
DiscordRpc.EventHandlers handlers;
|
||||||
|
|
||||||
@ -45,18 +58,29 @@ public class DiscordController : MonoBehaviour {
|
|||||||
{
|
{
|
||||||
++callbackCalls;
|
++callbackCalls;
|
||||||
Debug.Log(string.Format("Discord: join ({0})", secret));
|
Debug.Log(string.Format("Discord: join ({0})", secret));
|
||||||
|
onJoin.Invoke(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SpectateCallback(string secret)
|
public void SpectateCallback(string secret)
|
||||||
{
|
{
|
||||||
++callbackCalls;
|
++callbackCalls;
|
||||||
Debug.Log(string.Format("Discord: spectate ({0})", secret));
|
Debug.Log(string.Format("Discord: spectate ({0})", secret));
|
||||||
|
onSpectate.Invoke(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Start () {
|
public void RequestCallback(DiscordRpc.JoinRequest request)
|
||||||
|
{
|
||||||
|
++callbackCalls;
|
||||||
|
Debug.Log(string.Format("Discord: join request {0}: {1}", request.username, request.userId));
|
||||||
|
onJoinRequest.Invoke(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Update () {
|
void Start()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update()
|
||||||
|
{
|
||||||
DiscordRpc.RunCallbacks();
|
DiscordRpc.RunCallbacks();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,6 +95,7 @@ public class DiscordController : MonoBehaviour {
|
|||||||
handlers.errorCallback += ErrorCallback;
|
handlers.errorCallback += ErrorCallback;
|
||||||
handlers.joinCallback += JoinCallback;
|
handlers.joinCallback += JoinCallback;
|
||||||
handlers.spectateCallback += SpectateCallback;
|
handlers.spectateCallback += SpectateCallback;
|
||||||
|
handlers.requestCallback += RequestCallback;
|
||||||
DiscordRpc.Initialize(applicationId, ref handlers, true, optionalSteamId);
|
DiscordRpc.Initialize(applicationId, ref handlers, true, optionalSteamId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,9 @@ public class DiscordRpc
|
|||||||
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
public delegate void SpectateCallback(string secret);
|
public delegate void SpectateCallback(string secret);
|
||||||
|
|
||||||
|
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
|
||||||
|
public delegate void RequestCallback(JoinRequest request);
|
||||||
|
|
||||||
public struct EventHandlers
|
public struct EventHandlers
|
||||||
{
|
{
|
||||||
public ReadyCallback readyCallback;
|
public ReadyCallback readyCallback;
|
||||||
@ -24,6 +27,7 @@ public class DiscordRpc
|
|||||||
public ErrorCallback errorCallback;
|
public ErrorCallback errorCallback;
|
||||||
public JoinCallback joinCallback;
|
public JoinCallback joinCallback;
|
||||||
public SpectateCallback spectateCallback;
|
public SpectateCallback spectateCallback;
|
||||||
|
public RequestCallback requestCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
[System.Serializable]
|
[System.Serializable]
|
||||||
@ -46,6 +50,21 @@ public class DiscordRpc
|
|||||||
public bool instance;
|
public bool instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[System.Serializable]
|
||||||
|
public struct JoinRequest
|
||||||
|
{
|
||||||
|
public string userId;
|
||||||
|
public string username;
|
||||||
|
public string avatar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Reply
|
||||||
|
{
|
||||||
|
No = 0,
|
||||||
|
Yes = 1,
|
||||||
|
Ignore = 2
|
||||||
|
}
|
||||||
|
|
||||||
[DllImport("discord-rpc", EntryPoint = "Discord_Initialize", CallingConvention = CallingConvention.Cdecl)]
|
[DllImport("discord-rpc", EntryPoint = "Discord_Initialize", CallingConvention = CallingConvention.Cdecl)]
|
||||||
public static extern void Initialize(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId);
|
public static extern void Initialize(string applicationId, ref EventHandlers handlers, bool autoRegister, string optionalSteamId);
|
||||||
|
|
||||||
@ -57,5 +76,8 @@ public class DiscordRpc
|
|||||||
|
|
||||||
[DllImport("discord-rpc", EntryPoint = "Discord_UpdatePresence", CallingConvention = CallingConvention.Cdecl)]
|
[DllImport("discord-rpc", EntryPoint = "Discord_UpdatePresence", CallingConvention = CallingConvention.Cdecl)]
|
||||||
public static extern void UpdatePresence(ref RichPresence presence);
|
public static extern void UpdatePresence(ref RichPresence presence);
|
||||||
|
|
||||||
|
[DllImport("discord-rpc", EntryPoint = "Discord_Respond", CallingConvention = CallingConvention.Cdecl)]
|
||||||
|
public static extern void Respond(string userId, Reply reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
@ -703,6 +703,21 @@ MonoBehaviour:
|
|||||||
m_CallState: 2
|
m_CallState: 2
|
||||||
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
|
m_TypeName: UnityEngine.Events.UnityEvent, UnityEngine, Version=0.0.0.0, Culture=neutral,
|
||||||
PublicKeyToken=null
|
PublicKeyToken=null
|
||||||
|
onJoin:
|
||||||
|
m_PersistentCalls:
|
||||||
|
m_Calls: []
|
||||||
|
m_TypeName: DiscordJoinEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral,
|
||||||
|
PublicKeyToken=null
|
||||||
|
onSpectate:
|
||||||
|
m_PersistentCalls:
|
||||||
|
m_Calls: []
|
||||||
|
m_TypeName: DiscordJoinEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral,
|
||||||
|
PublicKeyToken=null
|
||||||
|
onJoinRequest:
|
||||||
|
m_PersistentCalls:
|
||||||
|
m_Calls: []
|
||||||
|
m_TypeName: DiscordJoinRequestEvent, Assembly-CSharp, Version=0.0.0.0, Culture=neutral,
|
||||||
|
PublicKeyToken=null
|
||||||
--- !u!4 &1929635630
|
--- !u!4 &1929635630
|
||||||
Transform:
|
Transform:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
@ -1 +1 @@
|
|||||||
m_EditorVersion: 2017.1.0f3
|
m_EditorVersion: 2017.1.1f1
|
||||||
|
@ -83,7 +83,7 @@ static void handleDiscordJoinRequest(const DiscordJoinRequest* request)
|
|||||||
char yn[4];
|
char yn[4];
|
||||||
printf("\nDiscord: join request from %s - %s - %s\n",
|
printf("\nDiscord: join request from %s - %s - %s\n",
|
||||||
request->username,
|
request->username,
|
||||||
request->avatarUrl,
|
request->avatar,
|
||||||
request->userId);
|
request->userId);
|
||||||
do {
|
do {
|
||||||
printf("Accept? (y/n)");
|
printf("Accept? (y/n)");
|
||||||
|
@ -42,9 +42,9 @@ typedef struct DiscordRichPresence {
|
|||||||
} DiscordRichPresence;
|
} DiscordRichPresence;
|
||||||
|
|
||||||
typedef struct DiscordJoinRequest {
|
typedef struct DiscordJoinRequest {
|
||||||
char userId[24];
|
const char* userId;
|
||||||
char username[48];
|
const char* username;
|
||||||
char avatarUrl[128];
|
const char* avatar;
|
||||||
} DiscordJoinRequest;
|
} DiscordJoinRequest;
|
||||||
|
|
||||||
typedef struct DiscordEventHandlers {
|
typedef struct DiscordEventHandlers {
|
||||||
|
@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
#include "backoff.h"
|
#include "backoff.h"
|
||||||
#include "discord_register.h"
|
#include "discord_register.h"
|
||||||
|
#include "msg_queue.h"
|
||||||
#include "rpc_connection.h"
|
#include "rpc_connection.h"
|
||||||
#include "serialization.h"
|
#include "serialization.h"
|
||||||
#include "msg_queue.h"
|
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
@ -32,6 +32,12 @@ struct QueuedMessage {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct JoinRequest {
|
||||||
|
char userId[24];
|
||||||
|
char username[48];
|
||||||
|
char avatar[128];
|
||||||
|
};
|
||||||
|
|
||||||
static RpcConnection* Connection{nullptr};
|
static RpcConnection* Connection{nullptr};
|
||||||
static DiscordEventHandlers Handlers{};
|
static DiscordEventHandlers Handlers{};
|
||||||
static std::atomic_bool WasJustConnected{false};
|
static std::atomic_bool WasJustConnected{false};
|
||||||
@ -48,7 +54,7 @@ static char LastDisconnectErrorMessage[256];
|
|||||||
static std::mutex PresenceMutex;
|
static std::mutex PresenceMutex;
|
||||||
static QueuedMessage QueuedPresence{};
|
static QueuedMessage QueuedPresence{};
|
||||||
static MsgQueue<QueuedMessage, MessageQueueSize> SendQueue;
|
static MsgQueue<QueuedMessage, MessageQueueSize> SendQueue;
|
||||||
static MsgQueue<DiscordJoinRequest, JoinQueueSize> JoinAskQueue;
|
static MsgQueue<JoinRequest, JoinQueueSize> JoinAskQueue;
|
||||||
|
|
||||||
// We want to auto connect, and retry on failure, but not as fast as possible. This does expoential
|
// 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
|
// backoff from 0.5 seconds to 1 minute
|
||||||
@ -71,7 +77,7 @@ static void UpdateReconnectTime()
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef DISCORD_DISABLE_IO_THREAD
|
#ifdef DISCORD_DISABLE_IO_THREAD
|
||||||
DISCORD_EXPORT void Discord_UpdateConnection(void)
|
extern "C" DISCORD_EXPORT void Discord_UpdateConnection(void)
|
||||||
#else
|
#else
|
||||||
static void Discord_UpdateConnection(void)
|
static void Discord_UpdateConnection(void)
|
||||||
#endif
|
#endif
|
||||||
@ -136,16 +142,16 @@ static void Discord_UpdateConnection(void)
|
|||||||
auto user = GetObjMember(data, "user");
|
auto user = GetObjMember(data, "user");
|
||||||
auto userId = GetStrMember(user, "id");
|
auto userId = GetStrMember(user, "id");
|
||||||
auto username = GetStrMember(user, "username");
|
auto username = GetStrMember(user, "username");
|
||||||
auto avatarUrl = GetStrMember(user, "avatar");
|
auto avatar = GetStrMember(user, "avatar");
|
||||||
auto joinReq = JoinAskQueue.GetNextAddMessage();
|
auto joinReq = JoinAskQueue.GetNextAddMessage();
|
||||||
if (userId && username && joinReq) {
|
if (userId && username && joinReq) {
|
||||||
StringCopy(joinReq->userId, userId);
|
StringCopy(joinReq->userId, userId);
|
||||||
StringCopy(joinReq->username, username);
|
StringCopy(joinReq->username, username);
|
||||||
if (avatarUrl) {
|
if (avatar) {
|
||||||
StringCopy(joinReq->avatarUrl, avatarUrl);
|
StringCopy(joinReq->avatar, avatar);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
joinReq->avatarUrl[0] = 0;
|
joinReq->avatar[0] = 0;
|
||||||
}
|
}
|
||||||
JoinAskQueue.CommitAdd();
|
JoinAskQueue.CommitAdd();
|
||||||
}
|
}
|
||||||
@ -210,10 +216,10 @@ static bool RegisterForEvent(const char* evtName)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
|
extern "C" DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
|
||||||
DiscordEventHandlers* handlers,
|
DiscordEventHandlers* handlers,
|
||||||
int autoRegister,
|
int autoRegister,
|
||||||
const char* optionalSteamId)
|
const char* optionalSteamId)
|
||||||
{
|
{
|
||||||
if (autoRegister) {
|
if (autoRegister) {
|
||||||
if (optionalSteamId && optionalSteamId[0]) {
|
if (optionalSteamId && optionalSteamId[0]) {
|
||||||
@ -267,7 +273,7 @@ DISCORD_EXPORT void Discord_Initialize(const char* applicationId,
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
DISCORD_EXPORT void Discord_Shutdown()
|
extern "C" DISCORD_EXPORT void Discord_Shutdown()
|
||||||
{
|
{
|
||||||
if (!Connection) {
|
if (!Connection) {
|
||||||
return;
|
return;
|
||||||
@ -285,7 +291,7 @@ DISCORD_EXPORT void Discord_Shutdown()
|
|||||||
RpcConnection::Destroy(Connection);
|
RpcConnection::Destroy(Connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence)
|
extern "C" DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence)
|
||||||
{
|
{
|
||||||
PresenceMutex.lock();
|
PresenceMutex.lock();
|
||||||
QueuedPresence.length = JsonWriteRichPresenceObj(
|
QueuedPresence.length = JsonWriteRichPresenceObj(
|
||||||
@ -294,7 +300,7 @@ DISCORD_EXPORT void Discord_UpdatePresence(const DiscordRichPresence* presence)
|
|||||||
SignalIOActivity();
|
SignalIOActivity();
|
||||||
}
|
}
|
||||||
|
|
||||||
DISCORD_EXPORT void Discord_Respond(const char* userId, /* DISCORD_REPLY_ */ int reply)
|
extern "C" DISCORD_EXPORT 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 we are not connected, let's not batch up stale messages for later
|
||||||
if (!Connection || !Connection->IsOpen()) {
|
if (!Connection || !Connection->IsOpen()) {
|
||||||
@ -309,7 +315,7 @@ DISCORD_EXPORT void Discord_Respond(const char* userId, /* DISCORD_REPLY_ */ int
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DISCORD_EXPORT void Discord_RunCallbacks()
|
extern "C" DISCORD_EXPORT void Discord_RunCallbacks()
|
||||||
{
|
{
|
||||||
// Note on some weirdness: internally we might connect, get other signals, disconnect any number
|
// 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
|
// of times inbetween calls here. Externally, we want the sequence to seem sane, so any other
|
||||||
@ -353,7 +359,8 @@ DISCORD_EXPORT void Discord_RunCallbacks()
|
|||||||
while (JoinAskQueue.HavePendingSends()) {
|
while (JoinAskQueue.HavePendingSends()) {
|
||||||
auto req = JoinAskQueue.GetNextSendMessage();
|
auto req = JoinAskQueue.GetNextSendMessage();
|
||||||
if (Handlers.joinRequest) {
|
if (Handlers.joinRequest) {
|
||||||
Handlers.joinRequest(req);
|
DiscordJoinRequest djr{req->userId, req->username, req->avatar};
|
||||||
|
Handlers.joinRequest(&djr);
|
||||||
}
|
}
|
||||||
JoinAskQueue.CommitSend();
|
JoinAskQueue.CommitSend();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user