From d08a40bcd8db1b0ae83609178e6aee18b85f9126 Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 23 Dec 2023 15:19:22 +0100 Subject: [PATCH 1/2] DiscordRPC: Show session time in Discord Rich Presence --- src/core/achievements.cpp | 2 +- src/core/system.cpp | 14 +++++++++----- src/core/system.h | 2 +- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/core/achievements.cpp b/src/core/achievements.cpp index b9d75f184..8b81324de 100644 --- a/src/core/achievements.cpp +++ b/src/core/achievements.cpp @@ -795,7 +795,7 @@ void Achievements::UpdateRichPresence(std::unique_lock& lo #ifdef ENABLE_DISCORD_PRESENCE lock.unlock(); - System::UpdateDiscordPresence(); + System::UpdateDiscordPresence(false); lock.lock(); #endif } diff --git a/src/core/system.cpp b/src/core/system.cpp index 5007caae5..40ad38257 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -234,6 +234,7 @@ static u64 s_session_start_time = 0; #ifdef ENABLE_DISCORD_PRESENCE static bool s_discord_presence_active = false; +static time_t s_discord_presence_time_epoch; #endif static TinyString GetTimestampStringForFileName() @@ -1691,7 +1692,7 @@ void System::ClearRunningGame() Achievements::GameChanged(s_running_game_path, nullptr); #ifdef ENABLE_DISCORD_PRESENCE - UpdateDiscordPresence(); + UpdateDiscordPresence(true); #endif } @@ -3372,7 +3373,7 @@ void System::UpdateRunningGame(const char* path, CDImage* image, bool booting) SaveStateSelectorUI::ClearList(); #ifdef ENABLE_DISCORD_PRESENCE - UpdateDiscordPresence(); + UpdateDiscordPresence(booting); #endif Host::OnGameChanged(s_running_game_path, s_running_game_serial, s_running_game_title); @@ -4834,7 +4835,7 @@ void System::InitializeDiscordPresence() Discord_Initialize("705325712680288296", &handlers, 0, nullptr); s_discord_presence_active = true; - UpdateDiscordPresence(); + UpdateDiscordPresence(true); } void System::ShutdownDiscordPresence() @@ -4847,16 +4848,19 @@ void System::ShutdownDiscordPresence() s_discord_presence_active = false; } -void System::UpdateDiscordPresence() +void System::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 = "duckstation_logo"; rp.largeImageText = "DuckStation PS1/PSX Emulator"; - rp.startTimestamp = std::time(nullptr); + rp.startTimestamp = s_discord_presence_time_epoch; rp.details = "No Game Running"; if (IsValidOrInitializing()) { diff --git a/src/core/system.h b/src/core/system.h index 12b9de398..4685b7af8 100644 --- a/src/core/system.h +++ b/src/core/system.h @@ -480,7 +480,7 @@ void SetRunaheadReplayFlag(); #ifdef ENABLE_DISCORD_PRESENCE /// Called when rich presence changes. -void UpdateDiscordPresence(); +void UpdateDiscordPresence(bool update_session_time); #endif namespace Internal { From 8d491d3faa8363df31ab2b3cc31f62e575f61db1 Mon Sep 17 00:00:00 2001 From: Silent Date: Sat, 23 Dec 2023 15:27:10 +0100 Subject: [PATCH 2/2] System: Mark the VM as Stopping in ShutdownSystem unconditionally Fixes an issue where Discord RPC didn't revert to "No Game Running" on shutdown. --- src/core/system.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core/system.cpp b/src/core/system.cpp index 40ad38257..773580a1b 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -4138,9 +4138,8 @@ void System::ShutdownSystem(bool save_resume_state) if (save_resume_state) SaveResumeState(); - if (s_system_executing) - s_state = State::Stopping; - else + s_state = State::Stopping; + if (!s_system_executing) DestroySystem(); }