EventSystem.cs 13 KB

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