Skip to content

Commit afcc64c

Browse files
Rich Presence support using Discord Social SDK (#4457)
* Rich Presence support using Discord Social SDK Download DiscordSocialSdk-1.4.9649.zip from https://discord.com/developers/applications/1394782217405862001/social-sdk/downloads Add -DUSE_DISCORD:BOOL=ON to your cmake line. The Discord app needs to be set to be a public client in the OAuth2 tab. All Discord-related code are contained within one file, llstartup.cpp, and other classes access it through some opaque layer, static functions, otherwise we'd get these "duplicate symbol" linking errors. * Move Discord-related code to llappviewer.cpp The doFrame is the one called over and over again, so running the Discord callbacks from there shouldn't have one extra function overhead, while running the Discord initialisation is only once so it's much more okay to have the extra function overhead there. * panel_preferences_privacy tabs Add tab and checkboxes for discord social SDK integration options to panel_preferences_privacy.xml * Shorten Discord-related local variable names * Connect to Discord now through privacy tab Now the access token is saved the way passwords are saved, but without a username, so we can have some persistence without having to implement an OAuth2 backend server cause we would have to store those tokens there anyway still, and it's just simpler to not go that way. Discord Social SDK doesn't have a helper for sending code to a custom server anyway, that we would have to have some asynchronous HTTP requestor ready. Show location check button gets enabled only when Discord integration is enabled, though it's not functioning yet. * Location for Discord Rich Presence Activity State I was going to use LLAgentUI::buildLocationString but there's no location format that shows only region and coords without having to have the parcel name empty, so I copied buildLocationString implementation in the case of LOCATION_FORMAT_NO_MATURITY but when the parcel name is empty. I had to make updateDiscordActivity check agent's ID and the existence of agent avatar pointer first before trying to set Activity Details or State, cause I like the "Show location" button be checkable not only after online when both the ID & pointer will have existed. I think this way is simpler than programmatically enabling the "Show location" button after the user is logged in. I put a trigger to Activity update somewhere after the user is logged in for now, not yet after a TP. The elapsed time gets reset whenever Activity is updated for now, but I'll try to make elapsed time extended instead. No Party for now, because I couldn't find a way to make a Party shown without showing its CurrentSize (I could still get away not showing its MaxSize by setting it to 0), so the State (location) is shown above the elapsed time, not on the right of it. I'll try to figure out to get some representative numbers for its CurrentSize & MaxSize next. Also no privacy on hiding the username for now, until the UI is ready. * Update Rich Presence location on region change I had to find a spot in source code where it doesn't cause a crash (it did in LLAgent::setRegion), but I'm not removing the one in llstartup.cpp because on login, the one in llviewermessage.cpp gets only the placeholder coords (10, 10, 10). * Show display name too on Discord Rich Presence Avatar name cache can be used right away upon login now after I moved the update call to the end of PRECACHE section in llstartup. * Show Discord Rich Presence Activity Party By setting CurrentSize to the number of people within chat radius, and MaxSize to the number of people within near range. * Call updateDiscordActivity too in Discord init so when the user enables the integration after being logged in, the init can show the name and location right away. * Discord Rich Presence: Hide name & connect to llappviewer.cpp Add option to show/hide avatar name in privacy panel & connect rich presense directly to llappviewer.cpp * Discord time elapsed not reset on region change Time elapses right after viewer launch even before login. Plus parameter name change in header to make it the same as in implementation. * Cache bool setting retrievals in updateDiscordActivity As suggested by Andrey Kleschev. getBOOL and getF32 are expensive, so using `static LLCachedControl<>` is the way to do it in llappviewer.cpp. * Check Discord creds existence before getting token as suggested by Andrey Kleshchev, anticipating external factors such as user moving settings from another PC. * Tracy visibility for looped Discord function calls As suggested by Andrey Kleshchev. They likely can get pricey so they need to be visible in the profiler. * Discord-related error handling/logging plus delay saving Discord credentials to only after the access token is successfully updated on Discord, and try to disconnect from Discord when the integration gets disabled regardless whether there are credentials to delete or not and whether there's an access token to revoke or not. * Use getAvatars already called for Discord Party numbers so we don't have to make any extra getAvatars calls just for this, as it's pricy in crowds, and we'll just be piggybacking `updateSpeakerList` and `updateNearbyList`. * Assemble Discord Activity Details only once by saving it to a static global string for reuse. * Remove updateDiscordActivity call in startup loop The State field (region & coords) is updated well enough without it now. * Rename handleDiscordSocial to toggleDiscordIntegration * Update Discord Activity only when integration is enabled No need to check setting for the status change callback one, because getting there would need to be connected to Discord first, which in turn needs the integration to be enabled first. --------- Co-authored-by: Secret Foxtail <[email protected]>
1 parent afe5d29 commit afcc64c

File tree

12 files changed

+339
-4
lines changed

12 files changed

+339
-4
lines changed

indra/cmake/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ set(cmake_SOURCE_FILES
2020
Copy3rdPartyLibs.cmake
2121
DBusGlib.cmake
2222
DeploySharedLibs.cmake
23+
Discord.cmake
2324
DragDrop.cmake
2425
EXPAT.cmake
2526
FindAutobuild.cmake

indra/cmake/Discord.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
include(Prebuilt)
2+
3+
add_library(ll::discord INTERFACE IMPORTED)
4+
target_compile_definitions(ll::discord INTERFACE LL_DISCORD=1)
5+
6+
use_prebuilt_binary(discord)
7+
8+
target_include_directories(ll::discord SYSTEM INTERFACE ${LIBS_PREBUILT_DIR}/include)
9+
target_link_libraries(ll::discord INTERFACE discord_partner_sdk)

indra/newview/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ include(CMakeCopyIfDifferent)
1515
include(CubemapToEquirectangularJS)
1616
include(DBusGlib)
1717
include(DragDrop)
18+
if (USE_DISCORD)
19+
include(Discord)
20+
endif ()
1821
include(EXPAT)
1922
include(Hunspell)
2023
include(JPEGEncoderBasic)
@@ -1995,6 +1998,10 @@ target_link_libraries(${VIEWER_BINARY_NAME}
19951998
ll::openxr
19961999
)
19972000

2001+
if (USE_DISCORD)
2002+
target_link_libraries(${VIEWER_BINARY_NAME} ll::discord )
2003+
endif ()
2004+
19982005
if( TARGET ll::intel_memops )
19992006
target_link_libraries(${VIEWER_BINARY_NAME} ll::intel_memops )
20002007
endif()

indra/newview/app_settings/settings.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,39 @@
11391139
<key>Value</key>
11401140
<integer>1</integer>
11411141
</map>
1142+
<key>EnableDiscord</key>
1143+
<map>
1144+
<key>Comment</key>
1145+
<string>When set, connect to Discord to enable Rich Presence</string>
1146+
<key>Persist</key>
1147+
<integer>1</integer>
1148+
<key>Type</key>
1149+
<string>Boolean</string>
1150+
<key>Value</key>
1151+
<integer>0</integer>
1152+
</map>
1153+
<key>ShowDiscordActivityDetails</key>
1154+
<map>
1155+
<key>Comment</key>
1156+
<string>When set, show avatar name on Discord Rich Presence</string>
1157+
<key>Persist</key>
1158+
<integer>1</integer>
1159+
<key>Type</key>
1160+
<string>Boolean</string>
1161+
<key>Value</key>
1162+
<integer>0</integer>
1163+
</map>
1164+
<key>ShowDiscordActivityState</key>
1165+
<map>
1166+
<key>Comment</key>
1167+
<string>When set, show location on Discord Rich Presence</string>
1168+
<key>Persist</key>
1169+
<integer>1</integer>
1170+
<key>Type</key>
1171+
<string>Boolean</string>
1172+
<key>Value</key>
1173+
<integer>0</integer>
1174+
</map>
11421175
<key>EnableDiskCacheDebugInfo</key>
11431176
<map>
11441177
<key>Comment</key>

indra/newview/llappviewer.cpp

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,16 @@ using namespace LL;
268268
#include "glib.h"
269269
#endif // (LL_LINUX) && LL_GTK
270270

271+
#ifdef LL_DISCORD
272+
#define DISCORDPP_IMPLEMENTATION
273+
#include <discordpp.h>
274+
static std::shared_ptr<discordpp::Client> gDiscordClient;
275+
static uint64_t gDiscordTimestampsStart;
276+
static std::string gDiscordActivityDetails;
277+
static int32_t gDiscordPartyCurrentSize;
278+
static int32_t gDiscordPartyMaxSize;
279+
#endif
280+
271281
static LLAppViewerListener sAppViewerListener(LLAppViewer::instance);
272282

273283
////// Windows-specific includes to the bottom - nasty defines in these pollute the preprocessor
@@ -1319,6 +1329,13 @@ bool LLAppViewer::frame()
13191329

13201330
bool LLAppViewer::doFrame()
13211331
{
1332+
#ifdef LL_DISCORD
1333+
{
1334+
LL_PROFILE_ZONE_NAMED("discord_callbacks");
1335+
discordpp::RunCallbacks();
1336+
}
1337+
#endif
1338+
13221339
LL_RECORD_BLOCK_TIME(FTM_FRAME);
13231340
{
13241341
// and now adjust the visuals from previous frame.
@@ -5862,3 +5879,180 @@ void LLAppViewer::metricsSend(bool enable_reporting)
58625879
gViewerAssetStats->restart();
58635880
}
58645881

5882+
#ifdef LL_DISCORD
5883+
5884+
void LLAppViewer::initDiscordSocial()
5885+
{
5886+
gDiscordPartyCurrentSize = 1;
5887+
gDiscordPartyMaxSize = 0;
5888+
gDiscordTimestampsStart = time(nullptr);
5889+
gDiscordClient = std::make_shared<discordpp::Client>();
5890+
gDiscordClient->SetStatusChangedCallback([](discordpp::Client::Status status, discordpp::Client::Error, int32_t) {
5891+
if (status == discordpp::Client::Status::Ready)
5892+
{
5893+
updateDiscordActivity();
5894+
}
5895+
});
5896+
if (gSavedSettings.getBOOL("EnableDiscord"))
5897+
{
5898+
auto credential = gSecAPIHandler->loadCredential("Discord");
5899+
if (credential.notNull())
5900+
{
5901+
gDiscordClient->UpdateToken(discordpp::AuthorizationTokenType::Bearer, credential->getAuthenticator()["token"].asString(), [](discordpp::ClientResult result) {
5902+
if (result.Successful())
5903+
gDiscordClient->Connect();
5904+
else
5905+
LL_WARNS("Discord") << result.Error() << LL_ENDL;
5906+
});
5907+
}
5908+
else
5909+
{
5910+
LL_WARNS("Discord") << "Integration was enabled, but no credentials. Disabling integration." << LL_ENDL;
5911+
gSavedSettings.setBOOL("EnableDiscord", false);
5912+
}
5913+
}
5914+
}
5915+
5916+
void LLAppViewer::toggleDiscordIntegration(const LLSD& value)
5917+
{
5918+
static const uint64_t APPLICATION_ID = 1394782217405862001;
5919+
if (value.asBoolean())
5920+
{
5921+
discordpp::AuthorizationArgs args{};
5922+
args.SetClientId(APPLICATION_ID);
5923+
args.SetScopes(discordpp::Client::GetDefaultPresenceScopes());
5924+
auto codeVerifier = gDiscordClient->CreateAuthorizationCodeVerifier();
5925+
args.SetCodeChallenge(codeVerifier.Challenge());
5926+
gDiscordClient->Authorize(args, [codeVerifier](auto result, auto code, auto redirectUri) {
5927+
if (result.Successful())
5928+
{
5929+
gDiscordClient->GetToken(APPLICATION_ID, code, codeVerifier.Verifier(), redirectUri, [](discordpp::ClientResult result, std::string accessToken, std::string, discordpp::AuthorizationTokenType, int32_t, std::string) {
5930+
if (result.Successful())
5931+
{
5932+
gDiscordClient->UpdateToken(discordpp::AuthorizationTokenType::Bearer, accessToken, [accessToken](discordpp::ClientResult result) {
5933+
if (result.Successful())
5934+
{
5935+
LLSD authenticator = LLSD::emptyMap();
5936+
authenticator["token"] = accessToken;
5937+
gSecAPIHandler->saveCredential(gSecAPIHandler->createCredential("Discord", LLSD::emptyMap(), authenticator), true);
5938+
gDiscordClient->Connect();
5939+
}
5940+
else
5941+
{
5942+
LL_WARNS("Discord") << result.Error() << LL_ENDL;
5943+
}
5944+
});
5945+
}
5946+
else
5947+
{
5948+
LL_WARNS("Discord") << result.Error() << LL_ENDL;
5949+
}
5950+
});
5951+
}
5952+
else
5953+
{
5954+
LL_WARNS("Discord") << result.Error() << LL_ENDL;
5955+
gSavedSettings.setBOOL("EnableDiscord", false);
5956+
}
5957+
});
5958+
}
5959+
else
5960+
{
5961+
gDiscordClient->Disconnect();
5962+
auto credential = gSecAPIHandler->loadCredential("Discord");
5963+
if (credential.notNull())
5964+
{
5965+
gDiscordClient->RevokeToken(APPLICATION_ID, credential->getAuthenticator()["token"].asString(), [](discordpp::ClientResult result) {
5966+
if (result.Successful())
5967+
LL_INFOS("Discord") << "Access token successfully revoked." << LL_ENDL;
5968+
else
5969+
LL_WARNS("Discord") << "No access token to revoke." << LL_ENDL;
5970+
});
5971+
auto cred = new LLCredential("Discord");
5972+
gSecAPIHandler->deleteCredential(cred);
5973+
}
5974+
else
5975+
{
5976+
LL_WARNS("Discord") << "Credentials are already nonexistent." << LL_ENDL;
5977+
}
5978+
}
5979+
}
5980+
5981+
void LLAppViewer::updateDiscordActivity()
5982+
{
5983+
LL_PROFILE_ZONE_SCOPED;
5984+
discordpp::Activity activity;
5985+
activity.SetType(discordpp::ActivityTypes::Playing);
5986+
discordpp::ActivityTimestamps timestamps;
5987+
timestamps.SetStart(gDiscordTimestampsStart);
5988+
activity.SetTimestamps(timestamps);
5989+
5990+
if (gAgent.getID() == LLUUID::null)
5991+
{
5992+
gDiscordClient->UpdateRichPresence(activity, [](discordpp::ClientResult) {});
5993+
return;
5994+
}
5995+
5996+
static LLCachedControl<bool> show_details(gSavedSettings, "ShowDiscordActivityDetails", false);
5997+
if (show_details)
5998+
{
5999+
if (gDiscordActivityDetails.empty())
6000+
{
6001+
LLAvatarName av_name;
6002+
LLAvatarNameCache::get(gAgent.getID(), &av_name);
6003+
gDiscordActivityDetails = av_name.getUserName();
6004+
auto displayName = av_name.getDisplayName();
6005+
if (gDiscordActivityDetails != displayName)
6006+
gDiscordActivityDetails = displayName + " (" + gDiscordActivityDetails + ")";
6007+
}
6008+
activity.SetDetails(gDiscordActivityDetails);
6009+
}
6010+
6011+
static LLCachedControl<bool> show_state(gSavedSettings, "ShowDiscordActivityState", false);
6012+
if (show_state)
6013+
{
6014+
auto agent_pos_region = gAgent.getPositionAgent();
6015+
S32 pos_x = S32(agent_pos_region.mV[VX] + 0.5f);
6016+
S32 pos_y = S32(agent_pos_region.mV[VY] + 0.5f);
6017+
S32 pos_z = S32(agent_pos_region.mV[VZ] + 0.5f);
6018+
F32 velocity_mag_sq = gAgent.getVelocity().magVecSquared();
6019+
const F32 FLY_CUTOFF = 6.f;
6020+
const F32 FLY_CUTOFF_SQ = FLY_CUTOFF * FLY_CUTOFF;
6021+
const F32 WALK_CUTOFF = 1.5f;
6022+
const F32 WALK_CUTOFF_SQ = WALK_CUTOFF * WALK_CUTOFF;
6023+
if (velocity_mag_sq > FLY_CUTOFF_SQ)
6024+
{
6025+
pos_x -= pos_x % 4;
6026+
pos_y -= pos_y % 4;
6027+
}
6028+
else if (velocity_mag_sq > WALK_CUTOFF_SQ)
6029+
{
6030+
pos_x -= pos_x % 2;
6031+
pos_y -= pos_y % 2;
6032+
}
6033+
auto location = llformat("%s (%d, %d, %d)", gAgent.getRegion()->getName().c_str(), pos_x, pos_y, pos_z);
6034+
activity.SetState(location);
6035+
6036+
discordpp::ActivityParty party;
6037+
party.SetId(location);
6038+
party.SetCurrentSize(gDiscordPartyCurrentSize);
6039+
party.SetMaxSize(gDiscordPartyMaxSize);
6040+
activity.SetParty(party);
6041+
}
6042+
6043+
gDiscordClient->UpdateRichPresence(activity, [](discordpp::ClientResult) {});
6044+
}
6045+
6046+
void LLAppViewer::updateDiscordPartyCurrentSize(int32_t size)
6047+
{
6048+
gDiscordPartyCurrentSize = size;
6049+
updateDiscordActivity();
6050+
}
6051+
6052+
void LLAppViewer::updateDiscordPartyMaxSize(int32_t size)
6053+
{
6054+
gDiscordPartyMaxSize = size;
6055+
updateDiscordActivity();
6056+
}
6057+
6058+
#endif

indra/newview/llappviewer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,14 @@ class LLAppViewer : public LLApp
250250
// Note: mQuitRequested can be aborted by user.
251251
void outOfMemorySoftQuit();
252252

253+
#ifdef LL_DISCORD
254+
static void initDiscordSocial();
255+
static void toggleDiscordIntegration(const LLSD& value);
256+
static void updateDiscordActivity();
257+
static void updateDiscordPartyCurrentSize(int32_t size);
258+
static void updateDiscordPartyMaxSize(int32_t size);
259+
#endif
260+
253261
protected:
254262
virtual bool initWindow(); // Initialize the viewer's window.
255263
virtual void initLoggingAndGetLastDuration(); // Initialize log files, logging system

indra/newview/llfloaterpreference.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ LLFloaterPreference::LLFloaterPreference(const LLSD& key)
366366
mCommitCallbackRegistrar.add("Pref.ClearLog", boost::bind(&LLConversationLog::onClearLog, &LLConversationLog::instance()));
367367
mCommitCallbackRegistrar.add("Pref.DeleteTranscripts", boost::bind(&LLFloaterPreference::onDeleteTranscripts, this));
368368
mCommitCallbackRegistrar.add("UpdateFilter", boost::bind(&LLFloaterPreference::onUpdateFilterTerm, this, false)); // <FS:ND/> Hook up for filtering
369+
#ifdef LL_DISCORD
370+
gSavedSettings.getControl("EnableDiscord")->getCommitSignal()->connect(boost::bind(&LLAppViewer::toggleDiscordIntegration, _2));
371+
gSavedSettings.getControl("ShowDiscordActivityDetails")->getCommitSignal()->connect(boost::bind(&LLAppViewer::updateDiscordActivity));
372+
gSavedSettings.getControl("ShowDiscordActivityState")->getCommitSignal()->connect(boost::bind(&LLAppViewer::updateDiscordActivity));
373+
#endif
369374
}
370375

371376
void LLFloaterPreference::processProperties( void* pData, EAvatarProcessorType type )

indra/newview/llpanelpeople.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,10 @@ void LLPanelPeople::updateNearbyList()
843843

844844
LLWorld::getInstance()->getAvatars(&mNearbyList->getIDs(), &positions, gAgent.getPositionGlobal(), gSavedSettings.getF32("NearMeRange"));
845845
mNearbyList->setDirty();
846+
#ifdef LL_DISCORD
847+
if (gSavedSettings.getBOOL("EnableDiscord"))
848+
LLAppViewer::updateDiscordPartyMaxSize(mNearbyList->getIDs().size());
849+
#endif
846850

847851
DISTANCE_COMPARATOR.updateAvatarsPositions(positions, mNearbyList->getIDs());
848852
LLActiveSpeakerMgr::instance().update(true);

indra/newview/llspeakers.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,10 @@ void LLLocalSpeakerMgr::updateSpeakerList()
10261026
uuid_vec_t avatar_ids;
10271027
std::vector<LLVector3d> positions;
10281028
LLWorld::getInstance()->getAvatars(&avatar_ids, &positions, gAgent.getPositionGlobal(), CHAT_NORMAL_RADIUS);
1029+
#ifdef LL_DISCORD
1030+
if (gSavedSettings.getBOOL("EnableDiscord"))
1031+
LLAppViewer::updateDiscordPartyCurrentSize(avatar_ids.size());
1032+
#endif
10291033
for(U32 i=0; i<avatar_ids.size(); i++)
10301034
{
10311035
setSpeaker(avatar_ids[i]);

indra/newview/llstartup.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,10 @@ bool idle_startup()
724724
LL_WARNS("AppInit") << "Unreliable timers detected (may be bad PCI chipset)!!" << LL_ENDL;
725725
}
726726

727+
#ifdef LL_DISCORD
728+
LLAppViewer::initDiscordSocial();
729+
#endif
730+
727731
//
728732
// Log on to system
729733
//

0 commit comments

Comments
 (0)