MessageDispatherComponent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using ILRuntime.CLR.Method;
  5. using ILRuntime.Runtime.Intepreter;
  6. namespace Model
  7. {
  8. /// <summary>
  9. /// 用来抹平ILRuntime跟mono层差异
  10. /// </summary>
  11. public interface IMessageMethod
  12. {
  13. void Run(Session session, AMessage a);
  14. }
  15. public class IMessageMonoMethod : IMessageMethod
  16. {
  17. private readonly IMHandler iMHandler;
  18. public IMessageMonoMethod(IMHandler iMHandler)
  19. {
  20. this.iMHandler = iMHandler;
  21. }
  22. public void Run(Session session, AMessage a)
  23. {
  24. this.iMHandler.Handle(session, a);
  25. }
  26. }
  27. public class IMessageILMethod : IMessageMethod
  28. {
  29. private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  30. private readonly ILTypeInstance instance;
  31. private readonly IMethod method;
  32. private readonly object[] param;
  33. public IMessageILMethod(Type type, string methodName)
  34. {
  35. appDomain = Init.Instance.AppDomain;
  36. this.instance = this.appDomain.Instantiate(type.FullName);
  37. this.method = this.instance.Type.GetMethod(methodName, 2);
  38. int n = this.method.ParameterCount;
  39. this.param = new object[n];
  40. }
  41. public void Run(Session session, AMessage a)
  42. {
  43. this.param[0] = a;
  44. this.appDomain.Invoke(this.method, this.instance, param);
  45. }
  46. }
  47. [ObjectSystem]
  48. public class MessageDispatherComponentSystem : ObjectSystem<MessageDispatherComponent>, IAwake, ILoad
  49. {
  50. public void Awake()
  51. {
  52. this.Get().Awake();
  53. }
  54. public void Load()
  55. {
  56. this.Get().Load();
  57. }
  58. }
  59. /// <summary>
  60. /// 消息分发组件
  61. /// </summary>
  62. public class MessageDispatherComponent : Component
  63. {
  64. private Dictionary<ushort, List<IMessageMethod>> handlers;
  65. public void Awake()
  66. {
  67. this.Load();
  68. }
  69. public void Load()
  70. {
  71. handlers = new Dictionary<ushort, List<IMessageMethod>>();
  72. Type[] types = DllHelper.GetMonoTypes();
  73. foreach (Type type in types)
  74. {
  75. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  76. if (attrs.Length == 0)
  77. {
  78. continue;
  79. }
  80. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  81. IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
  82. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  83. {
  84. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
  85. }
  86. this.handlers[messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
  87. }
  88. // hotfix dll
  89. Type[] hotfixTypes = DllHelper.GetHotfixTypes();
  90. foreach (Type type in hotfixTypes)
  91. {
  92. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  93. if (attrs.Length == 0)
  94. {
  95. continue;
  96. }
  97. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  98. #if ILRuntime
  99. IMessageMethod iMessageMethod = new IMessageILMethod(type, "Handle");
  100. #else
  101. IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
  102. IMessageMethod iMessageMethod = new IMessageMonoMethod(iMHandler);
  103. #endif
  104. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  105. {
  106. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
  107. }
  108. this.handlers[messageHandlerAttribute.Opcode].Add(iMessageMethod);
  109. }
  110. }
  111. public void Handle(Session session, MessageInfo messageInfo)
  112. {
  113. List<IMessageMethod> actions;
  114. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  115. {
  116. Log.Error($"消息 {messageInfo.Opcode} 没有处理");
  117. return;
  118. }
  119. foreach (IMessageMethod ev in actions)
  120. {
  121. try
  122. {
  123. ev.Run(session, messageInfo.Message);
  124. }
  125. catch (Exception e)
  126. {
  127. Log.Error(e.ToString());
  128. }
  129. }
  130. }
  131. public override void Dispose()
  132. {
  133. if (Id == 0)
  134. {
  135. return;
  136. }
  137. base.Dispose();
  138. }
  139. }
  140. }