EventSystem.cs 12 KB

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