EventComponent.cs 5.2 KB

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