public TerminalMethods() { methods = new List <MethodInfo>(); MonoBehaviour[] sceneActive = GameObject.FindObjectsOfType <MonoBehaviour>(); foreach (MonoBehaviour mono in sceneActive) { Type monoType = mono.GetType(); // Retreive the fields from the mono instance MethodInfo[] methodFields = monoType.GetMethods(BindingFlags.Instance | BindingFlags.Public); // search all fields and find the attribute for (int i = 0; i < methodFields.Length; i++) { TerminalCommandAttribute attribute = Attribute.GetCustomAttribute(methodFields[i], typeof(TerminalCommandAttribute)) as TerminalCommandAttribute; // if we detect any attribute print out the data. if (attribute != null) { methodNames.Add(attribute.commandName); methods.Add(methodFields[i]); } } } }
public string Help() { string help_string = "List of available commands:"; foreach (var method in Terminal.instance.terminalMethods.methods) { foreach (var attribute in method.GetCustomAttributes(true)) { if (attribute is TerminalCommandAttribute) //Does not pass { TerminalCommandAttribute attr = (TerminalCommandAttribute)attribute; help_string += "\n " + attr.commandName + " --> " + attr.commandDesc; } } } return help_string; }
public TerminalMethods() { methods = new List <MethodInfo>(); foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (var method in assembly.GetTypes().SelectMany(x => x.GetMethods()).Where(y => y.GetCustomAttributes(true).OfType <TerminalCommandAttribute>().Any()).ToList()) { methods.Add(method) foreach (var attribute in method.GetCustomAttributes(true)) { if (attribute is TerminalCommandAttribute) //Does not pass { TerminalCommandAttribute attr = (TerminalCommandAttribute)attribute; methodNames.Add(attr.commandName); } } } } }
private string ExecuteCommand(string inputText) { autoCompList.Clear(); bool registered = false; string result = null; string insideParentheses = Regex.Match(inputText, @"\(([^)]*)\)").Groups[1].Value; List<string> args = new List<string>(); string command; if (!string.IsNullOrEmpty(insideParentheses)) { args = insideParentheses.Split(new char[] { ',' }).ToList(); command = inputText.Replace(insideParentheses, "").Replace("(", "").Replace(")", "").Replace(";", ""); } else command = inputText.Replace("(", "").Replace(")", "").Replace(";", ""); foreach (var method in terminalMethods.methods) { foreach (object attribute in method.GetCustomAttributes(true)) // Returns all 3 of my attributes. if (attribute is TerminalCommandAttribute) { TerminalCommandAttribute attr = (TerminalCommandAttribute)attribute; if (attr.commandName == command) { if (registered) Debug.LogError(TerminalStrings.MULTIPLE_COMMAND_NAMES + command); Type type = (method.DeclaringType); ParameterInfo[] methodParameters = method.GetParameters(); List<object> argList = new List<object>(); // Cast Arguments if there is any if (args.Count != 0) { if (methodParameters.Length != args.Count) { result = string.Format(TerminalStrings.ARGUMENT_COUNT_MISSMATCH, command, methodParameters.Length, args.Count); Debug.Log(result); return result; } else { // Cast string arguments to input objects types for (int i = 0; i < methodParameters.Length; i++) { try { var a = Convert.ChangeType(args[i], methodParameters[i].ParameterType); argList.Add(a); } catch { result = string.Format("Counld not convert {0} to Type {1}", args[i], methodParameters[i].ParameterType); Debug.LogError(result); return result; } } } } if (type.IsSubclassOf(typeof(UnityEngine.Object))) { var instance_classes = GameObject.FindObjectsOfType(type); if (instance_classes != null) { foreach (var instance_class in instance_classes) { result = (string)method.Invoke(instance_class, argList.ToArray()); } } } else { var instance_class = Activator.CreateInstance(type); result = (string)method.Invoke(instance_class, argList.ToArray()); } registered = true; break; } } } if (!string.IsNullOrEmpty(result)) return result; if (registered) return null; return TerminalStrings.COMMAND_NOT_FOUND; }