MessageDispatherComponent.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(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(AMessage a)
  23. {
  24. this.iMHandler.Handle(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);
  38. int n = this.method.ParameterCount;
  39. this.param = new object[n];
  40. }
  41. public void Run(AMessage a)
  42. {
  43. this.param[0] = a;
  44. this.appDomain.Invoke(this.method, this.instance, param);
  45. }
  46. }
  47. [ObjectEvent]
  48. public class MessageDispatherComponentEvent : ObjectEvent<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. public static class Opcode2Name
  60. {
  61. private static Dictionary<int, string> _init = new Dictionary<int, string>();
  62. public static string GetName(int code)
  63. {
  64. if (_init.Count == 0)
  65. {
  66. Type type = typeof(Opcode);
  67. FieldInfo[] fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
  68. foreach (FieldInfo field in fields)
  69. {
  70. if (!field.IsStatic)
  71. {
  72. continue;
  73. }
  74. int codeID = (ushort)field.GetValue(null);
  75. if (_init.ContainsKey(codeID))
  76. {
  77. Log.Warning($"重复的Opcode:{codeID}");
  78. continue;
  79. }
  80. _init.Add(codeID, field.Name);
  81. }
  82. }
  83. return _init[code];
  84. }
  85. }
  86. /// <summary>
  87. /// 消息分发组件
  88. /// </summary>
  89. public class MessageDispatherComponent : Component
  90. {
  91. private Dictionary<ushort, List<IMessageMethod>> handlers;
  92. public void Awake()
  93. {
  94. this.Load();
  95. }
  96. public void Load()
  97. {
  98. handlers = new Dictionary<ushort, List<IMessageMethod>>();
  99. Type[] types = DllHelper.GetMonoTypes();
  100. foreach (Type type in types)
  101. {
  102. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  103. if (attrs.Length == 0)
  104. {
  105. continue;
  106. }
  107. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  108. IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
  109. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  110. {
  111. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
  112. }
  113. this.handlers[messageHandlerAttribute.Opcode].Add(new IMessageMonoMethod(iMHandler));
  114. }
  115. // hotfix dll
  116. Type[] hotfixTypes = DllHelper.GetHotfixTypes();
  117. foreach (Type type in hotfixTypes)
  118. {
  119. object[] attrs = type.GetCustomAttributes(typeof(MessageHandlerAttribute), false);
  120. if (attrs.Length == 0)
  121. {
  122. continue;
  123. }
  124. MessageHandlerAttribute messageHandlerAttribute = (MessageHandlerAttribute)attrs[0];
  125. #if ILRuntime
  126. IMessageMethod iMessageMethod = new IMessageILMethod(type, "Run");
  127. #else
  128. IMHandler iMHandler = (IMHandler)Activator.CreateInstance(type);
  129. IMessageMethod iMessageMethod = new IMessageMonoMethod(iMHandler);
  130. #endif
  131. if (!this.handlers.ContainsKey(messageHandlerAttribute.Opcode))
  132. {
  133. this.handlers.Add(messageHandlerAttribute.Opcode, new List<IMessageMethod>());
  134. }
  135. this.handlers[messageHandlerAttribute.Opcode].Add(iMessageMethod);
  136. }
  137. }
  138. public void Handle(MessageInfo messageInfo)
  139. {
  140. List<IMessageMethod> actions;
  141. if (!this.handlers.TryGetValue(messageInfo.Opcode, out actions))
  142. {
  143. Log.Error($"消息 {Opcode2Name.GetName(messageInfo.Opcode)}({messageInfo.Opcode}) 没有处理");
  144. return;
  145. }
  146. foreach (IMessageMethod ev in actions)
  147. {
  148. try
  149. {
  150. ev.Run(messageInfo.Message);
  151. }
  152. catch (Exception e)
  153. {
  154. Log.Error(e.ToString());
  155. }
  156. }
  157. }
  158. public override void Dispose()
  159. {
  160. if (Id == 0)
  161. {
  162. return;
  163. }
  164. base.Dispose();
  165. }
  166. }
  167. }