Skip to content
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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions desktop_version/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ option(BUNDLE_DEPENDENCIES "Use bundled TinyXML-2, PhysicsFS, and FAudio (if dis

option(STEAM "Use the Steam API" OFF)
option(GOG "Use the GOG API" OFF)
option(DISCORD "Use the Discord API" OFF)

option(OFFICIAL_BUILD "Compile an official build of the game" OFF)

Expand All @@ -19,6 +20,7 @@ option(MAKEANDPLAY "Compile a version of the game without the main campaign (pro
if(OFFICIAL_BUILD AND NOT MAKEANDPLAY)
set(STEAM ON)
set(GOG ON)
set(DISCORD ON)
endif()

option(REMOVE_ABSOLUTE_PATHS "If supported by the compiler, replace all absolute paths to source directories compiled into the binary (if any) with relative paths" ON)
Expand Down Expand Up @@ -133,6 +135,9 @@ endif()
if(GOG)
list(APPEND VVV_C_SRC src/GOGNetwork.c)
endif()
if(DISCORD)
list(APPEND VVV_C_SRC src/DiscordNetwork.c)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "iOS")
list(APPEND VVV_C_SRC src/SDL_uikit_main.c)
endif()
Expand Down Expand Up @@ -210,6 +215,9 @@ endif()
if(GOG)
target_compile_definitions(VVVVVV PRIVATE -DGOG_NETWORK)
endif()
if(DISCORD)
target_compile_definitions(VVVVVV PRIVATE -DDISCORD_NETWORK)
endif()

set(XML2_SRC
../third_party/tinyxml2/tinyxml2.cpp
Expand Down
183 changes: 183 additions & 0 deletions desktop_version/src/DiscordNetwork.c
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!
Comment on lines +25 to +27
Copy link
Collaborator

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...?

Copy link
Contributor

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:

If you're integrating our SDK into an already-released game, there's a good chance that we may already have an application in our database for your game! Reach out to our Dev Support to learn more

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?)

Copy link
Author

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)

Copy link
Contributor

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.



struct DISCORD_application {
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__)
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, &params, &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?
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
4 changes: 2 additions & 2 deletions desktop_version/src/GOGNetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ void GOG_shutdown(void)
{
}

void GOG_update(void)
void GOG_update(const char* area, const char* roomname)
{
}

void GOG_unlockAchievement(const char *name)
void GOG_unlockAchievement(const char* name)
{
}

Expand Down
25 changes: 20 additions & 5 deletions desktop_version/src/Network.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdint.h>
#include <stdio.h>
Buggem marked this conversation as resolved.
Show resolved Hide resolved

#include "MakeAndPlay.h"
#include "Unused.h"
Expand All @@ -10,6 +11,9 @@
#ifdef GOG_NETWORK
#undef GOG_NETWORK
#endif
#ifdef DISCORD_NETWORK
#undef DISCORD_NETWORK
#endif
#endif

#ifdef STEAM_NETWORK
Expand All @@ -22,27 +26,35 @@
#else
#define GOG_NUM 0
#endif
#ifdef DISCORD_NETWORK
#define DISCORD_NUM 1
#else
#define DISCORD_NUM 0
#endif

#define NUM_BACKENDS (STEAM_NUM+GOG_NUM)
#define NUM_BACKENDS (STEAM_NUM+GOG_NUM+DISCORD_NUM)
#define DECLARE_BACKEND(name) \
int32_t name##_init(void); \
void name##_shutdown(void); \
void name##_update(void); \
void name##_update(const char *area, const char *roomname); \
void name##_unlockAchievement(const char *name);
#ifdef STEAM_NETWORK
DECLARE_BACKEND(STEAM)
#endif
#ifdef GOG_NETWORK
DECLARE_BACKEND(GOG)
#endif
#ifdef DISCORD_NETWORK
DECLARE_BACKEND(DISCORD)
#endif
#undef DECLARE_BACKEND

typedef struct NetworkBackend
{
int32_t IsInit;
int32_t (*Init)(void);
void (*Shutdown)(void);
void (*Update)(void);
void (*Update)(const char*, const char*);
void (*UnlockAchievement)(const char*);
} NetworkBackend;

Expand All @@ -64,6 +76,9 @@ int NETWORK_init(void)
#ifdef GOG_NETWORK
ASSIGN_BACKEND(GOG, STEAM_NUM)
#endif
#ifdef DISCORD_NETWORK
ASSIGN_BACKEND(DISCORD, STEAM_NUM+GOG_NUM)
#endif
#undef ASSIGN_BACKEND
#if NUM_BACKENDS > 0
for (i = 0; i < NUM_BACKENDS; i += 1)
Expand All @@ -89,14 +104,14 @@ void NETWORK_shutdown(void)
#endif
}

void NETWORK_update(void)
void NETWORK_update(const char *area, const char *roomname)
{
#if NUM_BACKENDS > 0
int32_t i;
for (i = 0; i < NUM_BACKENDS; i += 1)
if (backends[i].IsInit)
{
backends[i].Update();
backends[i].Update(area, roomname);
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion desktop_version/src/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int32_t NETWORK_init(void);

void NETWORK_shutdown(void);

void NETWORK_update(void);
void NETWORK_update(const char *area, const char *roomname);

void NETWORK_unlockAchievement(const char *name);

Expand Down
4 changes: 2 additions & 2 deletions desktop_version/src/SteamNetwork.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ void STEAM_shutdown(void)
}
}

void STEAM_update(void)
void STEAM_update(const char* area, const char* roomname)
{
if (!libHandle)
{
Expand All @@ -254,7 +254,7 @@ void STEAM_update(void)
}
}

void STEAM_unlockAchievement(const char *name)
void STEAM_unlockAchievement(const char* name)
{
if (libHandle)
{
Expand Down
Loading