CrossComponent.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Model
  4. {
  5. /// <summary>
  6. /// 事件分发,可以将事件分发到IL层
  7. /// </summary>
  8. [EntityEvent((int)EntityEventId.CrossComponent)]
  9. public class CrossComponent : Component, IAwake
  10. {
  11. private static object[] args0 = new object[0];
  12. private static object[] args1 = new object[1];
  13. private static object[] args2 = new object[2];
  14. private static object[] args3 = new object[3];
  15. private static object[] args4 = new object[4];
  16. private Dictionary<int, List<IInstanceMethod>> allEvents;
  17. public void Awake()
  18. {
  19. this.Load();
  20. }
  21. private void Load()
  22. {
  23. this.allEvents = new Dictionary<int, List<IInstanceMethod>>();
  24. Type[] types = DllHelper.GetHotfixTypes();
  25. foreach (Type type in types)
  26. {
  27. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  28. foreach (object attr in attrs)
  29. {
  30. EventAttribute aEventAttribute = (EventAttribute)attr;
  31. IInstanceMethod method = new ILInstanceMethod(type, "Run");
  32. if (!this.allEvents.ContainsKey(aEventAttribute.Type))
  33. {
  34. this.allEvents.Add(aEventAttribute.Type, new List<IInstanceMethod>());
  35. }
  36. this.allEvents[aEventAttribute.Type].Add(method);
  37. }
  38. }
  39. }
  40. public void Run(int type)
  41. {
  42. List<IInstanceMethod> iEvents = null;
  43. if (!this.allEvents.TryGetValue(type, out iEvents))
  44. {
  45. return;
  46. }
  47. foreach (IInstanceMethod obj in iEvents)
  48. {
  49. try
  50. {
  51. obj.Run(args0);
  52. }
  53. catch (Exception err)
  54. {
  55. Log.Error(err.ToString());
  56. }
  57. }
  58. }
  59. public void Run<A>(int type, A a)
  60. {
  61. List<IInstanceMethod> iEvents = null;
  62. if (!this.allEvents.TryGetValue(type, out iEvents))
  63. {
  64. return;
  65. }
  66. foreach (IInstanceMethod obj in iEvents)
  67. {
  68. try
  69. {
  70. args1[0] = a;
  71. obj.Run(args1);
  72. }
  73. catch (Exception err)
  74. {
  75. Log.Error(err.ToString());
  76. }
  77. }
  78. }
  79. public void Run<A, B>(int type, A a, B b)
  80. {
  81. List<IInstanceMethod> iEvents = null;
  82. if (!this.allEvents.TryGetValue(type, out iEvents))
  83. {
  84. return;
  85. }
  86. foreach (IInstanceMethod obj in iEvents)
  87. {
  88. try
  89. {
  90. args2[0] = a;
  91. args2[1] = b;
  92. obj.Run(args2);
  93. }
  94. catch (Exception err)
  95. {
  96. Log.Error(err.ToString());
  97. }
  98. }
  99. }
  100. public void Run<A, B, C>(int type, A a, B b, C c)
  101. {
  102. List<IInstanceMethod> iEvents = null;
  103. if (!this.allEvents.TryGetValue(type, out iEvents))
  104. {
  105. return;
  106. }
  107. foreach (IInstanceMethod obj in iEvents)
  108. {
  109. try
  110. {
  111. args3[0] = a;
  112. args3[1] = b;
  113. args3[2] = c;
  114. obj.Run(args3);
  115. }
  116. catch (Exception err)
  117. {
  118. Log.Error(err.ToString());
  119. }
  120. }
  121. }
  122. public void Run<A, B, C, D>(int type, A a, B b, C c, D d)
  123. {
  124. List<IInstanceMethod> iEvents = null;
  125. if (!this.allEvents.TryGetValue(type, out iEvents))
  126. {
  127. return;
  128. }
  129. foreach (IInstanceMethod obj in iEvents)
  130. {
  131. try
  132. {
  133. args4[0] = a;
  134. args4[1] = b;
  135. args4[2] = c;
  136. args4[3] = d;
  137. obj.Run(args4);
  138. }
  139. catch (Exception err)
  140. {
  141. Log.Error(err.ToString());
  142. }
  143. }
  144. }
  145. public void Run(int type, params object[] param)
  146. {
  147. List<IInstanceMethod> iEvents = null;
  148. if (!this.allEvents.TryGetValue(type, out iEvents))
  149. {
  150. return;
  151. }
  152. foreach (IInstanceMethod obj in iEvents)
  153. {
  154. try
  155. {
  156. obj.Run(param);
  157. }
  158. catch (Exception err)
  159. {
  160. Log.Error(err.ToString());
  161. }
  162. }
  163. }
  164. }
  165. }