From d7707d4176e450899ca5178b25930b946da56d19 Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Mon, 3 Feb 2025 23:12:50 -0600 Subject: [PATCH 1/2] UI: RPC: Only update presence if a value is actually different from the current presence --- src/Ryujinx/DiscordIntegrationModule.cs | 27 ++++++++++++------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/Ryujinx/DiscordIntegrationModule.cs b/src/Ryujinx/DiscordIntegrationModule.cs index 5d55eba42..e71b49408 100644 --- a/src/Ryujinx/DiscordIntegrationModule.cs +++ b/src/Ryujinx/DiscordIntegrationModule.cs @@ -117,9 +117,15 @@ namespace Ryujinx.Ava _currentApp = appMeta; } - private static void UpdatePlayingState() + private static bool UpdatePlayingState() { + if (_discordClient is null) return false; + if (_discordClient.CurrentPresence.Details.Equals(_discordPresencePlaying.Details)) + return false; + _discordClient?.SetPresence(_discordPresencePlaying); + + return true; } private static void SwitchToMainState() @@ -137,19 +143,12 @@ namespace Ryujinx.Ava PlayReport.Analyzer.FormatPlayReportValue(TitleIDs.CurrentApplication.Value, _currentApp, playReport) .Match(out bool handled, - () => - { - _discordPresencePlaying.Details = $"Playing {_currentApp.Title}"; - Logger.Info?.Print(LogClass.UI, "Reset Discord RPC based on a supported play report value formatter."); - }, - formattedString => - { - _discordPresencePlaying.Details = formattedString; - Logger.Info?.Print(LogClass.UI, "Updated Discord RPC based on a supported play report."); - }); - - if (handled) - UpdatePlayingState(); + () => _discordPresencePlaying.Details = $"Playing {_currentApp.Title}", + formattedString => _discordPresencePlaying.Details = formattedString + ); + + if (handled && UpdatePlayingState()) + Logger.Info?.Print(LogClass.UI, "Updated Discord RPC based on a supported play report."); } private static string TruncateToByteLength(string input) From 566f3d079a6fdca6233550aa76686eea272fd548 Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Tue, 4 Feb 2025 00:56:59 -0600 Subject: [PATCH 2/2] misc: chore: Play Report analyzer code simplification --- src/Ryujinx/DiscordIntegrationModule.cs | 31 +++++++++------------ src/Ryujinx/Utilities/PlayReportAnalyzer.cs | 19 +------------ 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/src/Ryujinx/DiscordIntegrationModule.cs b/src/Ryujinx/DiscordIntegrationModule.cs index e71b49408..20b296511 100644 --- a/src/Ryujinx/DiscordIntegrationModule.cs +++ b/src/Ryujinx/DiscordIntegrationModule.cs @@ -117,17 +117,6 @@ namespace Ryujinx.Ava _currentApp = appMeta; } - private static bool UpdatePlayingState() - { - if (_discordClient is null) return false; - if (_discordClient.CurrentPresence.Details.Equals(_discordPresencePlaying.Details)) - return false; - - _discordClient?.SetPresence(_discordPresencePlaying); - - return true; - } - private static void SwitchToMainState() { _discordClient?.SetPresence(_discordPresenceMain); @@ -141,14 +130,20 @@ namespace Ryujinx.Ava if (!TitleIDs.CurrentApplication.Value.HasValue) return; if (_discordPresencePlaying is null) return; - PlayReport.Analyzer.FormatPlayReportValue(TitleIDs.CurrentApplication.Value, _currentApp, playReport) - .Match(out bool handled, - () => _discordPresencePlaying.Details = $"Playing {_currentApp.Title}", - formattedString => _discordPresencePlaying.Details = formattedString - ); + PlayReportAnalyzer.FormattedValue formattedValue = + PlayReport.Analyzer.Format(TitleIDs.CurrentApplication.Value, _currentApp, playReport); - if (handled && UpdatePlayingState()) - Logger.Info?.Print(LogClass.UI, "Updated Discord RPC based on a supported play report."); + if (!formattedValue.Handled) return; + + _discordPresencePlaying.Details = formattedValue.Reset + ? $"Playing {_currentApp.Title}" + : formattedValue.FormattedString; + + if (_discordClient.CurrentPresence.Details.Equals(_discordPresencePlaying.Details)) + return; //don't trigger an update if the set presence Details are identical to current + + _discordClient.SetPresence(_discordPresencePlaying); + Logger.Info?.Print(LogClass.UI, "Updated Discord RPC based on a supported play report."); } private static string TruncateToByteLength(string input) diff --git a/src/Ryujinx/Utilities/PlayReportAnalyzer.cs b/src/Ryujinx/Utilities/PlayReportAnalyzer.cs index e22969bb9..47c36a396 100644 --- a/src/Ryujinx/Utilities/PlayReportAnalyzer.cs +++ b/src/Ryujinx/Utilities/PlayReportAnalyzer.cs @@ -86,7 +86,7 @@ namespace Ryujinx.Ava.Utilities /// The Application metadata information, including localized game name and play time information. /// The Play Report received from HLE. /// A struct representing a possible formatted value. - public FormattedValue FormatPlayReportValue( + public FormattedValue Format( string runningGameId, ApplicationMetadata appMeta, MessagePackObject playReport @@ -132,23 +132,6 @@ namespace Ryujinx.Ava.Utilities /// public string FormattedString { get; private init; } - public void Match(out bool wasHandled, Action onReset, Action onSuccess) - { - if (!Handled) - { - wasHandled = false; - return; - } - - if (Reset) - onReset(); - else - onSuccess(FormattedString); - - wasHandled = true; - } - - /// /// The intended path of execution for having a string to return: simply return the string. /// This implicit conversion will make the struct for you.