Skip to content
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

DiscordRPC: Show session time in Discord Rich Presence #10397

Merged
merged 1 commit into from
Dec 23, 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
2 changes: 1 addition & 1 deletion pcsx2/Achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ void Achievements::UpdateRichPresence(std::unique_lock<std::recursive_mutex>& lo
Host::OnAchievementsRefreshed();

lock.unlock();
VMManager::UpdateDiscordPresence();
VMManager::UpdateDiscordPresence(false);
lock.lock();
}

Expand Down
16 changes: 10 additions & 6 deletions pcsx2/VMManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ static bool s_screensaver_inhibited = false;
static PINEServer s_pine_server;

static bool s_discord_presence_active = false;
static time_t s_discord_presence_time_epoch;

bool VMManager::PerformEarlyHardwareChecks(const char** error)
{
Expand Down Expand Up @@ -934,7 +935,7 @@ void VMManager::UpdateDiscDetails(bool booting)
if (MTGS::IsOpen())
MTGS::GameChanged();
ReloadPINE();
UpdateDiscordPresence();
UpdateDiscordPresence(s_state.load(std::memory_order_relaxed) == VMState::Initializing);

if (!GSDumpReplayer::IsReplayingDump())
FileMcd_Reopen(memcardFilters.empty() ? s_disc_serial : memcardFilters);
Expand Down Expand Up @@ -1089,7 +1090,7 @@ bool VMManager::Initialize(VMBootParameters boot_params)

Achievements::GameChanged(0, 0);
FullscreenUI::GameChanged(s_title, std::string(), s_disc_serial, 0, 0);
UpdateDiscordPresence();
UpdateDiscordPresence(true);
Host::OnGameChanged(s_title, std::string(), std::string(), s_disc_serial, 0, 0);

UpdateGameSettingsLayer();
Expand Down Expand Up @@ -1402,7 +1403,7 @@ void VMManager::Shutdown(bool save_resume_state)

Achievements::GameChanged(0, 0);
FullscreenUI::GameChanged(s_title, std::string(), s_disc_serial, 0, 0);
UpdateDiscordPresence();
UpdateDiscordPresence(true);
Host::OnGameChanged(s_title, std::string(), std::string(), s_disc_serial, 0, 0);

s_fast_boot_requested = false;
Expand Down Expand Up @@ -3087,7 +3088,7 @@ void VMManager::InitializeDiscordPresence()
Discord_Initialize("1025789002055430154", &handlers, 0, nullptr);
s_discord_presence_active = true;

UpdateDiscordPresence();
UpdateDiscordPresence(true);
}

void VMManager::ShutdownDiscordPresence()
Expand All @@ -3101,16 +3102,19 @@ void VMManager::ShutdownDiscordPresence()
s_discord_presence_active = false;
}

void VMManager::UpdateDiscordPresence()
void VMManager::UpdateDiscordPresence(bool update_session_time)
{
if (!s_discord_presence_active)
return;

if (update_session_time)
s_discord_presence_time_epoch = std::time(nullptr);

// https://discord.com/developers/docs/rich-presence/how-to#updating-presence-update-presence-payload-fields
DiscordRichPresence rp = {};
rp.largeImageKey = "4k-pcsx2";
rp.largeImageText = "PCSX2 Emulator";
rp.startTimestamp = std::time(nullptr);
rp.startTimestamp = s_discord_presence_time_epoch;
rp.details = s_title.empty() ? "No Game Running" : s_title.c_str();

std::string state_string;
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/VMManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ namespace VMManager
u64 GetSessionPlayedTime();

/// Called when the rich presence string, provided by RetroAchievements, changes.
void UpdateDiscordPresence();
void UpdateDiscordPresence(bool update_session_time);

/// Internal callbacks, implemented in the emu core.
namespace Internal
Expand Down