CommandList.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using CommonLang.Property;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. namespace CommonServer.Command
  9. {
  10. public abstract class AbstractCommand
  11. {
  12. public abstract string Key { get; }
  13. public abstract void DoCommand(string arg, TextWriter output);
  14. public virtual string Help
  15. {
  16. get
  17. {
  18. DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(GetType());
  19. if (desc != null) { return (desc.Desc); }
  20. return GetType().Name;
  21. }
  22. }
  23. private static Regex reg = new Regex(@"\s+");
  24. public static string[] ToArgs(string arg)
  25. {
  26. return reg.Split(arg);
  27. }
  28. }
  29. public class CommandList<T> where T : AbstractCommand
  30. {
  31. private SortedDictionary<string, T> cmd_list = new SortedDictionary<string, T>();
  32. private static Regex split = new Regex(@"\s+");
  33. public T[] CmdList
  34. {
  35. get { return cmd_list.Values.ToArray(); }
  36. }
  37. public CommandList()
  38. {
  39. Type type = typeof(T);
  40. List<Type> allcmd = ReflectionUtil.GetNoneVirtualSubTypes(type, type.Assembly);
  41. foreach (Type tcmd in allcmd)
  42. {
  43. T cmd = ReflectionUtil.CreateInstance(tcmd) as T;
  44. if (cmd != null)
  45. {
  46. cmd_list.Add(cmd.Key, cmd);
  47. }
  48. }
  49. }
  50. public bool DoCommand(string line, TextWriter output)
  51. {
  52. foreach (AbstractCommand cmd in cmd_list.Values)
  53. {
  54. if (line.ToLower().StartsWith(cmd.Key.ToLower()))
  55. {
  56. var arg = line.Substring(cmd.Key.Length);
  57. if (arg.Trim().Length > 0)
  58. {
  59. var mat = split.Match(arg);
  60. if (!mat.Success || mat.Index != 0)
  61. {
  62. continue;
  63. }
  64. }
  65. try
  66. {
  67. if (line.EndsWith("/?") || line.EndsWith("/help"))
  68. {
  69. output.WriteLine(cmd.Help);
  70. }
  71. else
  72. {
  73. cmd.DoCommand(arg.Trim(), output);
  74. }
  75. }
  76. catch (Exception err)
  77. {
  78. output.WriteLine(err.Message);
  79. }
  80. return true;
  81. }
  82. }
  83. return false;
  84. }
  85. public string ListCommand(string prefix)
  86. {
  87. List<AbstractCommand> cmdlist = new List<AbstractCommand>();
  88. SortedDictionary<string, List<AbstractCommand>> catgory_map = new SortedDictionary<string, List<AbstractCommand>>();
  89. foreach (AbstractCommand cmd in CmdList)
  90. {
  91. DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(cmd.GetType());
  92. if (desc == null || string.IsNullOrEmpty(desc.Category))
  93. {
  94. cmdlist.Add(cmd);
  95. }
  96. else
  97. {
  98. List<AbstractCommand> catgory;
  99. if (!catgory_map.TryGetValue(desc.Category, out catgory))
  100. {
  101. catgory = new List<AbstractCommand>();
  102. catgory_map.Add(desc.Category, catgory);
  103. }
  104. catgory.Add(cmd);
  105. }
  106. }
  107. StringBuilder sb = new StringBuilder();
  108. ListCommand_Internal(sb, prefix, cmdlist);
  109. foreach (KeyValuePair<string, List<AbstractCommand>> catgory in catgory_map)
  110. {
  111. sb.AppendLine("[" + catgory.Key + "]");
  112. ListCommand_Internal(sb, prefix, catgory.Value);
  113. }
  114. return sb.ToString();
  115. }
  116. private void ListCommand_Internal(StringBuilder sb, string prefix, List<AbstractCommand> cmdlist)
  117. {
  118. bool show_prefix = string.IsNullOrEmpty(prefix);
  119. foreach (AbstractCommand cmd in cmdlist)
  120. {
  121. if (show_prefix || cmd.Key.StartsWith(prefix))
  122. {
  123. string line = cmd.Key;
  124. DescAttribute desc = PropertyUtil.GetAttribute<DescAttribute>(cmd.GetType());
  125. if (desc != null)
  126. {
  127. line += "\t - " + desc.Desc;
  128. }
  129. sb.AppendLine(line);
  130. }
  131. }
  132. }
  133. }
  134. }