EventSystem.cs 12 KB

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