EventComponent.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Common.Base;
  6. using Common.Logger;
  7. namespace Model
  8. {
  9. public class EventComponent<AttributeType>: Component<World>, IAssemblyLoader
  10. where AttributeType : AEventAttribute
  11. {
  12. private Dictionary<EventType, List<object>> allEvents;
  13. public void Load(Assembly assembly)
  14. {
  15. this.allEvents = new Dictionary<EventType, List<object>>();
  16. ServerType serverType = World.Instance.Options.ServerType;
  17. Type[] types = assembly.GetTypes();
  18. foreach (Type t in types)
  19. {
  20. object[] attrs = t.GetCustomAttributes(typeof (AttributeType), false);
  21. if (attrs.Length == 0)
  22. {
  23. continue;
  24. }
  25. AEventAttribute aEventAttribute = (AEventAttribute) attrs[0];
  26. if (!aEventAttribute.Contains(serverType))
  27. {
  28. continue;
  29. }
  30. object obj = Activator.CreateInstance(t);
  31. if (obj == null)
  32. {
  33. throw new Exception(string.Format("event not inherit IEvent or IEventAsync interface: {0}",
  34. obj.GetType().FullName));
  35. }
  36. if (!this.allEvents.ContainsKey(aEventAttribute.Type))
  37. {
  38. this.allEvents.Add(aEventAttribute.Type, new List<object>());
  39. }
  40. this.allEvents[aEventAttribute.Type].Add(obj);
  41. }
  42. }
  43. public async Task RunAsync(EventType type)
  44. {
  45. List<object> iEvents = null;
  46. if (!this.allEvents.TryGetValue(type, out iEvents))
  47. {
  48. return;
  49. }
  50. foreach (object obj in iEvents)
  51. {
  52. try
  53. {
  54. AEvent iEvent = obj as AEvent;
  55. if (iEvent == null)
  56. {
  57. throw new GameException(string.Format("event type: {0} is not IEvent", type));
  58. }
  59. iEvent.Run();
  60. await iEvent.RunAsync();
  61. }
  62. catch (Exception err)
  63. {
  64. Log.Debug(err.ToString());
  65. }
  66. }
  67. }
  68. public async Task RunAsync<A>(EventType type, A a)
  69. {
  70. List<object> iEvents = null;
  71. if (!this.allEvents.TryGetValue(type, out iEvents))
  72. {
  73. return;
  74. }
  75. foreach (object obj in iEvents)
  76. {
  77. try
  78. {
  79. AEvent<A> iEvent = obj as AEvent<A>;
  80. if (iEvent == null)
  81. {
  82. throw new GameException(string.Format("event type: {0} is not IEvent<{1}>", type, typeof(A).Name));
  83. }
  84. iEvent.Run(a);
  85. await iEvent.RunAsync(a);
  86. }
  87. catch (Exception err)
  88. {
  89. Log.Debug(err.ToString());
  90. }
  91. }
  92. }
  93. public async Task RunAsync<A, B>(EventType type, A a, B b)
  94. {
  95. List<object> iEvents = null;
  96. if (!this.allEvents.TryGetValue(type, out iEvents))
  97. {
  98. return;
  99. }
  100. foreach (object obj in iEvents)
  101. {
  102. try
  103. {
  104. AEvent<A, B> iEvent = obj as AEvent<A, B>;
  105. if (iEvent == null)
  106. {
  107. throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}>", type, typeof(A).Name, typeof(B).Name));
  108. }
  109. iEvent.Run(a, b);
  110. await iEvent.RunAsync(a, b);
  111. }
  112. catch (Exception err)
  113. {
  114. Log.Debug(err.ToString());
  115. }
  116. }
  117. }
  118. public async Task RunAsync<A, B, C>(EventType type, A a, B b, C c)
  119. {
  120. List<object> iEvents = null;
  121. if (!this.allEvents.TryGetValue(type, out iEvents))
  122. {
  123. return;
  124. }
  125. foreach (object obj in iEvents)
  126. {
  127. try
  128. {
  129. AEvent<A, B, C> iEvent = obj as AEvent<A, B, C>;
  130. if (iEvent == null)
  131. {
  132. throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}, {3}>", type,
  133. typeof(A).Name, typeof(B).Name, typeof(C).Name));
  134. }
  135. iEvent.Run(a, b, c);
  136. await iEvent.RunAsync(a, b, c);
  137. }
  138. catch (Exception err)
  139. {
  140. Log.Debug(err.ToString());
  141. }
  142. }
  143. }
  144. public async Task RunAsync<A, B, C, D>(EventType 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. AEvent<A, B, C, D> iEvent = obj as AEvent<A, B, C, D>;
  156. if (iEvent == null)
  157. {
  158. throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}, {3}, {4}>", type,
  159. typeof(A).Name, typeof(B).Name, typeof(C).Name, typeof(D).Name));
  160. }
  161. iEvent.Run(a, b, c, d);
  162. await iEvent.RunAsync(a, b, c, d);
  163. }
  164. catch (Exception err)
  165. {
  166. Log.Debug(err.ToString());
  167. }
  168. }
  169. }
  170. public async Task RunAsync<A, B, C, D, E>(EventType type, A a, B b, C c, D d, E e)
  171. {
  172. List<object> iEvents = null;
  173. if (!this.allEvents.TryGetValue(type, out iEvents))
  174. {
  175. return;
  176. }
  177. foreach (object obj in iEvents)
  178. {
  179. try
  180. {
  181. AEvent<A, B, C, D, E> iEvent = obj as AEvent<A, B, C, D, E>;
  182. if (iEvent == null)
  183. {
  184. throw new GameException(string.Format("event type: {0} is not IEvent<{1}, {2}, {3}, {4}, {5}>", type,
  185. typeof(A).Name, typeof(B).Name, typeof(C).Name, typeof(D).Name, typeof(E).Name));
  186. }
  187. iEvent.Run(a, b, c, d, e);
  188. await iEvent.RunAsync(a, b, c, d, e);
  189. }
  190. catch (Exception err)
  191. {
  192. Log.Debug(err.ToString());
  193. }
  194. }
  195. }
  196. }
  197. }