EventSystem.cs 13 KB

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