DiscordIntegrationModule.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using DiscordRPC;
  2. using Humanizer;
  3. using Humanizer.Localisation;
  4. using Ryujinx.Common;
  5. using Ryujinx.HLE.Loaders.Processes;
  6. using Ryujinx.UI.App.Common;
  7. using Ryujinx.UI.Common.Configuration;
  8. using System.Linq;
  9. using System.Text;
  10. namespace Ryujinx.UI.Common
  11. {
  12. public static class DiscordIntegrationModule
  13. {
  14. public static Timestamps StartedAt { get; set; }
  15. private static string VersionString
  16. => (ReleaseInformation.IsCanaryBuild ? "Canary " : string.Empty) + $"v{ReleaseInformation.Version}";
  17. private static readonly string _description =
  18. ReleaseInformation.IsValid
  19. ? $"{VersionString} {ReleaseInformation.ReleaseChannelOwner}/{ReleaseInformation.ReleaseChannelSourceRepo}@{ReleaseInformation.BuildGitHash}"
  20. : "dev build";
  21. private const string ApplicationId = "1293250299716173864";
  22. private const int ApplicationByteLimit = 128;
  23. private const string Ellipsis = "…";
  24. private static DiscordRpcClient _discordClient;
  25. private static RichPresence _discordPresenceMain;
  26. public static void Initialize()
  27. {
  28. _discordPresenceMain = new RichPresence
  29. {
  30. Assets = new Assets
  31. {
  32. LargeImageKey = "ryujinx",
  33. LargeImageText = TruncateToByteLength(_description)
  34. },
  35. Details = "Main Menu",
  36. State = "Idling",
  37. Timestamps = StartedAt
  38. };
  39. ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
  40. }
  41. private static void Update(object sender, ReactiveEventArgs<bool> evnt)
  42. {
  43. if (evnt.OldValue != evnt.NewValue)
  44. {
  45. // If the integration was active, disable it and unload everything
  46. if (evnt.OldValue)
  47. {
  48. _discordClient?.Dispose();
  49. _discordClient = null;
  50. }
  51. // If we need to activate it and the client isn't active, initialize it
  52. if (evnt.NewValue && _discordClient == null)
  53. {
  54. _discordClient = new DiscordRpcClient(ApplicationId);
  55. _discordClient.Initialize();
  56. _discordClient.SetPresence(_discordPresenceMain);
  57. }
  58. }
  59. }
  60. public static void SwitchToPlayingState(ApplicationMetadata appMeta, ProcessResult procRes)
  61. {
  62. _discordClient?.SetPresence(new RichPresence
  63. {
  64. Assets = new Assets
  65. {
  66. LargeImageKey = TitleIDs.GetDiscordGameAsset(procRes.ProgramIdText),
  67. LargeImageText = TruncateToByteLength($"{appMeta.Title} (v{procRes.DisplayVersion})"),
  68. SmallImageKey = "ryujinx",
  69. SmallImageText = TruncateToByteLength(_description)
  70. },
  71. Details = TruncateToByteLength($"Playing {appMeta.Title}"),
  72. State = appMeta.LastPlayed.HasValue && appMeta.TimePlayed.TotalSeconds > 5
  73. ? $"Total play time: {appMeta.TimePlayed.Humanize(2, false, maxUnit: TimeUnit.Hour)}"
  74. : "Never played",
  75. Timestamps = Timestamps.Now
  76. });
  77. }
  78. public static void SwitchToMainState() => _discordClient?.SetPresence(_discordPresenceMain);
  79. private static string TruncateToByteLength(string input)
  80. {
  81. if (Encoding.UTF8.GetByteCount(input) <= ApplicationByteLimit)
  82. {
  83. return input;
  84. }
  85. // Find the length to trim the string to guarantee we have space for the trailing ellipsis.
  86. int trimLimit = ApplicationByteLimit - Encoding.UTF8.GetByteCount(Ellipsis);
  87. // Make sure the string is long enough to perform the basic trim.
  88. // Amount of bytes != Length of the string
  89. if (input.Length > trimLimit)
  90. {
  91. // Basic trim to best case scenario of 1 byte characters.
  92. input = input[..trimLimit];
  93. }
  94. while (Encoding.UTF8.GetByteCount(input) > trimLimit)
  95. {
  96. // Remove one character from the end of the string at a time.
  97. input = input[..^1];
  98. }
  99. return input.TrimEnd() + Ellipsis;
  100. }
  101. public static void Exit()
  102. {
  103. _discordClient?.Dispose();
  104. }
  105. }
  106. }