EventComponent.cs 5.2 KB

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