RobotCaseDispatcherComponentSystem.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. [ObjectSystem]
  6. public class RobotCaseDispatcherComponentAwakeSystem: AwakeSystem<RobotCaseDispatcherComponent>
  7. {
  8. public override void Awake(RobotCaseDispatcherComponent self)
  9. {
  10. RobotCaseDispatcherComponent.Instance = self;
  11. self.Load();
  12. }
  13. }
  14. [ObjectSystem]
  15. public class RobotCaseDispatcherComponentLoadSystem: LoadSystem<RobotCaseDispatcherComponent>
  16. {
  17. public override void Load(RobotCaseDispatcherComponent self)
  18. {
  19. self.Load();
  20. }
  21. }
  22. public static class RobotCaseDispatcherComponentSystem
  23. {
  24. public static void Load(this RobotCaseDispatcherComponent self)
  25. {
  26. self.Dictionary.Clear();
  27. HashSet<Type> types = Game.EventSystem.GetTypes(typeof(RobotCaseAttribute));
  28. foreach (Type type in types)
  29. {
  30. object[] attrs = type.GetCustomAttributes(typeof(RobotCaseAttribute), false);
  31. if (attrs.Length == 0)
  32. {
  33. continue;
  34. }
  35. RobotCaseAttribute attr = attrs[0] as RobotCaseAttribute;
  36. if (attr == null)
  37. {
  38. continue;
  39. }
  40. IRobotCase robotCase = Activator.CreateInstance(type) as IRobotCase;
  41. if (robotCase == null)
  42. {
  43. Log.Error($"RobotCase handle {type.Name} 需要继承 IRobotCase");
  44. continue;
  45. }
  46. self.Dictionary.Add(attr.CaseType, robotCase);
  47. }
  48. }
  49. public static async ETTask Run(this RobotCaseDispatcherComponent self, int caseType, string line)
  50. {
  51. if (!self.Dictionary.TryGetValue(caseType, out IRobotCase iRobotCase))
  52. {
  53. return;
  54. }
  55. try
  56. {
  57. using (RobotCase robotCase = await RobotCaseComponent.Instance.New())
  58. {
  59. robotCase.CommandLine = line;
  60. await iRobotCase.Run(robotCase);
  61. }
  62. }
  63. catch (Exception e)
  64. {
  65. Log.Error($"{self.DomainZone()} {e}");
  66. RobotLog.Console($"RobotCase Error {caseType}:\n\t{e}");
  67. }
  68. }
  69. }
  70. }