public void Add(CommandArgumentEntry newSequence) { if (!ValidSequences.Contains(newSequence)) { ValidSequences.Add(newSequence); } }
public Command GetCommand(string input, CommandPool pool) { Command cmd = pool.Find(x => x.Call.ToLower() == input.Split(' ')[0].ToLower()); if (cmd == null) { throw new Exception("Command not found!"); } if (cmd.ArgumentTable == null) { return(cmd); } input = input == cmd.Call.ToLower() ? "" : input.Remove(0, cmd.Call.Length + 1); CommandArgumentEntry args = cmd.ArgumentTable.FindMatchingSequence(input, cmd.AllowGlobs); cmd.SetInputArguments(args); return(cmd); }
public void SetInputArguments(CommandArgumentEntry args) { InputArgumentEntry = args; }
public CommandArgumentEntry FindMatchingSequence(string input, bool allowGlobs) { if (input != null && input != string.Empty) { input = input.TrimEnd(' '); CommandArgumentEntry result = null; string[] argSplit = input.Split(' '); List <string> stitchedArgs = new List <string>(); if (allowGlobs) { for (int i = 0; i < argSplit.Length; i++) { if (stitchedArgs.Count > 0 && (stitchedArgs.Last().StartsWith("\"") || stitchedArgs.Last().StartsWith("*\"") || stitchedArgs.Last().StartsWith("!*\"") || stitchedArgs.Last().StartsWith("!\"")) && !stitchedArgs.Last().EndsWith("\"") && !stitchedArgs.Last().EndsWith("\"*")) { stitchedArgs[stitchedArgs.Count - 1] += " " + argSplit[i]; } else { stitchedArgs.Add(argSplit[i]); } } } else { for (int i = 0; i < argSplit.Length; i++) { if (stitchedArgs.Count > 0 && stitchedArgs.Last().StartsWith("\"") && !stitchedArgs.Last().EndsWith("\"")) { stitchedArgs[stitchedArgs.Count - 1] += " " + argSplit[i]; } else { stitchedArgs.Add(argSplit[i]); } } for (int i = 0; i < stitchedArgs.Count; i++) { stitchedArgs[i] = stitchedArgs[i].Replace("\"", ""); } } string compareStr = ""; string patternStr = ""; List <ARGPROPS> props = new List <ARGPROPS>(); for (int i = 0; i < ValidSequences.Count; i++) { props = new List <ARGPROPS>(); compareStr = ""; patternStr = ""; ValidSequences[i].Arguments.ForEach((y) => { compareStr += y.Call == "" ? "sval " : y.Call + " "; compareStr += y.Type != typeof(object) && y.Call != "" ? "val " : ""; }); List <string> compareSplit = compareStr.TrimEnd(' ').Split(' ').ToList(); List <string> args = stitchedArgs; args.ForEach((y) => { patternStr += y.StartsWith("-") && !Regex.IsMatch(y.Remove(0, 1), "^[0-9]*$") ? y + " " : patternStr.EndsWith("val ") || patternStr == "" ? "sval " : "val "; }); List <string> patternSplit = patternStr.TrimEnd(' ').Split(' ').ToList(); if (args.Count == compareSplit.Count) { bool matchFound = true; for (int j = 0; j < compareSplit.Count; j++) { if (ValidSequences[i].OrderSensitive) { if (compareSplit[j] != patternSplit[j]) { matchFound = false; break; } } else { if (!patternSplit.Contains(compareSplit[j]) && compareSplit[j] != "val" && compareSplit[j] != "sval") { matchFound = false; break; } } } if (matchFound) { for (int j = 0; j < compareSplit.Count; j++) { if (ValidSequences[i].OrderSensitive) { switch (compareSplit[j]) { case "val": props.Last().VALUE = args[j]; break; case "sval": props.Add(new ARGPROPS() { VALUE = args[j], CALL = "" }); break; default: props.Add(new ARGPROPS() { CALL = args[j] }); break; } } else { switch (patternSplit[j]) { case "val": props.Last().VALUE = args[j]; break; case "sval": props.Add(new ARGPROPS() { VALUE = args[j], CALL = "" }); break; default: props.Add(new ARGPROPS() { CALL = args[j] }); break; } } } result = ValidSequences[i]; break; } } } if (result != null) { result.Arguments.ForEach((y) => { y.SetValue(null); }); for (int j = 0; j < props.Count; j++) { if (result.Arguments.Exists(y => y.Call == props[j].CALL) && result.Arguments.Find(y => y.Call == props[j].CALL).Type == typeof(bool) && props[j].VALUE == null) { result.Arguments.Find(y => y.Call == props[j].CALL).SetValue(false); continue; } result.Arguments.Find(y => y.Call == props[j].CALL && y.Value == null).SetValue(props[j].VALUE); } return(result); } else { throw new Exception("Invalid arguments!"); } } else { return(new CommandArgumentEntry()); } }
public void Remove(CommandArgumentEntry sequenceToRemove) { ValidSequences.Remove(sequenceToRemove); }