EventComponent.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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($"event not inherit IEvent or IEventAsync interface: {obj.GetType().FullName}");
  34. }
  35. if (!this.allEvents.ContainsKey(aEventAttribute.Type))
  36. {
  37. this.allEvents.Add(aEventAttribute.Type, new List<object>());
  38. }
  39. this.allEvents[aEventAttribute.Type].Add(obj);
  40. }
  41. }
  42. public async Task RunAsync(EventType type)
  43. {
  44. List<object> iEvents = null;
  45. if (!this.allEvents.TryGetValue(type, out iEvents))
  46. {
  47. return;
  48. }
  49. foreach (object obj in iEvents)
  50. {
  51. try
  52. {
  53. AEvent iEvent = obj as AEvent;
  54. if (iEvent == null)
  55. {
  56. throw new GameException($"event type: {type} is not IEvent");
  57. }
  58. iEvent.Run();
  59. await iEvent.RunAsync();
  60. }
  61. catch (Exception err)
  62. {
  63. Log.Debug(err.ToString());
  64. }
  65. }
  66. }
  67. public async Task RunAsync<A>(EventType type, A a)
  68. {
  69. List<object> iEvents = null;
  70. if (!this.allEvents.TryGetValue(type, out iEvents))
  71. {
  72. return;
  73. }
  74. foreach (object obj in iEvents)
  75. {
  76. try
  77. {
  78. AEvent<A> iEvent = obj as AEvent<A>;
  79. if (iEvent == null)
  80. {
  81. throw new GameException($"event type: {type} is not IEvent<{typeof (A).Name}>");
  82. }
  83. iEvent.Run(a);
  84. await iEvent.RunAsync(a);
  85. }
  86. catch (Exception err)
  87. {
  88. Log.Debug(err.ToString());
  89. }
  90. }
  91. }
  92. public async Task RunAsync<A, B>(EventType type, A a, B b)
  93. {
  94. List<object> iEvents = null;
  95. if (!this.allEvents.TryGetValue(type, out iEvents))
  96. {
  97. return;
  98. }
  99. foreach (object obj in iEvents)
  100. {
  101. try
  102. {
  103. AEvent<A, B> iEvent = obj as AEvent<A, B>;
  104. if (iEvent == null)
  105. {
  106. throw new GameException($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}>");
  107. }
  108. iEvent.Run(a, b);
  109. await iEvent.RunAsync(a, b);
  110. }
  111. catch (Exception err)
  112. {
  113. Log.Debug(err.ToString());
  114. }
  115. }
  116. }
  117. public async Task RunAsync<A, B, C>(EventType type, A a, B b, C c)
  118. {
  119. List<object> iEvents = null;
  120. if (!this.allEvents.TryGetValue(type, out iEvents))
  121. {
  122. return;
  123. }
  124. foreach (object obj in iEvents)
  125. {
  126. try
  127. {
  128. AEvent<A, B, C> iEvent = obj as AEvent<A, B, C>;
  129. if (iEvent == null)
  130. {
  131. throw new GameException($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}>");
  132. }
  133. iEvent.Run(a, b, c);
  134. await iEvent.RunAsync(a, b, c);
  135. }
  136. catch (Exception err)
  137. {
  138. Log.Debug(err.ToString());
  139. }
  140. }
  141. }
  142. public async Task RunAsync<A, B, C, D>(EventType type, A a, B b, C c, D d)
  143. {
  144. List<object> iEvents = null;
  145. if (!this.allEvents.TryGetValue(type, out iEvents))
  146. {
  147. return;
  148. }
  149. foreach (object obj in iEvents)
  150. {
  151. try
  152. {
  153. AEvent<A, B, C, D> iEvent = obj as AEvent<A, B, C, D>;
  154. if (iEvent == null)
  155. {
  156. throw new GameException($"event type: {type} is not IEvent<{typeof (A).Name}, {typeof (B).Name}, {typeof (C).Name}, {typeof (D).Name}>");
  157. }
  158. iEvent.Run(a, b, c, d);
  159. await iEvent.RunAsync(a, b, c, d);
  160. }
  161. catch (Exception err)
  162. {
  163. Log.Debug(err.ToString());
  164. }
  165. }
  166. }
  167. public async Task RunAsync<A, B, C, D, E>(EventType 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. AEvent<A, B, C, D, E> iEvent = obj as AEvent<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. await iEvent.RunAsync(a, b, c, d, e);
  186. }
  187. catch (Exception err)
  188. {
  189. Log.Debug(err.ToString());
  190. }
  191. }
  192. }
  193. }
  194. }