MessageDispatherComponent.cs 2.6 KB

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