EventComponent.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Base;
  5. namespace Model
  6. {
  7. /// <summary>
  8. /// 事件分发
  9. /// </summary>
  10. [EntityEvent(EntityEventId.EventComponent)]
  11. public class EventComponent: Component
  12. {
  13. private Dictionary<int, List<object>> allEvents;
  14. private void Awake()
  15. {
  16. this.Load();
  17. }
  18. private void Load()
  19. {
  20. this.allEvents = new Dictionary<int, List<object>>();
  21. Type[] types = DllHelper.GetBaseTypes();
  22. foreach (Type type in types)
  23. {
  24. object[] attrs = type.GetCustomAttributes(typeof (EventAttribute), false);
  25. foreach (object attr in attrs)
  26. {
  27. EventAttribute aEventAttribute = (EventAttribute) attr;
  28. object obj = Activator.CreateInstance(type);
  29. if (!this.allEvents.ContainsKey(aEventAttribute.Type))
  30. {
  31. this.allEvents.Add(aEventAttribute.Type, new List<object>());
  32. }
  33. this.allEvents[aEventAttribute.Type].Add(obj);
  34. }
  35. }
  36. }
  37. public void Run(int type)
  38. {
  39. List<object> iEvents = null;
  40. if (!this.allEvents.TryGetValue(type, out iEvents))
  41. {
  42. return;
  43. }
  44. foreach (object obj in iEvents)
  45. {
  46. try
  47. {
  48. IEvent iEvent = obj as IEvent;
  49. if (iEvent == null)
  50. {
  51. throw new Exception($"event type: {type} is not IEvent");
  52. }
  53. iEvent.Run();
  54. }
  55. catch (Exception err)
  56. {
  57. Log.Error(err.ToString());
  58. }
  59. }
  60. }
  61. public void Run<A>(int type, A a)
  62. {
  63. List<object> iEvents = null;
  64. if (!this.allEvents.TryGetValue(type, out iEvents))
  65. {
  66. return;
  67. }
  68. foreach (object obj in iEvents)
  69. {
  70. try
  71. {
  72. var iEvent = obj as IEvent<A>;
  73. if (iEvent == null)
  74. {
  75. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}>");
  76. }
  77. iEvent.Run(a);
  78. }
  79. catch (Exception err)
  80. {
  81. Log.Error(err.ToString());
  82. }
  83. }
  84. }
  85. public void Run<A, B>(int type, A a, B b)
  86. {
  87. List<object> iEvents = null;
  88. if (!this.allEvents.TryGetValue(type, out iEvents))
  89. {
  90. return;
  91. }
  92. foreach (object obj in iEvents)
  93. {
  94. try
  95. {
  96. var iEvent = obj as IEvent<A, B>;
  97. if (iEvent == null)
  98. {
  99. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}>");
  100. }
  101. iEvent.Run(a, b);
  102. }
  103. catch (Exception err)
  104. {
  105. Log.Error(err.ToString());
  106. }
  107. }
  108. }
  109. public void Run<A, B, C>(int type, A a, B b, C c)
  110. {
  111. List<object> iEvents = null;
  112. if (!this.allEvents.TryGetValue(type, out iEvents))
  113. {
  114. return;
  115. }
  116. foreach (object obj in iEvents)
  117. {
  118. try
  119. {
  120. var iEvent = obj as IEvent<A, B, C>;
  121. if (iEvent == null)
  122. {
  123. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}>");
  124. }
  125. iEvent.Run(a, b, c);
  126. }
  127. catch (Exception err)
  128. {
  129. Log.Error(err.ToString());
  130. }
  131. }
  132. }
  133. public void Run<A, B, C, D>(int type, A a, B b, C c, D d)
  134. {
  135. List<object> iEvents = null;
  136. if (!this.allEvents.TryGetValue(type, out iEvents))
  137. {
  138. return;
  139. }
  140. foreach (object obj in iEvents)
  141. {
  142. try
  143. {
  144. var iEvent = obj as IEvent<A, B, C, D>;
  145. if (iEvent == null)
  146. {
  147. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}>");
  148. }
  149. iEvent.Run(a, b, c, d);
  150. }
  151. catch (Exception err)
  152. {
  153. Log.Error(err.ToString());
  154. }
  155. }
  156. }
  157. public void Run<A, B, C, D, E>(int type, A a, B b, C c, D d, E e)
  158. {
  159. List<object> iEvents = null;
  160. if (!this.allEvents.TryGetValue(type, out iEvents))
  161. {
  162. return;
  163. }
  164. foreach (object obj in iEvents)
  165. {
  166. try
  167. {
  168. var iEvent = obj as IEvent<A, B, C, D, E>;
  169. if (iEvent == null)
  170. {
  171. throw new Exception(
  172. $"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}, {typeof (E).Name}>");
  173. }
  174. iEvent.Run(a, b, c, d, e);
  175. }
  176. catch (Exception err)
  177. {
  178. Log.Error(err.ToString());
  179. }
  180. }
  181. }
  182. public void Run<A, B, C, D, E, F>(int type, A a, B b, C c, D d, E e, F f)
  183. {
  184. List<object> iEvents = null;
  185. if (!this.allEvents.TryGetValue(type, out iEvents))
  186. {
  187. return;
  188. }
  189. foreach (object obj in iEvents)
  190. {
  191. try
  192. {
  193. var iEvent = obj as IEvent<A, B, C, D, E, F>;
  194. if (iEvent == null)
  195. {
  196. throw new Exception(
  197. $"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}, {typeof (E).Name}>");
  198. }
  199. iEvent.Run(a, b, c, d, e, f);
  200. }
  201. catch (Exception err)
  202. {
  203. Log.Error(err.ToString());
  204. }
  205. }
  206. }
  207. }
  208. }