1 module discord_rpc;
2 
3 import discord_register;
4 
5 import std.stdio : writeln;
6 
7 alias int8_t  = byte;
8 alias int64_t = long;
9 
10 version(SL_DISCORD_RPC) {} else
11 static this()
12 {
13     import dynalib;
14     import discord_rpc_conditionals;
15     
16     DynaLib lib;
17     if (!lib.loadFromConfig("discord-rpc", "discord"))
18     {
19         version(DL_ERROR)
20             writeln("There was a problem loading discord-rpc");
21         return;
22     }
23     
24     // Register
25     lib.bind(&Discord_Register, "Discord_Register");
26     lib.bind(&Discord_RegisterSteamGame, "Discord_RegisterSteamGame");
27     
28     // RPC
29     lib.bind(&Discord_Initialize, "Discord_Initialize");
30     lib.bind(&Discord_Shutdown, "Discord_Shutdown");
31     lib.bind(&Discord_RunCallbacks, "Discord_RunCallbacks");
32     
33     if (lib.bind(&Discord_UpdateConnection, "Discord_UpdateConnection"))
34     {
35         version(DL_NOTIFICATION)
36             writeln("Dynalib-Conditional: [Discord-RPC] Disable-IO-Thread active.");
37         (cast(bool*)&conditional_DisableIOThread)[0] = true;
38     } else (cast(bool*)&conditional_DisableIOThread)[0] = false;
39     
40     lib.bind(&Discord_UpdatePresence, "Discord_UpdatePresence");
41     lib.bind(&Discord_ClearPresence, "Discord_ClearPresence");
42     lib.bind(&Discord_Respond, "Discord_Respond");
43     lib.bind(&Discord_UpdateHandlers, "Discord_UpdateHandlers");
44 }
45 
46 @nogc nothrow __gshared extern(C):
47     struct DiscordRichPresence
48     {
49         const(char)* state;   /* max 128 bytes */
50         const(char)* details; /* max 128 bytes */
51         int64_t startTimestamp;
52         int64_t endTimestamp;
53         const(char)* largeImageKey;  /* max 32 bytes */
54         const(char)* largeImageText; /* max 128 bytes */
55         const(char)* smallImageKey;  /* max 32 bytes */
56         const(char)* smallImageText; /* max 128 bytes */
57         const(char)* partyId;        /* max 128 bytes */
58         int partySize;
59         int partyMax;
60         int partyPrivacy;
61         const(char)* matchSecret;    /* max 128 bytes */
62         const(char)* joinSecret;     /* max 128 bytes */
63         const(char)* spectateSecret; /* max 128 bytes */
64         int8_t instance;
65     }
66 
67     struct DiscordUser
68     {
69         const(char)* userId;
70         const(char)* username;
71         const(char)* discriminator;
72         const(char)* avatar;
73     }
74 
75     struct DiscordEventHandlers
76     {
77         @nogc nothrow __gshared extern(C):
78             void function(const(DiscordUser)* request)         ready;
79             void function(int errorCode, const(char)* message) disconnected;
80             void function(int errorCode, const(char)* message) errored;
81             void function(const(char)* joinSecret)             joinGame;
82             void function(const(char)* spectateSecret)         spectateGame;
83             void function(const(DiscordUser)* request)         joinRequest;
84     }
85 
86     enum DISCORD_REPLY_NO = 0;
87     enum DISCORD_REPLY_YES = 1;
88     enum DISCORD_REPLY_IGNORE = 2;
89     enum DISCORD_REPLY_PRIVATE = 0;
90     enum DISCORD_REPLY_PUBLIC = 1;
91 
92 version(SL_DISCORD_RPC)
93 {
94     void Discord_Initialize(const(char)* applicationId,
95                             DiscordEventHandlers* handlers,
96                             int autoRegister,
97                             const(char)* optionalSteamId);
98     
99     void Discord_Shutdown();
100     
101     /* checks for incoming messages, dispatches callbacks */
102     void Discord_RunCallbacks();
103 
104     /* If you disable the lib starting its own io thread, you'll need to call this from your own */
105     version (DISCORD_DISABLE_IO_THREAD)
106         void Discord_UpdateConnection();
107 
108     void Discord_UpdatePresence(const DiscordRichPresence* presence);
109     
110     void Discord_ClearPresence();
111 
112     void Discord_Respond(const(char)* userid, /* DISCORD_REPLY_ */ int reply);
113 
114     void Discord_UpdateHandlers(DiscordEventHandlers* handlers);
115 }
116 else
117 {
118     void function(const(char)* applicationId,
119                   DiscordEventHandlers* handlers,
120                   int autoRegister,
121                   const(char)* optionalSteamId) Discord_Initialize;
122     
123     void function() Discord_Shutdown;
124     
125     /* checks for incoming messages, dispatches callbacks */
126     void function() Discord_RunCallbacks;
127 
128     /* If you disable the lib starting its own io thread, you'll need to call this from your own */
129     void function() Discord_UpdateConnection;
130 
131     void function(const DiscordRichPresence* presence) Discord_UpdatePresence;
132     
133     void function() Discord_ClearPresence;
134 
135     void function(const(char)* userid, /* DISCORD_REPLY_ */ int reply) Discord_Respond;
136 
137     void function(DiscordEventHandlers* handlers) Discord_UpdateHandlers;
138 }