diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-11-01 17:42:18 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-11-01 17:42:18 -0400 |
commit | e0b72374cd14298aacc6f71dc391fdc9814be37c (patch) | |
tree | 2e5d85937c34539c1a0df48423b5136508693ca8 /src/SMAPI.Mods.ConsoleCommands/ConsoleCommandsMod.cs | |
parent | 79118316065a01322d8ea12a14589ec016794c32 (diff) | |
parent | 089e6de749ae7cb109af00164d2597c6644c255e (diff) | |
download | SMAPI-e0b72374cd14298aacc6f71dc391fdc9814be37c.tar.gz SMAPI-e0b72374cd14298aacc6f71dc391fdc9814be37c.tar.bz2 SMAPI-e0b72374cd14298aacc6f71dc391fdc9814be37c.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Mods.ConsoleCommands/ConsoleCommandsMod.cs')
-rw-r--r-- | src/SMAPI.Mods.ConsoleCommands/ConsoleCommandsMod.cs | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/SMAPI.Mods.ConsoleCommands/ConsoleCommandsMod.cs b/src/SMAPI.Mods.ConsoleCommands/ConsoleCommandsMod.cs new file mode 100644 index 00000000..96658928 --- /dev/null +++ b/src/SMAPI.Mods.ConsoleCommands/ConsoleCommandsMod.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using StardewModdingAPI.Events; +using StardewModdingAPI.Mods.ConsoleCommands.Framework.Commands; + +namespace StardewModdingAPI.Mods.ConsoleCommands +{ + /// <summary>The main entry point for the mod.</summary> + public class ConsoleCommandsMod : Mod + { + /********* + ** Properties + *********/ + /// <summary>The commands to handle.</summary> + private ITrainerCommand[] Commands; + + + /********* + ** Public methods + *********/ + /// <summary>The mod entry point, called after the mod is first loaded.</summary> + /// <param name="helper">Provides simplified APIs for writing mods.</param> + public override void Entry(IModHelper helper) + { + // register commands + this.Commands = this.ScanForCommands().ToArray(); + foreach (ITrainerCommand command in this.Commands) + helper.ConsoleCommands.Add(command.Name, command.Description, (name, args) => this.HandleCommand(command, name, args)); + + // hook events + GameEvents.UpdateTick += this.GameEvents_UpdateTick; + } + + + /********* + ** Private methods + *********/ + /// <summary>The method invoked when the game updates its state.</summary> + /// <param name="sender">The event sender.</param> + /// <param name="e">The event arguments.</param> + private void GameEvents_UpdateTick(object sender, EventArgs e) + { + if (!Context.IsWorldReady) + return; + + foreach (ITrainerCommand command in this.Commands) + { + if (command.NeedsUpdate) + command.Update(this.Monitor); + } + } + + /// <summary>Handle a console command.</summary> + /// <param name="command">The command to invoke.</param> + /// <param name="commandName">The command name specified by the user.</param> + /// <param name="args">The command arguments.</param> + private void HandleCommand(ITrainerCommand command, string commandName, string[] args) + { + ArgumentParser argParser = new ArgumentParser(commandName, args, this.Monitor); + command.Handle(this.Monitor, commandName, argParser); + } + + /// <summary>Find all commands in the assembly.</summary> + private IEnumerable<ITrainerCommand> ScanForCommands() + { + return ( + from type in this.GetType().Assembly.GetTypes() + where !type.IsAbstract && typeof(ITrainerCommand).IsAssignableFrom(type) + select (ITrainerCommand)Activator.CreateInstance(type) + ); + } + } +} |