ActorMessageDispatherComponentSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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();
  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. foreach (Type type in types)
  59. {
  60. object[] attrs = type.GetCustomAttributes(typeof(ActorMessageHandlerAttribute), false);
  61. if (attrs.Length == 0)
  62. {
  63. continue;
  64. }
  65. ActorMessageHandlerAttribute messageHandlerAttribute = (ActorMessageHandlerAttribute) attrs[0];
  66. if (!messageHandlerAttribute.Type.Is(appType))
  67. {
  68. continue;
  69. }
  70. object obj = Activator.CreateInstance(type);
  71. IMActorHandler imHandler = obj as IMActorHandler;
  72. if (imHandler == null)
  73. {
  74. throw new Exception($"message handler not inherit IMActorHandler abstract class: {obj.GetType().FullName}");
  75. }
  76. Type messageType = imHandler.GetMessageType();
  77. self.ActorMessageHandlers.Add(messageType, imHandler);
  78. }
  79. }
  80. /// <summary>
  81. /// 一个actor收到的所有消息先由其指定的ActorTypeHandle处理
  82. /// </summary>
  83. public static async Task ActorTypeHandle(
  84. this ActorMessageDispatherComponent self, string actorType, Entity entity, ActorMessageInfo actorMessageInfo)
  85. {
  86. IActorTypeHandler iActorTypeHandler;
  87. if (!self.ActorTypeHandlers.TryGetValue(actorType, out iActorTypeHandler))
  88. {
  89. throw new Exception($"not found actortype handler: {actorType}");
  90. }
  91. await iActorTypeHandler.Handle(actorMessageInfo.Session, entity, actorMessageInfo.Message);
  92. }
  93. /// <summary>
  94. /// 根据actor消息分发给ActorMessageHandler处理
  95. /// </summary>
  96. public static async Task Handle(this ActorMessageDispatherComponent self, Session session, Entity entity, IActorMessage actorRequest)
  97. {
  98. if (!self.ActorMessageHandlers.TryGetValue(actorRequest.GetType(), out IMActorHandler handler))
  99. {
  100. throw new Exception($"not found message handler: {MongoHelper.ToJson(actorRequest)}");
  101. }
  102. await handler.Handle(session, entity, actorRequest);
  103. }
  104. }
  105. }