DiscordIntegrationModule.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using DiscordRPC;
  2. using Ryujinx.Common;
  3. using System;
  4. using System.IO;
  5. using System.Linq;
  6. namespace Ryujinx.Configuration
  7. {
  8. static class DiscordIntegrationModule
  9. {
  10. private static DiscordRpcClient DiscordClient;
  11. private static string LargeDescription = "Ryujinx is a Nintendo Switch emulator.";
  12. public static RichPresence DiscordPresence { get; private set; }
  13. public static void Initialize()
  14. {
  15. DiscordPresence = new RichPresence
  16. {
  17. Assets = new Assets
  18. {
  19. LargeImageKey = "ryujinx",
  20. LargeImageText = LargeDescription
  21. },
  22. Details = "Main Menu",
  23. State = "Idling",
  24. Timestamps = new Timestamps(DateTime.UtcNow)
  25. };
  26. ConfigurationState.Instance.EnableDiscordIntegration.Event += Update;
  27. }
  28. private static void Update(object sender, ReactiveEventArgs<bool> e)
  29. {
  30. if (e.OldValue != e.NewValue)
  31. {
  32. // If the integration was active, disable it and unload everything
  33. if (e.OldValue)
  34. {
  35. DiscordClient?.Dispose();
  36. DiscordClient = null;
  37. }
  38. // If we need to activate it and the client isn't active, initialize it
  39. if (e.NewValue && DiscordClient == null)
  40. {
  41. DiscordClient = new DiscordRpcClient("568815339807309834");
  42. DiscordClient.Initialize();
  43. DiscordClient.SetPresence(DiscordPresence);
  44. }
  45. }
  46. }
  47. public static void SwitchToPlayingState(string titleId, string titleName)
  48. {
  49. if (File.ReadAllLines(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "RPsupported.dat")).Contains(titleId))
  50. {
  51. DiscordPresence.Assets.LargeImageKey = titleId;
  52. }
  53. string state = titleId;
  54. if (state == null)
  55. {
  56. state = "Ryujinx";
  57. }
  58. else
  59. {
  60. state = state.ToUpper();
  61. }
  62. string details = "Idling";
  63. if (titleName != null)
  64. {
  65. details = $"Playing {titleName}";
  66. }
  67. DiscordPresence.Details = details;
  68. DiscordPresence.State = state;
  69. DiscordPresence.Assets.LargeImageText = titleName;
  70. DiscordPresence.Assets.SmallImageKey = "ryujinx";
  71. DiscordPresence.Assets.SmallImageText = LargeDescription;
  72. DiscordPresence.Timestamps = new Timestamps(DateTime.UtcNow);
  73. DiscordClient?.SetPresence(DiscordPresence);
  74. }
  75. }
  76. }