DiscordIntegrationModule.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using DiscordRPC;
  2. using Ryujinx.Common;
  3. using Ryujinx.Configuration;
  4. using System;
  5. namespace Ryujinx.Modules
  6. {
  7. static class DiscordIntegrationModule
  8. {
  9. private const string Description = "A simple, experimental Nintendo Switch emulator.";
  10. private const string CliendId = "568815339807309834";
  11. private static DiscordRpcClient _discordClient;
  12. private static RichPresence _discordPresenceMain;
  13. public static void Initialize()
  14. {
  15. _discordPresenceMain = new RichPresence
  16. {
  17. Assets = new Assets
  18. {
  19. LargeImageKey = "ryujinx",
  20. LargeImageText = Description
  21. },
  22. Details = "Main Menu",
  23. State = "Idling",
  24. Timestamps = Timestamps.Now,
  25. Buttons = new Button[]
  26. {
  27. new Button()
  28. {
  29. Label = "Website",
  30. Url = "https://ryujinx.org/"
  31. }
  32. }
  33. };
  34. ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
  35. }
  36. private static void Update(object sender, ReactiveEventArgs<bool> evnt)
  37. {
  38. if (evnt.OldValue != evnt.NewValue)
  39. {
  40. // If the integration was active, disable it and unload everything
  41. if (evnt.OldValue)
  42. {
  43. _discordClient?.Dispose();
  44. _discordClient = null;
  45. }
  46. // If we need to activate it and the client isn't active, initialize it
  47. if (evnt.NewValue && _discordClient == null)
  48. {
  49. _discordClient = new DiscordRpcClient(CliendId);
  50. _discordClient.Initialize();
  51. _discordClient.SetPresence(_discordPresenceMain);
  52. }
  53. }
  54. }
  55. public static void SwitchToPlayingState(string titleId, string titleName)
  56. {
  57. _discordClient?.SetPresence(new RichPresence
  58. {
  59. Assets = new Assets
  60. {
  61. LargeImageKey = "game",
  62. LargeImageText = titleName,
  63. SmallImageKey = "ryujinx",
  64. SmallImageText = Description,
  65. },
  66. Details = $"Playing {titleName}",
  67. State = (titleId == "0000000000000000") ? "Homebrew" : titleId.ToUpper(),
  68. Timestamps = Timestamps.Now,
  69. Buttons = new Button[]
  70. {
  71. new Button()
  72. {
  73. Label = "Website",
  74. Url = "https://ryujinx.org/"
  75. }
  76. }
  77. });
  78. }
  79. public static void SwitchToMainMenu()
  80. {
  81. _discordClient?.SetPresence(_discordPresenceMain);
  82. }
  83. public static void Exit()
  84. {
  85. _discordClient?.Dispose();
  86. }
  87. }
  88. }