EventSystem.cs 13 KB

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