public AddCommandForm(CommandHandler[] _handlers, RegisteredCommand currentCommand) { InitializeComponent(); this.Text = "Editing Command"; handlers = _handlers; FlagTextbox.Text = currentCommand.Flag; modOnly.Checked = currentCommand.IsModOnly; Isregex.Checked = currentCommand.FlagIsRegex; CaseSensitive.Checked = currentCommand.FlagIsCaseSensitive; LoadHandler(currentCommand.Handler); LoadUserInput(currentCommand.UserDataInput); }
private void addButton_Click(object sender, EventArgs e) { if (SelectedHandler == null) { MessageBox.Show("Select a command"); return; } if (FlagTextbox.Text == string.Empty) { MessageBox.Show("Enter a flag"); return; } var objectData = new Dictionary<string, object>(); if(hasUserInput) { foreach(Control c in UserInputPanel.Controls) { UserInputTagData data = c.Tag as UserInputTagData; if (data == null) continue; if (objectData.ContainsKey(data.InputID)) continue; if(data.InputType == typeof(string) && string.IsNullOrEmpty((string)data.Value)) { MessageBox.Show(string.Format("Enter a value for \"{0}\".", data.InputID)); return; } objectData.Add(data.InputID, data.Value); } } NewCommand = new RegisteredCommand(FlagTextbox.Text, SelectedHandler); NewCommand.IsModOnly = modOnly.Checked; NewCommand.FlagIsRegex = Isregex.Checked; NewCommand.FlagIsCaseSensitive = CaseSensitive.Checked; if (objectData.Count > 0) NewCommand.UserDataInput = new UserData(objectData); else NewCommand.UserDataInput = null; this.DialogResult = System.Windows.Forms.DialogResult.OK; }
/// <summary> /// Registers the specified command /// </summary> /// <param name="command"></param> /// <param name="plugin"></param> /// <param name="callback"></param> public void RegisterCommand(string command, Plugin plugin, CommandCallback callback) { // Convert command to lowercase and remove whitespace command = command.ToLowerInvariant().Trim(); // Setup console command name var split = command.Split('.'); var parent = split.Length >= 2 ? split[0].Trim() : "global"; var name = split.Length >= 2 ? string.Join(".", split.Skip(1).ToArray()) : split[0].Trim(); var fullName = $"{parent}.{name}"; if (parent == "global") { command = name; } // Setup a new Covalence command var newCommand = new RegisteredCommand(plugin, command, callback); // Check if the command can be overridden if (!CanOverrideCommand(command)) { throw new CommandAlreadyExistsException(command); } // Check if command already exists in another Covalence plugin RegisteredCommand cmd; if (registeredCommands.TryGetValue(command, out cmd)) { if (cmd.OriginalCallback != null) { newCommand.OriginalCallback = cmd.OriginalCallback; } var previousPluginName = cmd.Source?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{command}' command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); ConsoleSystem.Index.Server.Dict.Remove(fullName); if (parent == "global") { ConsoleSystem.Index.Server.GlobalDict.Remove(name); } ConsoleSystem.Index.All = ConsoleSystem.Index.Server.Dict.Values.ToArray(); } // Check if command already exists in a Rust plugin as a chat command Command.ChatCommand chatCommand; if (cmdlib.chatCommands.TryGetValue(command, out chatCommand)) { var previousPluginName = chatCommand.Plugin?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{command}' chat command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); cmdlib.chatCommands.Remove(command); } // Check if command already exists in a Rust plugin as a console command Command.ConsoleCommand consoleCommand; if (cmdlib.consoleCommands.TryGetValue(fullName, out consoleCommand)) { if (consoleCommand.OriginalCallback != null) { newCommand.OriginalCallback = consoleCommand.OriginalCallback; } var previousPluginName = consoleCommand.Callback.Plugin?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{fullName}' console command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); ConsoleSystem.Index.Server.Dict.Remove(consoleCommand.RustCommand.FullName); if (parent == "global") { ConsoleSystem.Index.Server.GlobalDict.Remove(consoleCommand.RustCommand.Name); } ConsoleSystem.Index.All = ConsoleSystem.Index.Server.Dict.Values.ToArray(); cmdlib.consoleCommands.Remove(consoleCommand.RustCommand.FullName); } // Check if command is a vanilla Rust command ConsoleSystem.Command rustCommand; if (ConsoleSystem.Index.Server.Dict.TryGetValue(fullName, out rustCommand)) { if (rustCommand.Variable) { var newPluginName = plugin?.Name ?? "An unknown plugin"; Interface.Oxide.LogError($"{newPluginName} tried to register the {fullName} console variable as a command!"); return; } newCommand.OriginalCallback = rustCommand.Call; } // Create a new Rust console command newCommand.RustCommand = new ConsoleSystem.Command { Name = name, Parent = parent, FullName = command, ServerUser = true, ServerAdmin = true, Client = true, ClientInfo = false, Variable = false, Call = arg => { if (arg == null) { return; } if (arg.Connection != null && arg.Player()) { var iplayer = rustCovalence.PlayerManager.FindPlayerById(arg.Connection.userid.ToString()) as RustPlayer; if (iplayer == null) { return; } if (!Interface.Oxide.Config.Options.Modded && !iplayer.IsAdmin && !iplayer.BelongsToGroup("admin")) { return; } iplayer.LastCommand = CommandType.Console; callback(iplayer, command, ExtractArgs(arg)); return; } callback(consolePlayer, command, ExtractArgs(arg)); } }; // Register the command as a console command ConsoleSystem.Index.Server.Dict[fullName] = newCommand.RustCommand; if (parent == "global") { ConsoleSystem.Index.Server.GlobalDict[name] = newCommand.RustCommand; } ConsoleSystem.Index.All = ConsoleSystem.Index.Server.Dict.Values.ToArray(); // Register the command as a chat command registeredCommands[command] = newCommand; }
/// <summary> /// Registers the specified command /// </summary> /// <param name="command"></param> /// <param name="plugin"></param> /// <param name="callback"></param> public void RegisterCommand(string command, Plugin plugin, CommandCallback callback) { // Hack us the dictionary if (rustCommands == null) { rustCommands = typeof(ConsoleSystem.Index).GetField("dictionary", BindingFlags.NonPublic | BindingFlags.Static)?.GetValue(null) as IDictionary <string, ConsoleSystem.Command>; } // Convert to lowercase var commandName = command.ToLowerInvariant().Trim(); // Setup console command name var split = commandName.Split('.'); var parent = split.Length >= 2 ? split[0].Trim() : "global"; var name = split.Length >= 2 ? string.Join(".", split.Skip(1).ToArray()) : split[0].Trim(); var fullname = $"{parent}.{name}"; if (parent == "global") { commandName = name; } // Setup a new Covalence command var newCommand = new RegisteredCommand(plugin, commandName, callback); // Check if the command can be overridden if (!CanOverrideCommand(commandName)) { throw new CommandAlreadyExistsException(commandName); } // Check if it already exists in another Covalence plugin RegisteredCommand cmd; if (registeredCommands.TryGetValue(commandName, out cmd)) { if (cmd.OriginalCallback != null) { newCommand.OriginalCallback = cmd.OriginalCallback; } var previousPluginName = cmd.Source?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{commandName}' command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); rustCommands.Remove(fullname); ConsoleSystem.Index.GetAll().Remove(cmd.RustCommand); } // Check if it already exists in a Rust plugin as a chat command Command.ChatCommand chatCommand; if (cmdlib.chatCommands.TryGetValue(commandName, out chatCommand)) { var previousPluginName = chatCommand.Plugin?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{commandName}' chat command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); cmdlib.chatCommands.Remove(commandName); } // Check if it already exists in a Rust plugin as a console command Command.ConsoleCommand consoleCommand; if (cmdlib.consoleCommands.TryGetValue(fullname, out consoleCommand)) { if (consoleCommand.OriginalCallback != null) { newCommand.OriginalCallback = consoleCommand.OriginalCallback; } var previousPluginName = consoleCommand.PluginCallbacks[0].Plugin?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{fullname}' console command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); rustCommands.Remove(consoleCommand.RustCommand.namefull); ConsoleSystem.Index.GetAll().Remove(consoleCommand.RustCommand); cmdlib.consoleCommands.Remove(consoleCommand.RustCommand.namefull); } // Check if this is a vanilla rust command ConsoleSystem.Command rustCommand; if (rustCommands.TryGetValue(fullname, out rustCommand)) { if (rustCommand.isVariable) { var newPluginName = plugin?.Name ?? "An unknown plugin"; Interface.Oxide.LogError($"{newPluginName} tried to register the {fullname} console variable as a command!"); return; } newCommand.OriginalCallback = rustCommand.Call; } // Create a new Rust console command newCommand.RustCommand = new ConsoleSystem.Command { name = name, parent = parent, namefull = commandName, isCommand = true, isUser = true, isAdmin = true, GetString = () => string.Empty, SetString = s => { }, isVariable = false, Call = arg => { if (arg == null) { return; } if (arg.connection != null) { if (arg.Player()) { var iplayer = rustCovalence.PlayerManager.GetPlayer(arg.connection.userid.ToString()) as RustPlayer; if (iplayer == null) { return; } iplayer.LastCommand = CommandType.Console; callback(iplayer, commandName, ExtractArgs(arg)); return; } } callback(consolePlayer, commandName, ExtractArgs(arg)); } }; // Register the command as a console command rustCommands[fullname] = newCommand.RustCommand; ConsoleSystem.Index.GetAll().Add(newCommand.RustCommand); // Register the command as a chat command registeredCommands[commandName] = newCommand; }
void AddCommandToList(RegisteredCommand command) { ListViewItem i = new ListViewItem(command.Flag); i.SubItems.Add(command.Handler.Command.Name); List<string> PropertyValues = new List<string>(); if (command.IsModOnly) PropertyValues.Add("Mod Only"); if (command.FlagIsRegex) PropertyValues.Add("Regex"); if (command.FlagIsCaseSensitive) PropertyValues.Add("Case sensitive"); string properties = string.Join(", ", PropertyValues.ToArray()); if (string.IsNullOrEmpty(properties)) properties = "None"; i.SubItems.Add(properties); i.Tag = command; listView1.Items.Add(i); SaveSettings(); }
public void LoadSettings() { try { XDocument xDoc = XDocument.Load("Modulebot.save"); var main = xDoc.Element("MBot"); foreach(var commandElement in main.Descendants("commands").Descendants("Registered")) { string ID = commandElement.Attribute("ID").Value; if(HandlerList.ContainsKey(ID)) { var handler = HandlerList[ID]; var Flag = commandElement.Attribute("Flag").Value; var Isregex = commandElement.Attribute("IsRegex").Value; var ModOnly = commandElement.Attribute("ModOnly").Value; var CaseSensitive = commandElement.Attribute("CaseSensistve").Value; var registeredCommand = new RegisteredCommand(Flag, handler); var data = new Dictionary<string, object>(); foreach(var UserDataElement in commandElement.Descendants("UserData").Descendants("object")) { string name = UserDataElement.Attribute("Name").Value; if (data.ContainsKey(name)) continue; object o = DeserilizeObject(UserDataElement.Attribute("Value").Value); if (o == null) continue; data.Add(name, o); } registeredCommand.UserDataInput = new UserData(data); registeredCommand.FlagIsRegex = (Isregex == "1"); registeredCommand.IsModOnly = (ModOnly == "1"); registeredCommand.FlagIsCaseSensitive = (CaseSensitive == "1"); AddCommandToList(registeredCommand); } } foreach (var commandElement in main.Descendants("Mods").Descendants("User")) { ListViewItem mod = new ListViewItem(commandElement.Attribute("Name").Value); modList.Items.Add(mod); } } catch { MessageBox.Show("Failed to load settings"); } }
private void smiRefresh_Click(object sender, EventArgs e) { RegisteredCommand refreshCommand = CommandService.Find(ViewCommands.Refresh); refreshCommand?.Invoke(this, this, null); }
/// <summary> /// Registers the specified command /// </summary> /// <param name="command"></param> /// <param name="plugin"></param> /// <param name="callback"></param> public void RegisterCommand(string command, Plugin plugin, CommandCallback callback) { // Convert command to lowercase and remove whitespace command = command.ToLowerInvariant().Trim(); // Setup a new Covalence command var newCommand = new RegisteredCommand(plugin, command, callback); // Check if the command can be overridden if (!CanOverrideCommand(command)) { throw new CommandAlreadyExistsException(command); } // Check if command already exists in another Covalence plugin RegisteredCommand cmd; if (registeredCommands.TryGetValue(command, out cmd)) { if (cmd.OriginalCallback != null) { newCommand.OriginalCallback = cmd.OriginalCallback; } var previousPluginName = cmd.Source?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{command}' command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); } // Check if command already exists in a Reign of Kings plugin as a chat command Command.ChatCommand chatCommand; if (cmdlib.ChatCommands.TryGetValue(command, out chatCommand)) { if (chatCommand.OriginalCallback != null) { newCommand.OriginalCallback = chatCommand.OriginalCallback; } var previousPluginName = chatCommand.Plugin?.Name ?? "an unknown plugin"; var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{command}' chat command previously registered by {previousPluginName}"; Interface.Oxide.LogWarning(message); cmdlib.ChatCommands.Remove(command); } // Check if command is a vanilla Reign of Kings command if (CommandManager.RegisteredCommands.ContainsKey(command)) { if (newCommand.OriginalCallback == null) { newCommand.OriginalCallback = CommandManager.RegisteredCommands[command]; } CommandManager.RegisteredCommands.Remove(command); if (cmd == null && chatCommand == null) { var newPluginName = plugin?.Name ?? "An unknown plugin"; var message = $"{newPluginName} has replaced the '{command}' command previously registered by Reign of Kings"; Interface.Oxide.LogWarning(message); } } // Register the command as a chat command registeredCommands[command] = newCommand; CommandManager.RegisteredCommands[command] = new CommandAttribute("/" + command, string.Empty) { Method = (Action <CommandInfo>)Delegate.CreateDelegate(typeof(Action <CommandInfo>), this, GetType().GetMethod("HandleCommand", BindingFlags.NonPublic | BindingFlags.Instance)) }; }
/// <summary> /// Registers the specified command /// </summary> /// <param name="command"></param> /// <param name="plugin"></param> /// <param name="callback"></param> public void RegisterCommand(string command, Plugin plugin, CommandCallback callback) { // Convert command to lowercase and remove whitespace command = command.ToLowerInvariant().Trim(); // Split the command string[] split = command.Split('.'); string parent = split.Length >= 2 ? split[0].Trim() : "global"; string name = split.Length >= 2 ? string.Join(".", split.Skip(1).ToArray()) : split[0].Trim(); if (parent == "global") { command = name; } // Check if the command can be overridden if (!CanOverrideCommand(command)) { throw new CommandAlreadyExistsException(command); } // Set up a new command RegisteredCommand newCommand = new RegisteredCommand(plugin, command, callback) { // Create a new native command SevenDaysToDieCommand = new NativeCommand { Command = command, Callback = callback, Sender = consolePlayer } }; // Check if the command already exists in another plugin if (registeredCommands.TryGetValue(command, out RegisteredCommand cmd)) { if (newCommand.SevenDaysToDieCommand == cmd.SevenDaysToDieCommand) { newCommand.OriginalSevenDaysToDieCommand = cmd.SevenDaysToDieCommand; } string newPluginName = plugin?.Name ?? "An unknown plugin"; // TODO: Localization string previousPluginName = cmd.Source?.Name ?? "an unknown plugin"; // TODO: Localization Interface.Oxide.LogWarning($"{newPluginName} has replaced the '{command}' command previously registered by {previousPluginName}"); // TODO: Localization } // Check if the command already exists as a native command IConsoleCommand nativeCommand = SdtdConsole.Instance.GetCommand(command); if (nativeCommand != null) { if (newCommand.OriginalCallback == null) { newCommand.OriginalCallback = nativeCommand; } if (SdtdConsole.Instance.m_Commands.Contains(nativeCommand)) { SdtdConsole.Instance.m_Commands.Remove(nativeCommand); foreach (string nCommand in newCommand.SevenDaysToDieCommand.GetCommands()) { SdtdConsole.Instance.m_CommandsAllVariants.Remove(nCommand); } } string newPluginName = plugin?.Name ?? "An unknown plugin"; // TODO: Localization Interface.Oxide.LogWarning($"{newPluginName} has replaced the '{command}' command previously registered by {provider.GameName}"); // TODO: Localization } // Register the command registeredCommands.Add(command, newCommand); if (!SdtdConsole.Instance.m_Commands.Contains(newCommand.SevenDaysToDieCommand)) { SdtdConsole.Instance.m_Commands.Add(newCommand.SevenDaysToDieCommand); } foreach (string nCommand in newCommand.SevenDaysToDieCommand.GetCommands()) { if (!string.IsNullOrEmpty(nCommand)) { SdtdConsole.Instance.m_CommandsAllVariants.Add(nCommand, newCommand.SevenDaysToDieCommand); } } // Register the command permission string[] variantCommands = newCommand.SevenDaysToDieCommand.GetCommands(); if (!GameManager.Instance.adminTools.IsPermissionDefinedForCommand(variantCommands) && newCommand.SevenDaysToDieCommand.DefaultPermissionLevel != 0) { GameManager.Instance.adminTools.AddCommandPermission(command, newCommand.SevenDaysToDieCommand.DefaultPermissionLevel, false); } }