EventSystem.cs 12 KB

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