EventSystem.cs 13 KB

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