EventSystem.cs 15 KB

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