From 6da83688b2073b5897335f12598e6bef119286b2 Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Thu, 10 Oct 2024 18:15:38 -0500 Subject: [PATCH] UI: Improve Discord RPC for select games & show play time in place of title ID. --- .../DiscordIntegrationModule.cs | 53 ++++++++----------- .../Ryujinx.UI.Common.csproj | 1 + src/Ryujinx/AppHost.cs | 10 ++-- 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs b/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs index 6966038b6..d57acefbe 100644 --- a/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs +++ b/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs @@ -1,14 +1,20 @@ using DiscordRPC; +using Humanizer; using Ryujinx.Common; +using Ryujinx.UI.App.Common; using Ryujinx.UI.Common.Configuration; +using System.Collections.Generic; +using System.Linq; using System.Text; namespace Ryujinx.UI.Common { public static class DiscordIntegrationModule { + public static Timestamps StartedAt { get; set; } + private const string Description = "A simple, experimental Nintendo Switch emulator."; - private const string ApplicationId = "1216775165866807456"; + private const string ApplicationId = "1293250299716173864"; private const int ApplicationByteLimit = 128; private const string Ellipsis = "…"; @@ -23,19 +29,11 @@ namespace Ryujinx.UI.Common Assets = new Assets { LargeImageKey = "ryujinx", - LargeImageText = Description, + LargeImageText = Description }, Details = "Main Menu", State = "Idling", - Timestamps = Timestamps.Now, - Buttons = - [ - new Button - { - Label = "Website", - Url = "https://ryujinx.org/", - }, - ], + Timestamps = StartedAt }; ConfigurationState.Instance.EnableDiscordIntegration.Event += Update; @@ -64,36 +62,29 @@ namespace Ryujinx.UI.Common } } - public static void SwitchToPlayingState(string titleId, string applicationName) + private static readonly string[] _discordGameAssets = [ + "0100f2c0115b6000", // Tears of the Kingdom + "0100744001588000", // Cars 3: Driven to Win + + ]; + + public static void SwitchToPlayingState(string titleId, ApplicationMetadata appMeta) { _discordClient?.SetPresence(new RichPresence { Assets = new Assets { - LargeImageKey = "game", - LargeImageText = TruncateToByteLength(applicationName, ApplicationByteLimit), + LargeImageKey = _discordGameAssets.Contains(titleId.ToLower()) ? titleId : "game", + LargeImageText = TruncateToByteLength(appMeta.Title, ApplicationByteLimit), SmallImageKey = "ryujinx", - SmallImageText = Description, + SmallImageText = Description }, - Details = TruncateToByteLength($"Playing {applicationName}", ApplicationByteLimit), - State = (titleId == "0000000000000000") ? "Homebrew" : titleId.ToUpper(), - Timestamps = Timestamps.Now, - Buttons = - [ - new Button - { - Label = "Website", - Url = "https://ryujinx.org/", - }, - ], + Details = TruncateToByteLength($"Playing {appMeta.Title}", ApplicationByteLimit), + State = $"Total play time: {appMeta.TimePlayed.Humanize(2, false)}", + Timestamps = Timestamps.Now }); } - public static void SwitchToMainMenu() - { - _discordClient?.SetPresence(_discordPresenceMain); - } - private static string TruncateToByteLength(string input, int byteLimit) { if (Encoding.UTF8.GetByteCount(input) <= byteLimit) diff --git a/src/Ryujinx.UI.Common/Ryujinx.UI.Common.csproj b/src/Ryujinx.UI.Common/Ryujinx.UI.Common.csproj index fcbbaba30..980347228 100644 --- a/src/Ryujinx.UI.Common/Ryujinx.UI.Common.csproj +++ b/src/Ryujinx.UI.Common/Ryujinx.UI.Common.csproj @@ -57,6 +57,7 @@ + diff --git a/src/Ryujinx/AppHost.cs b/src/Ryujinx/AppHost.cs index f4bfd1169..9adf6bca2 100644 --- a/src/Ryujinx/AppHost.cs +++ b/src/Ryujinx/AppHost.cs @@ -785,13 +785,13 @@ namespace Ryujinx.Ava return false; } - DiscordIntegrationModule.SwitchToPlayingState(Device.Processes.ActiveApplication.ProgramIdText, Device.Processes.ActiveApplication.Name); - - ApplicationLibrary.LoadAndSaveMetaData(Device.Processes.ActiveApplication.ProgramIdText, appMetadata => + ApplicationMetadata appMeta = ApplicationLibrary.LoadAndSaveMetaData(Device.Processes.ActiveApplication.ProgramIdText, appMetadata => { appMetadata.UpdatePreGame(); }); + DiscordIntegrationModule.SwitchToPlayingState(Device.Processes.ActiveApplication.ProgramIdText, appMeta); + return true; } @@ -948,10 +948,8 @@ namespace Ryujinx.Ava private void MainLoop() { - while (_isActive) + while (UpdateFrame()) { - UpdateFrame(); - // Polling becomes expensive if it's not slept. Thread.Sleep(1); }