EventComponent.cs 4.6 KB

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