ActorMessageDispatherComponentSystem.cs 3.5 KB

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