DiscordIntegrationModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using DiscordRPC;
  2. using Humanizer;
  3. using LibHac.Bcat;
  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.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. namespace Ryujinx.UI.Common
  12. {
  13. public static class DiscordIntegrationModule
  14. {
  15. public static Timestamps StartedAt { get; set; }
  16. private static string VersionString
  17. => (ReleaseInformation.IsCanaryBuild ? "Canary " : string.Empty) + $"v{ReleaseInformation.Version}";
  18. private static readonly string _description =
  19. ReleaseInformation.IsValid
  20. ? $"{VersionString} {ReleaseInformation.ReleaseChannelOwner}/{ReleaseInformation.ReleaseChannelSourceRepo}@{ReleaseInformation.BuildGitHash}"
  21. : "dev build";
  22. private const string ApplicationId = "1293250299716173864";
  23. private const int ApplicationByteLimit = 128;
  24. private const string Ellipsis = "…";
  25. private static DiscordRpcClient _discordClient;
  26. private static RichPresence _discordPresenceMain;
  27. public static void Initialize()
  28. {
  29. _discordPresenceMain = new RichPresence
  30. {
  31. Assets = new Assets
  32. {
  33. LargeImageKey = "ryujinx",
  34. LargeImageText = TruncateToByteLength(_description)
  35. },
  36. Details = "Main Menu",
  37. State = "Idling",
  38. Timestamps = StartedAt
  39. };
  40. ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
  41. }
  42. private static void Update(object sender, ReactiveEventArgs<bool> evnt)
  43. {
  44. if (evnt.OldValue != evnt.NewValue)
  45. {
  46. // If the integration was active, disable it and unload everything
  47. if (evnt.OldValue)
  48. {
  49. _discordClient?.Dispose();
  50. _discordClient = null;
  51. }
  52. // If we need to activate it and the client isn't active, initialize it
  53. if (evnt.NewValue && _discordClient == null)
  54. {
  55. _discordClient = new DiscordRpcClient(ApplicationId);
  56. _discordClient.Initialize();
  57. _discordClient.SetPresence(_discordPresenceMain);
  58. }
  59. }
  60. }
  61. public static void SwitchToPlayingState(ApplicationMetadata appMeta, ProcessResult procRes)
  62. {
  63. _discordClient?.SetPresence(new RichPresence
  64. {
  65. Assets = new Assets
  66. {
  67. LargeImageKey = _discordGameAssetKeys.Contains(procRes.ProgramIdText) ? procRes.ProgramIdText : "game",
  68. LargeImageText = TruncateToByteLength($"{appMeta.Title} | {procRes.DisplayVersion}"),
  69. SmallImageKey = "ryujinx",
  70. SmallImageText = TruncateToByteLength(_description)
  71. },
  72. Details = TruncateToByteLength($"Playing {appMeta.Title}"),
  73. State = appMeta.LastPlayed.HasValue && appMeta.TimePlayed.TotalSeconds > 5
  74. ? $"Total play time: {appMeta.TimePlayed.Humanize(2, false)}"
  75. : "Never played",
  76. Timestamps = Timestamps.Now
  77. });
  78. }
  79. public static void SwitchToMainState() => _discordClient?.SetPresence(_discordPresenceMain);
  80. private static string TruncateToByteLength(string input)
  81. {
  82. if (Encoding.UTF8.GetByteCount(input) <= ApplicationByteLimit)
  83. {
  84. return input;
  85. }
  86. // Find the length to trim the string to guarantee we have space for the trailing ellipsis.
  87. int trimLimit = ApplicationByteLimit - Encoding.UTF8.GetByteCount(Ellipsis);
  88. // Make sure the string is long enough to perform the basic trim.
  89. // Amount of bytes != Length of the string
  90. if (input.Length > trimLimit)
  91. {
  92. // Basic trim to best case scenario of 1 byte characters.
  93. input = input[..trimLimit];
  94. }
  95. while (Encoding.UTF8.GetByteCount(input) > trimLimit)
  96. {
  97. // Remove one character from the end of the string at a time.
  98. input = input[..^1];
  99. }
  100. return input.TrimEnd() + Ellipsis;
  101. }
  102. public static void Exit()
  103. {
  104. _discordClient?.Dispose();
  105. }
  106. private static readonly string[] _discordGameAssetKeys =
  107. [
  108. "010055d009f78000", // Fire Emblem: Three Houses
  109. "0100a12011cc8000", // Fire Emblem: Shadow Dragon
  110. "0100a6301214e000", // Fire Emblem Engage
  111. "0100f15003e64000", // Fire Emblem Warriors
  112. "010071f0143ea000", // Fire Emblem Warriors: Three Hopes
  113. "01007e3006dda000", // Kirby Star Allies
  114. "01004d300c5ae000", // Kirby and the Forgotten Land
  115. "01006b601380e000", // Kirby's Return to Dream Land Deluxe
  116. "01003fb00c5a8000", // Super Kirby Clash
  117. "0100227010460000", // Kirby Fighters 2
  118. "0100a8e016236000", // Kirby's Dream Buffet
  119. "01007ef00011e000", // The Legend of Zelda: Breath of the Wild
  120. "01006bb00c6f0000", // The Legend of Zelda: Link's Awakening
  121. "01002da013484000", // The Legend of Zelda: Skyward Sword HD
  122. "0100f2c0115b6000", // The Legend of Zelda: Tears of the Kingdom
  123. "01008cf01baac000", // The Legend of Zelda: Echoes of Wisdom
  124. "01000b900d8b0000", // Cadence of Hyrule
  125. "0100ae00096ea000", // Hyrule Warriors: Definitive Edition
  126. "01002b00111a2000", // Hyrule Warriors: Age of Calamity
  127. "010048701995e000", // Luigi's Mansion 2 HD
  128. "0100dca0064a6000", // Luigi's Mansion 3
  129. "010093801237c000", // Metroid Dread
  130. "010012101468c000", // Metroid Prime Remastered
  131. "0100000000010000", // SUPER MARIO ODYSSEY
  132. "0100ea80032ea000", // Super Mario Bros. U Deluxe
  133. "01009b90006dc000", // Super Mario Maker 2
  134. "010049900f546000", // Super Mario 3D All-Stars
  135. "010049900F546001", // ^ 64
  136. "010049900F546002", // ^ Sunshine
  137. "010049900F546003", // ^ Galaxy
  138. "010028600ebda000", // Super Mario 3D World + Bowser's Fury
  139. "010015100b514000", // Super Mario Bros. Wonder
  140. "0100152000022000", // Mario Kart 8 Deluxe
  141. "010036b0034e4000", // Super Mario Party
  142. "01006fe013472000", // Mario Party Superstars
  143. "0100965017338000", // Super Mario Party Jamboree
  144. "01006d0017f7a000", // Mario & Luigi: Brothership
  145. "010067300059a000", // Mario + Rabbids: Kingdom Battle
  146. "0100317013770000", // Mario + Rabbids: Sparks of Hope
  147. "0100a3900c3e2000", // Paper Mario: The Origami King
  148. "0100ecd018ebe000", // Paper Mario: The Thousand-Year Door
  149. "0100bc0018138000", // Super Mario RPG
  150. "0100bde00862a000", // Mario Tennis Aces
  151. "0100c9c00e25c000", // Mario Golf: Super Rush
  152. "010019401051c000", // Mario Strikers: Battle League
  153. "010003000e146000", // Mario & Sonic at the Olympic Games Tokyo 2020
  154. "0100b99019412000", // Mario vs. Donkey Kong
  155. "0100aa80194b0000", // Pikmin 1
  156. "0100d680194b2000", // Pikmin 2
  157. "0100f4c009322000", // Pikmin 3 Deluxe
  158. "0100b7c00933a000", // Pikmin 4
  159. "010003f003a34000", // Pokémon: Let's Go Pikachu!
  160. "0100187003a36000", // Pokémon: Let's Go Eevee!
  161. "0100abf008968000", // Pokémon Sword
  162. "01008db008c2c000", // Pokémon Shield
  163. "0100000011d90000", // Pokémon Brilliant Diamond
  164. "010018e011d92000", // Pokémon Shining Pearl
  165. "01001f5010dfa000", // Pokémon Legends: Arceus
  166. "0100a3d008c5c000", // Pokémon Scarlet
  167. "01008f6008c5e000", // Pokémon Violet
  168. "0100b3f000be2000", // Pokkén Tournament DX
  169. "0100f4300bf2c000", // New Pokémon Snap
  170. "01003bc0000a0000", // Splatoon 2 (US)
  171. "0100f8f0000a2000", // Splatoon 2 (EU)
  172. "01003c700009c000", // Splatoon 2 (JP)
  173. "0100c2500fc20000", // Splatoon 3
  174. "0100ba0018500000", // Splatoon 3: Splatfest World Premiere
  175. "010040600c5ce000", // Tetris 99
  176. "0100277011f1a000", // Super Mario Bros. 35
  177. "0100ad9012510000", // PAC-MAN 99
  178. "0100ccf019c8c000", // F-ZERO 99
  179. "0100d870045b6000", // NES - Nintendo Switch Online
  180. "01008d300c50c000", // SNES - Nintendo Switch Online
  181. "0100c9a00ece6000", // N64 - Nintendo Switch Online
  182. "0100e0601c632000", // N64 - Nintendo Switch Online 18+
  183. "0100c62011050000", // GB - Nintendo Switch Online
  184. "010012f017576000", // GBA - Nintendo Switch Online
  185. "01000320000cc000", // 1-2 Switch
  186. "0100300012f2a000", // Advance Wars 1+2: Re-Boot Camp
  187. "01006f8002326000", // Animal Crossing: New Horizons
  188. "0100620012d6e000", // Big Brain Academy: Brain vs. Brain
  189. "010018300d006000", // BOXBOY! + BOXGIRL!
  190. "0100c1f0051b6000", // Donkey Kong Country: Tropical Freeze
  191. "0100ed000d390000", // Dr. Kawashima's Brain Training
  192. "010067b017588000", // Endless Ocean Luminous
  193. "0100d2f00d5c0000", // Nintendo Switch Sports
  194. "01006b5012b32000", // Part Time UFO
  195. "0100704000B3A000", // Snipperclips
  196. "01006a800016e000", // Super Smash Bros. Ultimate
  197. "0100a9400c9c2000", // Tokyo Mirage Sessions #FE Encore
  198. "010076f0049a2000", // Bayonetta
  199. "01007960049a0000", // Bayonetta 2
  200. "01004a4010fea000", // Bayonetta 3
  201. "0100cf5010fec000", // Bayonetta Origins: Cereza and the Lost Demon
  202. "0100dcd01525a000", // Persona 3 Portable
  203. "010062b01525c000", // Persona 4 Golden
  204. "010075a016a3a000", // Persona 4 Arena Ultimax
  205. "01005ca01580e000", // Persona 5 Royal
  206. "0100801011c3e000", // Persona 5 Strikers
  207. "010087701b092000", // Persona 5 Tactica
  208. "01009aa000faa000", // Sonic Mania
  209. "01004ad014bf0000", // Sonic Frontiers
  210. "01005ea01c0fc000", // SONIC X SHADOW GENERATIONS
  211. "01005ea01c0fc001", // ^
  212. "010056e00853a000", // A Hat in Time
  213. "0100dbf01000a000", // Burnout Paradise Remastered
  214. "0100744001588000", // Cars 3: Driven to Win
  215. "0100b41013c82000", // Cruis'n Blast
  216. "01008c8012920000", // Dying Light Platinum Edition
  217. "01000a10041ea000", // The Elder Scrolls V: Skyrim
  218. "0100770008dd8000", // Monster Hunter Generations Ultimate
  219. "0100b04011742000", // Monster Hunter Rise
  220. "0100853015e86000", // No Man's Sky
  221. "01007bb017812000", // Portal
  222. "0100abd01785c000", // Portal 2
  223. "01008e200c5c2000", // Muse Dash
  224. "01007820196a6000", // Red Dead Redemption
  225. "01002f7013224000", // Rune Factory 5
  226. "01008d100d43e000", // Saints Row IV
  227. "0100de600beee000", // Saints Row: The Third - The Full Package
  228. "01001180021fa000", // Shovel Knight: Specter of Torment
  229. "0100d7a01b7a2000", // Star Wars: Bounty Hunter
  230. "0100800015926000", // Suika Game
  231. "0100e46006708000", // Terraria
  232. "010080b00ad66000", // Undertale
  233. ];
  234. }
  235. }