EventComponent.cs 4.6 KB

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