EventComponent.cs 5.2 KB

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