From f463ea1c5df6840067b0bc3d5a81295e1992e839 Mon Sep 17 00:00:00 2001 From: Evan Husted Date: Sun, 29 Dec 2024 03:27:05 -0600 Subject: [PATCH] misc: Refactor Discord integration to listen on TitleIDs.CurrentApplication changes instead of waiting to be directly told when to change states. --- src/Ryujinx.Common/TitleIDs.cs | 15 ++++++++++++++- .../Processes/Extensions/FileSystemExtensions.cs | 1 - .../Loaders/Processes/ProcessLoader.cs | 8 ++++++++ src/Ryujinx.HLE/Switch.cs | 8 ++++++++ src/Ryujinx.UI.Common/DiscordIntegrationModule.cs | 15 +++++++++++++-- src/Ryujinx/AppHost.cs | 7 ++----- 6 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/Ryujinx.Common/TitleIDs.cs b/src/Ryujinx.Common/TitleIDs.cs index a50121943..82a08b24e 100644 --- a/src/Ryujinx.Common/TitleIDs.cs +++ b/src/Ryujinx.Common/TitleIDs.cs @@ -8,7 +8,20 @@ namespace Ryujinx.Common { public static class TitleIDs { - public static Optional CurrentApplication { get; set; } + private static string _currentApplication; + + public static Optional CurrentApplication + { + get => _currentApplication; + set + { + _currentApplication = value.OrElse(null); + + CurrentApplicationChanged?.Invoke(_currentApplication); + } + } + + public static event Action> CurrentApplicationChanged; public static GraphicsBackend SelectGraphicsBackend(string titleId, GraphicsBackend currentBackend) { diff --git a/src/Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs b/src/Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs index 00ae4e5e1..d1d13b00f 100644 --- a/src/Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs +++ b/src/Ryujinx.HLE/Loaders/Processes/Extensions/FileSystemExtensions.cs @@ -105,7 +105,6 @@ namespace Ryujinx.HLE.Loaders.Processes.Extensions // Initialize GPU. GraphicsConfig.TitleId = programId.ToString("X16"); - TitleIDs.CurrentApplication = GraphicsConfig.TitleId; device.Gpu.HostInitalized.Set(); if (!MemoryBlock.SupportsFlags(MemoryAllocationFlags.ViewCompatible)) diff --git a/src/Ryujinx.HLE/Loaders/Processes/ProcessLoader.cs b/src/Ryujinx.HLE/Loaders/Processes/ProcessLoader.cs index 7a0c486bb..1b90f2707 100644 --- a/src/Ryujinx.HLE/Loaders/Processes/ProcessLoader.cs +++ b/src/Ryujinx.HLE/Loaders/Processes/ProcessLoader.cs @@ -61,6 +61,8 @@ namespace Ryujinx.HLE.Loaders.Processes { _latestPid = processResult.ProcessId; + TitleIDs.CurrentApplication = processResult.ProgramIdText; + return true; } } @@ -88,6 +90,8 @@ namespace Ryujinx.HLE.Loaders.Processes { _latestPid = processResult.ProcessId; + TitleIDs.CurrentApplication = processResult.ProgramIdText; + return true; } } @@ -115,6 +119,8 @@ namespace Ryujinx.HLE.Loaders.Processes if (processResult.ProgramId > 0x01000000000007FF) { _latestPid = processResult.ProcessId; + + TitleIDs.CurrentApplication = processResult.ProgramIdText; } return true; @@ -134,6 +140,8 @@ namespace Ryujinx.HLE.Loaders.Processes { _latestPid = processResult.ProcessId; + TitleIDs.CurrentApplication = processResult.ProgramIdText; + return true; } } diff --git a/src/Ryujinx.HLE/Switch.cs b/src/Ryujinx.HLE/Switch.cs index e0f94274c..f73776eda 100644 --- a/src/Ryujinx.HLE/Switch.cs +++ b/src/Ryujinx.HLE/Switch.cs @@ -2,6 +2,7 @@ using LibHac.Common; using LibHac.Ns; using Ryujinx.Audio.Backends.CompatLayer; using Ryujinx.Audio.Integration; +using Ryujinx.Common; using Ryujinx.Common.Configuration; using Ryujinx.Graphics.Gpu; using Ryujinx.HLE.FileSystem; @@ -17,6 +18,8 @@ namespace Ryujinx.HLE { public class Switch : IDisposable { + public static Switch Shared { get; private set; } + public HLEConfiguration Configuration { get; } public IHardwareDeviceDriver AudioDeviceDriver { get; } public MemoryBlock Memory { get; } @@ -77,6 +80,8 @@ namespace Ryujinx.HLE DirtyHacks = Configuration.Hacks; UpdateVSyncInterval(); #pragma warning restore IDE0055 + + Shared = this; } public void ProcessFrame() @@ -145,6 +150,9 @@ namespace Ryujinx.HLE AudioDeviceDriver.Dispose(); FileSystem.Dispose(); Memory.Dispose(); + + TitleIDs.CurrentApplication = null; + Shared = null; } } } diff --git a/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs b/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs index 574aaff87..de778a648 100644 --- a/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs +++ b/src/Ryujinx.UI.Common/DiscordIntegrationModule.cs @@ -2,6 +2,7 @@ using DiscordRPC; using Humanizer; using Humanizer.Localisation; using Ryujinx.Common; +using Ryujinx.HLE; using Ryujinx.HLE.Loaders.Processes; using Ryujinx.UI.App.Common; using Ryujinx.UI.Common.Configuration; @@ -44,6 +45,16 @@ namespace Ryujinx.UI.Common }; ConfigurationState.Instance.EnableDiscordIntegration.Event += Update; + TitleIDs.CurrentApplicationChanged += titleId => + { + if (titleId) + SwitchToPlayingState( + ApplicationLibrary.LoadAndSaveMetaData(titleId), + Switch.Shared.Processes.ActiveApplication + ); + else + SwitchToMainState(); + }; } private static void Update(object sender, ReactiveEventArgs evnt) @@ -69,7 +80,7 @@ namespace Ryujinx.UI.Common } } - public static void SwitchToPlayingState(ApplicationMetadata appMeta, ProcessResult procRes) + private static void SwitchToPlayingState(ApplicationMetadata appMeta, ProcessResult procRes) { _discordClient?.SetPresence(new RichPresence { @@ -88,7 +99,7 @@ namespace Ryujinx.UI.Common }); } - public static void SwitchToMainState() => _discordClient?.SetPresence(_discordPresenceMain); + private static void SwitchToMainState() => _discordClient?.SetPresence(_discordPresenceMain); private static string TruncateToByteLength(string input) { diff --git a/src/Ryujinx/AppHost.cs b/src/Ryujinx/AppHost.cs index 0f8b8030c..1f538868b 100644 --- a/src/Ryujinx/AppHost.cs +++ b/src/Ryujinx/AppHost.cs @@ -578,7 +578,6 @@ namespace Ryujinx.Ava public void Stop() { _isActive = false; - DiscordIntegrationModule.SwitchToMainState(); } private void Exit() @@ -862,13 +861,11 @@ namespace Ryujinx.Ava return false; } - - ApplicationMetadata appMeta = ApplicationLibrary.LoadAndSaveMetaData(Device.Processes.ActiveApplication.ProgramIdText, + + ApplicationLibrary.LoadAndSaveMetaData(Device.Processes.ActiveApplication.ProgramIdText, appMetadata => appMetadata.UpdatePreGame() ); - DiscordIntegrationModule.SwitchToPlayingState(appMeta, Device.Processes.ActiveApplication); - return true; }