MessageDispatcherComponentSystem.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ET
  4. {
  5. /// <summary>
  6. /// 消息分发组件
  7. /// </summary>
  8. [FriendOf(typeof(MessageDispatcherComponent))]
  9. public static class MessageDispatcherComponentHelper
  10. {
  11. [ObjectSystem]
  12. public class MessageDispatcherComponentAwakeSystem: AwakeSystem<MessageDispatcherComponent>
  13. {
  14. protected override void Awake(MessageDispatcherComponent self)
  15. {
  16. MessageDispatcherComponent.Instance = self;
  17. self.Load();
  18. }
  19. }
  20. [ObjectSystem]
  21. public class MessageDispatcherComponentLoadSystem: LoadSystem<MessageDispatcherComponent>
  22. {
  23. protected override void Load(MessageDispatcherComponent self)
  24. {
  25. self.Load();
  26. }
  27. }
  28. [ObjectSystem]
  29. public class MessageDispatcherComponentDestroySystem: DestroySystem<MessageDispatcherComponent>
  30. {
  31. protected override void Destroy(MessageDispatcherComponent self)
  32. {
  33. MessageDispatcherComponent.Instance = null;
  34. self.Handlers.Clear();
  35. }
  36. }
  37. private static void Load(this MessageDispatcherComponent self)
  38. {
  39. self.Handlers.Clear();
  40. HashSet<Type> types = EventSystem.Instance.GetTypes(typeof (MessageHandlerAttribute));
  41. foreach (Type type in types)
  42. {
  43. IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
  44. if (iMHandler == null)
  45. {
  46. Log.Error($"message handle {type.Name} 需要继承 IMHandler");
  47. continue;
  48. }
  49. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  50. foreach (object attr in attrs)
  51. {
  52. MessageHandlerAttribute messageHandlerAttribute = attr as MessageHandlerAttribute;
  53. Type messageType = iMHandler.GetMessageType();
  54. ushort opcode = NetServices.Instance.GetOpcode(messageType);
  55. if (opcode == 0)
  56. {
  57. Log.Error($"消息opcode为0: {messageType.Name}");
  58. continue;
  59. }
  60. MessageDispatcherInfo messageDispatcherInfo = new (messageHandlerAttribute.SceneType, iMHandler);
  61. self.RegisterHandler(opcode, messageDispatcherInfo);
  62. }
  63. }
  64. }
  65. private static void RegisterHandler(this MessageDispatcherComponent self, ushort opcode, MessageDispatcherInfo handler)
  66. {
  67. if (!self.Handlers.ContainsKey(opcode))
  68. {
  69. self.Handlers.Add(opcode, new List<MessageDispatcherInfo>());
  70. }
  71. self.Handlers[opcode].Add(handler);
  72. }
  73. public static void Handle(this MessageDispatcherComponent self, Session session, object message)
  74. {
  75. List<MessageDispatcherInfo> actions;
  76. ushort opcode = NetServices.Instance.GetOpcode(message.GetType());
  77. if (!self.Handlers.TryGetValue(opcode, out actions))
  78. {
  79. Log.Error($"消息没有处理: {opcode} {message}");
  80. return;
  81. }
  82. SceneType sceneType = session.DomainScene().SceneType;
  83. foreach (MessageDispatcherInfo ev in actions)
  84. {
  85. if (ev.SceneType != sceneType)
  86. {
  87. continue;
  88. }
  89. try
  90. {
  91. ev.IMHandler.Handle(session, message);
  92. }
  93. catch (Exception e)
  94. {
  95. Log.Error(e);
  96. }
  97. }
  98. }
  99. }
  100. }