MessageDispatherComponent.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using ETModel;
  4. namespace ETHotfix
  5. {
  6. [ObjectSystem]
  7. public class MessageDispatherComponentAwakeSystem : AwakeSystem<MessageDispatherComponent>
  8. {
  9. public override void Awake(MessageDispatherComponent self)
  10. {
  11. self.Awake();
  12. }
  13. }
  14. [ObjectSystem]
  15. public class MessageDispatherComponentLoadSystem : LoadSystem<MessageDispatherComponent>
  16. {
  17. public override void Load(MessageDispatherComponent self)
  18. {
  19. self.Load();
  20. }
  21. }
  22. /// <summary>
  23. /// 消息分发组件
  24. /// </summary>
  25. public class MessageDispatherComponent : Component
  26. {
  27. private readonly Dictionary<ushort, List<IMHandler>> handlers = new Dictionary<ushort, List<IMHandler>>();
  28. public void Awake()
  29. {
  30. this.Load();
  31. }
  32. public void Load()
  33. {
  34. this.handlers.Clear();
  35. ETModel.MessageDispatherComponent messageDispatherComponent = ETModel.Game.Scene.GetComponent<ETModel.MessageDispatherComponent>();
  36. ETModel.OpcodeTypeComponent opcodeTypeComponent = ETModel.Game.Scene.GetComponent<ETModel.OpcodeTypeComponent>();
  37. Type[] types = ETModel.Game.Hotfix.GetHotfixTypes();
  38. foreach (Type type in types)
  39. {
  40. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  41. if (attrs.Length == 0)
  42. {
  43. continue;
  44. }
  45. IMHandler iMHandler = Activator.CreateInstance(type) as IMHandler;
  46. if (iMHandler == null)
  47. {
  48. Log.Error($"message handle {type.Name} 需要继承 IMHandler");
  49. continue;
  50. }
  51. Type messageType = iMHandler.GetMessageType();
  52. ushort opcode = this.Entity.GetComponent<OpcodeTypeComponent>().GetOpcode(messageType);
  53. if (opcode != 0)
  54. {
  55. this.RegisterHandler(opcode, iMHandler);
  56. }
  57. // 尝试注册到mono层
  58. if (messageDispatherComponent != null && opcodeTypeComponent != null)
  59. {
  60. ushort monoOpcode = opcodeTypeComponent.GetOpcode(messageType);
  61. if (monoOpcode == 0)
  62. {
  63. continue;
  64. }
  65. MessageProxy messageProxy = new MessageProxy(messageType, (session, o) => { iMHandler.Handle(session, o); });
  66. messageDispatherComponent.RegisterHandler(monoOpcode, messageProxy);
  67. }
  68. }
  69. }
  70. public void RegisterHandler(ushort opcode, IMHandler handler)
  71. {
  72. if (!this.handlers.ContainsKey(opcode))
  73. {
  74. this.handlers.Add(opcode, new List<IMHandler>());
  75. }
  76. this.handlers[opcode].Add(handler);
  77. }
  78. public void Handle(Session session, MessageInfo messageInfo)
  79. {
  80. List<IMHandler> actions;
  81. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  82. {
  83. Log.Error($"消息 {messageInfo.Message.GetType().FullName} 没有处理");
  84. return;
  85. }
  86. foreach (IMHandler ev in actions)
  87. {
  88. try
  89. {
  90. ev.Handle(session, messageInfo.Message);
  91. }
  92. catch (Exception e)
  93. {
  94. Log.Error(e);
  95. }
  96. }
  97. }
  98. public override void Dispose()
  99. {
  100. if (this.IsDisposed)
  101. {
  102. return;
  103. }
  104. base.Dispose();
  105. }
  106. }
  107. }