EventSystem.cs 11 KB

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