MessageDispatherComponent.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ETModel
  4. {
  5. [ObjectSystem]
  6. public class MessageDispatherComponentAwakeSystem : AwakeSystem<MessageDispatherComponent>
  7. {
  8. public override void Awake(MessageDispatherComponent self)
  9. {
  10. self.AppType = Game.Scene.GetComponent<StartConfigComponent>().StartConfig.AppType;
  11. self.Load();
  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. public AppType AppType;
  28. private readonly Dictionary<ushort, List<IMHandler>> handlers = new Dictionary<ushort, List<IMHandler>>();
  29. public void Load()
  30. {
  31. this.handlers.Clear();
  32. Type[] types = DllHelper.GetMonoTypes();
  33. foreach (Type type in types)
  34. {
  35. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  36. if (attrs.Length == 0)
  37. {
  38. continue;
  39. }
  40. MessageHandlerAttribute messageHandlerAttribute = attrs[0] as MessageHandlerAttribute;
  41. if (!messageHandlerAttribute.Type.Is(this.AppType))
  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. Log.Error($"消息opcode为0: {messageType.Name}");
  56. continue;
  57. }
  58. this.RegisterHandler(opcode, iMHandler);
  59. }
  60. }
  61. public void RegisterHandler(ushort opcode, IMHandler handler)
  62. {
  63. if (!this.handlers.ContainsKey(opcode))
  64. {
  65. this.handlers.Add(opcode, new List<IMHandler>());
  66. }
  67. this.handlers[opcode].Add(handler);
  68. }
  69. public void Handle(Session session, MessageInfo messageInfo)
  70. {
  71. List<IMHandler> actions;
  72. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  73. {
  74. Log.Error($"消息没有处理: {messageInfo.Opcode} {JsonHelper.ToJson(messageInfo.Message)}");
  75. return;
  76. }
  77. foreach (IMHandler ev in actions)
  78. {
  79. try
  80. {
  81. ev.Handle(session, messageInfo.Message);
  82. }
  83. catch (Exception e)
  84. {
  85. Log.Error(e);
  86. }
  87. }
  88. }
  89. public override void Dispose()
  90. {
  91. if (this.IsDisposed)
  92. {
  93. return;
  94. }
  95. base.Dispose();
  96. }
  97. }
  98. }