Skip to content

Commit

Permalink
Add server name to presence state
Browse files Browse the repository at this point in the history
Also fixes some edge cases where the rich presence won't be displayed properly (after enabling it for the first time, or when disconnecting from a server)
  • Loading branch information
Lpsd committed Oct 12, 2023
1 parent 97e6c7b commit bf80916
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Client/core/CCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2393,3 +2393,8 @@ std::shared_ptr<CDiscordInterface> CCore::GetDiscord()
{
return m_pDiscordRichPresence;
}

SString CCore::GetLastConnectedServerName() const
{
return m_strLastConnectedServerName;
}
6 changes: 6 additions & 0 deletions Client/core/CCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
void SetCustomStreamingMemory(size_t szMB);
bool IsUsingCustomStreamingMemorySize();
size_t GetStreamingMemory();

SString GetLastConnectedServerName() const;
void SetLastConnectedServerName(SString strServerName) { m_strLastConnectedServerName = strServerName; }

private:
void ApplyCoreInitSettings();

Expand Down Expand Up @@ -386,6 +390,8 @@ class CCore : public CCoreInterface, public CSingleton<CCore>
bool m_bIsRenderingGrass;
bool m_bFakeLagCommandEnabled;

SString m_strLastConnectedServerName{};

// Command line
static void ParseCommandLine(std::map<std::string, std::string>& options, const char*& szArgs, const char** pszNoValOptions = NULL);
std::map<std::string, std::string> m_CommandLineOptions; // e.g. "-o option" -> {"o" = "option"}
Expand Down
1 change: 1 addition & 0 deletions Client/core/CDiscordRichPresence.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CDiscordRichPresence : public CDiscordInterface
bool IsDiscordCustomDetailsDisallowed() const;
bool IsDiscordRPCEnabled() const;
bool SetApplicationID(const char* szAppID);
int GetMaxServerNameLength() const;

// void SetPresenceTimestamp();
// void SetPresenceImage();
Expand Down
8 changes: 8 additions & 0 deletions Client/core/CSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3452,6 +3452,14 @@ void CSettings::SaveData()
CVARS_SET("allow_discord_rpc", bAllowDiscordRPC);
g_pCore->GetDiscord()->SetDiscordRPCEnabled(bAllowDiscordRPC);

if (bAllowDiscordRPC)
{
auto discord = g_pCore->GetDiscord();

if (discord)
discord->SetPresenceState(CCore::GetSingleton().IsConnected() ? SString("In-game (%s)", g_pCore->GetLastConnectedServerName()) : "Main menu", false);
}

// Grass
bool bGrassEnabled = m_pCheckBoxGrass->GetSelected();
CVARS_SET("grass", bGrassEnabled);
Expand Down
1 change: 1 addition & 0 deletions Client/mods/deathmatch/logic/CClientGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ CClientGame::~CClientGame()
if (discord && discord->IsDiscordRPCEnabled())
{
discord->ResetDiscordData();
discord->SetPresenceState("Main menu", false);
discord->UpdatePresence();
}

Expand Down
15 changes: 15 additions & 0 deletions Client/mods/deathmatch/logic/CPacketHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ void CPacketHandler::Packet_ServerJoined(NetBitStreamInterface& bitStream)
// 2 - URL
// unsigned short (2) - HTTP Download URL Size
// unsigned char (X) - HTTP Download URL
// unsigned char (X) - Server name

// Make sure any existing messageboxes are hided
g_pCore->RemoveMessageBox();
Expand Down Expand Up @@ -464,6 +465,20 @@ void CPacketHandler::Packet_ServerJoined(NetBitStreamInterface& bitStream)
g_pClientGame->m_pLocalPlayer->CallEvent("onClientPlayerJoin", Arguments, true);

g_pCore->UpdateRecentlyPlayed();

if (bitStream.Can(eBitStreamVersion::CPlayerJoinCompletePacket_ServerName))
{
auto discord = g_pCore->GetDiscord();
if (discord && discord->IsDiscordRPCEnabled())
{
std::string serverName;
bitStream.ReadString(serverName);

g_pCore->SetLastConnectedServerName(serverName);
discord->SetPresenceState(SString("In-game (%s)", serverName), false);
}
}

}

void CPacketHandler::Packet_ServerDisconnected(NetBitStreamInterface& bitStream)
Expand Down
3 changes: 3 additions & 0 deletions Client/sdk/core/CCoreInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ class CCoreInterface
virtual void SetCustomStreamingMemory(size_t sizeBytes) = 0;
virtual bool IsUsingCustomStreamingMemorySize() = 0;
virtual size_t GetStreamingMemory() = 0;

virtual SString GetLastConnectedServerName() const = 0;
virtual void SetLastConnectedServerName(SString strServerName) = 0;
};

class CClientTime
Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/CGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@ void CGame::JoinPlayer(CPlayer& Player)
Player.Send(CPlayerJoinCompletePacket(
Player.GetID(), m_pMapManager->GetRootElement()->GetID(), m_pMainConfig->GetHTTPDownloadType(), m_pMainConfig->GetHTTPPort(),
m_pMainConfig->GetHTTPDownloadURL().c_str(), m_pMainConfig->GetHTTPMaxConnectionsPerClient(), m_pMainConfig->GetEnableClientChecks(),
m_pMainConfig->IsVoiceEnabled(), m_pMainConfig->GetVoiceSampleRate(), m_pMainConfig->GetVoiceQuality(), m_pMainConfig->GetVoiceBitrate()));
m_pMainConfig->IsVoiceEnabled(), m_pMainConfig->GetVoiceSampleRate(), m_pMainConfig->GetVoiceQuality(), m_pMainConfig->GetVoiceBitrate(), m_pMainConfig->GetServerName().c_str()));

marker.Set("CPlayerJoinCompletePacket");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ CPlayerJoinCompletePacket::CPlayerJoinCompletePacket()
m_ucSampleRate = 1;
m_ucQuality = 4;
m_uiBitrate = 0;
m_szServerName = "";
}

CPlayerJoinCompletePacket::CPlayerJoinCompletePacket(ElementID PlayerID, ElementID RootElementID, eHTTPDownloadType ucHTTPDownloadType,
unsigned short usHTTPDownloadPort, const char* szHTTPDownloadURL, int iHTTPMaxConnectionsPerClient,
int iEnableClientChecks, bool bVoiceEnabled, unsigned char ucSampleRate, unsigned char ucVoiceQuality,
unsigned int uiBitrate)
unsigned int uiBitrate, const char* szServerName)
{
m_PlayerID = PlayerID;
m_RootElementID = RootElementID;
Expand All @@ -43,6 +44,7 @@ CPlayerJoinCompletePacket::CPlayerJoinCompletePacket(ElementID PlayerID, Element
m_ucSampleRate = ucSampleRate;
m_ucQuality = ucVoiceQuality;
m_uiBitrate = uiBitrate;
m_szServerName = szServerName;

switch (m_ucHTTPDownloadType)
{
Expand Down Expand Up @@ -120,5 +122,8 @@ bool CPlayerJoinCompletePacket::Write(NetBitStreamInterface& BitStream) const
break;
}

if (BitStream.Can(eBitStreamVersion::CPlayerJoinCompletePacket_ServerName))
BitStream.WriteString(m_szServerName);

return true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CPlayerJoinCompletePacket final : public CPacket
CPlayerJoinCompletePacket();
CPlayerJoinCompletePacket(ElementID PlayerID, ElementID RootElementID, eHTTPDownloadType ucHTTPDownloadType, unsigned short usHTTPDownloadPort,
const char* szHTTPDownloadURL, int iHTTPMaxConnectionsPerClient, int iEnableClientChecks, bool bVoiceEnabled,
unsigned char ucSampleRate, unsigned char ucVoiceQuality, unsigned int uiBitrate);
unsigned char ucSampleRate, unsigned char ucVoiceQuality, unsigned int uiBitrate, const char* szServerName);

ePacketID GetPacketID() const { return PACKET_ID_SERVER_JOINEDGAME; };
unsigned long GetFlags() const { return PACKET_HIGH_PRIORITY | PACKET_RELIABLE | PACKET_SEQUENCED; };
Expand All @@ -40,4 +40,5 @@ class CPlayerJoinCompletePacket final : public CPacket
unsigned char m_ucSampleRate;
unsigned char m_ucQuality;
unsigned int m_uiBitrate;
const char* m_szServerName;
};
4 changes: 4 additions & 0 deletions Shared/sdk/net/bitstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ enum class eBitStreamVersion : unsigned short
// 2023-09-09
WorldSpecialProperty_FireballDestruct,

// Send server name to player in CPlayerJoinCompletePacket
// 2023-10-12
CPlayerJoinCompletePacket_ServerName,

// This allows us to automatically increment the BitStreamVersion when things are added to this enum.
// Make sure you only add things above this comment.
Next,
Expand Down

0 comments on commit bf80916

Please sign in to comment.