EventSystem.cs 15 KB

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