|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- using System;
- using System.Reflection;
-
- using IllusionPlugin;
- using GamecraftModdingAPI.Events;
- using GamecraftModdingAPI.Commands;
- using Discord;
-
- namespace GamecraftRPC
- {
- public class Plugin : IPlugin // the Illusion Plugin Architecture (IPA) will ignore classes that don't implement IPlugin'
- {
- public string Name { get; } = Assembly.GetExecutingAssembly().GetName().Name;
-
- public string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
-
- private static readonly long CLIENT_ID = 692733325902872619;
-
- private Discord.Discord discordRPC;
-
- // called when Gamecraft shuts down
- public void OnApplicationQuit()
- {
- // Shutdown this mod
- GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has shutdown");
-
- // Shutdown the Gamecraft modding API last
- GamecraftModdingAPI.Main.Shutdown();
- }
-
- // called when Gamecraft starts up
- public void OnApplicationStart()
- {
- // Initialize the Gamecraft modding API first
- GamecraftModdingAPI.Main.Init();
-
- // Initialize this mod
- discordRPC = new Discord.Discord(CLIENT_ID, (UInt64)Discord.CreateFlags.Default);
-
- SetDiscordActivity(discordRPC, state: "Loading...", details: "Initializing Gamecraft", start: (int)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
-
- EventManager.AddEventHandler(new Events.GamePresenceHandler(discordRPC));
- EventManager.AddEventHandler(new Events.MenuPresenceHandler(discordRPC));
- EventManager.AddEventHandler(new Events.EditPresenceHandler(discordRPC));
- EventManager.AddEventHandler(new Events.SimulatePresenceHandler(discordRPC));
-
- SimpleCustomCommandEngine<string> rpcCommand = new SimpleCustomCommandEngine<string>(
- (s) => { SetDiscordActivity(discordRPC, state: s); }, // TODO: command action
- "SetRichPresence", // command name (used to invoke it in the console)
- "Set Discord status (experimental)" // command description (displayed when help command is executed)
- ); // this command can also be executed using the Command Computer
-
- // register the command so the modding API knows about it
- CommandManager.AddCommand(rpcCommand);
-
- GamecraftModdingAPI.Utility.Logging.LogDebug($"{Name} has started up");
- }
-
- // unused methods
-
- public void OnFixedUpdate() { } // called once per physics update
-
- public void OnLevelWasInitialized(int level) { } // called after a level is initialized
-
- public void OnLevelWasLoaded(int level) { } // called after a level is loaded
-
- public void OnUpdate() // called once per rendered frame (frame update)
- {
- if (discordRPC != null ) discordRPC.RunCallbacks();
- }
-
- 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)
- {
- if (discordRPC == null) return;
-
- Activity activity = new Activity
- {
- Instance = instance,
- };
-
- if (state != null) activity.State = state;
- if (details != null) activity.Details = details;
- if (start != 0) activity.Timestamps.Start = start;
- if (end != 0) activity.Timestamps.End = end;
- if (!string.IsNullOrEmpty(largeImg))
- {
- activity.Assets.LargeImage = largeImg;
- activity.Assets.LargeText = largeTxt;
- }
- if (!string.IsNullOrEmpty(smallImg))
- {
- activity.Assets.SmallImage = smallImg;
- activity.Assets.SmallText = smallTxt;
- }
-
- discordRPC.GetActivityManager().UpdateActivity(activity, result =>
- {
- GamecraftModdingAPI.Utility.Logging.MetaLog($"Update Activity Result: {result}");
- });
- }
- }
- }
|