Skip to content

Commit

Permalink
Fix Discord resetting whenever any resource stops (#3237)
Browse files Browse the repository at this point in the history
  • Loading branch information
znvjder authored Nov 11, 2023
1 parent 636220f commit 3b1dc67
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
14 changes: 13 additions & 1 deletion Client/core/CDiscordRichPresence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ CDiscordRichPresence::CDiscordRichPresence() : m_uiDiscordAppStart(0), m_uiDisco
SetDefaultData();

m_strDiscordAppState.clear();
m_strDiscordCustomResourceName.clear();
m_bConnected = false;
}

Expand All @@ -50,6 +51,9 @@ void CDiscordRichPresence::InitializeDiscord()

void CDiscordRichPresence::ShutdownDiscord()
{
if (!m_bDiscordRPCEnabled)
return;

Discord_ClearPresence();
Discord_Shutdown();

Expand All @@ -58,6 +62,9 @@ void CDiscordRichPresence::ShutdownDiscord()

void CDiscordRichPresence::RestartDiscord()
{
if (!m_bDiscordRPCEnabled)
return;

ShutdownDiscord();
InitializeDiscord();
}
Expand All @@ -72,6 +79,7 @@ void CDiscordRichPresence::SetDefaultData()
m_strDiscordAppAssetSmallText = DEFAULT_APP_ASSET_SMALL_TEXT;

m_strDiscordAppCurrentId = DEFAULT_APP_ID;
m_strDiscordCustomResourceName.clear();
m_strDiscordAppCustomDetails.clear();
m_strDiscordAppCustomState.clear();

Expand Down Expand Up @@ -225,7 +233,7 @@ bool CDiscordRichPresence::ResetDiscordData()
return true;
}

bool CDiscordRichPresence::SetApplicationID(const char* szAppID)
bool CDiscordRichPresence::SetApplicationID(const char* szResourceName, const char* szAppID)
{
m_strDiscordAppCurrentId = (szAppID && *szAppID) ? szAppID : DEFAULT_APP_ID;

Expand All @@ -234,6 +242,10 @@ bool CDiscordRichPresence::SetApplicationID(const char* szAppID)
RestartDiscord();
m_bUpdateRichPresence = true;
}

if (*szResourceName)
m_strDiscordCustomResourceName = szResourceName;

return true;
}

Expand Down
4 changes: 3 additions & 1 deletion Client/core/CDiscordRichPresence.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ class CDiscordRichPresence : public CDiscordInterface
bool SetDiscordRPCEnabled(bool bEnabled);
bool IsDiscordCustomDetailsDisallowed() const;
bool IsDiscordRPCEnabled() const;
bool SetApplicationID(const char* szAppID);
bool SetApplicationID(const char* resourceName, const char* szAppID);
void SetDiscordClientConnected(bool bConnected) { m_bConnected = bConnected; };
bool IsDiscordClientConnected() const;
std::string GetDiscordResourceName() const { return m_strDiscordCustomResourceName; };

// handlers
static void HandleDiscordReady(const struct DiscordUser* pDiscordUser);
Expand All @@ -59,6 +60,7 @@ class CDiscordRichPresence : public CDiscordInterface
std::string m_strDiscordAppAssetSmall;
std::string m_strDiscordAppAssetSmallText;

std::string m_strDiscordCustomResourceName;
std::string m_strDiscordAppCurrentId;
std::string m_strDiscordAppState;
std::string m_strDiscordAppDetails;
Expand Down
13 changes: 8 additions & 5 deletions Client/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,15 @@ void CResource::Stop()

// When a custom application is used - reset discord stuff
const auto discord = g_pCore->GetDiscord();
if (discord && discord->IsDiscordRPCEnabled() && !discord->IsDiscordCustomDetailsDisallowed())
if (discord && !discord->IsDiscordCustomDetailsDisallowed() && discord->GetDiscordResourceName() == m_strResourceName)
{
discord->ResetDiscordData();
discord->SetPresenceState(_("In-game"), false);
discord->SetPresenceStartTimestamp(time(nullptr));
discord->UpdatePresence();
if (discord->IsDiscordRPCEnabled())
{
discord->ResetDiscordData();
discord->SetPresenceState(_("In-game"), false);
discord->SetPresenceStartTimestamp(time(nullptr));
discord->UpdatePresence();
}
}
}

Expand Down
13 changes: 11 additions & 2 deletions Client/mods/deathmatch/logic/luadefs/CLuaDiscordDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ bool CLuaDiscordDefs::SetState(std::string strState)
return true;
}

bool CLuaDiscordDefs::SetAppID(std::string strAppID)
bool CLuaDiscordDefs::SetAppID(lua_State* luaVM, std::string strAppID)
{
int appIDLength = strAppID.length();

Expand All @@ -100,7 +100,16 @@ bool CLuaDiscordDefs::SetAppID(std::string strAppID)

auto discord = g_pCore->GetDiscord();

if (!discord || !discord->IsDiscordRPCEnabled() || !discord->SetApplicationID(strAppID.c_str()))
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
const char* resourceName = "";
if (pLuaMain)
{
CResource* pResource = pLuaMain->GetResource();
if (pResource)
resourceName = pResource->GetName();
}

if (!discord || !discord->IsDiscordRPCEnabled() || !discord->SetApplicationID(resourceName, strAppID.c_str()))
return false;

return true;
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaDiscordDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CLuaDiscordDefs : public CLuaDefs
private:
static bool ResetData();
static bool SetState(std::string strState);
static bool SetAppID(std::string strAppID);
static bool SetAppID(lua_State* luaVM, std::string strAppID);
static bool SetDetails(std::string strDetails);
static bool SetAsset(std::string szAsset, std::string szAssetText, bool bIsLarge = false);
static bool SetButtons(unsigned short int iIndex, std::string szName, std::string szUrl);
Expand Down
3 changes: 2 additions & 1 deletion Client/sdk/core/CDiscordInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CDiscordInterface
virtual ~CDiscordInterface() = default;
virtual void UpdatePresence() = 0;
virtual bool SetPresenceDetails(const char* szDetails, bool bCustom) = 0;
virtual bool SetApplicationID(const char* szAppID) = 0;
virtual bool SetApplicationID(const char* szResourceName, const char* szAppID) = 0;
virtual bool ResetDiscordData() = 0;
virtual bool SetPresenceState(const char* szState, bool bCustom) = 0;
virtual void SetAssetLargeData(const char* szAsset, const char* szAssetText) = 0;
Expand All @@ -28,6 +28,7 @@ class CDiscordInterface
virtual void SetPresenceEndTimestamp(const unsigned long ulEnd) = 0;
virtual bool SetPresenceButtons(unsigned short int iIndex, const char* szName, const char* szUrl) = 0;
virtual void SetPresencePartySize(int iSize, int iMax, bool bCustom) = 0;
virtual std::string GetDiscordResourceName() const = 0;

virtual bool SetDiscordRPCEnabled(bool bEnabled) = 0;
virtual void SetDiscordClientConnected(bool bConnected) = 0;
Expand Down

0 comments on commit 3b1dc67

Please sign in to comment.