EventSystem.cs 11 KB

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