Skip to content

Add UserID Sharing Capability with Consent Dialog (Related with Discord-RPC) #3267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Client/core/CClientVariables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ void CClientVariables::LoadDefaults()
DEFAULT("browser_remote_javascript", true); // Execute javascript on remote websites?
DEFAULT("filter_duplicate_log_lines", true); // Filter duplicate log lines for debug view and clientscript.log
DEFAULT("always_show_transferbox", false); // Should the transfer box always be visible for downloads? (and ignore scripted control)
DEFAULT("allow_discord_rpc", true); // Enable Discord Rich Presence
DEFAULT("allow_discord_rpc", true); // Enable Discord Rich Presence
DEFAULT("discord_rpc_share_data", false); // Consistent Rich Presence data sharing
DEFAULT("discord_rpc_share_data_firsttime", false); // Display the user data sharing consent dialog box - for the first time
DEFAULT("_beta_qc_rightclick_command", _S("reconnect")); // Command to run when right clicking quick connect (beta - can be removed at any time)

if (!Exists("locale"))
Expand Down
20 changes: 20 additions & 0 deletions Client/core/CDiscordRichPresence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ void CDiscordRichPresence::SetPresencePartySize(int iSize, int iMax, bool bCusto
}
}

void CDiscordRichPresence::SetDiscordUserID(const std::string& strUserID)
{
if (CVARS_GET_VALUE<bool>("discord_rpc_share_data"))
m_strDiscordUserID = strUserID;
}

std::string CDiscordRichPresence::GetDiscordUserID() const
{
if (CVARS_GET_VALUE<bool>("discord_rpc_share_data"))
return m_strDiscordUserID;

return {};
};

#ifdef DISCORD_DISABLE_IO_THREAD
void CDiscordRichPresence::UpdatePresenceConnection()
{
Expand All @@ -290,15 +304,21 @@ void CDiscordRichPresence::UpdatePresenceConnection()
void CDiscordRichPresence::HandleDiscordReady(const DiscordUser* pDiscordUser)
{
if (const auto discord = g_pCore->GetDiscord(); discord && discord->IsDiscordRPCEnabled())
{
discord->SetDiscordClientConnected(true);
discord->SetDiscordUserID(pDiscordUser->userId);
}
}

void CDiscordRichPresence::HandleDiscordDisconnected(int iErrorCode, const char* szMessage)
{
WriteDebugEvent(SString("[DISCORD] Disconnected %s (error #%d)", szMessage, iErrorCode));

if (const auto discord = g_pCore->GetDiscord(); discord)
{
discord->SetDiscordUserID("");
discord->SetDiscordClientConnected(false);
}
}

void CDiscordRichPresence::HandleDiscordError(int iErrorCode, const char* szMessage)
Expand Down
3 changes: 3 additions & 0 deletions Client/core/CDiscordRichPresence.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class CDiscordRichPresence : public CDiscordInterface
void SetAssetSmallData(const char* szAsset, const char* szAssetText);
void SetDiscordClientConnected(bool bConnected) { m_bConnected = bConnected; };
void SetPresencePartySize(int iSize, int iMax, bool bCustom);
void SetDiscordUserID(const std::string& strUserID);

bool ResetDiscordData();
bool SetPresenceState(const char* szState, bool bCustom);
Expand All @@ -47,13 +48,15 @@ class CDiscordRichPresence : public CDiscordInterface
bool IsDiscordClientConnected() const { return m_bConnected; };

std::string GetDiscordResourceName() const { return m_strDiscordCustomResourceName; };
std::string GetDiscordUserID() const;

// static handlers
static void HandleDiscordReady(const struct DiscordUser* pDiscordUser);
static void HandleDiscordDisconnected(int iErrorCode, const char* szMessage);
static void HandleDiscordError(int iErrorCode, const char* szMessage);

private:
std::string m_strDiscordUserID;
std::string m_strDiscordAppId;
std::string m_strDiscordAppAsset;
std::string m_strDiscordAppAssetText;
Expand Down
14 changes: 13 additions & 1 deletion Client/core/CMainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ CMainMenu::CMainMenu(CGUI* pManager)
discord->SetPresenceState(_("Main menu"), false);
discord->SetPresenceStartTimestamp(0);
}

// Store the pointer to the graphics subsystem
m_pGraphics = CGraphics::GetSingletonPtr();

Expand Down Expand Up @@ -683,6 +682,19 @@ void CMainMenu::Update()
}
#endif

if (WaitForMenu == 299)
{
if (!g_pCore->GetCVars()->GetValue("discord_rpc_share_data_firsttime", false)
&& g_pCore->GetCVars()->GetValue("allow_discord_rpc", false)
&& !g_pCore->GetCVars()->GetValue("discord_rpc_share_data", false))
{
m_Settings.ShowRichPresenceShareDataQuestionBox();
CVARS_SET("discord_rpc_share_data_firsttime", true);
}
else
CVARS_SET("discord_rpc_share_data_firsttime", true);
}

if (WaitForMenu < 300)
WaitForMenu++;
}
Expand Down
30 changes: 29 additions & 1 deletion Client/core/CSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4515,10 +4515,38 @@ bool CSettings::OnAllowExternalSoundsClick(CGUIElement* pElement)
//
bool CSettings::OnAllowDiscordRPC(CGUIElement* pElement)
{
g_pCore->GetDiscord()->SetDiscordRPCEnabled(m_pCheckBoxAllowDiscordRPC->GetSelected());
bool isEnabled = m_pCheckBoxAllowDiscordRPC->GetSelected();
g_pCore->GetDiscord()->SetDiscordRPCEnabled(isEnabled);

if (isEnabled)
ShowRichPresenceShareDataQuestionBox(); // show question box

return true;
}

static void ShowRichPresenceShareDataCallback(void* ptr, unsigned int uiButton)
{
CCore::GetSingleton().GetLocalGUI()->GetMainMenu()->GetQuestionWindow()->Reset();

CVARS_SET("discord_rpc_share_data", static_cast<bool>(uiButton));
}

void CSettings::ShowRichPresenceShareDataQuestionBox() const
{
SStringX strMessage(
_("It seems that you have the Rich Presence connection option enabled."
"\nDo you want to allow servers to share their data?"
"\n\nThis includes yours unique ID identifier."));
CQuestionBox* pQuestionBox = CCore::GetSingleton().GetLocalGUI()->GetMainMenu()->GetQuestionWindow();
pQuestionBox->Reset();
pQuestionBox->SetTitle(_("CONSENT TO ALLOW DATA SHARING"));
pQuestionBox->SetMessage(strMessage);
pQuestionBox->SetButton(0, _("No"));
pQuestionBox->SetButton(1, _("Yes"));
pQuestionBox->SetCallback(ShowRichPresenceShareDataCallback);
pQuestionBox->Show();
}

//
// CustomizedSAFiles
//
Expand Down
1 change: 1 addition & 0 deletions Client/core/CSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ class CSettings
bool IsActive();

void SetSelectedIndex(unsigned int uiIndex);
void ShowRichPresenceShareDataQuestionBox() const;

protected:
const static int SecKeyNum = 3; // Number of secondary keys
Expand Down
12 changes: 11 additions & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaDiscordDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void CLuaDiscordDefs::LoadFunctions()
{"setDiscordRichPresencePartySize", ArgumentParser<SetPartySize>},
{"resetDiscordRichPresenceData", ArgumentParser<ResetData>},
{"isDiscordRichPresenceConnected", ArgumentParser <IsDiscordRPCConnected>},

{"getDiscordRichPresenceUserID", ArgumentParser<GetDiscordUserID>},
};

// Add functions
Expand Down Expand Up @@ -266,3 +266,13 @@ bool CLuaDiscordDefs::IsDiscordRPCConnected()

return discord->IsDiscordClientConnected();
}

std::string CLuaDiscordDefs::GetDiscordUserID()
{
auto discord = g_pCore->GetDiscord();

if (!discord || !discord->IsDiscordRPCEnabled())
return {};

return discord->GetDiscordUserID();
}
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 @@ -29,6 +29,6 @@ class CLuaDiscordDefs : public CLuaDefs
static bool SetEndTime(unsigned long ulTime);
static bool SetPartySize(int iMin, int iMax);
static bool IsDiscordRPCConnected();

static std::string GetDiscordUserID();
};

2 changes: 2 additions & 0 deletions Client/sdk/core/CDiscordInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CDiscordInterface
virtual void SetPresenceEndTimestamp(const unsigned long ulEnd) = 0;
virtual void SetPresencePartySize(int iSize, int iMax, bool bCustom) = 0;
virtual void SetDiscordClientConnected(bool bConnected) = 0;
virtual void SetDiscordUserID(const std::string& strUserID) = 0;

virtual bool SetPresenceDetails(const char* szDetails, bool bCustom) = 0;
virtual bool SetApplicationID(const char* szResourceName, const char* szAppID) = 0;
Expand All @@ -39,4 +40,5 @@ class CDiscordInterface
virtual bool IsDiscordClientConnected() const = 0;

virtual std::string GetDiscordResourceName() const = 0;
virtual std::string GetDiscordUserID() const = 0;
};