EventComponent.cs 5.1 KB

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