Skip to content

Commit

Permalink
Replace deprecated Discord RPC w/ Discord Game SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
TotallyMehis committed Aug 5, 2024
1 parent f7132e1 commit 6d8089e
Show file tree
Hide file tree
Showing 38 changed files with 4,876 additions and 209 deletions.
2 changes: 1 addition & 1 deletion mp/src/game/client/client_base.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ $Configuration
$Compiler
{
$AdditionalIncludeDirectories ".\;$BASE;$SRCDIR\vgui2\include;$SRCDIR\vgui2\controls;$SRCDIR\game\shared;.\game_controls"
$PreprocessorDefinitions "$BASE;NO_STRING_T;CLIENT_DLL;VECTOR;VERSION_SAFE_STEAM_API_INTERFACES;PROTECTED_THINGS_ENABLE;strncpy=use_Q_strncpy_instead;_snprintf=use_Q_snprintf_instead"
$PreprocessorDefinitions "$BASE;NO_STRING_T;CLIENT_DLL;VECTOR;VERSION_SAFE_STEAM_API_INTERFACES;PROTECTED_THINGS_ENABLE"
$PreprocessorDefinitions "$BASE;fopen=dont_use_fopen" [$WIN32]
$PreprocessorDefinitions "$BASE;USE_WEBM_FOR_REPLAY;" [$LINUXALL]
$PreprocessorDefinitions "$BASE;CURL_STATICLIB" [$WIN32 && $BUILD_REPLAY]
Expand Down
35 changes: 31 additions & 4 deletions mp/src/game/client/client_zmr.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ $Configuration
$PreprocessorDefinitions "$BASE;HL2MP;HL2_CLIENT_DLL;ZMR"
$PreprocessorDefinitions "$BASE;ZMR_STEAM" [$ZMR_STEAM]
$PreprocessorDefinitions "$BASE;ZMR_FINAL" [$ZMR_FINAL]
$PreprocessorDefinitions "$BASE;_HAS_EXCEPTIONS=0" [$WIN32] // Disable exceptions in STL (Discord RPC thing -_-)
}
}

$Project "Client (ZMR)"
{
$Folder "Libs (ZMR)"
{
$Lib discord-rpc [$WIN32 && $ZMR_FINAL]
$Lib discord_game_sdk.dll [$WIN32 && $ZMR_FINAL]
$Lib fmod_vc [$WIN32 && $ZMR_FINAL]
//$Lib libdiscord-rpc [$LINUXALL]
//$Lib libfmod [$LINUXALL && $ZMR_FINAL]
$Lib raytrace
}

$Folder "Source Files"
{
-$File "$SRCDIR\game\shared\weapon_parse_default.cpp"
Expand Down Expand Up @@ -84,6 +84,34 @@ $Project "Client (ZMR)"
}
$Folder "ZMR"
{
$Folder "Discord" [$WIN32 && $ZMR_FINAL]
{
$File "zmr\c_zmr_discord.cpp"

$File "$SRCDIR\public\discord\achievement_manager.cpp" \
"$SRCDIR\public\discord\activity_manager.cpp" \
"$SRCDIR\public\discord\application_manager.cpp" \
"$SRCDIR\public\discord\core.cpp" \
"$SRCDIR\public\discord\image_manager.cpp" \
"$SRCDIR\public\discord\lobby_manager.cpp" \
"$SRCDIR\public\discord\network_manager.cpp" \
"$SRCDIR\public\discord\overlay_manager.cpp" \
"$SRCDIR\public\discord\relationship_manager.cpp" \
"$SRCDIR\public\discord\storage_manager.cpp" \
"$SRCDIR\public\discord\store_manager.cpp" \
"$SRCDIR\public\discord\types.cpp" \
"$SRCDIR\public\discord\user_manager.cpp" \
"$SRCDIR\public\discord\voice_manager.cpp"
{
$Configuration
{
$Compiler
{
$Create/UsePrecompiledHeader "Not Using Precompiled Headers"
}
}
}
}
$Folder "Game Shader"
{
$File "replmats.cpp"
Expand Down Expand Up @@ -349,7 +377,6 @@ $Project "Client (ZMR)"
$File "zmr\c_zmr_concommands.cpp"
$File "zmr\c_zmr_crosshair.cpp"
$File "zmr\c_zmr_crosshair.h"
$File "zmr\c_zmr_discord.cpp" [$WIN32 && $ZMR_FINAL]
$File "zmr\c_zmr_entities.cpp"
$File "zmr\c_zmr_entities.h"
$File "zmr\c_zmr_fireglow_system.cpp"
Expand Down
126 changes: 35 additions & 91 deletions mp/src/game/client/zmr/c_zmr_discord.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
#include "cbase.h"

#include <ctime>

#include <discordrpc/discord_rpc.h>
#ifdef WIN32
#define _WINREG_
#undef ReadConsoleInput
#undef INVALID_HANDLE_VALUE
#undef GetCommandLine
#endif
#include <discord/discord.h>


#include "c_zmr_player_resource.h"
Expand All @@ -10,10 +17,7 @@
#include "tier0/memdbgon.h"



#define DISCORD_APP_ID "551073398789505065"


static const discord::ClientId DISCORD_APP_ID = 551073398789505065;

class CZMDiscordSystem : public CAutoGameSystemPerFrame
{
Expand All @@ -23,61 +27,32 @@ class CZMDiscordSystem : public CAutoGameSystemPerFrame


virtual void PostInit() OVERRIDE;
virtual void Shutdown() OVERRIDE;

virtual void LevelInitPostEntity() OVERRIDE;
virtual void LevelShutdownPostEntity() OVERRIDE;

virtual void Update( float frametime ) OVERRIDE;


private:
void PresenceEmpty();
void PresenceInGame();
void UpdateGameStartTime();
void InitDiscord();

bool IsConnected() const { return m_bConnected; }


static void SetDisconnected();
static void SetConnected();

private:
int GetPlayerCount() const;

static bool IsInGame();


float m_flNextDiscordUpdateTime;

int m_nLastPlayerCount;

uint64_t m_GameStartTime;

bool m_bConnected;
discord::Core* m_pDiscordCore;
};

static CZMDiscordSystem g_ZMDiscordSystem;


static void Discord_Event_Ready( const DiscordUser* request )
{
CZMDiscordSystem::SetConnected();
}

static void Discord_Event_Disconnected( int errorCode, const char* message )
{
CZMDiscordSystem::SetDisconnected();
}

static void Discord_Event_Error( int errorCode, const char* message )
{
Assert( 0 );
DevWarning( "Discord error (%i): %s", errorCode, message );
}



//
//
//
Expand All @@ -86,7 +61,7 @@ CZMDiscordSystem::CZMDiscordSystem() : CAutoGameSystemPerFrame( "ZMDiscordSystem
m_flNextDiscordUpdateTime = 0.0f;
m_nLastPlayerCount = -1;

m_bConnected = false;
m_pDiscordCore = nullptr;
}

CZMDiscordSystem::~CZMDiscordSystem()
Expand All @@ -102,40 +77,32 @@ void CZMDiscordSystem::PostInit()
PresenceEmpty();
}

void CZMDiscordSystem::Shutdown()
{
Discord_Shutdown();
}

void CZMDiscordSystem::LevelInitPostEntity()
{
m_nLastPlayerCount = -1;
m_flNextDiscordUpdateTime = 0.0f;

Update( 0.0f );
}

void CZMDiscordSystem::LevelShutdownPostEntity()
{
m_nLastPlayerCount = -1;

PresenceEmpty();
m_flNextDiscordUpdateTime = 0.0f;
}

void CZMDiscordSystem::Update( float frametime )
{
Discord_RunCallbacks();
m_pDiscordCore->RunCallbacks();

if ( m_flNextDiscordUpdateTime <= gpGlobals->realtime )
{
if ( IsConnected() )
if ( IsInGame() )
{
if ( IsInGame() )
PresenceInGame();
else
PresenceEmpty();
PresenceInGame();
}
else
{
PresenceEmpty();
}


m_flNextDiscordUpdateTime = gpGlobals->realtime + 0.2f;
}
Expand All @@ -161,36 +128,18 @@ bool CZMDiscordSystem::IsInGame()
return engine->IsInGame() && !engine->IsLevelMainMenuBackground();
}

void CZMDiscordSystem::SetDisconnected()
{
g_ZMDiscordSystem.m_bConnected = false;

g_ZMDiscordSystem.m_nLastPlayerCount = -1;
g_ZMDiscordSystem.m_flNextDiscordUpdateTime = 0.0f;
}

void CZMDiscordSystem::SetConnected()
{
g_ZMDiscordSystem.m_bConnected = true;

g_ZMDiscordSystem.m_nLastPlayerCount = -1;
g_ZMDiscordSystem.m_flNextDiscordUpdateTime = 0.0f;
}

void CZMDiscordSystem::PresenceEmpty()
{
// Don't bother updating again.
if ( m_nLastPlayerCount == 0 )
return;


DiscordRichPresence p = { 0 };
p.startTimestamp = m_GameStartTime;
p.largeImageKey = "zmrmain";


Discord_UpdatePresence( &p );

discord::Activity activity {};
activity.SetState( "In Menu" );
activity.GetAssets().SetLargeImage( "zmrmain" );
activity.GetTimestamps().SetStart( m_GameStartTime );
m_pDiscordCore->ActivityManager().UpdateActivity( activity, []( discord::Result result ) {} );

m_nLastPlayerCount = 0;
}
Expand Down Expand Up @@ -225,14 +174,12 @@ void CZMDiscordSystem::PresenceInGame()
nPlayers,
nMaxPlayers );


DiscordRichPresence p = { 0 };

p.details = details;
p.startTimestamp = m_GameStartTime;
p.largeImageKey = "zmrmain";
Discord_UpdatePresence( &p );

discord::Activity activity {};
activity.SetState( "Playing" );
activity.SetDetails( details );
activity.GetAssets().SetLargeImage( "zmrmain" );
activity.GetTimestamps().SetStart( m_GameStartTime );
m_pDiscordCore->ActivityManager().UpdateActivity( activity, []( discord::Result result ) {} );

m_nLastPlayerCount = nPlayers;
}
Expand All @@ -247,11 +194,8 @@ void CZMDiscordSystem::UpdateGameStartTime()

void CZMDiscordSystem::InitDiscord()
{
DiscordEventHandlers hndlrs = { 0 };
hndlrs.ready = Discord_Event_Ready;
hndlrs.disconnected = Discord_Event_Disconnected;
hndlrs.errored = Discord_Event_Error;


Discord_Initialize( DISCORD_APP_ID, &hndlrs, 1, nullptr );
auto result = discord::Core::Create( DISCORD_APP_ID, DiscordCreateFlags_NoRequireDiscord, &m_pDiscordCore );
if ( result != discord::Result::Ok ) {
Warning( "Failed to init Discord rich presence. Returned result %i.\n", result );
}
}
Binary file removed mp/src/lib/public/discord-rpc.lib
Binary file not shown.
Binary file added mp/src/lib/public/discord_game_sdk.dll.lib
Binary file not shown.
Loading

0 comments on commit 6d8089e

Please sign in to comment.