ActorMessageDispatherComponentSystem.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 ActorMessageDispatherComponentHelper
  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 = StartConfigComponent.Instance.StartConfig.AppType;
  35. self.ActorMessageHandlers.Clear();
  36. self.ActorTypeHandlers.Clear();
  37. List<Type> types = Game.EventSystem.GetTypes(typeof(ActorInterceptTypeHandlerAttribute));
  38. foreach (Type type in types)
  39. {
  40. object[] attrs = type.GetCustomAttributes(typeof(ActorInterceptTypeHandlerAttribute), false);
  41. if (attrs.Length == 0)
  42. {
  43. continue;
  44. }
  45. ActorInterceptTypeHandlerAttribute actorInterceptTypeHandlerAttribute = (ActorInterceptTypeHandlerAttribute) attrs[0];
  46. if (!actorInterceptTypeHandlerAttribute.Type.Is(appType))
  47. {
  48. continue;
  49. }
  50. object obj = Activator.CreateInstance(type);
  51. IActorInterceptTypeHandler iActorInterceptTypeHandler = obj as IActorInterceptTypeHandler;
  52. if (iActorInterceptTypeHandler == null)
  53. {
  54. throw new Exception($"actor handler not inherit IEntityActorHandler: {obj.GetType().FullName}");
  55. }
  56. self.ActorTypeHandlers.Add(actorInterceptTypeHandlerAttribute.ActorType, iActorInterceptTypeHandler);
  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. public static async Task Handle(
  82. this ActorMessageDispatherComponent self, MailBoxComponent mailBoxComponent, ActorMessageInfo actorMessageInfo)
  83. {
  84. // 有拦截器使用拦截器处理
  85. IActorInterceptTypeHandler iActorInterceptTypeHandler;
  86. if (self.ActorTypeHandlers.TryGetValue(mailBoxComponent.ActorInterceptType, out iActorInterceptTypeHandler))
  87. {
  88. await iActorInterceptTypeHandler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
  89. return;
  90. }
  91. // 没有拦截器就用IMActorHandler处理
  92. if (!self.ActorMessageHandlers.TryGetValue(actorMessageInfo.Message.GetType(), out IMActorHandler handler))
  93. {
  94. throw new Exception($"not found message handler: {MongoHelper.ToJson(actorMessageInfo.Message)}");
  95. }
  96. await handler.Handle(actorMessageInfo.Session, mailBoxComponent.Entity, actorMessageInfo.Message);
  97. }
  98. }
  99. }