RobotCaseDispatcherComponentSystem.cs 2.6 KB

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