private void UnloadCommand(BotCommand com) { if (!commandPaths.Remove(com.FullQualifiedName)) { return; } UnloadICommand(com, com.InvokeName); }
private E <string> LoadCommand(BotCommand com) { if (commandPaths.Contains(com.FullQualifiedName)) { return("Command already exists: " + com.InvokeName); } commandPaths.Add(com.FullQualifiedName); return(LoadICommand(com, com.InvokeName)); }
public CommadSerializeObj(BotCommand botCmd) { this.botCmd = botCmd; Parameter = ( from x in botCmd.CommandParameter where x.kind.IsNormal() select UnwrapParamType(x.type).Name + (x.optional ? "?" : "")).ToArray(); Modules = ( from x in botCmd.CommandParameter where x.kind == ParamKind.Dependency select x.type.Name + (x.optional ? "?" : "")).ToArray(); Return = UnwrapReturnType(botCmd.CommandReturn).Name; }
// TODO: prevent stupid behaviour like: // string A(int b) // string A(ExecutionInformation i, int b) // since the CommandManager can't distinguish these two, when calling private void LoadCommand(BotCommand com) // TODO test { if (!CommandNamespaceValidator.IsMatch(com.InvokeName)) { throw new InvalidOperationException("BotCommand has an invalid invoke name: " + com.InvokeName); } if (commandPaths.Contains(com.FullQualifiedName)) { throw new InvalidOperationException("Command already exists: " + com.InvokeName); } commandPaths.Add(com.FullQualifiedName); LoadICommand(com, com.InvokeName); }
private void UnloadCommand(BotCommand com) { if (!commandPaths.Remove(com.FullQualifiedName)) { return; } var comPath = com.InvokeName.Split(' '); var node = new CommandUnloadNode { ParentNode = null, Self = CommandSystem.RootCommand, }; // build up the list to our desired node for (int i = 0; i < comPath.Length - 1; i++) { if (!(node.Self.GetCommand(comPath[i]) is CommandGroup nextGroup)) { break; } node = new CommandUnloadNode { ParentNode = node, Self = nextGroup, }; } var subGroup = node.Self.GetCommand(comPath.Last()); // nothing to remove if (subGroup is null) { return; } // if the subnode is a plain FunctionCommand then we found our command to delete else if (subGroup is FunctionCommand) { node.Self.RemoveCommand(com); } // here we can delete our command from the overloader else if (subGroup is OverloadedFunctionCommand subOverloadGroup) { subOverloadGroup.RemoveCommand(com); } // now to the special case when a command gets inserted with an empty string else if (subGroup is CommandGroup insertGroup) { // since we check precisely that only one command and only a simple FunctionCommand // can be added with an empty string, wen can delete it safely this way insertGroup.RemoveCommand(string.Empty); // add the node for cleanup node = new CommandUnloadNode { ParentNode = node, Self = insertGroup, }; } // and finally clean all empty nodes up while (node != null) { if (node.Self.IsEmpty) { node.ParentNode?.Self.RemoveCommand(node.Self); } node = node.ParentNode; } }
private static E <string> GenerateError(string msg, BotCommand involvedCom) { return($"Command error path: {involvedCom?.InvokeName}" + $"Command: {involvedCom?.FullQualifiedName}" + $"Error: {msg}"); }
public void RegisterCommand(BotCommand command) { LoadCommand(command); dynamicCommands.Add(command); }
private void UnloadCommand(BotCommand com) { if (!CommandPaths.Contains(com.FullQualifiedName)) return; CommandPaths.Remove(com.FullQualifiedName); var comPath = com.InvokeName.Split(' '); CommandUnloadNode node = new CommandUnloadNode { parentNode = null, self = CommandSystem.RootCommand, }; // build up the list to our desired node for (int i = 0; i < comPath.Length - 1; i++) { var nextGroup = node.self.GetCommand(comPath[i]) as CommandGroup; if (nextGroup == null) break; node = new CommandUnloadNode { parentNode = node, self = nextGroup, }; } var subGroup = node.self.GetCommand(comPath.Last()); // nothing to remove if (subGroup == null) return; // if the subnode is a plain FunctionCommand then we found our command to delete else if (subGroup is FunctionCommand) { node.self.RemoveCommand(com); } // here we can delete our command from the overloader else if (subGroup is OverloadedFunctionCommand) { ((OverloadedFunctionCommand)subGroup).RemoveCommand(com); } // now to the special case when a command gets inserted with an empty string else if (subGroup is CommandGroup) { var insertGroup = (CommandGroup)subGroup; // since we check precisely that only one command and only a simple FunctionCommand // can be added with an empty string, wen can delte it safely this way insertGroup.RemoveCommand(string.Empty); // add the node for cleanup node = new CommandUnloadNode { parentNode = node, self = insertGroup, }; } // and finally clean all empty nodes up while (node != null) { if (node.self.IsEmpty && node.parentNode != null) node.parentNode.self.RemoveCommand(node.self); node = node.parentNode; } }
// TODO test // TODO: prevent stupid behaviour like: // string A(int b) // string A(ExecutionInformation i, int b) // since the CommandManager can't distinguish these two, when calling private void LoadCommand(BotCommand com) { if (!CommandNamespaceValidator.IsMatch(com.InvokeName)) throw new InvalidOperationException("BotCommand has an invalid invoke name: " + com.InvokeName); if (CommandPaths.Contains(com.FullQualifiedName)) throw new InvalidOperationException("Command already exists: " + com.InvokeName); CommandPaths.Add(com.FullQualifiedName); var comPath = com.InvokeName.Split(' '); CommandGroup group = CommandSystem.RootCommand; // this for loop iterates through the seperate names of // the command to be added. for (int i = 0; i < comPath.Length - 1; i++) { ICommand currentCommand = group.GetCommand(comPath[i]); // if a group to hold the next level command doesn't exist // it will be created here if (currentCommand == null) { var nextGroup = new CommandGroup(); group.AddCommand(comPath[i], nextGroup); group = nextGroup; } // if the group already exists we can take it. else if (currentCommand is CommandGroup) { group = (CommandGroup)currentCommand; } // if the element is anything else, we have to replace it // with a group and put the old element back into it. else if (currentCommand is FunctionCommand) { var subGroup = new CommandGroup(); group.RemoveCommand(comPath[i]); group.AddCommand(comPath[i], subGroup); var botCom = currentCommand as BotCommand; if (botCom != null && botCom.NormalParameters > 0) Log.Write(Log.Level.Warning, "\"{0}\" has at least one parameter and won't be reachable due to an overloading function.", botCom.InvokeName); subGroup.AddCommand(string.Empty, currentCommand); group = subGroup; } else throw new InvalidOperationException("An overloaded command cannot be replaced by a CommandGroup: " + com.InvokeName); } ICommand subCommand = group.GetCommand(comPath.Last()); // the group we are trying to insert has no element with the current // name, so just insert it if (subCommand == null) { group.AddCommand(comPath.Last(), com); return; } // if we have a simple function, we need to create a overlaoder // and then add both functions to it else if (subCommand is FunctionCommand) { group.RemoveCommand(comPath.Last()); var overloader = new OverloadedFunctionCommand(); overloader.AddCommand((FunctionCommand)subCommand); overloader.AddCommand(com); group.AddCommand(comPath.Last(), overloader); } // if we have a overloaded function, we can simply add it else if (subCommand is OverloadedFunctionCommand) { var insertCommand = (OverloadedFunctionCommand)subCommand; insertCommand.AddCommand(com); } // to add a command to CommandGroup will have to treat it as a subcommand // with an empty string as a name else if (subCommand is CommandGroup) { var insertCommand = (CommandGroup)subCommand; var noparamCommand = insertCommand.GetCommand(string.Empty); if (noparamCommand == null) { insertCommand.AddCommand(string.Empty, com); if (com.NormalParameters > 0) Log.Write(Log.Level.Warning, "parameter of an empty named function under a group will be ignored!!"); } else throw new InvalidOperationException("An empty named function under a group cannot be overloaded (" + com.InvokeName + ")"); } else throw new InvalidOperationException("Unknown insertion error with " + com.FullQualifiedName); }
// TODO: prevent stupid behaviour like: // string A(int b) // string A(ExecutionInformation i, int b) // since the CommandManager can't distinguish these two, when calling private void LoadCommand(BotCommand com) // TODO test { if (!CommandNamespaceValidator.IsMatch(com.InvokeName)) { throw new InvalidOperationException("BotCommand has an invalid invoke name: " + com.InvokeName); } if (CommandPaths.Contains(com.FullQualifiedName)) { throw new InvalidOperationException("Command already exists: " + com.InvokeName); } CommandPaths.Add(com.FullQualifiedName); var comPath = com.InvokeName.Split(' '); CommandGroup group = CommandSystem.RootCommand; // this for loop iterates through the seperate names of // the command to be added. for (int i = 0; i < comPath.Length - 1; i++) { ICommand currentCommand = group.GetCommand(comPath[i]); // if a group to hold the next level command doesn't exist // it will be created here if (currentCommand == null) { var nextGroup = new CommandGroup(); group.AddCommand(comPath[i], nextGroup); group = nextGroup; } // if the group already exists we can take it. else if (currentCommand is CommandGroup) { group = (CommandGroup)currentCommand; } // if the element is anything else, we have to replace it // with a group and put the old element back into it. else if (currentCommand is FunctionCommand) { var subGroup = new CommandGroup(); group.RemoveCommand(comPath[i]); group.AddCommand(comPath[i], subGroup); var botCom = currentCommand as BotCommand; if (botCom != null && botCom.NormalParameters > 0) { Log.Write(Log.Level.Warning, "\"{0}\" has at least one parameter and won't be reachable due to an overloading function.", botCom.InvokeName); } subGroup.AddCommand(string.Empty, currentCommand); group = subGroup; } else { throw new InvalidOperationException("An overloaded command cannot be replaced by a CommandGroup: " + com.InvokeName); } } ICommand subCommand = group.GetCommand(comPath.Last()); // the group we are trying to insert has no element with the current // name, so just insert it if (subCommand == null) { group.AddCommand(comPath.Last(), com); return; } // if we have a simple function, we need to create a overlaoder // and then add both functions to it else if (subCommand is FunctionCommand) { group.RemoveCommand(comPath.Last()); var overloader = new OverloadedFunctionCommand(); overloader.AddCommand((FunctionCommand)subCommand); overloader.AddCommand(com); group.AddCommand(comPath.Last(), overloader); } // if we have a overloaded function, we can simply add it else if (subCommand is OverloadedFunctionCommand) { var insertCommand = (OverloadedFunctionCommand)subCommand; insertCommand.AddCommand(com); } // to add a command to CommandGroup will have to treat it as a subcommand // with an empty string as a name else if (subCommand is CommandGroup) { var insertCommand = (CommandGroup)subCommand; var noparamCommand = insertCommand.GetCommand(string.Empty); if (noparamCommand == null) { insertCommand.AddCommand(string.Empty, com); if (com.NormalParameters > 0) { Log.Write(Log.Level.Warning, "parameter of an empty named function under a group will be ignored!!"); } } else { throw new InvalidOperationException("An empty named function under a group cannot be overloaded (" + com.InvokeName + ")"); } } else { throw new InvalidOperationException("Unknown insertion error with " + com.FullQualifiedName); } }