ActorMessageDispatherComponentSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Log.Info("apptype: " + appType);
  35. self.ActorMessageHandlers.Clear();
  36. self.ActorTypeHandlers.Clear();
  37. Type[] types = DllHelper.GetMonoTypes();
  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. }