EventSystem.cs 10 KB

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