This repository has moved! Please check out the latest version at the new location https://git.exmods.org/NGnius/GameSDKcraft
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

102 lines
4.0KB

  1. using System;
  2. using System.Reflection;
  3. using IllusionPlugin;
  4. using GamecraftModdingAPI.Events;
  5. using GamecraftModdingAPI.Commands;
  6. using Discord;
  7. namespace GamecraftRPC
  8. {
  9. public class Plugin : IPlugin // the Illusion Plugin Architecture (IPA) will ignore classes that don't implement IPlugin'
  10. {
  11. public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
  12. public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  13. private static readonly long CLIENT_ID = 692733325902872619;
  14. private Discord.Discord discordRPC;
  15. // called when Gamecraft shuts down
  16. public void OnApplicationQuit()
  17. {
  18. // Shutdown this mod
  19. GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has shutdown");
  20. // Shutdown the Gamecraft modding API last
  21. GamecraftModdingAPI.Main.Shutdown();
  22. }
  23. // called when Gamecraft starts up
  24. public void OnApplicationStart()
  25. {
  26. // Initialize the Gamecraft modding API first
  27. GamecraftModdingAPI.Main.Init();
  28. // Initialize this mod
  29. discordRPC = new Discord.Discord(CLIENT_ID, (UInt64)Discord.CreateFlags.Default);
  30. SetDiscordActivity(discordRPC, state: "Loading...", details: "Initializing Gamecraft", start: (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
  31. EventManager.AddEventHandler(new Events.GamePresenceHandler(discordRPC));
  32. EventManager.AddEventHandler(new Events.MenuPresenceHandler(discordRPC));
  33. EventManager.AddEventHandler(new Events.EditPresenceHandler(discordRPC));
  34. EventManager.AddEventHandler(new Events.SimulatePresenceHandler(discordRPC));
  35. SimpleCustomCommandEngine<string> rpcCommand = new SimpleCustomCommandEngine<string>(
  36. (s) => { SetDiscordActivity(discordRPC, state: s); }, // TODO: command action
  37. "SetRichPresence", // command name (used to invoke it in the console)
  38. "Set Discord status (experimental)" // command description (displayed when help command is executed)
  39. ); // this command can also be executed using the Command Computer
  40. // register the command so the modding API knows about it
  41. CommandManager.AddCommand(rpcCommand);
  42. GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has started up");
  43. }
  44. // unused methods
  45. public void OnFixedUpdate() { } // called once per physics update
  46. public void OnLevelWasInitialized(int level) { } // called after a level is initialized
  47. public void OnLevelWasLoaded(int level) { } // called after a level is loaded
  48. public void OnUpdate() // called once per rendered frame (frame update)
  49. {
  50. if (discordRPC != null ) discordRPC.RunCallbacks();
  51. }
  52. public static void SetDiscordActivity(Discord.Discord discordRPC, string state = null, string details = null, int start = 0, int end = 0, string largeImg = "gamecraft-logo-g", string largeTxt = "Gamecraft", string smallImg = "exmods-logo-xm2", string smallTxt = "Exmods", bool instance = true)
  53. {
  54. if (discordRPC == null) return;
  55. Activity activity = new Activity
  56. {
  57. Instance = instance,
  58. };
  59. if (state != null) activity.State = state;
  60. if (details != null) activity.Details = details;
  61. if (start != 0) activity.Timestamps.Start = start;
  62. if (end != 0) activity.Timestamps.End = end;
  63. if (!string.IsNullOrEmpty(largeImg))
  64. {
  65. activity.Assets.LargeImage = largeImg;
  66. activity.Assets.LargeText = largeTxt;
  67. }
  68. if (!string.IsNullOrEmpty(smallImg))
  69. {
  70. activity.Assets.SmallImage = smallImg;
  71. activity.Assets.SmallText = smallTxt;
  72. }
  73. discordRPC.GetActivityManager().UpdateActivity(activity, result =>
  74. {
  75. GamecraftModdingAPI.Utility.Logging.MetaLog($"Update Activity Result: {result}");
  76. });
  77. }
  78. }
  79. }