ActorMessageDispatherComponentSystem.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using ETModel;
  5. namespace ETHotfix
  6. {
  7. [ObjectSystem]
  8. public class ActorMessageDispatherComponentStartSystem: AwakeSystem<ActorMessageDispatherComponent>
  9. {
  10. public override void Awake(ActorMessageDispatherComponent self)
  11. {
  12. self.Awake();
  13. }
  14. }
  15. [ObjectSystem]
  16. public class ActorMessageDispatherComponentLoadSystem: LoadSystem<ActorMessageDispatherComponent>
  17. {
  18. public override void Load(ActorMessageDispatherComponent self)
  19. {
  20. self.Load();
  21. }
  22. }
  23. /// <summary>
  24. /// Actor消息分发组件
  25. /// </summary>
  26. public static class ActorMessageDispatherComponentEx
  27. {
  28. public static void Awake(this ActorMessageDispatherComponent self)
  29. {
  30. self.Load();
  31. }
  32. public static void Load(this ActorMessageDispatherComponent self)
  33. {
  34. AppType appType = self.Entity.GetComponent<StartConfigComponent>().StartConfig.AppType;
  35. self.ActorMessageHandlers.Clear();
  36. self.ActorTypeHandlers.Clear();
  37. List<Type> types = Game.EventSystem.GetTypes(typeof(ActorTypeHandlerAttribute));
  38. foreach (Type type in types)
  39. {
  40. object[] attrs = type.GetCustomAttributes(typeof(ActorTypeHandlerAttribute), false);
  41. if (attrs.Length == 0)
  42. {
  43. continue;
  44. }
  45. ActorTypeHandlerAttribute actorTypeHandlerAttribute = (ActorTypeHandlerAttribute) attrs[0];
  46. if (!actorTypeHandlerAttribute.Type.Is(appType))
  47. {
  48. continue;
  49. }
  50. object obj = Activator.CreateInstance(type);
  51. IActorTypeHandler iActorTypeHandler = obj as IActorTypeHandler;
  52. if (iActorTypeHandler == null)
  53. {
  54. throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
  55. }
  56. self.ActorTypeHandlers.Add(actorTypeHandlerAttribute.ActorType, iActorTypeHandler);
  57. }
  58. types = Game.EventSystem.GetTypes(typeof (ActorMessageHandlerAttribute));
  59. foreach (Type type in types)
  60. {
  61. object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false);
  62. if (attrs.Length == 0)
  63. {
  64. continue;
  65. }
  66. ActorMessageHandlerAttribute messageHandlerAttribute = (ActorMessageHandlerAttribute) attrs[0];
  67. if (!messageHandlerAttribute.Type.Is(appType))
  68. {
  69. continue;
  70. }
  71. object obj = Activator.CreateInstance(type);
  72. IMActorHandler imHandler = obj as IMActorHandler;
  73. if (imHandler == null)
  74. {
  75. throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}");
  76. }
  77. Type messageType = imHandler.GetMessageType();
  78. self.ActorMessageHandlers.Add(messageType, imHandler);
  79. }
  80. }
  81. /// <summary>
  82. /// 一个actor收到的所有消息先由其指定的ActorTypeHandle处理
  83. /// </summary>
  84. public static async Task ActorTypeHandle(
  85. this ActorMessageDispatherComponent self, string actorType, Entity entity, ActorMessageInfo actorMessageInfo)
  86. {
  87. IActorTypeHandler iActorTypeHandler;
  88. if (!self.ActorTypeHandlers.TryGetValue(actorType, out iActorTypeHandler))
  89. {
  90. throw new Exception($"not found actortype handler: {actorType}");
  91. }
  92. await iActorTypeHandler.Handle(actorMessageInfo.Session, entity, actorMessageInfo.Message);
  93. }
  94. /// <summary>
  95. /// 根据actor消息分发给ActorMessageHandler处理
  96. /// </summary>
  97. public static async Task Handle(this ActorMessageDispatherComponent self, Session session, Entity entity, IActorMessage actorRequest)
  98. {
  99. if (!self.ActorMessageHandlers.TryGetValue(actorRequest.GetType(), out IMActorHandler handler))
  100. {
  101. throw new Exception($"not found message handler: {MongoHelper.ToJson(actorRequest)}");
  102. }
  103. await handler.Handle(session, entity, actorRequest);
  104. }
  105. }
  106. }