EventSystem.cs 13 KB

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