Skip to content

Commit

Permalink
fix problem with client getting wrong strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikk155 committed Apr 7, 2024
1 parent b58d3a7 commit d76650b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 63 deletions.
11 changes: 7 additions & 4 deletions src/game/client/discord/CDiscord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ void CDiscord :: RPCStateUpdate()
g_Discord.m_szHeader = "In main menu";
}

if( !g_Discord.m_szDescription || g_Discord.m_szDescription[0] == '\0' || !gEngfuncs.GetEntityByIndex(0) )
{
if( FStrEq( g_Discord.m_szHeader, "In main menu" )
&& !FStrEq( g_Discord.m_szDescription, "Just started playing." )
&& !gEngfuncs.GetEntityByIndex(0)
// NO FUCKING IDEA WHY THEY'RE BOTH EQUALS BUT THEY ARE.
|| FStrEq( g_Discord.m_szHeader, g_Discord.m_szDescription )
) {
g_Discord.m_szDescription = "";
}

discordPresence.details = g_Discord.m_szHeader;
discordPresence.state = g_Discord.m_szDescription;
discordPresence.largeImageKey = g_Discord.m_szDefaultLogo;
discordPresence.details = g_Discord.m_szHeader;
Discord_UpdatePresence(&discordPresence);
}

Expand Down
3 changes: 1 addition & 2 deletions src/game/client/discord/CDiscord.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ class CDiscord final
void RPCStateUpdate();
void RPCShutDown();
const char* m_szDefaultLogo = "logo";
const char* m_szImage = "";
const char* m_szHeader = "";
const char* m_szDescription = "";
const char* m_szDescription = "Just started playing.";
};

inline CDiscord g_Discord;
20 changes: 14 additions & 6 deletions src/game/client/ui/hud/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,13 +392,21 @@ void CHud::MsgFunc_SetFOV(const char* pszName, BufferReader& reader)

void CHud :: MsgFunc_DiscordRPC(const char* pszName, BufferReader& reader)
{
const char* szHeader = reader.ReadString();
const char* szDescription = reader.ReadString();
const char* szString = reader.ReadString();
int i = reader.ReadByte();

if( szHeader )
g_Discord.m_szHeader = szHeader;
if( szDescription )
g_Discord.m_szDescription = szDescription;
if( szString )
{
if( i == 1 )
{
g_Discord.m_szHeader = szString;
}
else
{
//ConsolePrint( fmt::format( "m_szDescription {}\n", g_Discord.m_szDescription ).c_str() );
g_Discord.m_szDescription = szString;
}
}
}

void CHud::AddHudElem(CHudBase* phudelem)
Expand Down
106 changes: 60 additions & 46 deletions src/game/server/discord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,72 +19,86 @@
#include "UserMessages.h"
#include "util.h"
#include "CGameRules.h"
#include "world.h"

void CDiscordServer :: SendPresence()
#include <fmt/format.h>

void CDiscordServer :: SendMessage( const char* szCvar, int i )
{
if( gpGlobals->time < m_flNextThink )
return;
char str[61];

std::string szStr = g_DiscordServer.format( szCvar );

std::strcpy( str, szStr.c_str() );

MESSAGE_BEGIN( MSG_ALL, gmsgDiscordRPC );
WRITE_STRING( GetArguments( CVAR_GET_STRING( "discord_rpc_header" ) ) );
WRITE_STRING( GetArguments( CVAR_GET_STRING( "discord_rpc_description" ) ) );
WRITE_STRING( str );
WRITE_BYTE( i );
MESSAGE_END();
}

m_flNextThink = gpGlobals->time + 2.0f;
void CDiscordServer :: SendPresence()
{
if( m_flNextThink < gpGlobals->time )
{
SendMessage( "discord_rpc_header", 1 );
SendMessage( "discord_rpc_description", 0 );
m_flNextThink = gpGlobals->time + 2.0f;
}
}

const char* CDiscordServer :: GetArguments( const char* szCommand )
std::string CDiscordServer :: format( const char* szCvar )
{
if( !szCommand )
return "";
std::string szStr = std::string( CVAR_GET_STRING( szCvar ) );

std::string szString = std::string( szCommand );
if( !szStr.empty() )
{
if( szStr.find( "$players$" ) != std::string::npos )
{
int iPlayers = 0;

CBaseEntity::Logger->error( "szCommand {}", szCommand );
for( auto player : UTIL_FindPlayers() )
{
if( player && player->IsPlayer() && player->IsConnected() )
{
iPlayers++;
}
}

if( szString.find( "$players$") != std::string::npos )
{
int iplayer = 0;
szStr.replace( szStr.find("$players$"), std::string("$players$").size(), fmt::format( "{} of {}", iPlayers, gpGlobals->maxClients ) );
}

for( int i = 0; i <= gpGlobals->maxClients; i++ )
if( szStr.find( "$gamemode$") != std::string::npos )
{
if( auto player = UTIL_PlayerByIndex( i ); player && player->IsConnected() )
iplayer++;
}
std::string szRep = std::to_string( iplayer ) + " of " + std::to_string( gpGlobals->maxClients );
szString.replace( szString.find("$players$"), std::string("$players$").size(), szRep );
}
std::string szRep =
std::string(
( g_pGameRules->IsCoOp() ? "Co-Operative"
: g_pGameRules->IsTeamplay() ? "Team Play"
: g_pGameRules->IsCTF() ? "Capture The Flag"
: g_pGameRules->IsDeathmatch() ? "Death Match"
: "Single Player"
));

if( szString.find( "$gamemode$") != std::string::npos )
{
std::string szRep =
std::string(
( g_pGameRules->IsCoOp() ? "Co-Operative"
: g_pGameRules->IsTeamplay() ? "Team Play"
: g_pGameRules->IsCTF() ? "Capture The Flag"
: g_pGameRules->IsDeathmatch() ? "Death Match"
: "Single Player"
));
szString.replace( szString.find("$gamemode$"), std::string("$gamemode$").size(), std::string(szRep) );
}
szStr.replace( szStr.find("$gamemode$"), std::string("$gamemode$").size(), std::string(szRep) );
}

if( szString.find( "$server$") != std::string::npos )
{
szString.replace( szString.find("$server$"), std::string("$server$").size(), std::string(CVAR_GET_STRING( "hostname" )) );
}
if( szStr.find( "$server$") != std::string::npos )
{
szStr.replace( szStr.find("$server$"), std::string("$server$").size(), fmt::format( "{}", CVAR_GET_STRING( "hostname" ) ) );
}

if( szString.find( "$map$") != std::string::npos )
{
szString.replace( szString.find("$map$"), std::string("$map$").size(), std::string(STRING(gpGlobals->mapname)) );
if( szStr.find( "$map$") != std::string::npos )
{
CBaseEntity* pWorldspawn = CBaseEntity::Instance( INDEXENT(0) );
string_t MapName = ( pWorldspawn && !FStringNull( pWorldspawn->pev->message ) ? pWorldspawn->pev->message : gpGlobals->mapname );
szStr.replace( szStr.find("$map$"), std::string("$map$").size(), fmt::format( STRING( MapName ) ) );

Check failure on line 94 in src/game/server/discord.cpp

View workflow job for this annotation

GitHub Actions / Linux-x86

call to non-‘constexpr’ function ‘const char* STRING(string_t)’
}
}
szCommand = szString.c_str();
CBaseEntity::Logger->error( "szString {}", szCommand );

return szCommand;
return fmt::format( "{:<60}", szStr );
}

void CDiscordServer :: UpdatePresence()
{
CVAR_SET_STRING( "discord_rpc_header", "Playing $gamemode$ $players$" );
CVAR_SET_STRING( "discord_rpc_description", ( g_pGameRules->IsMultiplayer() ? "In server $server$" : "In map $map$" ) );
CVAR_SET_STRING( "discord_rpc_header", "Playing $gamemode$ $players$ in $map$" );
CVAR_SET_STRING( "discord_rpc_description", ( g_pGameRules->IsMultiplayer() ? "In server $server$" : "In $map$" ) );
}
14 changes: 9 additions & 5 deletions src/game/server/discord.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

class CDiscordServer final
{
public:
void SendPresence();
void UpdatePresence();
const char* GetArguments( const char* szCommand );
float m_flNextThink;
public:
void SendPresence();
void SendMessage( const char* szCvar, int i );
void UpdatePresence();

private:
float m_flNextThink;
std::string format( const char* szCvar );

};

inline CDiscordServer g_DiscordServer;

0 comments on commit d76650b

Please sign in to comment.