2023-12-04 14:17:13 +01:00
|
|
|
using DiscordRPC;
|
2024-10-10 18:15:38 -05:00
|
|
|
using Humanizer;
|
2024-11-11 18:22:14 -06:00
|
|
|
using Humanizer.Localisation;
|
2019-12-21 20:52:31 +01:00
|
|
|
using Ryujinx.Common;
|
2024-10-11 23:55:50 -05:00
|
|
|
using Ryujinx.HLE.Loaders.Processes;
|
2024-10-10 18:15:38 -05:00
|
|
|
using Ryujinx.UI.App.Common;
|
2024-02-11 02:09:18 +00:00
|
|
|
using Ryujinx.UI.Common.Configuration;
|
2024-05-14 15:36:44 +01:00
|
|
|
using System.Text;
|
2019-12-21 20:52:31 +01:00
|
|
|
|
2024-02-11 02:09:18 +00:00
|
|
|
namespace Ryujinx.UI.Common
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2022-05-15 11:30:15 +00:00
|
|
|
public static class DiscordIntegrationModule
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2024-10-10 18:15:38 -05:00
|
|
|
public static Timestamps StartedAt { get; set; }
|
2024-10-21 23:16:41 -05:00
|
|
|
|
2024-11-10 23:26:15 -06:00
|
|
|
private static string VersionString
|
|
|
|
|
=> (ReleaseInformation.IsCanaryBuild ? "Canary " : string.Empty) + $"v{ReleaseInformation.Version}";
|
|
|
|
|
|
|
|
|
|
private static readonly string _description =
|
|
|
|
|
ReleaseInformation.IsValid
|
2024-11-10 23:32:37 -06:00
|
|
|
? $"{VersionString} {ReleaseInformation.ReleaseChannelOwner}/{ReleaseInformation.ReleaseChannelSourceRepo}@{ReleaseInformation.BuildGitHash}"
|
2024-11-10 23:26:15 -06:00
|
|
|
: "dev build";
|
2024-10-16 19:23:11 -05:00
|
|
|
|
2024-10-10 18:15:38 -05:00
|
|
|
private const string ApplicationId = "1293250299716173864";
|
2019-12-21 20:52:31 +01:00
|
|
|
|
2024-05-14 15:36:44 +01:00
|
|
|
private const int ApplicationByteLimit = 128;
|
|
|
|
|
private const string Ellipsis = "…";
|
|
|
|
|
|
2021-05-02 22:17:34 +02:00
|
|
|
private static DiscordRpcClient _discordClient;
|
2023-06-29 02:39:22 +02:00
|
|
|
private static RichPresence _discordPresenceMain;
|
2019-12-21 20:52:31 +01:00
|
|
|
|
|
|
|
|
public static void Initialize()
|
|
|
|
|
{
|
2021-05-02 22:17:34 +02:00
|
|
|
_discordPresenceMain = new RichPresence
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2021-01-08 09:14:13 +01:00
|
|
|
Assets = new Assets
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2023-06-29 02:39:22 +02:00
|
|
|
LargeImageKey = "ryujinx",
|
2024-10-11 23:55:50 -05:00
|
|
|
LargeImageText = TruncateToByteLength(_description)
|
2019-12-21 20:52:31 +01:00
|
|
|
},
|
2023-06-29 02:39:22 +02:00
|
|
|
Details = "Main Menu",
|
|
|
|
|
State = "Idling",
|
2024-10-10 18:15:38 -05:00
|
|
|
Timestamps = StartedAt
|
2019-12-21 20:52:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-02 22:17:34 +02:00
|
|
|
private static void Update(object sender, ReactiveEventArgs<bool> evnt)
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2021-05-02 22:17:34 +02:00
|
|
|
if (evnt.OldValue != evnt.NewValue)
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
|
|
|
|
// If the integration was active, disable it and unload everything
|
2021-05-02 22:17:34 +02:00
|
|
|
if (evnt.OldValue)
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient?.Dispose();
|
2019-12-21 20:52:31 +01:00
|
|
|
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient = null;
|
2019-12-21 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we need to activate it and the client isn't active, initialize it
|
2021-05-02 22:17:34 +02:00
|
|
|
if (evnt.NewValue && _discordClient == null)
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2024-03-16 19:41:38 +01:00
|
|
|
_discordClient = new DiscordRpcClient(ApplicationId);
|
2019-12-21 20:52:31 +01:00
|
|
|
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient.Initialize();
|
2021-05-02 22:17:34 +02:00
|
|
|
_discordClient.SetPresence(_discordPresenceMain);
|
2019-12-21 20:52:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-11 23:55:50 -05:00
|
|
|
public static void SwitchToPlayingState(ApplicationMetadata appMeta, ProcessResult procRes)
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2021-05-02 22:17:34 +02:00
|
|
|
_discordClient?.SetPresence(new RichPresence
|
2019-12-21 20:52:31 +01:00
|
|
|
{
|
2021-05-02 22:17:34 +02:00
|
|
|
Assets = new Assets
|
|
|
|
|
{
|
2024-12-26 00:29:00 -06:00
|
|
|
LargeImageKey = TitleIDs.GetDiscordGameAsset(procRes.ProgramIdText),
|
2024-11-11 18:22:14 -06:00
|
|
|
LargeImageText = TruncateToByteLength($"{appMeta.Title} (v{procRes.DisplayVersion})"),
|
2023-06-29 02:39:22 +02:00
|
|
|
SmallImageKey = "ryujinx",
|
2024-10-11 23:55:50 -05:00
|
|
|
SmallImageText = TruncateToByteLength(_description)
|
2021-05-02 22:17:34 +02:00
|
|
|
},
|
2024-10-11 17:57:33 -05:00
|
|
|
Details = TruncateToByteLength($"Playing {appMeta.Title}"),
|
2024-10-16 19:23:11 -05:00
|
|
|
State = appMeta.LastPlayed.HasValue && appMeta.TimePlayed.TotalSeconds > 5
|
2024-11-11 18:22:14 -06:00
|
|
|
? $"Total play time: {appMeta.TimePlayed.Humanize(2, false, maxUnit: TimeUnit.Hour)}"
|
2024-10-11 23:55:50 -05:00
|
|
|
: "Never played",
|
2024-10-10 18:15:38 -05:00
|
|
|
Timestamps = Timestamps.Now
|
2021-05-02 22:17:34 +02:00
|
|
|
});
|
2019-12-21 20:52:31 +01:00
|
|
|
}
|
2020-01-21 23:23:11 +01:00
|
|
|
|
2024-10-19 16:40:28 -05:00
|
|
|
public static void SwitchToMainState() => _discordClient?.SetPresence(_discordPresenceMain);
|
|
|
|
|
|
2024-10-11 17:57:33 -05:00
|
|
|
private static string TruncateToByteLength(string input)
|
2024-05-14 15:36:44 +01:00
|
|
|
{
|
2024-10-11 17:57:33 -05:00
|
|
|
if (Encoding.UTF8.GetByteCount(input) <= ApplicationByteLimit)
|
2024-05-14 15:36:44 +01:00
|
|
|
{
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the length to trim the string to guarantee we have space for the trailing ellipsis.
|
2024-10-11 17:57:33 -05:00
|
|
|
int trimLimit = ApplicationByteLimit - Encoding.UTF8.GetByteCount(Ellipsis);
|
2024-05-14 15:36:44 +01:00
|
|
|
|
2024-06-26 11:27:23 +02:00
|
|
|
// Make sure the string is long enough to perform the basic trim.
|
|
|
|
|
// Amount of bytes != Length of the string
|
|
|
|
|
if (input.Length > trimLimit)
|
|
|
|
|
{
|
|
|
|
|
// Basic trim to best case scenario of 1 byte characters.
|
|
|
|
|
input = input[..trimLimit];
|
|
|
|
|
}
|
2024-05-14 15:36:44 +01:00
|
|
|
|
|
|
|
|
while (Encoding.UTF8.GetByteCount(input) > trimLimit)
|
|
|
|
|
{
|
|
|
|
|
// Remove one character from the end of the string at a time.
|
|
|
|
|
input = input[..^1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return input.TrimEnd() + Ellipsis;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-21 23:23:11 +01:00
|
|
|
public static void Exit()
|
|
|
|
|
{
|
2020-03-29 04:25:54 +01:00
|
|
|
_discordClient?.Dispose();
|
2020-01-21 23:23:11 +01:00
|
|
|
}
|
2019-12-21 20:52:31 +01:00
|
|
|
}
|
2023-06-29 02:39:22 +02:00
|
|
|
}
|