ConsoleComponentSystem.cs 3.7 KB

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