DiscordIntegrationModule.cs 5.0 KB

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