public static void GetModuleMethods(SubModuleBase module, List <MethodInfo> list) { var methods = module.GetType().GetMethods(); foreach (var method in methods) { var executable = method.GetCustomAttributes(typeof(ExecutableAttribute), inherit: false); if (executable == null || executable.Length == 0) { continue; } var accessGroups = method.GetCustomAttributes(typeof(AccessLevelAttribute), inherit: false); if (accessGroups != null && accessGroups.Length > 0) { var accessGroup = accessGroups[0] as AccessLevelAttribute; if (accessGroup.group == AccessGroup.Private && ConsoleManager.instance.accessMode != AccessGroup.Private) { continue; } } list.Add(method); } }
public static void GetModuleMethods(SubModuleBase module, List <MethodInfo> list) { var methods = module.GetType().GetMethods(); foreach (var method in methods) { var executable = method.GetCustomAttributes(typeof(ExecutableAttribute), false); if (executable == null || executable.Length == 0) { continue; } list.Add(method); } }
public void ShowHelp_INTERNAL(SubModuleBase module) { this.screen.AddLine("----------------------------------------------"); var moduleName = module.GetType().Name; this.screen.AddLine(string.Format("<color=#c2c>Module `{0}` commands:</color>", moduleName)); var help = ConsoleManager.GetHelp(module, string.Empty); foreach (var line in help) { this.screen.AddLine(string.Format("\t{0}", line)); } this.screen.AddLine("----------------------------------------------"); }
private bool Call(string cmd, SubModuleBase module, string[] args) { var first = string.Empty; if (args.Length > 0) { first = args[0]; } if (module.HasCommandRewrite(args) == true) { var result = module.OnCommandRewrite(args); if (string.IsNullOrEmpty(result) == false) { this.screen.AddLine(string.Format("<color=red>{0}</color>", result)); if (module.PassCommandRewrite(ref args) == false) { return(false); } } else { if (module.PassCommandRewrite(ref args) == false) { return(true); } } } var i = 0; var methods = ListPool <MethodInfo> .Get(); ConsoleManager.GetModuleMethods(module, methods); foreach (var method in methods) { if (method.Name.ToLower() == first.ToLower().Trim() && method.GetParameters().Length == args.Length - 1) { var pars = new List <object>(); i = 1; foreach (var par in method.GetParameters()) { if (par.ParameterType == typeof(bool)) { pars.Add((object)bool.Parse(args[i])); } else if (par.ParameterType == typeof(int)) { pars.Add((object)int.Parse(args[i])); } else if (par.ParameterType == typeof(long)) { pars.Add((object)long.Parse(args[i])); } else if (par.ParameterType == typeof(float)) { pars.Add((object)float.Parse(args[i])); } else if (par.ParameterType.IsEnum == true) { var names = System.Enum.GetNames(par.ParameterType); for (int j = 0; j < names.Length; ++j) { names[j] = names[j].ToLower(); } var index = System.Array.IndexOf(names, args[i]); pars.Add(System.Enum.GetValues(par.ParameterType).GetValue(index)); } else { pars.Add((object)args[i]); } ++i; } var result = (string)method.Invoke(module, pars.ToArray()); if (string.IsNullOrEmpty(result) == false) { this.screen.AddLine(string.Format("<color=red>{0}</color>", result)); } else { return(true); } return(false); } } ListPool <MethodInfo> .Release(methods); this.AddCommandNotFound(cmd); return(false); }
public static void ShowHelp(SubModuleBase module) { ConsoleManager.instance.ShowHelp_INTERNAL(module); }
public string[] ShowHelp_INTERNAL(SubModuleBase module, string methodNameStart, bool distinct = false, bool commandsOnly = false) { //var moduleName = module.GetType().Name; var help = ListPool <string> .Get(); var helpMethods = ListPool <string> .Get(); var methods = ListPool <MethodInfo> .Get(); ConsoleManager.GetModuleMethods(module, methods); foreach (var method in methods) { var methodName = method.Name.ToLower(); if (string.IsNullOrEmpty(methodNameStart) == true || methodName.StartsWith(methodNameStart) == true) { if (distinct == true) { if (helpMethods.Contains(methodName) == true) { continue; } helpMethods.Add(methodName); } var pstr = string.Empty; var pars = method.GetParameters(); foreach (var par in pars) { if (par.ParameterType == typeof(bool)) { pstr += string.Format("[b:{0}]", par.Name); } else if (par.ParameterType == typeof(int)) { pstr += string.Format("[i:{0}]", par.Name); } else if (par.ParameterType == typeof(long)) { pstr += string.Format("[l:{0}]", par.Name); } else if (par.ParameterType == typeof(float)) { pstr += string.Format("[f:{0}]", par.Name); } else if (par.ParameterType.IsEnum == true) { pstr += string.Format("[e:{0}]", par.Name); } else if (par.ParameterType == typeof(string)) { pstr += string.Format("[s:{0}]", par.Name); } else { pstr += string.Format("[{0}:{1}]", par.ParameterType, par.Name); } } HelpAttribute comment = null; var comments = method.GetCustomAttributes(typeof(HelpAttribute), false); if (comments.Length > 0) { comment = (HelpAttribute)comments[0]; } if (commandsOnly == true) { help.Add(methodName); } else { help.Add(methodName + (string.IsNullOrEmpty(pstr) == true ? string.Empty : string.Format("\t{0}", pstr)) + (comment == null || string.IsNullOrEmpty(comment.comment) == true ? string.Empty : (string.Format("\t<color=grey>// {0}</color>", comment.comment)))); } } } var result = help.ToArray(); ListPool <string> .Release(help); ListPool <string> .Release(helpMethods); ListPool <MethodInfo> .Release(methods); return(result); }
public static string[] GetHelp(SubModuleBase module, string methodNameStart, bool distinct = false, bool commandsOnly = false) { return(ConsoleManager.instance.ShowHelp_INTERNAL(module, methodNameStart, distinct, commandsOnly)); }