EventSystem.cs 12 KB

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