Skip to content

Commit

Permalink
Merge branch 'discord_game_sdk' of https://github.com/bachingo/TF2Vin…
Browse files Browse the repository at this point in the history
…tage into bachingo-discord_game_sdk
  • Loading branch information
Deathreus committed Nov 6, 2020
2 parents 8dfec7a + 75eb8a8 commit 6410c92
Show file tree
Hide file tree
Showing 60 changed files with 11,093 additions and 179 deletions.
Binary file removed game/tf2vintage/bin/discord-rpc.dll
Binary file not shown.
13 changes: 9 additions & 4 deletions src/game/client/cdll_client_int.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
#include "haptics/haptic_msgs.h"

// Discord RPC
#include "discord_register.h"
#include "discord.h"
ConVar cl_discord_appid( "cl_discord_appid", "451227888230858752", FCVAR_DEVELOPMENTONLY | FCVAR_PROTECTED, "This is for your Client ID for Discord Applications and is unique per sourcemod." );

#ifdef TF_VINTAGE_CLIENT
Expand Down Expand Up @@ -1321,9 +1321,14 @@ int CHLClient::Init( CreateInterfaceFn appSystemFactory, CreateInterfaceFn physi
// Discord RPC
if (!g_bTextMode)
{
char command[256];
Q_snprintf( command, sizeof( command ), "%s -game \"%s\" -novid -high -steam\n", CommandLine()->GetParm( 0 ), CommandLine()->ParmValue( "-game" ) );
Discord_Register( cl_discord_appid.GetString(), command );
discord::Core *core{};
auto result = discord::Core::Create( V_atoi64( cl_discord_appid.GetString() ), DiscordCreateFlags_NoRequireDiscord, &core );
if ( result != discord::Result::Ok )
return true;

char command[512];
V_snprintf( command, sizeof( command ), "%s -game \"%s\" -novid -steam", CommandLine()->GetParm( 0 ), CommandLine()->ParmValue( "-game" ) );
core->ActivityManager().RegisterCommand( command );
}

return true;
Expand Down
38 changes: 36 additions & 2 deletions src/game/client/client_base.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ $Configuration

$Compiler
{
$AdditionalIncludeDirectories ".\;$BASE;$SRCDIR\vgui2\include;$SRCDIR\vgui2\controls;$SRCDIR\game\shared;.\game_controls;$SRCDIR\thirdparty\sixensesdk\include"
$AdditionalIncludeDirectories ".\;$BASE;$SRCDIR\vgui2\include;$SRCDIR\vgui2\controls;$SRCDIR\game\shared;.\game_controls;$SRCDIR\thirdparty\sixensesdk\include;$SRCDIR\thirdparty\discord_game_sdk\cpp"
$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;fopen=dont_use_fopen" [$WIN32]
$PreprocessorDefinitions "$BASE;USE_WEBM_FOR_REPLAY;" [$LINUXALL]
Expand Down Expand Up @@ -661,6 +661,40 @@ $Project
$File "game_controls\IconPanel.cpp"
}

$Folder "Discord RPC"
{
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\achievement_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\achievement_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\activity_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\activity_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\application_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\application_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\core.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\core.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\discord.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\event.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\ffi.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\image_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\image_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\lobby_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\lobby_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\network_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\network_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\overlay_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\overlay_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\relationship_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\relationship_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\storage_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\storage_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\store_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\store_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\types.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\types.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\user_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\user_manager.h"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\voice_manager.cpp"
$File "$SRCDIR\thirdparty\discord_game_sdk\cpp\voice_manager.h"
}
}

$Folder "MP3" [$WIN32||$POSIX]
Expand Down Expand Up @@ -1256,7 +1290,7 @@ $Project
$Lib vgui_controls
$Lib vtf
$ImpLib steam_api
$Lib discord-rpc
$Lib "discord_game_sdk.dll"

$Libexternal $LIBCOMMON/libcrypto [$OSXALL]
$Libexternal "$SRCDIR\lib\common\$(CRYPTOPPDIR)\libcrypto" [$LINUXALL]
Expand Down
96 changes: 50 additions & 46 deletions src/game/client/tf/tf_presence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
#include "steam/isteamfriends.h"
#include "steam/steam_api.h"
#include "tier0/icommandline.h"
#include "discord_rpc.h"
#include "discord_register.h"
#include "discord.h"
#include <time.h>
#include <functional>

Expand Down Expand Up @@ -575,11 +574,10 @@ void CTFPresence::UploadStats()
#endif
}

#define DECL_DISCORD_HANDLER(obj, name, handler) obj.##name = &rpc->##handler

DiscordRichPresence CTFDiscordPresence::m_sPresence;
RealTimeCountdownTimer CTFDiscordPresence::m_updateThrottle;
int64 CTFDiscordPresence::m_iCreationTimestamp;
discord::Core *CTFDiscordPresence::m_pCore{};
discord::Activity CTFDiscordPresence::m_Activity{};
discord::User CTFDiscordPresence::m_CurrentUser{};

CTFDiscordPresence::CTFDiscordPresence()
: BaseClass( "TFDiscordPresence" )
Expand Down Expand Up @@ -614,8 +612,8 @@ void CTFDiscordPresence::FireGameEvent( IGameEvent *event )
if ( C_BasePlayer::GetLocalPlayer()->GetSteamID( &steamID ) )
Q_snprintf( m_szSteamID, 65, "%d", steamID.ConvertToUint64() );

m_sPresence.joinSecret = m_szServerInfo;
m_sPresence.partyId = m_szSteamID;
m_Activity.GetSecrets().SetJoin( m_szServerInfo );
m_Activity.GetSecrets().SetMatch( m_szSteamID );
}

if ( !engine->IsConnected() )
Expand All @@ -626,7 +624,7 @@ void CTFDiscordPresence::FireGameEvent( IGameEvent *event )
if ( !TFPlayerResource() )
return;

int maxPlayers = gpGlobals->maxClients;
const int maxPlayers = gpGlobals->maxClients;
int curPlayers = 0;

for ( int i = 0; i < maxPlayers; ++i )
Expand All @@ -635,8 +633,8 @@ void CTFDiscordPresence::FireGameEvent( IGameEvent *event )
curPlayers++;
}

m_sPresence.partySize = curPlayers;
m_sPresence.partyMax = maxPlayers;
m_Activity.GetParty().GetSize().SetCurrentSize( curPlayers );
m_Activity.GetParty().GetSize().SetMaxSize( maxPlayers );
}
else if ( name == "player_death" )
{
Expand Down Expand Up @@ -667,20 +665,18 @@ void CTFDiscordPresence::FireGameEvent( IGameEvent *event )
//-----------------------------------------------------------------------------
bool CTFDiscordPresence::Init( void )
{
Q_memset( &m_sPresence, 0, sizeof( m_sPresence ) );

DiscordEventHandlers handlers{};
DECL_DISCORD_HANDLER( handlers, ready, OnReady );
DECL_DISCORD_HANDLER( handlers, disconnected, OnDisconnected );
DECL_DISCORD_HANDLER( handlers, errored, OnError );
DECL_DISCORD_HANDLER( handlers, joinGame, OnJoinedGame );
DECL_DISCORD_HANDLER( handlers, spectateGame, OnSpectateGame );
DECL_DISCORD_HANDLER( handlers, joinRequest, OnJoinRequested );
Q_memset( &m_Activity, 0, sizeof( discord::Activity ) );
auto result = discord::Core::Create( V_atoi64( cl_discord_appid.GetString() ), DiscordCreateFlags_NoRequireDiscord, &m_pCore );
if ( result != discord::Result::Ok )
return false;

char command[512];
V_snprintf( command, sizeof( command ), "%s -game \"%s\" -novid -steam\n", CommandLine()->GetParm( 0 ), CommandLine()->ParmValue( "-game" ) );
Discord_Register( cl_discord_appid.GetString(), command );
Discord_Initialize( cl_discord_appid.GetString(), &handlers, false, CFmtStr( "%d", engine->GetAppID() ) );
V_snprintf( command, sizeof( command ), "%s -game \"%s\" -novid -steam", CommandLine()->GetParm( 0 ), CommandLine()->ParmValue( "-game" ) );
m_pCore->ActivityManager().RegisterCommand( command );
m_pCore->ActivityManager().RegisterSteam( engine->GetAppID() );

Q_memset( &m_CurrentUser, 0, sizeof( discord::User ) );
m_pCore->UserManager().GetCurrentUser( &m_CurrentUser );

ListenForGameEvent( "localplayer_changeteam" );
ListenForGameEvent( "localplayer_changeclass" );
Expand All @@ -704,7 +700,9 @@ void CTFDiscordPresence::Shutdown( void )
Assert( rpc == this );
rpc = NULL;

Discord_Shutdown();
Q_memset( &m_Activity, 0, sizeof( discord::Activity ) );
Q_memset( &m_CurrentUser, 0, sizeof( discord::User ) );
if ( m_pCore ) delete m_pCore;

if ( steamapicontext->SteamFriends() )
steamapicontext->SteamFriends()->ClearRichPresence();
Expand All @@ -721,9 +719,9 @@ void CTFDiscordPresence::Update( float frametime )
UpdatePresence();

if ( gpGlobals->tickcount % 2 )
Discord_RunCallbacks();
m_pCore->RunCallbacks();
}

/*
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -790,13 +788,13 @@ void CTFDiscordPresence::OnJoinRequested( const DiscordUser *joinRequest )
ConColorMsg( Color( 114, 137, 218, 255 ), "[Rich Presence] Join Request Accepted\n" );
Discord_Respond( joinRequest->userId, DISCORD_REPLY_YES );
}

*/
//-----------------------------------------------------------------------------
// Purpose: Map initialization
//-----------------------------------------------------------------------------
void CTFDiscordPresence::LevelInitPostEntity( void )
{
Q_memset( &m_sPresence, 0, sizeof( m_sPresence ) );
Q_memset( &m_Activity, 0, sizeof( discord::Activity ) );

char buffer[64];
Q_snprintf( buffer, sizeof( buffer ), "#TF_Map_%s", m_szMapName );
Expand All @@ -805,12 +803,12 @@ void CTFDiscordPresence::LevelInitPostEntity( void )
{
g_pVGuiLocalize->ConvertUnicodeToANSI( mapName, buffer, sizeof( buffer ) );
Q_snprintf( m_szGameState, sizeof( m_szGameState ), "Map: %s", buffer );
m_sPresence.largeImageKey = m_szMapName;
m_Activity.GetAssets().SetLargeImage( m_szMapName );
}
else
{
Q_snprintf( m_szGameState, sizeof( m_szGameState ), "Map: %s", m_szMapName );
m_sPresence.largeImageKey = "default";
m_Activity.GetAssets().SetLargeImage( "default" );
}


Expand All @@ -820,14 +818,14 @@ void CTFDiscordPresence::LevelInitPostEntity( void )
if ( gameType )
{
g_pVGuiLocalize->ConvertUnicodeToANSI( gameType, m_szGameType, DISCORD_FIELD_MAXLEN );
m_sPresence.largeImageText = m_szGameType;
m_Activity.GetAssets().SetLargeText( m_szGameType );
}
}

m_sPresence.details = m_szHostName;
m_sPresence.state = m_szGameState;
m_sPresence.smallImageKey = "tf2v_drp_logo";
m_sPresence.startTimestamp = m_iCreationTimestamp;
m_Activity.SetDetails( m_szHostName );
m_Activity.SetState( m_szGameState );
m_Activity.GetAssets().SetSmallImage( "tf2v_drp_logo" );
m_Activity.GetTimestamps().SetStart( m_iCreationTimestamp );

if ( steamapicontext->SteamFriends() )
{
Expand All @@ -838,7 +836,9 @@ void CTFDiscordPresence::LevelInitPostEntity( void )
steamapicontext->SteamFriends()->SetRichPresence( "steam_display", m_szMapName );
}

Discord_UpdatePresence( &m_sPresence );
m_pCore->ActivityManager().UpdateActivity( m_Activity, [ ] ( discord::Result result ) {
ConColorMsg( Color( 114, 137, 218, 255 ), "[DRP] Activity update: %s\n", ( (result == discord::Result::Ok) ? "Ok" : "Failed" ) );
} );
}

//-----------------------------------------------------------------------------
Expand All @@ -854,7 +854,7 @@ void CTFDiscordPresence::LevelShutdownPreEntity( void )
//-----------------------------------------------------------------------------
void CTFDiscordPresence::Reset( void )
{
Q_memset( &m_sPresence, 0, sizeof( m_sPresence ) );
Q_memset( &m_Activity, 0, sizeof( discord::Activity ) );

if ( steamapicontext->SteamFriends() )
{
Expand All @@ -865,10 +865,12 @@ void CTFDiscordPresence::Reset( void )
steamapicontext->SteamFriends()->SetRichPresence( "steam_player_group_size", NULL );
}

m_sPresence.details = "Main Menu";
m_sPresence.largeImageKey = "tf2v_drp_logo";
m_sPresence.startTimestamp = m_iCreationTimestamp;
Discord_UpdatePresence( &m_sPresence );
m_Activity.SetDetails( "Main Menu" );
m_Activity.GetAssets().SetLargeImage( "tf2v_drp_logo" );
m_Activity.GetTimestamps().SetStart( m_iCreationTimestamp );
m_pCore->ActivityManager().UpdateActivity( m_Activity, [ ] ( discord::Result result ) {
ConColorMsg( Color( 114, 137, 218, 255 ), "[DRP] Activity update: %s\n", ( (result == discord::Result::Ok) ? "Ok" : "Failed" ) );
} );
}

//-----------------------------------------------------------------------------
Expand All @@ -893,19 +895,21 @@ void CTFDiscordPresence::UpdatePresence( bool bForce, bool bIsDead )
{
case TF_TEAM_RED:
{
m_sPresence.smallImageKey = s_pClassImages[iClassNum].redTeamImage;
m_sPresence.smallImageText = m_szClassName;
m_Activity.GetAssets().SetSmallImage( s_pClassImages[iClassNum].redTeamImage );
m_Activity.GetAssets().SetSmallText( m_szClassName );
break;
}
case TF_TEAM_BLUE:
{
m_sPresence.smallImageKey = s_pClassImages[iClassNum].bluTeamImage;
m_sPresence.smallImageText = m_szClassName;
m_Activity.GetAssets().SetSmallImage( s_pClassImages[iClassNum].bluTeamImage );
m_Activity.GetAssets().SetSmallText( m_szClassName );
break;
}
default:
break;
}

Discord_UpdatePresence( &m_sPresence );
m_pCore->ActivityManager().UpdateActivity( m_Activity, [ ] ( discord::Result result ) {
ConColorMsg( Color( 114, 137, 218, 255 ), "[DRP] Activity update: %s\n", ( (result == discord::Result::Ok) ? "Ok" : "Failed" ) );
} );
}
21 changes: 7 additions & 14 deletions src/game/client/tf/tf_presence.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ class CTFPresence : public CBasePresence, public CGameEventListener

};

struct DiscordUser;
struct DiscordRichPresence;

namespace discord { class Core; class Activity; class User; }
#define DISCORD_FIELD_MAXLEN 128

class CTFDiscordPresence : public CAutoGameSystemPerFrame, public CGameEventListener
Expand All @@ -69,14 +67,6 @@ class CTFDiscordPresence : public CAutoGameSystemPerFrame, public CGameEventList
void UpdatePresence( bool bForce = false, bool bIsDead = false );
void SetLevelName( char const *pMapName ) { Q_strncpy( m_szMapName, pMapName, MAX_MAP_NAME ); }

// Discord handlers
static void OnReady( const DiscordUser *request );
static void OnDisconnected( int errorCode, const char *message );
static void OnError( int errorCode, const char *message );
static void OnJoinedGame( const char *joinSecret );
static void OnSpectateGame( const char *spectateSecret );
static void OnJoinRequested( const DiscordUser *request );

private:
// Updates run asyncrhonous, so stack allocation in a no go
char m_szMapName[ MAX_MAP_NAME ];
Expand All @@ -87,9 +77,12 @@ class CTFDiscordPresence : public CAutoGameSystemPerFrame, public CGameEventList
char m_szGameState[ DISCORD_FIELD_MAXLEN ];
char m_szClassName[ DISCORD_FIELD_MAXLEN ];

static RealTimeCountdownTimer m_updateThrottle;
static int64 m_iCreationTimestamp;
static DiscordRichPresence m_sPresence;
RealTimeCountdownTimer m_updateThrottle;
int64 m_iCreationTimestamp;

static discord::Core *m_pCore;
static discord::Activity m_Activity;
static discord::User m_CurrentUser;
};

extern CTFDiscordPresence *rpc;
Expand Down
Binary file removed src/lib/public/discord-rpc.lib
Binary file not shown.
Binary file added src/lib/public/discord_game_sdk.dll.lib
Binary file not shown.
26 changes: 0 additions & 26 deletions src/public/discord_register.h

This file was deleted.

Loading

0 comments on commit 6410c92

Please sign in to comment.