EventSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. using System;
  2. using System.Collections.Generic;
  3. using ETModel;
  4. namespace ETHotfix
  5. {
  6. public sealed class EventSystem
  7. {
  8. private readonly Dictionary<long, Component> allComponents = new Dictionary<long, Component>();
  9. private readonly Dictionary<string, List<IEvent>> allEvents = new Dictionary<string, List<IEvent>>();
  10. private readonly UnOrderMultiMap<Type, AAwakeSystem> awakeEvents = new UnOrderMultiMap<Type, AAwakeSystem>();
  11. private readonly UnOrderMultiMap<Type, AStartSystem> startEvents = new UnOrderMultiMap<Type, AStartSystem>();
  12. private readonly UnOrderMultiMap<Type, ALoadSystem> loadEvents = new UnOrderMultiMap<Type, ALoadSystem>();
  13. private readonly UnOrderMultiMap<Type, AUpdateSystem> updateEvents = new UnOrderMultiMap<Type, AUpdateSystem>();
  14. private readonly UnOrderMultiMap<Type, ALateUpdateSystem> lateUpdateEvents = new UnOrderMultiMap<Type, ALateUpdateSystem>();
  15. private Queue<long> updates = new Queue<long>();
  16. private Queue<long> updates2 = new Queue<long>();
  17. private readonly Queue<long> starts = new Queue<long>();
  18. private Queue<long> loaders = new Queue<long>();
  19. private Queue<long> loaders2 = new Queue<long>();
  20. private Queue<long> lateUpdates = new Queue<long>();
  21. private Queue<long> lateUpdates2 = new Queue<long>();
  22. public EventSystem()
  23. {
  24. Type[] types = ETModel.Game.Hotfix.GetHotfixTypes();
  25. foreach (Type type in types)
  26. {
  27. object[] attrs = type.GetCustomAttributes(typeof(ObjectSystemAttribute), false);
  28. if (attrs.Length == 0)
  29. {
  30. continue;
  31. }
  32. object obj = Activator.CreateInstance(type);
  33. AAwakeSystem objectSystem = obj as AAwakeSystem;
  34. if (objectSystem != null)
  35. {
  36. this.awakeEvents.Add(objectSystem.Type(), objectSystem);
  37. }
  38. AUpdateSystem aUpdateSystem = obj as AUpdateSystem;
  39. if (aUpdateSystem != null)
  40. {
  41. this.updateEvents.Add(aUpdateSystem.Type(), aUpdateSystem);
  42. }
  43. ALateUpdateSystem aLateUpdateSystem = obj as ALateUpdateSystem;
  44. if (aLateUpdateSystem != null)
  45. {
  46. this.lateUpdateEvents.Add(aLateUpdateSystem.Type(), aLateUpdateSystem);
  47. }
  48. AStartSystem aStartSystem = obj as AStartSystem;
  49. if (aStartSystem != null)
  50. {
  51. this.startEvents.Add(aStartSystem.Type(), aStartSystem);
  52. }
  53. ALoadSystem aLoadSystem = obj as ALoadSystem;
  54. if (aLoadSystem != null)
  55. {
  56. this.loadEvents.Add(aLoadSystem.Type(), aLoadSystem);
  57. }
  58. }
  59. this.allEvents.Clear();
  60. foreach (Type type in types)
  61. {
  62. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  63. foreach (object attr in attrs)
  64. {
  65. EventAttribute aEventAttribute = (EventAttribute)attr;
  66. object obj = Activator.CreateInstance(type);
  67. IEvent iEvent = obj as IEvent;
  68. if (iEvent == null)
  69. {
  70. Log.Error($"{obj.GetType().Name} 没有继承IEvent");
  71. }
  72. this.RegisterEvent(aEventAttribute.Type, iEvent);
  73. // hotfix的事件也要注册到mono层,hotfix可以订阅mono层的事件
  74. Action<List<object>> action = list => { Handle(aEventAttribute.Type, list); };
  75. ETModel.Game.EventSystem.RegisterEvent(aEventAttribute.Type, new EventProxy(action));
  76. }
  77. }
  78. this.Load();
  79. }
  80. public static void Handle(string type, List<object> param)
  81. {
  82. switch (param.Count)
  83. {
  84. case 0:
  85. Game.EventSystem.Run(type);
  86. break;
  87. case 1:
  88. Game.EventSystem.Run(type, param[0]);
  89. break;
  90. case 2:
  91. Game.EventSystem.Run(type, param[0], param[1]);
  92. break;
  93. case 3:
  94. Game.EventSystem.Run(type, param[0], param[1], param[2]);
  95. break;
  96. }
  97. }
  98. public void RegisterEvent(string eventId, IEvent e)
  99. {
  100. if (!this.allEvents.ContainsKey(eventId))
  101. {
  102. this.allEvents.Add(eventId, new List<IEvent>());
  103. }
  104. this.allEvents[eventId].Add(e);
  105. }
  106. public void Add(Component component)
  107. {
  108. this.allComponents.Add(component.InstanceId, component);
  109. Type type = component.GetType();
  110. if (this.loadEvents.ContainsKey(type))
  111. {
  112. this.loaders.Enqueue(component.InstanceId);
  113. }
  114. if (this.updateEvents.ContainsKey(type))
  115. {
  116. this.updates.Enqueue(component.InstanceId);
  117. }
  118. if (this.startEvents.ContainsKey(type))
  119. {
  120. this.starts.Enqueue(component.InstanceId);
  121. }
  122. if (this.lateUpdateEvents.ContainsKey(type))
  123. {
  124. this.lateUpdates.Enqueue(component.InstanceId);
  125. }
  126. }
  127. public void Remove(long instanceId)
  128. {
  129. this.allComponents.Remove(instanceId);
  130. }
  131. public void Awake(Component component)
  132. {
  133. List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
  134. if (iAwakeSystems == null)
  135. {
  136. return;
  137. }
  138. foreach (AAwakeSystem aAwakeSystem in iAwakeSystems)
  139. {
  140. if (aAwakeSystem == null)
  141. {
  142. continue;
  143. }
  144. IAwake iAwake = aAwakeSystem as IAwake;
  145. if (iAwake == null)
  146. {
  147. continue;
  148. }
  149. iAwake.Run(component);
  150. }
  151. }
  152. public void Awake<P1>(Component component, P1 p1)
  153. {
  154. List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
  155. if (iAwakeSystems == null)
  156. {
  157. return;
  158. }
  159. foreach (AAwakeSystem aAwakeSystem in iAwakeSystems)
  160. {
  161. if (aAwakeSystem == null)
  162. {
  163. continue;
  164. }
  165. IAwake<P1> iAwake = aAwakeSystem as IAwake<P1>;
  166. if (iAwake == null)
  167. {
  168. continue;
  169. }
  170. iAwake.Run(component, p1);
  171. }
  172. }
  173. public void Awake<P1, P2>(Component component, P1 p1, P2 p2)
  174. {
  175. List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
  176. if (iAwakeSystems == null)
  177. {
  178. return;
  179. }
  180. foreach (AAwakeSystem aAwakeSystem in iAwakeSystems)
  181. {
  182. if (aAwakeSystem == null)
  183. {
  184. continue;
  185. }
  186. IAwake<P1, P2> iAwake = aAwakeSystem as IAwake<P1, P2>;
  187. if (iAwake == null)
  188. {
  189. continue;
  190. }
  191. iAwake.Run(component, p1, p2);
  192. }
  193. }
  194. public void Awake<P1, P2, P3>(Component component, P1 p1, P2 p2, P3 p3)
  195. {
  196. List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
  197. if (iAwakeSystems == null)
  198. {
  199. return;
  200. }
  201. foreach (AAwakeSystem aAwakeSystem in iAwakeSystems)
  202. {
  203. if (aAwakeSystem == null)
  204. {
  205. continue;
  206. }
  207. IAwake<P1, P2, P3> iAwake = aAwakeSystem as IAwake<P1, P2, P3>;
  208. if (iAwake == null)
  209. {
  210. continue;
  211. }
  212. iAwake.Run(component, p1, p2, p3);
  213. }
  214. }
  215. public void Load()
  216. {
  217. while (this.loaders.Count > 0)
  218. {
  219. long instanceId = this.loaders.Dequeue();
  220. Component component;
  221. if (!this.allComponents.TryGetValue(instanceId, out component))
  222. {
  223. continue;
  224. }
  225. if (component.IsDisposed)
  226. {
  227. continue;
  228. }
  229. List<ALoadSystem> aLoadSystems = this.loadEvents[component.GetType()];
  230. if (aLoadSystems == null)
  231. {
  232. continue;
  233. }
  234. this.loaders2.Enqueue(instanceId);
  235. foreach (ALoadSystem aLoadSystem in aLoadSystems)
  236. {
  237. try
  238. {
  239. aLoadSystem.Run(component);
  240. }
  241. catch (Exception e)
  242. {
  243. Log.Error(e);
  244. }
  245. }
  246. }
  247. ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
  248. }
  249. private void Start()
  250. {
  251. while (this.starts.Count > 0)
  252. {
  253. long instanceId = this.starts.Dequeue();
  254. Component component;
  255. if (!this.allComponents.TryGetValue(instanceId, out component))
  256. {
  257. continue;
  258. }
  259. List<AStartSystem> aStartSystems = this.startEvents[component.GetType()];
  260. if (aStartSystems == null)
  261. {
  262. continue;
  263. }
  264. foreach (AStartSystem aStartSystem in aStartSystems)
  265. {
  266. try
  267. {
  268. aStartSystem.Run(component);
  269. }
  270. catch (Exception e)
  271. {
  272. Log.Error(e);
  273. }
  274. }
  275. }
  276. }
  277. public void Update()
  278. {
  279. this.Start();
  280. while (this.updates.Count > 0)
  281. {
  282. long instanceId = this.updates.Dequeue();
  283. Component component;
  284. if (!this.allComponents.TryGetValue(instanceId, out component))
  285. {
  286. continue;
  287. }
  288. if (component.IsDisposed)
  289. {
  290. continue;
  291. }
  292. List<AUpdateSystem> aUpdateSystems = this.updateEvents[component.GetType()];
  293. if (aUpdateSystems == null)
  294. {
  295. continue;
  296. }
  297. this.updates2.Enqueue(instanceId);
  298. foreach (AUpdateSystem aUpdateSystem in aUpdateSystems)
  299. {
  300. try
  301. {
  302. aUpdateSystem.Run(component);
  303. }
  304. catch (Exception e)
  305. {
  306. Log.Error(e);
  307. }
  308. }
  309. }
  310. ObjectHelper.Swap(ref this.updates, ref this.updates2);
  311. }
  312. public void LateUpdate()
  313. {
  314. while (this.lateUpdates.Count > 0)
  315. {
  316. long instanceId = this.lateUpdates.Dequeue();
  317. Component component;
  318. if (!this.allComponents.TryGetValue(instanceId, out component))
  319. {
  320. continue;
  321. }
  322. if (component.IsDisposed)
  323. {
  324. continue;
  325. }
  326. List<ALateUpdateSystem> aLateUpdateSystems = this.lateUpdateEvents[component.GetType()];
  327. if (aLateUpdateSystems == null)
  328. {
  329. continue;
  330. }
  331. this.lateUpdates2.Enqueue(instanceId);
  332. foreach (ALateUpdateSystem aLateUpdateSystem in aLateUpdateSystems)
  333. {
  334. try
  335. {
  336. aLateUpdateSystem.Run(component);
  337. }
  338. catch (Exception e)
  339. {
  340. Log.Error(e);
  341. }
  342. }
  343. }
  344. ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
  345. }
  346. public void Run(string type)
  347. {
  348. List<IEvent> iEvents;
  349. if (!this.allEvents.TryGetValue(type, out iEvents))
  350. {
  351. return;
  352. }
  353. foreach (IEvent iEvent in iEvents)
  354. {
  355. try
  356. {
  357. iEvent?.Handle();
  358. }
  359. catch (Exception e)
  360. {
  361. Log.Error(e);
  362. }
  363. }
  364. }
  365. public void Run<A>(string type, A a)
  366. {
  367. List<IEvent> iEvents;
  368. if (!this.allEvents.TryGetValue(type, out iEvents))
  369. {
  370. return;
  371. }
  372. foreach (IEvent iEvent in iEvents)
  373. {
  374. try
  375. {
  376. iEvent?.Handle(a);
  377. }
  378. catch (Exception e)
  379. {
  380. Log.Error(e);
  381. }
  382. }
  383. }
  384. public void Run<A, B>(string type, A a, B b)
  385. {
  386. List<IEvent> iEvents;
  387. if (!this.allEvents.TryGetValue(type, out iEvents))
  388. {
  389. return;
  390. }
  391. foreach (IEvent iEvent in iEvents)
  392. {
  393. try
  394. {
  395. iEvent?.Handle(a, b);
  396. }
  397. catch (Exception e)
  398. {
  399. Log.Error(e);
  400. }
  401. }
  402. }
  403. public void Run<A, B, C>(string type, A a, B b, C c)
  404. {
  405. List<IEvent> iEvents;
  406. if (!this.allEvents.TryGetValue(type, out iEvents))
  407. {
  408. return;
  409. }
  410. foreach (IEvent iEvent in iEvents)
  411. {
  412. try
  413. {
  414. iEvent?.Handle(a, b, c);
  415. }
  416. catch (Exception e)
  417. {
  418. Log.Error(e);
  419. }
  420. }
  421. }
  422. }
  423. }