2021-01-17 23:52:58 +00:00
|
|
|
#include <engine/discord.h>
|
|
|
|
|
2021-12-20 15:06:55 +00:00
|
|
|
// Hack for universal binary builds on macOS: Ignore arm64 until Discord
|
|
|
|
// releases a native arm64 SDK for macOS.
|
|
|
|
|
|
|
|
#if defined(CONF_DISCORD) && !(defined(CONF_ARCH_ARM64) && defined(CONF_PLATFORM_MACOS))
|
2021-01-17 23:52:58 +00:00
|
|
|
#include <discord_game_sdk.h>
|
|
|
|
|
Fix win32 discord compilation
/home/deen/isos/ddnet/ddnet-source/src/engine/client/discord.cpp: In function ‘EDiscordResult (* GetDiscordCreate())(DiscordVersion, DiscordCreateParams*, IDiscordCore**)’:
/home/deen/isos/ddnet/ddnet-source/src/engine/client/discord.cpp:25:16: error: invalid conversion from ‘EDiscordResult (__attribute__((stdcall)) *)(DiscordVersion, DiscordCreateParams*, IDiscordCore**)’ {aka ‘EDiscordResult (__attribute__((stdcall)) *)(int, DiscordCreateParams*, IDiscordCore**)’} to ‘FDiscordCreate’ {aka ‘EDiscordResult (*)(int, DiscordCreateParams*, IDiscordCore**)’} [-fpermissive]
25 | return DiscordCreate;
| ^~~~~~~~~~~~~
| |
| EDiscordResult (__attribute__((stdcall)) *)(DiscordVersion, DiscordCreateParams*, IDiscordCore**) {aka EDiscordResult (__attribute__((stdcall)) *)(int, DiscordCreateParams*, IDiscordCore**)}
2022-06-23 17:25:01 +00:00
|
|
|
typedef enum EDiscordResult DISCORD_API (*FDiscordCreate)(DiscordVersion, struct DiscordCreateParams *, struct IDiscordCore **);
|
2021-02-01 11:20:11 +00:00
|
|
|
|
|
|
|
#if defined(CONF_DISCORD_DYNAMIC)
|
|
|
|
#include <dlfcn.h>
|
|
|
|
FDiscordCreate GetDiscordCreate()
|
|
|
|
{
|
|
|
|
void *pSdk = dlopen("discord_game_sdk.so", RTLD_NOW);
|
|
|
|
if(!pSdk)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return (FDiscordCreate)dlsym(pSdk, "DiscordCreate");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
FDiscordCreate GetDiscordCreate()
|
|
|
|
{
|
|
|
|
return DiscordCreate;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-01-17 23:52:58 +00:00
|
|
|
class CDiscord : public IDiscord
|
|
|
|
{
|
|
|
|
IDiscordCore *m_pCore;
|
|
|
|
IDiscordActivityEvents m_ActivityEvents;
|
|
|
|
IDiscordActivityManager *m_pActivityManager;
|
|
|
|
|
|
|
|
public:
|
2021-02-01 11:20:11 +00:00
|
|
|
bool Init(FDiscordCreate pfnDiscordCreate)
|
2021-01-17 23:52:58 +00:00
|
|
|
{
|
|
|
|
m_pCore = 0;
|
|
|
|
mem_zero(&m_ActivityEvents, sizeof(m_ActivityEvents));
|
|
|
|
m_pActivityManager = 0;
|
|
|
|
|
|
|
|
DiscordCreateParams Params;
|
|
|
|
DiscordCreateParamsSetDefault(&Params);
|
|
|
|
|
|
|
|
Params.client_id = 752165779117441075; // DDNet
|
|
|
|
Params.flags = EDiscordCreateFlags::DiscordCreateFlags_NoRequireDiscord;
|
|
|
|
Params.event_data = this;
|
|
|
|
Params.activity_events = &m_ActivityEvents;
|
2021-02-01 11:20:11 +00:00
|
|
|
int Error = pfnDiscordCreate(DISCORD_VERSION, &Params, &m_pCore);
|
2021-01-17 23:52:58 +00:00
|
|
|
if(Error != DiscordResult_Ok)
|
|
|
|
{
|
|
|
|
dbg_msg("discord", "error initializing discord instance, error=%d", Error);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pActivityManager = m_pCore->get_activity_manager(m_pCore);
|
|
|
|
return false;
|
|
|
|
}
|
2022-05-17 21:51:51 +00:00
|
|
|
void Update() override
|
2021-01-17 23:52:58 +00:00
|
|
|
{
|
|
|
|
m_pCore->run_callbacks(m_pCore);
|
|
|
|
}
|
2022-05-17 21:51:51 +00:00
|
|
|
void ClearGameInfo() override
|
2021-01-17 23:52:58 +00:00
|
|
|
{
|
|
|
|
m_pActivityManager->clear_activity(m_pActivityManager, 0, 0);
|
|
|
|
}
|
2022-10-09 11:43:10 +00:00
|
|
|
void SetGameInfo(const NETADDR &ServerAddr, const char *pMapName, bool AnnounceAddr) override
|
2021-01-17 23:52:58 +00:00
|
|
|
{
|
|
|
|
DiscordActivity Activity;
|
|
|
|
mem_zero(&Activity, sizeof(DiscordActivity));
|
|
|
|
str_copy(Activity.assets.large_image, "ddnet_logo", sizeof(Activity.assets.large_image));
|
|
|
|
str_copy(Activity.assets.large_text, "DDNet logo", sizeof(Activity.assets.large_text));
|
|
|
|
Activity.timestamps.start = time_timestamp();
|
|
|
|
str_copy(Activity.details, pMapName, sizeof(Activity.details));
|
|
|
|
m_pActivityManager->update_activity(m_pActivityManager, &Activity, 0, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
IDiscord *CreateDiscordImpl()
|
|
|
|
{
|
2021-02-01 11:20:11 +00:00
|
|
|
FDiscordCreate pfnDiscordCreate = GetDiscordCreate();
|
|
|
|
if(!pfnDiscordCreate)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2021-01-17 23:52:58 +00:00
|
|
|
CDiscord *pDiscord = new CDiscord();
|
2021-02-01 11:20:11 +00:00
|
|
|
if(pDiscord->Init(pfnDiscordCreate))
|
2021-01-17 23:52:58 +00:00
|
|
|
{
|
|
|
|
delete pDiscord;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return pDiscord;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
IDiscord *CreateDiscordImpl()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class CDiscordStub : public IDiscord
|
|
|
|
{
|
2022-05-17 18:33:27 +00:00
|
|
|
void Update() override {}
|
|
|
|
void ClearGameInfo() override {}
|
2022-10-09 11:43:10 +00:00
|
|
|
void SetGameInfo(const NETADDR &ServerAddr, const char *pMapName, bool AnnounceAddr) override {}
|
2021-01-17 23:52:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
IDiscord *CreateDiscord()
|
|
|
|
{
|
|
|
|
IDiscord *pDiscord = CreateDiscordImpl();
|
|
|
|
if(pDiscord)
|
|
|
|
{
|
|
|
|
return pDiscord;
|
|
|
|
}
|
|
|
|
return new CDiscordStub();
|
|
|
|
}
|