DiscordIntegrationModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 = _discordGameAssetKeys.Contains(procRes.ProgramIdText) ? procRes.ProgramIdText : "game",
  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. private static readonly string[] _discordGameAssetKeys =
  106. [
  107. "010055d009f78000", // Fire Emblem: Three Houses
  108. "0100a12011cc8000", // Fire Emblem: Shadow Dragon
  109. "0100a6301214e000", // Fire Emblem Engage
  110. "0100f15003e64000", // Fire Emblem Warriors
  111. "010071f0143ea000", // Fire Emblem Warriors: Three Hopes
  112. "01007e3006dda000", // Kirby Star Allies
  113. "01004d300c5ae000", // Kirby and the Forgotten Land
  114. "01006b601380e000", // Kirby's Return to Dream Land Deluxe
  115. "01003fb00c5a8000", // Super Kirby Clash
  116. "0100227010460000", // Kirby Fighters 2
  117. "0100a8e016236000", // Kirby's Dream Buffet
  118. "01007ef00011e000", // The Legend of Zelda: Breath of the Wild
  119. "01006bb00c6f0000", // The Legend of Zelda: Link's Awakening
  120. "01002da013484000", // The Legend of Zelda: Skyward Sword HD
  121. "0100f2c0115b6000", // The Legend of Zelda: Tears of the Kingdom
  122. "01008cf01baac000", // The Legend of Zelda: Echoes of Wisdom
  123. "01000b900d8b0000", // Cadence of Hyrule
  124. "0100ae00096ea000", // Hyrule Warriors: Definitive Edition
  125. "01002b00111a2000", // Hyrule Warriors: Age of Calamity
  126. "010048701995e000", // Luigi's Mansion 2 HD
  127. "0100dca0064a6000", // Luigi's Mansion 3
  128. "010093801237c000", // Metroid Dread
  129. "010012101468c000", // Metroid Prime Remastered
  130. "0100000000010000", // SUPER MARIO ODYSSEY
  131. "0100ea80032ea000", // Super Mario Bros. U Deluxe
  132. "01009b90006dc000", // Super Mario Maker 2
  133. "010049900f546000", // Super Mario 3D All-Stars
  134. "010049900F546001", // ^ 64
  135. "010049900F546002", // ^ Sunshine
  136. "010049900F546003", // ^ Galaxy
  137. "010028600ebda000", // Super Mario 3D World + Bowser's Fury
  138. "010015100b514000", // Super Mario Bros. Wonder
  139. "0100152000022000", // Mario Kart 8 Deluxe
  140. "010036b0034e4000", // Super Mario Party
  141. "01006fe013472000", // Mario Party Superstars
  142. "0100965017338000", // Super Mario Party Jamboree
  143. "01006d0017f7a000", // Mario & Luigi: Brothership
  144. "010067300059a000", // Mario + Rabbids: Kingdom Battle
  145. "0100317013770000", // Mario + Rabbids: Sparks of Hope
  146. "0100a3900c3e2000", // Paper Mario: The Origami King
  147. "0100ecd018ebe000", // Paper Mario: The Thousand-Year Door
  148. "0100bc0018138000", // Super Mario RPG
  149. "0100bde00862a000", // Mario Tennis Aces
  150. "0100c9c00e25c000", // Mario Golf: Super Rush
  151. "010019401051c000", // Mario Strikers: Battle League
  152. "010003000e146000", // Mario & Sonic at the Olympic Games Tokyo 2020
  153. "0100b99019412000", // Mario vs. Donkey Kong
  154. "0100aa80194b0000", // Pikmin 1
  155. "0100d680194b2000", // Pikmin 2
  156. "0100f4c009322000", // Pikmin 3 Deluxe
  157. "0100b7c00933a000", // Pikmin 4
  158. "010003f003a34000", // Pokémon: Let's Go Pikachu!
  159. "0100187003a36000", // Pokémon: Let's Go Eevee!
  160. "0100abf008968000", // Pokémon Sword
  161. "01008db008c2c000", // Pokémon Shield
  162. "0100000011d90000", // Pokémon Brilliant Diamond
  163. "010018e011d92000", // Pokémon Shining Pearl
  164. "01001f5010dfa000", // Pokémon Legends: Arceus
  165. "0100a3d008c5c000", // Pokémon Scarlet
  166. "01008f6008c5e000", // Pokémon Violet
  167. "0100b3f000be2000", // Pokkén Tournament DX
  168. "0100f4300bf2c000", // New Pokémon Snap
  169. "01003bc0000a0000", // Splatoon 2 (US)
  170. "0100f8f0000a2000", // Splatoon 2 (EU)
  171. "01003c700009c000", // Splatoon 2 (JP)
  172. "0100c2500fc20000", // Splatoon 3
  173. "0100ba0018500000", // Splatoon 3: Splatfest World Premiere
  174. "010040600c5ce000", // Tetris 99
  175. "0100277011f1a000", // Super Mario Bros. 35
  176. "0100ad9012510000", // PAC-MAN 99
  177. "0100ccf019c8c000", // F-ZERO 99
  178. "0100d870045b6000", // NES - Nintendo Switch Online
  179. "01008d300c50c000", // SNES - Nintendo Switch Online
  180. "0100c9a00ece6000", // N64 - Nintendo Switch Online
  181. "0100e0601c632000", // N64 - Nintendo Switch Online 18+
  182. "0100c62011050000", // GB - Nintendo Switch Online
  183. "010012f017576000", // GBA - Nintendo Switch Online
  184. "01000320000cc000", // 1-2 Switch
  185. "0100300012f2a000", // Advance Wars 1+2: Re-Boot Camp
  186. "01006f8002326000", // Animal Crossing: New Horizons
  187. "0100620012d6e000", // Big Brain Academy: Brain vs. Brain
  188. "010018300d006000", // BOXBOY! + BOXGIRL!
  189. "0100c1f0051b6000", // Donkey Kong Country: Tropical Freeze
  190. "0100ed000d390000", // Dr. Kawashima's Brain Training
  191. "010067b017588000", // Endless Ocean Luminous
  192. "0100d2f00d5c0000", // Nintendo Switch Sports
  193. "01006b5012b32000", // Part Time UFO
  194. "0100704000B3A000", // Snipperclips
  195. "01006a800016e000", // Super Smash Bros. Ultimate
  196. "0100a9400c9c2000", // Tokyo Mirage Sessions #FE Encore
  197. "010076f0049a2000", // Bayonetta
  198. "01007960049a0000", // Bayonetta 2
  199. "01004a4010fea000", // Bayonetta 3
  200. "0100cf5010fec000", // Bayonetta Origins: Cereza and the Lost Demon
  201. "0100dcd01525a000", // Persona 3 Portable
  202. "010062b01525c000", // Persona 4 Golden
  203. "010075a016a3a000", // Persona 4 Arena Ultimax
  204. "01005ca01580e000", // Persona 5 Royal
  205. "0100801011c3e000", // Persona 5 Strikers
  206. "010087701b092000", // Persona 5 Tactica
  207. "01009aa000faa000", // Sonic Mania
  208. "01004ad014bf0000", // Sonic Frontiers
  209. "01005ea01c0fc000", // SONIC X SHADOW GENERATIONS
  210. "01005ea01c0fc001", // ^
  211. "010056e00853a000", // A Hat in Time
  212. "0100dbf01000a000", // Burnout Paradise Remastered
  213. "0100744001588000", // Cars 3: Driven to Win
  214. "0100b41013c82000", // Cruis'n Blast
  215. "01008c8012920000", // Dying Light Platinum Edition
  216. "010073c01af34000", // LEGO Horizon Adventures
  217. "0100770008dd8000", // Monster Hunter Generations Ultimate
  218. "0100b04011742000", // Monster Hunter Rise
  219. "0100853015e86000", // No Man's Sky
  220. "01007bb017812000", // Portal
  221. "0100abd01785c000", // Portal 2
  222. "01008e200c5c2000", // Muse Dash
  223. "01007820196a6000", // Red Dead Redemption
  224. "01002f7013224000", // Rune Factory 5
  225. "01008d100d43e000", // Saints Row IV
  226. "0100de600beee000", // Saints Row: The Third - The Full Package
  227. "01001180021fa000", // Shovel Knight: Specter of Torment
  228. "0100d7a01b7a2000", // Star Wars: Bounty Hunter
  229. "0100800015926000", // Suika Game
  230. "0100e46006708000", // Terraria
  231. "01000a10041ea000", // The Elder Scrolls V: Skyrim
  232. "010080b00ad66000", // Undertale
  233. ];
  234. }
  235. }