/// <summary> /// Executes a string as a command. /// </summary> /// <param name="text">The command text.</param> public static void Execute(string text) { if (String.IsNullOrEmpty(text)) return; var args = new CommandEventArgs(ParseParameters(text)); string commandName = args[-1].ToLowerInvariant(); Command command = ChatCommands.FirstOrDefault(c => c.Names.Contains(commandName)); if (command != null) command.Invoke(args); else Utils.ErrorMessage("Invalid command."); }
/// <summary> /// Invokes the command call back. /// </summary> /// <param name="e">The command event arguments.</param> public void Invoke(CommandEventArgs e) { callback(this, e); }
static void Set(object o, CommandEventArgs e) { if (e.Length == 0 || String.Equals(e[0], "list", StringComparison.OrdinalIgnoreCase)) { Utils.SuccessMessage("Config options:"); foreach (FieldInfo fi in typeof(Config).GetFields(BindingFlags.Instance | BindingFlags.Public)) { Type t = fi.FieldType; if (t != typeof(bool) && t != typeof(int) && t != typeof(string)) continue; Utils.InfoMessage("{0}: {1} ({2})", fi.Name, fi.GetValue(Raptor.Config), ((DescriptionAttribute)fi.GetCustomAttributes(false)[0]).Description); } } else { FieldInfo fi = typeof(Config).GetField(e[0], BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (fi == null) { Utils.ErrorMessage("Invalid option \"{0}\".", e[0]); return; } Type t = fi.FieldType; if (t != typeof(bool) && t != typeof(int) && t != typeof(string)) { Utils.ErrorMessage("Invalid option \"{0}\".", e[0]); return; } if (e.Length == 1) { if (t == typeof(bool)) Utils.ErrorMessage("Syntax: .{0} {1} <boolean>", e[-1], fi.Name); else if (t == typeof(int)) Utils.ErrorMessage("Syntax: .{0} {1} <integer>", e[-1], fi.Name); else Utils.ErrorMessage("Syntax: .{0} {1} <string>", e[-1], fi.Name); return; } if (t == typeof(bool)) { bool value; if (!bool.TryParse(e[1], out value)) { Utils.ErrorMessage("Invalid value for option \"{0}\".", fi.Name); return; } fi.SetValue(Raptor.Config, value); Utils.SuccessMessage("Set option \"{0}\" to value \"{1}\".", fi.Name, value); } else if (t == typeof(int)) { int value; if (!int.TryParse(e[1], out value) || value <= 0) { Utils.ErrorMessage("Invalid value for option \"{0}\".", fi.Name); return; } fi.SetValue(Raptor.Config, value); Utils.SuccessMessage("Set option \"{0}\" to value \"{1}\".", fi.Name, value); } else { string value = e.Eol(1); fi.SetValue(Raptor.Config, value); Utils.SuccessMessage("Set option \"{0}\" to value \"{1}\".", fi.Name, value); } File.WriteAllText("raptor.config", JsonConvert.SerializeObject(Raptor.Config, Formatting.Indented)); } }
static void Say(object o, CommandEventArgs e) { if (e.Length == 0) { Utils.SuccessMessage("Syntax: .{0} <message>", e[-1]); return; } if (Main.netMode == 1) NetMessage.SendData(25, -1, -1, e.Eol(0)); else Main.NewText(String.Format("<{0}> {1}", Main.player[Main.myPlayer].name, e.Eol(0))); }
static void Reload(object o, CommandEventArgs e) { string configPath = "raptor.config"; Raptor.Config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(configPath)); Utils.SuccessMessage("Reloaded configuration file."); }
static void Keybind(object o, CommandEventArgs e) { string subcommand = e.Length > 0 ? e[0].ToLowerInvariant() : "help"; switch (subcommand) { case "add": { if (e.Length < 3) { Utils.ErrorMessage("Syntax: .{0} {1} <keybind> <command>", e[-1], e[0]); return; } string key = e[1]; var keybind = new Input.Keybind(); while (!Utils.TryParseXNAKey(key, out keybind.Key)) { if (String.IsNullOrEmpty(key)) { Utils.ErrorMessage("Invalid keybind '{0}'!", e[0]); return; } switch (key[0]) { case '!': break; case '^': break; case '+': break; default: Utils.ErrorMessage("Invalid keybind modifier '{0}'!", key[0]); return; } key = key.Substring(1); } string keyString = e[1].ToLower(); if (Raptor.Config.Keybinds.ContainsKey(keyString)) Raptor.Config.Keybinds[keyString].Add(e.Eol(2)); else Raptor.Config.Keybinds.Add(keyString, new List<string> { e.Eol(2) }); File.WriteAllText("raptor.config", JsonConvert.SerializeObject(Raptor.Config, Formatting.Indented)); Utils.SuccessMessage("Bound the key '{0}' to '{1}'.", e.Eol(2), e[1]); } return; case "clr": case "clear": { Raptor.Config.Keybinds.Clear(); string configPath = "raptor.config"; File.WriteAllText(configPath, JsonConvert.SerializeObject(Raptor.Config, Formatting.Indented)); Utils.SuccessMessage("Cleared all key bindings."); } return; case "del": case "delete": case "remove": { if (e.Length == 1) { Utils.ErrorMessage("Syntax: .{0} {1} <keybind>", e[-1], e[0]); return; } string key = e[1]; var keybind = new Input.Keybind(); while (!Utils.TryParseXNAKey(key, out keybind.Key)) { switch (key[0]) { case '!': break; case '^': break; case '+': break; default: Utils.ErrorMessage("Invalid keybind modifier '{0}'!", key[0]); return; } key = key.Substring(1); } Raptor.Config.Keybinds.Remove(e[1].ToLower()); File.WriteAllText("raptor.config", JsonConvert.SerializeObject(Raptor.Config, Formatting.Indented)); Utils.SuccessMessage("Removed keybind '{0}'.", e[1]); } return; case "list": Utils.SuccessMessage("Key Bindings:"); foreach (var kv in Raptor.Config.Keybinds) { Utils.InfoMessage("Keybind '{0}':", kv.Key); foreach (var command in kv.Value) Utils.InfoMessage(" {0}", command); } return; case "help": default: Utils.SuccessMessage(".{0} help:", e[-1]); Utils.InfoMessage(".{0} add <key combination> <command> - Adds to a keybind.", e[-1]); Utils.InfoMessage(".{0} clr/clear - Clears all keybinds.", e[-1]); Utils.InfoMessage(".{0} del/delete/remove <key combination> - Deletes a keybind.", e[-1]); Utils.InfoMessage(".{0} list - Lists all keybinds.", e[-1]); return; } }
static void Help(object o, CommandEventArgs e) { if (e.Length == 0) { Utils.SuccessMessage("Raptor commands: "); Utils.InfoMessage(String.Join(", ", ChatCommands.Select(c => "." + c.Name))); return; } string commandName = e[0].ToLowerInvariant(); Command command = ChatCommands.FirstOrDefault(c => c.Names.Contains(commandName)); if (command != null) { Utils.SuccessMessage(".{0} help:", command.Name); foreach (string line in command.HelpText) Utils.InfoMessage("{0}", line); } else Utils.ErrorMessage("Invalid command \"{0}\".", commandName); }
static void Aliases(object o, CommandEventArgs e) { if (e.Length == 0) { Utils.ErrorMessage("Syntax: .{0} <command name>", e[-1]); return; } string commandName = e[0].ToLowerInvariant(); Command command = ChatCommands.FirstOrDefault(c => c.Names.Contains(commandName)); if (command != null) { Utils.SuccessMessage(".{0} aliases:", command.Name); Utils.InfoMessage(String.Join(", ", command.Names.Select(s => "." + s))); } else Utils.ErrorMessage("Invalid command \"{0}\".", commandName); }