/// <summary> /// Runs the provided command with the provided args. /// </summary> public static void RunCommand(string[] unit, string[] args) { ConsoleCommandUnit command = Commands.GetUnit(unit); if (command == null) { if (Commands.GetGroup(unit) == null) { Log("Command doesn't exist"); } } else { try { command.RunCommand(args); } catch (Exception e) { Log(e.ToString()); Debug.LogError("Exception occured while running command:" + e.ToString()); } } }
public void ShowAutocomplete() { if (GUI.Children.Count >= 3) { return; } STextField field = (STextField)GUI[1]; // AUTO COMPLETIONATOR 3000 // (by Zatherz) // // TODO: Make Tab autocomplete to the shared part of completions // TODO: AutocompletionRule interface and class per rule? // Create an input array by splitting it on spaces string inputtext = field.Text.Substring(0, field.CursorIndex); string[] input = SplitArgs(inputtext); string otherinput = string.Empty; if (field.CursorIndex < field.Text.Length) { otherinput = field.Text.Substring(field.CursorIndex + 1); } // Get where the command appears in the path so that we know where the arguments start int commandindex = Commands.GetFirstNonUnitIndexInPath(input); List <string> pathlist = new List <string>(); for (int i = 0; i < input.Length - (input.Length - commandindex); i++) { pathlist.Add(input[i]); } string[] path = pathlist.ToArray(); ConsoleCommandUnit unit = Commands.GetUnit(path); // Get an array of available completions int matchindex = input.Length - path.Length; /* * HACK! blame Zatherz * matchindex will be off by +1 if the current keyword your cursor is on isn't empty * this will check if there are no spaces on the left on the cursor * and if so, decrease matchindex * if there *are* spaces on the left of the cursor, that means the current * "token" the cursor is on is an empty string, so that doesn't have any problems * Hopefully this is a good enough explanation, if not, ping @Zatherz on Discord */ string matchkeyword = string.Empty; if (!inputtext.EndsWithInvariant(" ")) { matchindex--; matchkeyword = input[input.Length - 1]; } string[] completions = unit.Autocompletion.Match(Math.Max(matchindex, 0), matchkeyword); if (completions == null || completions.Length == 0) { Debug.Log("ETGModConsole: no completions available (match returned null)"); } else if (completions.Length == 1) { if (path.Length > 0) { field.Text = string.Join(" ", path) + " " + completions[0] + " " + otherinput; } else { field.Text = completions[0] + " " + otherinput; } field.MoveCursor(field.Text.Length); } else if (completions.Length > 1) { SGroup hints = new SGroup { Parent = GUI, AutoLayout = (SGroup g) => g.AutoLayoutVertical, ScrollDirection = SGroup.EDirection.Vertical, OnUpdateStyle = delegate(SElement elem) { elem.Size = new Vector2(elem.Parent.InnerSize.x, Mathf.Min(((SGroup)elem).ContentSize.y, 160f)); elem.Position = GUI[1].Position - new Vector2(0f, elem.Size.y + 4f); } }; for (int i = 0; i < completions.Length; i++) { hints.Children.Add(new SLabel(completions[i])); } } }
public UnitSearchResult(int index, ConsoleCommandUnit unit) { this.index = index; this.unit = unit; }