-
Notifications
You must be signed in to change notification settings - Fork 559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Discord Game SDK support #1217
base: master
Are you sure you want to change the base?
Changes from 19 commits
eb21260
0a1244b
7975b27
76d5738
76d4368
ce111cf
7d3e7db
17fea40
459437c
7fcba85
5cbf03d
132c504
29708db
3b5108e
bef03b6
d35fb29
9c05ffe
731f2ce
6e5fed0
bf9dd78
fba8f7d
28a38bd
ec132ac
62ecc83
4a12fae
6bc5649
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
#include "MakeAndPlay.h" | ||
|
||
#ifndef MAKEANDPLAY | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <SDL.h> | ||
|
||
#include "Vlogging.h" | ||
|
||
// Library includes | ||
#include "discord_game_sdk.h" | ||
|
||
#if defined(_WIN32) | ||
#define DISCORD_LIBRARY "discord_game_sdk.dll" | ||
#elif defined(__APPLE__) | ||
#define DISCORD_LIBRARY "libdiscord_game_sdk.dylib" | ||
#elif defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__) || defined(__DragonFly__) | ||
#define DISCORD_LIBRARY "libdiscord_game_sdk.so" | ||
#else | ||
#error DISCORD_LIBRARY: Unrecognized platform! | ||
#endif | ||
|
||
|
||
#define DISCORD_CLIENT_ID 1315544357532729447 | ||
|
||
// TO TERRY/FLIBIT: You can create your own Discord instance at the Discord Developer Portal. This ID belongs to me, so just be aware that if my account was to get hacked, VVVVVV RPC would too. Use your own! | ||
|
||
|
||
struct DISCORD_application { | ||
Buggem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
struct IDiscordCore* core; | ||
struct IDiscordActivityManager* activityMan; | ||
} app; | ||
|
||
struct DiscordActivity activity; | ||
|
||
bool discordDetected = false; | ||
bool discordPostInit = false; | ||
|
||
|
||
static void* libHandle = NULL; | ||
|
||
|
||
|
||
#define FUNC_LIST \ | ||
FOREACH_FUNC(enum EDiscordResult, DiscordCreate, (DiscordVersion, struct DiscordCreateParams*, struct IDiscordCore**)) | ||
|
||
|
||
|
||
#define FOREACH_FUNC(rettype, name, params) static rettype (*name) params = NULL; | ||
FUNC_LIST | ||
#undef FOREACH_FUNC | ||
|
||
bool DISCORD_REQUIRE(int x) | ||
{ | ||
if (!discordDetected && discordPostInit) | ||
{ | ||
return false; | ||
} | ||
if (x != DiscordResult_Ok) | ||
{ | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
|
||
|
||
static void ClearPointers(void) | ||
{ | ||
SDL_UnloadObject(libHandle); | ||
libHandle = NULL; | ||
#define FOREACH_FUNC(rettype, name, params) name = NULL; | ||
FUNC_LIST | ||
#undef FOREACH_FUNC | ||
} | ||
|
||
void DISCORD_shutdown(void) | ||
{ | ||
if (libHandle != NULL) | ||
{ | ||
app.core->destroy(app.core); | ||
ClearPointers(); | ||
} | ||
} | ||
|
||
int32_t DISCORD_init(void) | ||
{ | ||
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__HAIKU__) || defined(__DragonFly__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be rewritten to check for OSes that Discord does support, rather than ones it doesn't. |
||
return 0; | ||
#endif | ||
libHandle = SDL_LoadObject(DISCORD_LIBRARY); | ||
if (libHandle == NULL) | ||
{ | ||
vlog_info(DISCORD_LIBRARY " not found!"); | ||
vlog_debug("Can't load object %s : %s\n", DISCORD_LIBRARY, SDL_GetError()); | ||
return 0; | ||
} | ||
|
||
#define FOREACH_FUNC(rettype, name, params) \ | ||
name = (rettype (*) params) (intptr_t) SDL_LoadFunction(libHandle, #name); \ | ||
if (!name) \ | ||
{ \ | ||
vlog_error(DISCORD_LIBRARY " symbol " #name " not found!"); \ | ||
ClearPointers(); \ | ||
return 0; \ | ||
} | ||
FUNC_LIST | ||
#undef FOREACH_FUNC | ||
SDL_memset(&app, 0, sizeof(app)); | ||
|
||
struct DiscordCreateParams params; | ||
params.client_id = DISCORD_CLIENT_ID; | ||
params.flags = DiscordCreateFlags_NoRequireDiscord; | ||
|
||
if (!DISCORD_REQUIRE(DiscordCreate(DISCORD_VERSION, ¶ms, &app.core))) | ||
{ // Discord's API couldn't initialise, so just ignore it | ||
discordPostInit = true; // even if it fails, set post init to true. | ||
vlog_debug("Discord API failed to initialise!"); | ||
discordDetected = false; | ||
return 0; | ||
} | ||
|
||
if (app.core == NULL) | ||
{ | ||
discordPostInit = true; | ||
vlog_debug("app.core == null. DiscordCreate failed with a positive result?\nCheck DISCORD_REQUIRE for bugs."); | ||
discordDetected = false; | ||
return 0; | ||
} | ||
discordPostInit = true; | ||
|
||
// Placeholder, is this really nesaccary | ||
SDL_strlcpy(activity.assets.large_image, "vvvvvv", sizeof(activity.assets.large_image)); | ||
SDL_strlcpy(activity.assets.large_text, "Outside Dimension VVVVVV", sizeof(activity.assets.large_text)); | ||
|
||
app.activityMan = app.core->get_activity_manager(app.core); | ||
app.activityMan->update_activity(app.activityMan, &activity, NULL, NULL); | ||
|
||
|
||
discordDetected = true; | ||
return 1; | ||
} | ||
|
||
void DISCORD_update(const char* area, const char* roomname) | ||
{ | ||
if (libHandle == NULL) | ||
{ | ||
// no discord or just shutdown | ||
return; | ||
} | ||
if (app.core == NULL || !discordDetected) | ||
{ | ||
// No Discord | ||
return; | ||
} | ||
if (!DISCORD_REQUIRE(app.core->run_callbacks(app.core))) | ||
{ | ||
// Something or other is wrong, but do we care? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably not, but Discord support might...? Not sure what their policy is on reporting internal errors |
||
return; | ||
} | ||
if (app.activityMan == NULL) | ||
{ | ||
vlog_debug("No activityMan! - it\'s fine, we can recreate this" ); | ||
app.activityMan = app.core->get_activity_manager(app.core); | ||
} | ||
if (SDL_strcmp(activity.state, roomname) != 0 || SDL_strcmp(activity.assets.large_text, area) != 0) | ||
{ | ||
SDL_strlcpy(activity.state, roomname, sizeof(activity.state)); | ||
SDL_strlcpy(activity.assets.large_image, "vvvvvv", sizeof(activity.assets.large_image)); | ||
SDL_strlcpy(activity.assets.large_text, area, sizeof(activity.assets.large_text)); | ||
|
||
app.activityMan->update_activity(app.activityMan, &activity, NULL, NULL); | ||
} | ||
} | ||
void DISCORD_unlockAchievement(const char *name) | ||
{ | ||
// No "achievements" in Discord | ||
} | ||
|
||
|
||
|
||
#endif // MakeAndPlay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TerryCavanagh, does this already exist...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, the Discord docs do say this:
I wonder whether the reasons to use the ID they already have are good enough reasons to bother lol (maybe so it smoothly takes over the "Playing VVVVVV", "last played 2 days ago", etc that was already showing without rich presence?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Daaaav @flibitijibibo I have sent a message to Discord Support to see if this is true for VVVVVV. Hopefully I get a response before the 15th anniversary of VVVVVV. (I'm hoping this will be released then)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sure an application already exists for VVVVVV, as it existed on the Discord Game Store back when that existed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to lock this in before we can merge, so whoever can find this number should patch this line ASAP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The automatically assigned application ID is
491427544134975498
, but this doesn't currently work for rich presence. I'm not sure if the Discord Store release had a separate application ID.