/// <summary> /// 自动补全命令 /// </summary> public string GetAutoCompltion(string str) { string[] cmds = str.Split(' '); if (cmds.Length < 1) { return(""); } //模块名 if (cmds.Length == 1) { CheatItem it = GetParseCheatItem(cmds[0]); if (it == null) { return(str); } return(it.GetType().Name); } //模块名+方法名 CheatItem item = GetParseCheatItem(cmds[0]); if (item == null) { return(str); } cmds[0] = item.GetType().Name; cmds[1] = item.GetCommandNameList().Find((name) => name.ToLower().StartsWith(cmds[1].ToLower())); if (cmds[1] == null) { return(str); } return(string.Join(" ", cmds)); }
/// <summary> /// 获取指定命令的帮助信息 /// </summary> public string GetTargetCommandHelp(string cmdName) { CheatItem item = _cheatItems.Find((i) => i.GetType().Name == cmdName); if (item == null) { return(ConstLanguage.Get(ConstLanguage.ErrorNotFindCommand, cmdName)); } CommandInfo expl = (CommandInfo)System.Attribute.GetCustomAttribute(item.GetType(), typeof(CommandInfo)); return(ConstLanguage.Get("{0}>>>>>{1}\n\n{2}", string.IsNullOrEmpty(expl.Name) ? item.GetType().Name : expl.Name, expl.Explain, item.GetCommandDescList())); }
public string GetTargetCommandHelp(string cmdName) { CheatItem item = _cheatItems.Find((i) => i.GetType().Name == cmdName); if (item == null) { return("<color=#FF00FF>错误:未找到指定命令 [" + cmdName + "]</color>"); } CommandInfo expl = (CommandInfo)System.Attribute.GetCustomAttribute(item.GetType(), typeof(CommandInfo)); return(item.GetType().Name + ">>>>>" + expl.Explain + "\n\n" + item.GetCommandList()); }
private void Init() { Type cit = typeof(CheatItem); Assembly asm = this.GetType().Assembly; Type[] types = asm.GetTypes(); for (int i = 0; i < types.Length; i++) { if (types[i].IsSubclassOf(cit)) { CheatItem item = asm.CreateInstance(types[i].FullName) as CheatItem; _cheatItems.Add(item); } } }
private string RunMethod(CheatItem item, string name, object[] parms) { try { //获取模块信息 MethodInfo met = item.GetType().GetMethod(name); CommandInfo info = GetCommandInfo(met); if (info != null && !info.CanExecute) { return(ConstLanguage.Get(ConstLanguage.ErrorCommandPolicy, info.RequireLevelName)); } ParameterInfo[] paramsInfo = met.GetParameters(); if (parms != null) { if (parms.Length != paramsInfo.Length) { Array.Resize <object>(ref parms, paramsInfo.Length); } for (int i = 0; i < paramsInfo.Length; i++) { if (parms[i] != null) { parms[i] = Convert.ChangeType(parms[i], paramsInfo[i].ParameterType); } else { parms[i] = paramsInfo[i].DefaultValue; } } } object res = met.Invoke(item, parms); return(res == null ? "" : ConstLanguage.Get(ConstLanguage.CommandResult, res.ToString())); } catch (Exception ex) { string cmdList = item.GetCommandDescList(); if (string.IsNullOrEmpty(cmdList)) { return(ConstLanguage.Get(ConstLanguage.ErrorCommand, ex.Message)); } return(ConstLanguage.Get(ConstLanguage.ErrorCommandParams, ex.Message, cmdList)); } }
/// <summary> /// 获取指定可以进行自动补全命令的列表 /// </summary> public List <string> GetCommandCompletionList(string str) { string[] cmds = str.Split(' '); if (cmds.Length < 1) { return(null); } //模块名 if (cmds.Length == 1) { return(_cheatItems.FindAll((i) => i.GetType().Name.ToLower().StartsWith(str.Trim().ToLower())).ConvertAll <string>((citem) => citem.GetType().Name)); } CheatItem item = GetParseCheatItem(cmds[0]); string name = item.GetType().Name; List <string> cmdList = new List <string>(); cmdList = item.GetCommandNameList().FindAll((cmdName) => cmdName.ToLower().StartsWith(cmds[1].ToLower())).ConvertAll <string>((rawName) => name + rawName); return(cmdList); }
/// <summary> /// 执行命令 /// </summary> /// <param name="command"></param> /// <returns></returns> public string RunCommand(string command) { if (string.IsNullOrEmpty(command)) { return(ConstLanguage.ErrorNullCommand); } command = command.Trim(); string[] cmds = command.Split(' '); CheatItem item = _cheatItems.Find((i) => i.GetType().Name == cmds[0]); if (item == null) { return(ConstLanguage.Get(ConstLanguage.ErrorNotFindCommand, cmds[0])); } //无参数默认命令 if (cmds.Length == 1) { #region 处理只有一个单位的默认命令 return(RunMethod(item, "DefaultSingleMethod", null)); #endregion } //有一个参数默认命令 if (item.GetCommandNameList().FindIndex((cmdName) => cmdName == cmds[1]) == -1) { object[] vals = new object[cmds.Length - 1]; Array.Copy(cmds, 1, vals, 0, vals.Length); return(RunMethod(item, "DefaultOneParamsMethod", vals)); } string name = cmds[1]; object[] parms = new object[cmds.Length - 2]; Array.Copy(cmds, 2, parms, 0, parms.Length); return(RunMethod(item, name, parms)); }
/// <summary> /// 获取指定命令名字开头与之匹配的CheatItem(忽略大小写) /// </summary> /// <param name="str"></param> /// <returns></returns> private CheatItem GetParseCheatItem(string str) { CheatItem item = _cheatItems.Find((i) => i.GetType().Name.ToLower().StartsWith(str.Trim().ToLower())); return(item); }
public string RunCommand(string command) { if (string.IsNullOrEmpty(command)) { return("<color=#FF00FF>错误:命令为空!</color>"); } //if (command == "Help") // return GetCommandList(); command = command.Trim(); string[] cmds = command.Split(' '); CheatItem item = _cheatItems.Find((i) => i.GetType().Name == cmds[0]); if (item == null) { return("<color=#FF00FF>错误:未找到命令 [" + cmds[0] + "]</color>\n有关命令帮助,请输入“Help”查看."); } if (cmds.Length < 2) { #region 处理只有一个单位的命令 MethodInfo met = item.GetType().GetMethod("SingleMethod"); if (met != null) { try { met.Invoke(item, null); return(""); } catch (Exception ex) { return("<color=#FF00FF>错误:" + ex.Message + "</color>"); } } #endregion return("<color=#FF00FF>错误:该命令需要至少一个参数。</color>\n" + cmds[0] + "可用参数有:\n" + item.GetCommandList()); } string name = cmds[1]; object[] parms = new object[cmds.Length - 2]; if (cmds.Length > 2) { for (int i = 2; i < cmds.Length; i++) { parms[i - 2] = cmds[i]; } } try { MethodInfo met = item.GetType().GetMethod(name); CommandInfo info = GetCommandInfo(met); if (!info.CanExecute) { return("<color=#FF00FF>权限不足:该命令要求至少为 " + info.LevelName + " 权限</color>"); } object res = met.Invoke(item, parms); string resInfo = res == null ? "成功" : res.ToString(); return("执行结果:" + resInfo); } catch (Exception ex) { return("<color=#FF00FF>错误:" + ex.Message + "</color>"); } }