DiscordIntegrationModule.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using DiscordRPC;
  2. using Humanizer;
  3. using Humanizer.Localisation;
  4. using Ryujinx.Ava.Utilities.AppLibrary;
  5. using Ryujinx.Ava.Utilities.Configuration;
  6. using Ryujinx.Common;
  7. using Ryujinx.HLE;
  8. using Ryujinx.HLE.Loaders.Processes;
  9. using System.Text;
  10. namespace Ryujinx.Ava
  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. TitleIDs.CurrentApplication.Event += (_, e) =>
  41. {
  42. if (e.NewValue)
  43. SwitchToPlayingState(
  44. ApplicationLibrary.LoadAndSaveMetaData(e.NewValue),
  45. Switch.Shared.Processes.ActiveApplication
  46. );
  47. else
  48. SwitchToMainState();
  49. };
  50. }
  51. private static void Update(object sender, ReactiveEventArgs<bool> evnt)
  52. {
  53. if (evnt.OldValue != evnt.NewValue)
  54. {
  55. // If the integration was active, disable it and unload everything
  56. if (evnt.OldValue)
  57. {
  58. _discordClient?.Dispose();
  59. _discordClient = null;
  60. }
  61. // If we need to activate it and the client isn't active, initialize it
  62. if (evnt.NewValue && _discordClient == null)
  63. {
  64. _discordClient = new DiscordRpcClient(ApplicationId);
  65. _discordClient.Initialize();
  66. _discordClient.SetPresence(_discordPresenceMain);
  67. }
  68. }
  69. }
  70. private static void SwitchToPlayingState(ApplicationMetadata appMeta, ProcessResult procRes)
  71. {
  72. _discordClient?.SetPresence(new RichPresence
  73. {
  74. Assets = new Assets
  75. {
  76. LargeImageKey = TitleIDs.GetDiscordGameAsset(procRes.ProgramIdText),
  77. LargeImageText = TruncateToByteLength($"{appMeta.Title} (v{procRes.DisplayVersion})"),
  78. SmallImageKey = "ryujinx",
  79. SmallImageText = TruncateToByteLength(_description)
  80. },
  81. Details = TruncateToByteLength($"Playing {appMeta.Title}"),
  82. State = appMeta.LastPlayed.HasValue && appMeta.TimePlayed.TotalSeconds > 5
  83. ? $"Total play time: {appMeta.TimePlayed.Humanize(2, false, maxUnit: TimeUnit.Hour)}"
  84. : "Never played",
  85. Timestamps = Timestamps.Now
  86. });
  87. }
  88. private static void SwitchToMainState() => _discordClient?.SetPresence(_discordPresenceMain);
  89. private static string TruncateToByteLength(string input)
  90. {
  91. if (Encoding.UTF8.GetByteCount(input) <= ApplicationByteLimit)
  92. {
  93. return input;
  94. }
  95. // Find the length to trim the string to guarantee we have space for the trailing ellipsis.
  96. int trimLimit = ApplicationByteLimit - Encoding.UTF8.GetByteCount(Ellipsis);
  97. // Make sure the string is long enough to perform the basic trim.
  98. // Amount of bytes != Length of the string
  99. if (input.Length > trimLimit)
  100. {
  101. // Basic trim to best case scenario of 1 byte characters.
  102. input = input[..trimLimit];
  103. }
  104. while (Encoding.UTF8.GetByteCount(input) > trimLimit)
  105. {
  106. // Remove one character from the end of the string at a time.
  107. input = input[..^1];
  108. }
  109. return input.TrimEnd() + Ellipsis;
  110. }
  111. public static void Exit()
  112. {
  113. _discordClient?.Dispose();
  114. }
  115. }
  116. }