EventComponent.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Collections.Generic;
  3. using Base;
  4. namespace Model
  5. {
  6. [ObjectEvent]
  7. public class EventComponentEvent : ObjectEvent<EventComponent>, IAwake, ILoad
  8. {
  9. public void Awake()
  10. {
  11. this.Get().Awake();
  12. }
  13. public void Load()
  14. {
  15. this.Get().Load();
  16. }
  17. }
  18. /// <summary>
  19. /// 事件分发
  20. /// </summary>
  21. public class EventComponent: Component
  22. {
  23. private Dictionary<int, List<object>> allEvents;
  24. public void Awake()
  25. {
  26. this.Load();
  27. }
  28. public void Load()
  29. {
  30. this.allEvents = new Dictionary<int, List<object>>();
  31. Type[] types = DllHelper.GetMonoTypes();
  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. public void Run(int type)
  48. {
  49. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  50. {
  51. return;
  52. }
  53. foreach (object obj in iEvents)
  54. {
  55. try
  56. {
  57. IEvent iEvent = obj as IEvent;
  58. if (iEvent == null)
  59. {
  60. throw new Exception($"event type: {type} is not IEvent");
  61. }
  62. iEvent.Run();
  63. }
  64. catch (Exception err)
  65. {
  66. Log.Error(err.ToString());
  67. }
  68. }
  69. }
  70. public void Run<A>(int type, A a)
  71. {
  72. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  73. {
  74. return;
  75. }
  76. foreach (object obj in iEvents)
  77. {
  78. try
  79. {
  80. var iEvent = obj as IEvent<A>;
  81. if (iEvent == null)
  82. {
  83. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}>");
  84. }
  85. iEvent.Run(a);
  86. }
  87. catch (Exception err)
  88. {
  89. Log.Error(err.ToString());
  90. }
  91. }
  92. }
  93. public void Run<A, B>(int type, A a, B b)
  94. {
  95. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  96. {
  97. return;
  98. }
  99. foreach (object obj in iEvents)
  100. {
  101. try
  102. {
  103. var iEvent = obj as IEvent<A, B>;
  104. if (iEvent == null)
  105. {
  106. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}>");
  107. }
  108. iEvent.Run(a, b);
  109. }
  110. catch (Exception err)
  111. {
  112. Log.Error(err.ToString());
  113. }
  114. }
  115. }
  116. public void Run<A, B, C>(int type, A a, B b, C c)
  117. {
  118. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  119. {
  120. return;
  121. }
  122. foreach (object obj in iEvents)
  123. {
  124. try
  125. {
  126. var iEvent = obj as IEvent<A, B, C>;
  127. if (iEvent == null)
  128. {
  129. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}>");
  130. }
  131. iEvent.Run(a, b, c);
  132. }
  133. catch (Exception err)
  134. {
  135. Log.Error(err.ToString());
  136. }
  137. }
  138. }
  139. public void Run<A, B, C, D>(int type, A a, B b, C c, D d)
  140. {
  141. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  142. {
  143. return;
  144. }
  145. foreach (object obj in iEvents)
  146. {
  147. try
  148. {
  149. var iEvent = obj as IEvent<A, B, C, D>;
  150. if (iEvent == null)
  151. {
  152. throw new Exception($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}>");
  153. }
  154. iEvent.Run(a, b, c, d);
  155. }
  156. catch (Exception err)
  157. {
  158. Log.Error(err.ToString());
  159. }
  160. }
  161. }
  162. public void Run<A, B, C, D, E>(int type, A a, B b, C c, D d, E e)
  163. {
  164. if (!this.allEvents.TryGetValue(type, out List<object> 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>(int type, A a, B b, C c, D d, E e, F f)
  187. {
  188. if (!this.allEvents.TryGetValue(type, out List<object> iEvents))
  189. {
  190. return;
  191. }
  192. foreach (object obj in iEvents)
  193. {
  194. try
  195. {
  196. var iEvent = obj as IEvent<A, B, C, D, E, F>;
  197. if (iEvent == null)
  198. {
  199. throw new Exception(
  200. $"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}, {typeof (E).Name}>");
  201. }
  202. iEvent.Run(a, b, c, d, e, f);
  203. }
  204. catch (Exception err)
  205. {
  206. Log.Error(err.ToString());
  207. }
  208. }
  209. }
  210. }
  211. }