EventSystem.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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<string, List<object>> allEvents = new Dictionary<string, 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[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  118. foreach (object attr in attrs)
  119. {
  120. EventAttribute aEventAttribute = (EventAttribute)attr;
  121. object obj = Activator.CreateInstance(type);
  122. IEvent iEvent = obj as IEvent;
  123. if (iEvent == null)
  124. {
  125. Log.Error($"{obj.GetType().Name} 没有继承IEvent");
  126. }
  127. this.RegisterEvent(aEventAttribute.Type, iEvent);
  128. }
  129. }
  130. this.Load();
  131. }
  132. public Assembly GetAssembly(string name)
  133. {
  134. return this.assemblies[name];
  135. }
  136. public void RegisterEvent(string eventId, IEvent e)
  137. {
  138. if (!this.allEvents.ContainsKey(eventId))
  139. {
  140. this.allEvents.Add(eventId, new List<object>());
  141. }
  142. this.allEvents[eventId].Add(e);
  143. }
  144. public HashSet<Type> GetTypes(Type systemAttributeType)
  145. {
  146. if (!this.types.ContainsKey(systemAttributeType))
  147. {
  148. return new HashSet<Type>();
  149. }
  150. return this.types[systemAttributeType];
  151. }
  152. public List<Type> GetTypes()
  153. {
  154. List<Type> allTypes = new List<Type>();
  155. foreach (Assembly assembly in this.assemblies.Values)
  156. {
  157. allTypes.AddRange(assembly.GetTypes());
  158. }
  159. return allTypes;
  160. }
  161. public void RegisterSystem(Entity component, bool isRegister = true)
  162. {
  163. if (!isRegister)
  164. {
  165. this.Remove(component.InstanceId);
  166. return;
  167. }
  168. this.allComponents.Add(component.InstanceId, component);
  169. Type type = component.GetType();
  170. if (this.loadSystems.ContainsKey(type))
  171. {
  172. this.loaders.Enqueue(component.InstanceId);
  173. }
  174. if (this.updateSystems.ContainsKey(type))
  175. {
  176. this.updates.Enqueue(component.InstanceId);
  177. }
  178. if (this.startSystems.ContainsKey(type))
  179. {
  180. this.starts.Enqueue(component.InstanceId);
  181. }
  182. if (this.lateUpdateSystems.ContainsKey(type))
  183. {
  184. this.lateUpdates.Enqueue(component.InstanceId);
  185. }
  186. }
  187. public void Remove(long instanceId)
  188. {
  189. this.allComponents.Remove(instanceId);
  190. }
  191. public Entity Get(long instanceId)
  192. {
  193. Entity component = null;
  194. this.allComponents.TryGetValue(instanceId, out component);
  195. return component;
  196. }
  197. public bool IsRegister(long instanceId)
  198. {
  199. return this.allComponents.ContainsKey(instanceId);
  200. }
  201. public void Deserialize(Entity component)
  202. {
  203. List<IDeserializeSystem> iDeserializeSystems = this.deserializeSystems[component.GetType()];
  204. if (iDeserializeSystems == null)
  205. {
  206. return;
  207. }
  208. foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
  209. {
  210. if (deserializeSystem == null)
  211. {
  212. continue;
  213. }
  214. try
  215. {
  216. deserializeSystem.Run(component);
  217. }
  218. catch (Exception e)
  219. {
  220. Log.Error(e);
  221. }
  222. }
  223. }
  224. public void Awake(Entity component)
  225. {
  226. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  227. if (iAwakeSystems == null)
  228. {
  229. return;
  230. }
  231. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  232. {
  233. if (aAwakeSystem == null)
  234. {
  235. continue;
  236. }
  237. IAwake iAwake = aAwakeSystem as IAwake;
  238. if (iAwake == null)
  239. {
  240. continue;
  241. }
  242. try
  243. {
  244. iAwake.Run(component);
  245. }
  246. catch (Exception e)
  247. {
  248. Log.Error(e);
  249. }
  250. }
  251. }
  252. public void Awake<P1>(Entity component, P1 p1)
  253. {
  254. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  255. if (iAwakeSystems == null)
  256. {
  257. return;
  258. }
  259. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  260. {
  261. if (aAwakeSystem == null)
  262. {
  263. continue;
  264. }
  265. IAwake<P1> iAwake = aAwakeSystem as IAwake<P1>;
  266. if (iAwake == null)
  267. {
  268. continue;
  269. }
  270. try
  271. {
  272. iAwake.Run(component, p1);
  273. }
  274. catch (Exception e)
  275. {
  276. Log.Error(e);
  277. }
  278. }
  279. }
  280. public void Awake<P1, P2>(Entity component, P1 p1, P2 p2)
  281. {
  282. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  283. if (iAwakeSystems == null)
  284. {
  285. return;
  286. }
  287. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  288. {
  289. if (aAwakeSystem == null)
  290. {
  291. continue;
  292. }
  293. IAwake<P1, P2> iAwake = aAwakeSystem as IAwake<P1, P2>;
  294. if (iAwake == null)
  295. {
  296. continue;
  297. }
  298. try
  299. {
  300. iAwake.Run(component, p1, p2);
  301. }
  302. catch (Exception e)
  303. {
  304. Log.Error(e);
  305. }
  306. }
  307. }
  308. public void Awake<P1, P2, P3>(Entity component, P1 p1, P2 p2, P3 p3)
  309. {
  310. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  311. if (iAwakeSystems == null)
  312. {
  313. return;
  314. }
  315. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  316. {
  317. if (aAwakeSystem == null)
  318. {
  319. continue;
  320. }
  321. IAwake<P1, P2, P3> iAwake = aAwakeSystem as IAwake<P1, P2, P3>;
  322. if (iAwake == null)
  323. {
  324. continue;
  325. }
  326. try
  327. {
  328. iAwake.Run(component, p1, p2, p3);
  329. }
  330. catch (Exception e)
  331. {
  332. Log.Error(e);
  333. }
  334. }
  335. }
  336. public void Awake<P1, P2, P3, P4>(Entity component, P1 p1, P2 p2, P3 p3, P4 p4)
  337. {
  338. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  339. if (iAwakeSystems == null)
  340. {
  341. return;
  342. }
  343. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  344. {
  345. if (aAwakeSystem == null)
  346. {
  347. continue;
  348. }
  349. IAwake<P1, P2, P3, P4> iAwake = aAwakeSystem as IAwake<P1, P2, P3, P4>;
  350. if (iAwake == null)
  351. {
  352. continue;
  353. }
  354. try
  355. {
  356. iAwake.Run(component, p1, p2, p3, p4);
  357. }
  358. catch (Exception e)
  359. {
  360. Log.Error(e);
  361. }
  362. }
  363. }
  364. public void Change(Entity component)
  365. {
  366. List<IChangeSystem> iChangeSystems = this.changeSystems[component.GetType()];
  367. if (iChangeSystems == null)
  368. {
  369. return;
  370. }
  371. foreach (IChangeSystem iChangeSystem in iChangeSystems)
  372. {
  373. if (iChangeSystem == null)
  374. {
  375. continue;
  376. }
  377. try
  378. {
  379. iChangeSystem.Run(component);
  380. }
  381. catch (Exception e)
  382. {
  383. Log.Error(e);
  384. }
  385. }
  386. }
  387. public void Load()
  388. {
  389. while (this.loaders.Count > 0)
  390. {
  391. long instanceId = this.loaders.Dequeue();
  392. Entity component;
  393. if (!this.allComponents.TryGetValue(instanceId, out component))
  394. {
  395. continue;
  396. }
  397. if (component.IsDisposed)
  398. {
  399. continue;
  400. }
  401. List<ILoadSystem> iLoadSystems = this.loadSystems[component.GetType()];
  402. if (iLoadSystems == null)
  403. {
  404. continue;
  405. }
  406. this.loaders2.Enqueue(instanceId);
  407. foreach (ILoadSystem iLoadSystem in iLoadSystems)
  408. {
  409. try
  410. {
  411. iLoadSystem.Run(component);
  412. }
  413. catch (Exception e)
  414. {
  415. Log.Error(e);
  416. }
  417. }
  418. }
  419. ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
  420. }
  421. private void Start()
  422. {
  423. while (this.starts.Count > 0)
  424. {
  425. long instanceId = this.starts.Dequeue();
  426. Entity component;
  427. if (!this.allComponents.TryGetValue(instanceId, out component))
  428. {
  429. continue;
  430. }
  431. List<IStartSystem> iStartSystems = this.startSystems[component.GetType()];
  432. if (iStartSystems == null)
  433. {
  434. continue;
  435. }
  436. foreach (IStartSystem iStartSystem in iStartSystems)
  437. {
  438. try
  439. {
  440. iStartSystem.Run(component);
  441. }
  442. catch (Exception e)
  443. {
  444. Log.Error(e);
  445. }
  446. }
  447. }
  448. }
  449. public void Destroy(Entity component)
  450. {
  451. List<IDestroySystem> iDestroySystems = this.destroySystems[component.GetType()];
  452. if (iDestroySystems == null)
  453. {
  454. return;
  455. }
  456. foreach (IDestroySystem iDestroySystem in iDestroySystems)
  457. {
  458. if (iDestroySystem == null)
  459. {
  460. continue;
  461. }
  462. try
  463. {
  464. iDestroySystem.Run(component);
  465. }
  466. catch (Exception e)
  467. {
  468. Log.Error(e);
  469. }
  470. }
  471. }
  472. public void Update()
  473. {
  474. this.Start();
  475. while (this.updates.Count > 0)
  476. {
  477. long instanceId = this.updates.Dequeue();
  478. Entity component;
  479. if (!this.allComponents.TryGetValue(instanceId, out component))
  480. {
  481. continue;
  482. }
  483. if (component.IsDisposed)
  484. {
  485. continue;
  486. }
  487. List<IUpdateSystem> iUpdateSystems = this.updateSystems[component.GetType()];
  488. if (iUpdateSystems == null)
  489. {
  490. continue;
  491. }
  492. this.updates2.Enqueue(instanceId);
  493. foreach (IUpdateSystem iUpdateSystem in iUpdateSystems)
  494. {
  495. try
  496. {
  497. iUpdateSystem.Run(component);
  498. }
  499. catch (Exception e)
  500. {
  501. Log.Error(e);
  502. }
  503. }
  504. }
  505. ObjectHelper.Swap(ref this.updates, ref this.updates2);
  506. }
  507. public void LateUpdate()
  508. {
  509. while (this.lateUpdates.Count > 0)
  510. {
  511. long instanceId = this.lateUpdates.Dequeue();
  512. Entity component;
  513. if (!this.allComponents.TryGetValue(instanceId, out component))
  514. {
  515. continue;
  516. }
  517. if (component.IsDisposed)
  518. {
  519. continue;
  520. }
  521. List<ILateUpdateSystem> iLateUpdateSystems = this.lateUpdateSystems[component.GetType()];
  522. if (iLateUpdateSystems == null)
  523. {
  524. continue;
  525. }
  526. this.lateUpdates2.Enqueue(instanceId);
  527. foreach (ILateUpdateSystem iLateUpdateSystem in iLateUpdateSystems)
  528. {
  529. try
  530. {
  531. iLateUpdateSystem.Run(component);
  532. }
  533. catch (Exception e)
  534. {
  535. Log.Error(e);
  536. }
  537. }
  538. }
  539. ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
  540. }
  541. public void Run(string type)
  542. {
  543. List<object> iEvents;
  544. if (!this.allEvents.TryGetValue(type, out iEvents))
  545. {
  546. return;
  547. }
  548. foreach (object obj in iEvents)
  549. {
  550. try
  551. {
  552. if (!(obj is AEvent aEvent))
  553. {
  554. Log.Error($"event error: {obj.GetType().Name}");
  555. continue;
  556. }
  557. aEvent.Run();
  558. }
  559. catch (Exception e)
  560. {
  561. Log.Error(e);
  562. }
  563. }
  564. }
  565. public void Run<A>(string type, A a)
  566. {
  567. List<object> iEvents;
  568. if (!this.allEvents.TryGetValue(type, out iEvents))
  569. {
  570. return;
  571. }
  572. foreach (object obj in iEvents)
  573. {
  574. try
  575. {
  576. if (!(obj is AEvent<A> aEvent))
  577. {
  578. Log.Error($"event error: {obj.GetType().Name}");
  579. continue;
  580. }
  581. aEvent.Run(a);
  582. }
  583. catch (Exception e)
  584. {
  585. Log.Error(e);
  586. }
  587. }
  588. }
  589. public void Run<A, B>(string type, A a, B b)
  590. {
  591. List<object> iEvents;
  592. if (!this.allEvents.TryGetValue(type, out iEvents))
  593. {
  594. return;
  595. }
  596. foreach (object obj in iEvents)
  597. {
  598. try
  599. {
  600. if (!(obj is AEvent<A, B> aEvent))
  601. {
  602. Log.Error($"event error: {obj.GetType().Name}");
  603. continue;
  604. }
  605. aEvent.Run(a, b);
  606. }
  607. catch (Exception e)
  608. {
  609. Log.Error(e);
  610. }
  611. }
  612. }
  613. public void Run<A, B, C>(string type, A a, B b, C c)
  614. {
  615. List<object> iEvents;
  616. if (!this.allEvents.TryGetValue(type, out iEvents))
  617. {
  618. return;
  619. }
  620. foreach (object obj in iEvents)
  621. {
  622. try
  623. {
  624. if (!(obj is AEvent<A, B, C> aEvent))
  625. {
  626. Log.Error($"event error: {obj.GetType().Name}");
  627. continue;
  628. }
  629. aEvent.Run(a, b, c);
  630. }
  631. catch (Exception e)
  632. {
  633. Log.Error(e);
  634. }
  635. }
  636. }
  637. public override string ToString()
  638. {
  639. StringBuilder sb = new StringBuilder();
  640. HashSet<Type> noParent = new HashSet<Type>();
  641. Dictionary<Type, int> typeCount = new Dictionary<Type, int>();
  642. HashSet<Type> noDomain = new HashSet<Type>();
  643. foreach (var kv in this.allComponents)
  644. {
  645. Type type = kv.Value.GetType();
  646. if (kv.Value.Parent == null)
  647. {
  648. noParent.Add(type);
  649. }
  650. if (kv.Value.Domain == null)
  651. {
  652. noDomain.Add(type);
  653. }
  654. if (typeCount.ContainsKey(type))
  655. {
  656. typeCount[type]++;
  657. }
  658. else
  659. {
  660. typeCount[type] = 1;
  661. }
  662. }
  663. sb.AppendLine("not set parent type: ");
  664. foreach (Type type in noParent)
  665. {
  666. sb.AppendLine($"\t{type.Name}");
  667. }
  668. sb.AppendLine("not set domain type: ");
  669. foreach (Type type in noDomain)
  670. {
  671. sb.AppendLine($"\t{type.Name}");
  672. }
  673. IOrderedEnumerable<KeyValuePair<Type, int>> orderByDescending = typeCount.OrderByDescending(s => s.Value);
  674. sb.AppendLine("Entity Count: ");
  675. foreach (var kv in orderByDescending)
  676. {
  677. if (kv.Value == 1)
  678. {
  679. continue;
  680. }
  681. sb.AppendLine($"\t{kv.Key.Name}: {kv.Value}");
  682. }
  683. return sb.ToString();
  684. }
  685. public void Dispose()
  686. {
  687. instance = null;
  688. }
  689. }
  690. }