public static List <string> FetchAutocompleteOptions(string command, string[] tokens) { // don't autocomplete once we are entering the variable if (tokens.Length >= 2) { return(null); } // at the variable autocomplete stage (ie. need reflection) if (tokens.Length > 0 && tokens[0].Count(character => character == '.') > 1) { return(CommandHelpers.GetAutocompleteOptions(tokens[0], command, CommandHelpers.AutocompleteCandidates.Variables)); } // fetch the game object autocomplete options string path = tokens.Length > 0 ? tokens[0] : ""; List <string> autocompleteOptions = CommandHelpers.GetComponentAutocompleteOptions(path, command); if (autocompleteOptions == null) { autocompleteOptions = new List <string>(); } // user has entered exact path to a component - switch to variable autocomplete stage if ((tokens.Length > 0) && (autocompleteOptions.Count <= 1) && (CommandHelpers.GetComponent(tokens[0]) != null)) { return(CommandHelpers.GetAutocompleteOptions(tokens[0], command, CommandHelpers.AutocompleteCandidates.Variables)); } // append the type based autocomplete options autocompleteOptions.AddRange(ConsoleDaemon.Instance.AvailableTypes.Keys .Where(typeName => typeName.StartsWith(path)) .Select(typeName => command + " " + typeName).ToList()); // if we have no autocomplete options at this stage then we are working with a class if (tokens.Length > 0 && (autocompleteOptions == null || autocompleteOptions.Count <= 1)) { string[] pathElements = tokens[0].Split('.'); // check if the class is in the cached types if (ConsoleDaemon.Instance.AvailableTypes.ContainsKey(pathElements[0])) { return(CommandHelpers.GetAutocompleteOptions(tokens[0], command, CommandHelpers.AutocompleteCandidates.Variables)); } } return(autocompleteOptions); }
public static string Execute(string[] tokens) { if (tokens.Length < 1) { return("[Error] You must provide a path to the component to remove."); } // attempt to find the component Component foundComponent = CommandHelpers.GetComponent(tokens[0]); if (foundComponent == null) { return("[Error] Could not find the specified component. Check that the path is correct."); } GameObject.Destroy(foundComponent); return("Component destroyed"); }
public static string Execute(string[] tokens) { // incorrect number of tokens found if (tokens.Length != 2) { return("[Error] Incorrect number of parameters. You must provide the true/false/enabled/disabled flag and a valid path to the component to modify."); } // invalid flag provided string enabledFlag = tokens[0].ToLower(); if (enabledFlag.ToLower() != "enabled" && enabledFlag.ToLower() != "disabled" && enabledFlag.ToLower() != "true" && enabledFlag.ToLower() != "false") { return("[Error] Unknown flag. You must provide the true/false/enabled/disabled flag and a valid path to the component to modify."); } // attempt to find the component Component foundComponent = CommandHelpers.GetComponent(tokens[1]); // component not found? if (foundComponent == null) { return("[Error] Could not find the specified component. Check the path. Paths must be case sensitive."); } // component cannot be enabled/disabled if (!(foundComponent is Behaviour)) { return("[Error] The component does not support being enabled or disabled."); } // Update the component (foundComponent as Behaviour).enabled = (enabledFlag == "enabled") || (enabledFlag == "true"); return("Component is now " + enabledFlag); }
public static string GetValue(string path) { // split based on the . as this might indicate a component or variable split string[] pathElements = path.Split('.'); System.Type foundType = null; Component foundComponent = null; System.Object foundObject = null; // attempt to find the type if (ConsoleDaemon.Instance.AvailableTypes.ContainsKey(pathElements[0])) { foundType = ConsoleDaemon.Instance.AvailableTypes[pathElements[0]]; } // if there are multiple path elements then we may have a component if (pathElements.Length >= 2) { // attempt to find the component, if found then retrieve the type foundComponent = CommandHelpers.GetComponent(pathElements[0] + "." + pathElements[1]); if (foundComponent != null) { foundType = foundComponent.GetType(); foundObject = foundComponent; } } // no matching type should only happen if invalid information has been entered if (foundType == null) { return("[Error] Failed to find a matching component or type. Check the entered path."); } // track the index into the path for the earliest spot a variable may be int variableStart = foundComponent != null ? 2 : 1; // cache the binding flags (can only search for instance if we have a component) BindingFlags flags = BindingFlags.Public | BindingFlags.Static; if (foundComponent != null) { flags |= BindingFlags.Instance; } // traverse the hierarchy of the path to find the last node bool lastElementIsPartial = false; for (int pathIndex = variableStart; pathIndex < pathElements.Length; ++pathIndex) { FieldInfo field = foundType.GetField(pathElements[pathIndex], flags); lastElementIsPartial = true; if (field != null) { // even if the parent was static after we are one field in we are dealing with an actual variable so add instance flags |= BindingFlags.Instance; foundType = field.FieldType; lastElementIsPartial = false; foundObject = field.GetValue(foundObject); } else { PropertyInfo property = foundType.GetProperty(pathElements[pathIndex], flags); if (property != null) { // even if the parent was static after we are one field in we are dealing with an actual variable so add instance flags |= BindingFlags.Instance; foundType = property.PropertyType; lastElementIsPartial = false; foundObject = property.GetValue(foundObject, null); } } // no found object if (foundObject == null) { return("[Error] Failed to access the object. It may have been destroyed."); } } // if the last element is partial then an error has occurred if (lastElementIsPartial) { return("[Error] An incomplete path to a variable was supplied. Check the path. Paths are case sensitive."); } return("The value is: " + foundObject.ToString()); }
public static List <string> GetAutocompleteOptions(string path, string command, AutocompleteCandidates candidateType) { // if we're at this point we know we have at least either a component or a class with static members // split based on the . as this might indicate a component or variable split string[] pathElements = path.Split('.'); System.Type foundType = null; Component foundComponent = null; // attempt to find the type if (ConsoleDaemon.Instance.AvailableTypes.ContainsKey(pathElements[0])) { foundType = ConsoleDaemon.Instance.AvailableTypes[pathElements[0]]; } // if there are multiple path elements then we may have a component if (pathElements.Length >= 2) { // attempt to find the component, if found then retrieve the type foundComponent = CommandHelpers.GetComponent(pathElements[0] + "." + pathElements[1]); if (foundComponent != null) { foundType = foundComponent.GetType(); } } // no matching type should only happen if invalid information has been entered if (foundType == null) { return(null); } // track the index into the path for the earliest spot a variable may be int variableStart = foundComponent != null ? 2 : 1; // cache the binding flags (can only search for instance if we have a component) BindingFlags flags = BindingFlags.Public | BindingFlags.Static; if (foundComponent != null) { flags |= BindingFlags.Instance; } // traverse the hierarchy of the path to find the last node bool lastElementIsPartial = false; for (int pathIndex = variableStart; pathIndex < pathElements.Length; ++pathIndex) { FieldInfo field = foundType.GetField(pathElements[pathIndex], flags); lastElementIsPartial = true; if (field != null) { // even if the parent was static after we are one field in we are dealing with an actual variable so add instance flags |= BindingFlags.Instance; foundType = field.FieldType; lastElementIsPartial = false; } else { PropertyInfo property = foundType.GetProperty(pathElements[pathIndex], flags); if (property != null) { // even if the parent was static after we are one field in we are dealing with an actual variable so add instance flags |= BindingFlags.Instance; foundType = property.PropertyType; lastElementIsPartial = false; } } } // find all candidate fields string partialName = lastElementIsPartial ? pathElements[pathElements.Length - 1] : ""; List <FieldInfo> fieldInfos = foundType.GetFields(flags).Where(field => field.Name.StartsWith(partialName)).ToList(); List <PropertyInfo> propertyInfos = foundType.GetProperties(flags).Where(property => property.Name.StartsWith(partialName)).ToList(); List <MethodInfo> methodInfos = null; // if we're doing a method search then also check methods if (candidateType == AutocompleteCandidates.Functions) { methodInfos = foundType.GetMethods(flags).Where(method => method.Name.StartsWith(partialName)).ToList(); } // no valid items found if ((fieldInfos == null || fieldInfos.Count == 0) && (propertyInfos == null || propertyInfos.Count == 0) && (methodInfos == null || methodInfos.Count == 0)) { return(null); } // assemble the base command string basePath = ""; int lastIndex = lastElementIsPartial ? pathElements.Length - 1 : pathElements.Length; for (int index = 0; index < lastIndex; ++index) { basePath += pathElements[index] + "."; } // build the autocomplete options List <string> autocompleteOptions = new List <string>(fieldInfos.Count); bool needsEscaping = basePath.Contains(" "); foreach (FieldInfo fieldInfo in fieldInfos) { string option = (command + " ") + (needsEscaping ? ("\"" + basePath + fieldInfo.Name + "\"") : (basePath + fieldInfo.Name)); autocompleteOptions.Add(option); } foreach (PropertyInfo propertyInfo in propertyInfos) { string option = (command + " ") + (needsEscaping ? ("\"" + basePath + propertyInfo.Name + "\"") : (basePath + propertyInfo.Name)); autocompleteOptions.Add(option); } if (candidateType == AutocompleteCandidates.Functions) { foreach (MethodInfo methodInfo in methodInfos) { string option = (command + " ") + (needsEscaping ? ("\"" + basePath + methodInfo.Name + "\"") : (basePath + methodInfo.Name)); // for now only include those without parameters if (methodInfo.GetParameters().Length > 0) { continue; } if (!autocompleteOptions.Contains(option)) { autocompleteOptions.Add(option); } } } return(autocompleteOptions); }