ConsoleComponentSystem.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace ET
  6. {
  7. [FriendOf(typeof(ConsoleComponent))]
  8. [FriendOf(typeof(ModeContex))]
  9. public static class ConsoleComponentSystem
  10. {
  11. [ObjectSystem]
  12. public class ConsoleComponentAwakeSystem: AwakeSystem<ConsoleComponent>
  13. {
  14. protected override void Awake(ConsoleComponent self)
  15. {
  16. self.Load();
  17. self.Start().Coroutine();
  18. }
  19. }
  20. [ObjectSystem]
  21. public class ConsoleComponentLoadSystem: LoadSystem<ConsoleComponent>
  22. {
  23. protected override void Load(ConsoleComponent self)
  24. {
  25. self.Load();
  26. }
  27. }
  28. public static void Load(this ConsoleComponent self)
  29. {
  30. self.Handlers.Clear();
  31. HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (ConsoleHandlerAttribute));
  32. foreach (Type type in types)
  33. {
  34. object[] attrs = type.GetCustomAttributes(typeof(ConsoleHandlerAttribute), false);
  35. if (attrs.Length == 0)
  36. {
  37. continue;
  38. }
  39. ConsoleHandlerAttribute consoleHandlerAttribute = (ConsoleHandlerAttribute)attrs[0];
  40. object obj = Activator.CreateInstance(type);
  41. IConsoleHandler iConsoleHandler = obj as IConsoleHandler;
  42. if (iConsoleHandler == null)
  43. {
  44. throw new Exception($"ConsoleHandler handler not inherit IConsoleHandler class: {obj.GetType().FullName}");
  45. }
  46. self.Handlers.Add(consoleHandlerAttribute.Mode, iConsoleHandler);
  47. }
  48. }
  49. public static async ETTask Start(this ConsoleComponent self)
  50. {
  51. self.CancellationTokenSource = new CancellationTokenSource();
  52. while (true)
  53. {
  54. try
  55. {
  56. ModeContex modeContex = self.GetComponent<ModeContex>();
  57. string line = await Task.Factory.StartNew(() =>
  58. {
  59. Console.Write($"{modeContex?.Mode ?? ""}> ");
  60. return Console.In.ReadLine();
  61. }, self.CancellationTokenSource.Token);
  62. line = line.Trim();
  63. switch (line)
  64. {
  65. case "":
  66. break;
  67. case "exit":
  68. self.RemoveComponent<ModeContex>();
  69. break;
  70. default:
  71. {
  72. string[] lines = line.Split(" ");
  73. string mode = modeContex == null? lines[0] : modeContex.Mode;
  74. if (!self.Handlers.TryGetValue(mode, out IConsoleHandler iConsoleHandler))
  75. {
  76. Log.Console($"not found command: {line}");
  77. break;
  78. }
  79. if (modeContex == null)
  80. {
  81. modeContex = self.AddComponent<ModeContex>();
  82. modeContex.Mode = mode;
  83. }
  84. await iConsoleHandler.Run(modeContex, line);
  85. break;
  86. }
  87. }
  88. }
  89. catch (Exception e)
  90. {
  91. Log.Console(e.ToString());
  92. }
  93. }
  94. }
  95. }
  96. }