EventComponent.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using System;
  2. using System.Collections.Generic;
  3. using ILRuntime.CLR.Method;
  4. using ILRuntime.Runtime.Intepreter;
  5. namespace Model
  6. {
  7. public interface IEventMethod
  8. {
  9. void Run();
  10. void Run<A>(A a);
  11. void Run<A, B>(A a, B b);
  12. void Run<A, B, C>(A a, B b, C c);
  13. void Run<A, B, C, D>(A a, B b, C c, D d);
  14. }
  15. public class IEventMonoMethod : IEventMethod
  16. {
  17. private readonly object obj;
  18. public IEventMonoMethod(object obj)
  19. {
  20. this.obj = obj;
  21. }
  22. public void Run()
  23. {
  24. ((IEvent)obj).Run();
  25. }
  26. public void Run<A>(A a)
  27. {
  28. ((IEvent<A>)obj).Run(a);
  29. }
  30. public void Run<A, B>(A a, B b)
  31. {
  32. ((IEvent<A, B>)obj).Run(a, b);
  33. }
  34. public void Run<A, B, C>(A a, B b, C c)
  35. {
  36. ((IEvent<A, B, C>)obj).Run(a, b, c);
  37. }
  38. public void Run<A, B, C, D>(A a, B b, C c, D d)
  39. {
  40. ((IEvent<A, B, C, D>)obj).Run(a, b, c, d);
  41. }
  42. }
  43. public class IEventILMethod : IEventMethod
  44. {
  45. private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain;
  46. private readonly ILTypeInstance instance;
  47. private readonly IMethod method;
  48. private readonly object[] param;
  49. public IEventILMethod(Type type, string methodName)
  50. {
  51. appDomain = Init.Instance.AppDomain;
  52. this.instance = this.appDomain.Instantiate(type.FullName);
  53. this.method = this.instance.Type.GetMethod(methodName);
  54. int n = this.method.ParameterCount;
  55. this.param = new object[n];
  56. }
  57. public void Run()
  58. {
  59. this.appDomain.Invoke(this.method, this.instance, param);
  60. }
  61. public void Run<A>(A a)
  62. {
  63. this.param[0] = a;
  64. this.appDomain.Invoke(this.method, this.instance, param);
  65. }
  66. public void Run<A, B>(A a, B b)
  67. {
  68. this.param[0] = a;
  69. this.param[1] = b;
  70. this.appDomain.Invoke(this.method, this.instance, param);
  71. }
  72. public void Run<A, B, C>(A a, B b, C c)
  73. {
  74. this.param[0] = a;
  75. this.param[1] = b;
  76. this.param[2] = c;
  77. this.appDomain.Invoke(this.method, this.instance, param);
  78. }
  79. public void Run<A, B, C, D>(A a, B b, C c, D d)
  80. {
  81. this.param[0] = a;
  82. this.param[1] = b;
  83. this.param[2] = c;
  84. this.param[3] = d;
  85. this.appDomain.Invoke(this.method, this.instance, param);
  86. }
  87. }
  88. [ObjectEvent]
  89. public class EventComponentEvent : ObjectEvent<EventComponent>, IAwake, ILoad
  90. {
  91. public void Awake()
  92. {
  93. this.Get().Awake();
  94. }
  95. public void Load()
  96. {
  97. this.Get().Load();
  98. }
  99. }
  100. public class EventComponent : Component
  101. {
  102. public static EventComponent Instance;
  103. private Dictionary<EventIdType, List<IEventMethod>> allEvents;
  104. public void Awake()
  105. {
  106. Instance = this;
  107. this.Load();
  108. }
  109. public void Load()
  110. {
  111. this.allEvents = new Dictionary<EventIdType, List<IEventMethod>>();
  112. Type[] types = DllHelper.GetMonoTypes();
  113. foreach (Type type in types)
  114. {
  115. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  116. foreach (object attr in attrs)
  117. {
  118. EventAttribute aEventAttribute = (EventAttribute)attr;
  119. object obj = Activator.CreateInstance(type);
  120. if (!this.allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
  121. {
  122. this.allEvents.Add((EventIdType)aEventAttribute.Type, new List<IEventMethod>());
  123. }
  124. this.allEvents[(EventIdType)aEventAttribute.Type].Add(new IEventMonoMethod(obj));
  125. }
  126. }
  127. // hotfix dll
  128. Type[] hotfixTypes = DllHelper.GetHotfixTypes();
  129. foreach (Type type in hotfixTypes)
  130. {
  131. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  132. foreach (object attr in attrs)
  133. {
  134. EventAttribute aEventAttribute = (EventAttribute)attr;
  135. #if ILRuntime
  136. IEventMethod method = new IEventILMethod(type, "Run");
  137. #else
  138. object obj = Activator.CreateInstance(type);
  139. IEventMethod method = new IEventMonoMethod(obj);
  140. #endif
  141. if (!allEvents.ContainsKey((EventIdType)aEventAttribute.Type))
  142. {
  143. allEvents.Add((EventIdType)aEventAttribute.Type, new List<IEventMethod>());
  144. }
  145. allEvents[(EventIdType)aEventAttribute.Type].Add(method);
  146. }
  147. }
  148. }
  149. public void Run(EventIdType type)
  150. {
  151. List<IEventMethod> iEvents;
  152. if (!this.allEvents.TryGetValue(type, out iEvents))
  153. {
  154. return;
  155. }
  156. foreach (IEventMethod iEvent in iEvents)
  157. {
  158. try
  159. {
  160. iEvent.Run();
  161. }
  162. catch (Exception e)
  163. {
  164. Log.Error(e.ToString());
  165. }
  166. }
  167. }
  168. public void Run<A>(EventIdType type, A a)
  169. {
  170. List<IEventMethod> iEvents;
  171. if (!this.allEvents.TryGetValue(type, out iEvents))
  172. {
  173. return;
  174. }
  175. foreach (IEventMethod iEvent in iEvents)
  176. {
  177. try
  178. {
  179. iEvent.Run(a);
  180. }
  181. catch (Exception e)
  182. {
  183. Log.Error(e.ToString());
  184. }
  185. }
  186. }
  187. public void Run<A, B>(EventIdType type, A a, B b)
  188. {
  189. List<IEventMethod> iEvents;
  190. if (!this.allEvents.TryGetValue(type, out iEvents))
  191. {
  192. return;
  193. }
  194. foreach (IEventMethod iEvent in iEvents)
  195. {
  196. try
  197. {
  198. iEvent.Run(a, b);
  199. }
  200. catch (Exception e)
  201. {
  202. Log.Error(e.ToString());
  203. }
  204. }
  205. }
  206. public void Run<A, B, C>(EventIdType type, A a, B b, C c)
  207. {
  208. List<IEventMethod> iEvents;
  209. if (!this.allEvents.TryGetValue(type, out iEvents))
  210. {
  211. return;
  212. }
  213. foreach (IEventMethod iEvent in iEvents)
  214. {
  215. try
  216. {
  217. iEvent.Run(a, b, c);
  218. }
  219. catch (Exception e)
  220. {
  221. Log.Error(e.ToString());
  222. }
  223. }
  224. }
  225. }
  226. }