ConsoleComponent.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. namespace ETModel
  5. {
  6. [ObjectSystem]
  7. public class ConsoleComponentAwakeSystem : StartSystem<ConsoleComponent>
  8. {
  9. public override void Start(ConsoleComponent self)
  10. {
  11. self.Start().NoAwait();
  12. }
  13. }
  14. public class ConsoleComponent: Component
  15. {
  16. public CancellationTokenSource CancellationTokenSource;
  17. public string OutputPrefix = "";
  18. public async ETVoid Start()
  19. {
  20. this.CancellationTokenSource = new CancellationTokenSource();
  21. while (true)
  22. {
  23. try
  24. {
  25. string line = await Task.Factory.StartNew(() =>
  26. {
  27. Console.Write($"{OutputPrefix}> ");
  28. return Console.In.ReadLine();
  29. }, this.CancellationTokenSource.Token);
  30. line = line.Trim();
  31. switch (line)
  32. {
  33. case "reload":
  34. try
  35. {
  36. Game.EventSystem.Add(DLLType.Hotfix, DllHelper.GetHotfixAssembly());
  37. }
  38. catch (Exception e)
  39. {
  40. Console.WriteLine(e);
  41. }
  42. break;
  43. case "repl":
  44. try
  45. {
  46. this.OutputPrefix = "repl";
  47. Game.Scene.AddComponent<ReplComponent>();
  48. }
  49. catch (Exception e)
  50. {
  51. Console.WriteLine(e);
  52. }
  53. break;
  54. default:
  55. ReplComponent replComponent = Game.Scene.GetComponent<ReplComponent>();
  56. if (replComponent == null)
  57. {
  58. Console.WriteLine($"no command: {line}!");
  59. break;
  60. }
  61. try
  62. {
  63. if (line == "exit")
  64. {
  65. this.OutputPrefix = "";
  66. Game.Scene.RemoveComponent<ReplComponent>();
  67. break;
  68. }
  69. switch (line)
  70. {
  71. case "exit":
  72. this.OutputPrefix = "";
  73. Game.Scene.RemoveComponent<ReplComponent>();
  74. break;
  75. case "reset":
  76. Game.Scene.RemoveComponent<ReplComponent>();
  77. Game.Scene.AddComponent<ReplComponent>();
  78. break;
  79. default:
  80. await replComponent.Run(line, this.CancellationTokenSource.Token);
  81. break;
  82. }
  83. }
  84. catch (Exception e)
  85. {
  86. Console.WriteLine(e);
  87. }
  88. break;
  89. }
  90. }
  91. catch (Exception e)
  92. {
  93. Console.WriteLine(e);
  94. }
  95. }
  96. }
  97. }
  98. }