EventSystem.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. using System.Reflection;
  2. using Cysharp.Threading.Tasks;
  3. namespace KYFramework
  4. {
  5. public sealed class EventSystem
  6. {
  7. private readonly Dictionary<long, Component> allComponents = new Dictionary<long, Component>();
  8. private readonly UnOrderMultiMap<Type, Type> types = new UnOrderMultiMap<Type, Type>();
  9. private readonly Dictionary<Type, List<EventInfo>> allEvents = new Dictionary<Type, List<EventInfo>>();
  10. private Dictionary<Type, Dictionary<int, object>> allInvokes = new();
  11. private readonly UnOrderMultiMap<Type, IAwakeSystem> awakeSystems = new UnOrderMultiMap<Type, IAwakeSystem>();
  12. private readonly UnOrderMultiMap<Type, IStartSystem> startSystems = new UnOrderMultiMap<Type, IStartSystem>();
  13. private readonly UnOrderMultiMap<Type, IDestroySystem> destroySystems =
  14. new UnOrderMultiMap<Type, IDestroySystem>();
  15. private readonly UnOrderMultiMap<Type, ILoadSystem> loadSystems = new UnOrderMultiMap<Type, ILoadSystem>();
  16. private readonly UnOrderMultiMap<Type, IUpdateSystem>
  17. updateSystems = new UnOrderMultiMap<Type, IUpdateSystem>();
  18. private readonly UnOrderMultiMap<Type, ILateUpdateSystem> lateUpdateSystems =
  19. new UnOrderMultiMap<Type, ILateUpdateSystem>();
  20. private readonly UnOrderMultiMap<Type, IChangeSystem>
  21. changeSystems = new UnOrderMultiMap<Type, IChangeSystem>();
  22. private readonly UnOrderMultiMap<Type, IDeserializeSystem> deserializeSystems =
  23. new UnOrderMultiMap<Type, IDeserializeSystem>();
  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 List<Assembly> assemblies = new List<Assembly>();
  32. public void Add(Assembly assembly)
  33. {
  34. this.assemblies.Add(assembly);
  35. this.types.Clear();
  36. foreach (Assembly assembly1 in assemblies)
  37. {
  38. foreach (Type type in assembly1.GetTypes())
  39. {
  40. object[] objects = type.GetCustomAttributes(typeof(BaseAttribute), false);
  41. if (objects.Length == 0) continue;
  42. BaseAttribute baseAttribute = (BaseAttribute)objects[0];
  43. this.types.Add(baseAttribute.AttributeType, type);
  44. }
  45. }
  46. this.awakeSystems.Clear();
  47. this.lateUpdateSystems.Clear();
  48. this.updateSystems.Clear();
  49. this.startSystems.Clear();
  50. this.loadSystems.Clear();
  51. this.changeSystems.Clear();
  52. this.destroySystems.Clear();
  53. this.deserializeSystems.Clear();
  54. if (types.ContainsKey(typeof(ObjectSystemAttribute)))
  55. {
  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. switch (obj)
  65. {
  66. case IAwakeSystem objectSystem:
  67. this.awakeSystems.Add(objectSystem.Type(), objectSystem);
  68. break;
  69. case IUpdateSystem updateSystem:
  70. this.updateSystems.Add(updateSystem.Type(), updateSystem);
  71. break;
  72. case ILateUpdateSystem lateUpdateSystem:
  73. this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem);
  74. break;
  75. case IStartSystem startSystem:
  76. this.startSystems.Add(startSystem.Type(), startSystem);
  77. break;
  78. case IDestroySystem destroySystem:
  79. this.destroySystems.Add(destroySystem.Type(), destroySystem);
  80. break;
  81. case ILoadSystem loadSystem:
  82. this.loadSystems.Add(loadSystem.Type(), loadSystem);
  83. break;
  84. case IChangeSystem changeSystem:
  85. this.changeSystems.Add(changeSystem.Type(), changeSystem);
  86. break;
  87. case IDeserializeSystem deserializeSystem:
  88. this.deserializeSystems.Add(deserializeSystem.Type(), deserializeSystem);
  89. break;
  90. }
  91. }
  92. }
  93. this.allEvents.Clear();
  94. if (types.ContainsKey(typeof(EventAttribute)))
  95. {
  96. foreach (Type type in types[typeof(EventAttribute)])
  97. {
  98. IEvent obj = Activator.CreateInstance(type) as IEvent;
  99. if (obj == null)
  100. {
  101. throw new Exception($"type not is AEvent: {type.Name}");
  102. }
  103. object[] attrs = type.GetCustomAttributes(typeof(EventAttribute), false);
  104. foreach (object attr in attrs)
  105. {
  106. EventAttribute eventAttribute = attr as EventAttribute;
  107. Type eventType = obj.GetEventType();
  108. EventInfo eventInfo = new(obj);
  109. if (!this.allEvents.ContainsKey(eventType))
  110. {
  111. this.allEvents.Add(eventType, new List<EventInfo>());
  112. }
  113. this.allEvents[eventType].Add(eventInfo);
  114. }
  115. }
  116. }
  117. this.allInvokes = new Dictionary<Type, Dictionary<int, object>>();
  118. if (types.ContainsKey(typeof(InvokeAttribute)))
  119. {
  120. foreach (Type type in types[typeof(InvokeAttribute)])
  121. {
  122. object obj = Activator.CreateInstance(type);
  123. IInvoke iInvoke = obj as IInvoke;
  124. if (iInvoke == null)
  125. {
  126. throw new Exception($"type not is callback: {type.Name}");
  127. }
  128. object[] attrs = type.GetCustomAttributes(typeof(InvokeAttribute), false);
  129. foreach (object attr in attrs)
  130. {
  131. if (!this.allInvokes.TryGetValue(iInvoke.Type, out var dict))
  132. {
  133. dict = new Dictionary<int, object>();
  134. this.allInvokes.Add(iInvoke.Type, dict);
  135. }
  136. InvokeAttribute invokeAttribute = attr as InvokeAttribute;
  137. try
  138. {
  139. dict.Add(invokeAttribute.Type, obj);
  140. }
  141. catch (Exception e)
  142. {
  143. throw new Exception($"action type duplicate: {iInvoke.Type.Name} {invokeAttribute.Type}",
  144. e);
  145. }
  146. }
  147. }
  148. }
  149. this.Load();
  150. }
  151. private class EventInfo
  152. {
  153. public IEvent IEvent { get; }
  154. public EventInfo(IEvent iEvent)
  155. {
  156. this.IEvent = iEvent;
  157. }
  158. }
  159. public List<Type> GetTypes(Type systemAttributeType)
  160. {
  161. if (!this.types.ContainsKey(systemAttributeType))
  162. {
  163. return new List<Type>();
  164. }
  165. return this.types[systemAttributeType];
  166. }
  167. public void Add(Component component)
  168. {
  169. this.allComponents.Add(component.InstanceId, component);
  170. Type type = component.GetType();
  171. if (this.loadSystems.ContainsKey(type))
  172. {
  173. this.loaders.Enqueue(component.InstanceId);
  174. }
  175. if (this.updateSystems.ContainsKey(type))
  176. {
  177. this.updates.Enqueue(component.InstanceId);
  178. }
  179. if (this.startSystems.ContainsKey(type))
  180. {
  181. this.starts.Enqueue(component.InstanceId);
  182. }
  183. if (this.lateUpdateSystems.ContainsKey(type))
  184. {
  185. this.lateUpdates.Enqueue(component.InstanceId);
  186. }
  187. }
  188. public void Remove(long instanceId)
  189. {
  190. this.allComponents.Remove(instanceId);
  191. }
  192. public Component Get(long instanceId)
  193. {
  194. Component component = null;
  195. this.allComponents.TryGetValue(instanceId, out component);
  196. return component;
  197. }
  198. public void Deserialize(Component component)
  199. {
  200. List<IDeserializeSystem> iDeserializeSystems = this.deserializeSystems[component.GetType()];
  201. if (iDeserializeSystems == null)
  202. {
  203. return;
  204. }
  205. foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
  206. {
  207. if (deserializeSystem == null)
  208. {
  209. continue;
  210. }
  211. try
  212. {
  213. deserializeSystem.Run(component);
  214. }
  215. catch (Exception e)
  216. {
  217. Log.Error(e);
  218. }
  219. }
  220. }
  221. public void Awake(Component component)
  222. {
  223. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  224. if (iAwakeSystems == null)
  225. {
  226. return;
  227. }
  228. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  229. {
  230. if (aAwakeSystem == null)
  231. {
  232. continue;
  233. }
  234. IAwake iAwake = aAwakeSystem as IAwake;
  235. if (iAwake == null)
  236. {
  237. continue;
  238. }
  239. try
  240. {
  241. iAwake.Run(component);
  242. }
  243. catch (Exception e)
  244. {
  245. Log.Error(e);
  246. }
  247. }
  248. }
  249. public void Awake<P1>(Component component, P1 p1)
  250. {
  251. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  252. if (iAwakeSystems == null)
  253. {
  254. return;
  255. }
  256. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  257. {
  258. if (aAwakeSystem == null)
  259. {
  260. continue;
  261. }
  262. IAwake<P1> iAwake = aAwakeSystem as IAwake<P1>;
  263. if (iAwake == null)
  264. {
  265. continue;
  266. }
  267. try
  268. {
  269. iAwake.Run(component, p1);
  270. }
  271. catch (Exception e)
  272. {
  273. Log.Error(e);
  274. }
  275. }
  276. }
  277. public void Awake<P1, P2>(Component component, P1 p1, P2 p2)
  278. {
  279. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  280. if (iAwakeSystems == null)
  281. {
  282. return;
  283. }
  284. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  285. {
  286. if (aAwakeSystem == null)
  287. {
  288. continue;
  289. }
  290. IAwake<P1, P2> iAwake = aAwakeSystem as IAwake<P1, P2>;
  291. if (iAwake == null)
  292. {
  293. continue;
  294. }
  295. try
  296. {
  297. iAwake.Run(component, p1, p2);
  298. }
  299. catch (Exception e)
  300. {
  301. Log.Error(e);
  302. }
  303. }
  304. }
  305. public void Awake<P1, P2, P3>(Component component, P1 p1, P2 p2, P3 p3)
  306. {
  307. List<IAwakeSystem> iAwakeSystems = this.awakeSystems[component.GetType()];
  308. if (iAwakeSystems == null)
  309. {
  310. return;
  311. }
  312. foreach (IAwakeSystem aAwakeSystem in iAwakeSystems)
  313. {
  314. if (aAwakeSystem == null)
  315. {
  316. continue;
  317. }
  318. IAwake<P1, P2, P3> iAwake = aAwakeSystem as IAwake<P1, P2, P3>;
  319. if (iAwake == null)
  320. {
  321. continue;
  322. }
  323. try
  324. {
  325. iAwake.Run(component, p1, p2, p3);
  326. }
  327. catch (Exception e)
  328. {
  329. Log.Error(e);
  330. }
  331. }
  332. }
  333. public void Change(Component component)
  334. {
  335. List<IChangeSystem> iChangeSystems = this.changeSystems[component.GetType()];
  336. if (iChangeSystems == null)
  337. {
  338. return;
  339. }
  340. foreach (IChangeSystem iChangeSystem in iChangeSystems)
  341. {
  342. if (iChangeSystem == null)
  343. {
  344. continue;
  345. }
  346. try
  347. {
  348. iChangeSystem.Run(component);
  349. }
  350. catch (Exception e)
  351. {
  352. Log.Error(e);
  353. }
  354. }
  355. }
  356. public void Load()
  357. {
  358. while (this.loaders.Count > 0)
  359. {
  360. long instanceId = this.loaders.Dequeue();
  361. Component component;
  362. if (!this.allComponents.TryGetValue(instanceId, out component))
  363. {
  364. continue;
  365. }
  366. if (component.IsDisposed)
  367. {
  368. continue;
  369. }
  370. List<ILoadSystem> iLoadSystems = this.loadSystems[component.GetType()];
  371. if (iLoadSystems == null)
  372. {
  373. continue;
  374. }
  375. this.loaders2.Enqueue(instanceId);
  376. foreach (ILoadSystem iLoadSystem in iLoadSystems)
  377. {
  378. try
  379. {
  380. iLoadSystem.Run(component);
  381. }
  382. catch (Exception e)
  383. {
  384. Log.Error(e);
  385. }
  386. }
  387. }
  388. ObjectHelper.Swap(ref this.loaders, ref this.loaders2);
  389. }
  390. private void Start()
  391. {
  392. while (this.starts.Count > 0)
  393. {
  394. long instanceId = this.starts.Dequeue();
  395. Component component;
  396. if (!this.allComponents.TryGetValue(instanceId, out component))
  397. {
  398. continue;
  399. }
  400. List<IStartSystem> iStartSystems = this.startSystems[component.GetType()];
  401. if (iStartSystems == null)
  402. {
  403. continue;
  404. }
  405. foreach (IStartSystem iStartSystem in iStartSystems)
  406. {
  407. try
  408. {
  409. iStartSystem.Run(component);
  410. }
  411. catch (Exception e)
  412. {
  413. Log.Error(e);
  414. }
  415. }
  416. }
  417. }
  418. public void Destroy(Component component)
  419. {
  420. List<IDestroySystem> iDestroySystems = this.destroySystems[component.GetType()];
  421. if (iDestroySystems == null)
  422. {
  423. return;
  424. }
  425. foreach (IDestroySystem iDestroySystem in iDestroySystems)
  426. {
  427. if (iDestroySystem == null)
  428. {
  429. continue;
  430. }
  431. try
  432. {
  433. iDestroySystem.Run(component);
  434. }
  435. catch (Exception e)
  436. {
  437. Log.Error(e);
  438. }
  439. }
  440. }
  441. public void Update()
  442. {
  443. this.Start();
  444. while (this.updates.Count > 0)
  445. {
  446. long instanceId = this.updates.Dequeue();
  447. Component component;
  448. if (!this.allComponents.TryGetValue(instanceId, out component))
  449. {
  450. continue;
  451. }
  452. if (component.IsDisposed)
  453. {
  454. continue;
  455. }
  456. List<IUpdateSystem> iUpdateSystems = this.updateSystems[component.GetType()];
  457. if (iUpdateSystems == null)
  458. {
  459. continue;
  460. }
  461. this.updates2.Enqueue(instanceId);
  462. foreach (IUpdateSystem iUpdateSystem in iUpdateSystems)
  463. {
  464. try
  465. {
  466. iUpdateSystem.Run(component);
  467. }
  468. catch (Exception e)
  469. {
  470. Log.Error(e);
  471. }
  472. }
  473. }
  474. ObjectHelper.Swap(ref this.updates, ref this.updates2);
  475. }
  476. public void LateUpdate()
  477. {
  478. while (this.lateUpdates.Count > 0)
  479. {
  480. long instanceId = this.lateUpdates.Dequeue();
  481. Component component;
  482. if (!this.allComponents.TryGetValue(instanceId, out component))
  483. {
  484. continue;
  485. }
  486. if (component.IsDisposed)
  487. {
  488. continue;
  489. }
  490. List<ILateUpdateSystem> iLateUpdateSystems = this.lateUpdateSystems[component.GetType()];
  491. if (iLateUpdateSystems == null)
  492. {
  493. continue;
  494. }
  495. this.lateUpdates2.Enqueue(instanceId);
  496. foreach (ILateUpdateSystem iLateUpdateSystem in iLateUpdateSystems)
  497. {
  498. try
  499. {
  500. iLateUpdateSystem.Run(component);
  501. }
  502. catch (Exception e)
  503. {
  504. Log.Error(e);
  505. }
  506. }
  507. }
  508. ObjectHelper.Swap(ref this.lateUpdates, ref this.lateUpdates2);
  509. }
  510. public void Publish<T>(T a) where T : struct
  511. {
  512. List<EventInfo> iEvents;
  513. if (!this.allEvents.TryGetValue(typeof(T), out iEvents))
  514. {
  515. return;
  516. }
  517. foreach (EventInfo eventInfo in iEvents)
  518. {
  519. if (!(eventInfo.IEvent is AEvent<T> aEvent))
  520. {
  521. Log.Error($"event error: {eventInfo.IEvent.GetType().Name}");
  522. continue;
  523. }
  524. aEvent.Handle(a).Forget();
  525. }
  526. }
  527. // Invoke跟Publish的区别(特别注意)
  528. // Invoke类似函数,必须有被调用方,否则异常,调用者跟被调用者属于同一模块,比如MoveComponent中的Timer计时器,调用跟被调用的代码均属于移动模块
  529. // 既然Invoke跟函数一样,那么为什么不使用函数呢? 因为有时候不方便直接调用,比如Config加载,在客户端跟服务端加载方式不一样。比如TimerComponent需要根据Id分发
  530. // 注意,不要把Invoke当函数使用,这样会造成代码可读性降低,能用函数不要用Invoke
  531. // publish是事件,抛出去可以没人订阅,调用者跟被调用者属于两个模块,比如任务系统需要知道道具使用的信息,则订阅道具使用事件
  532. public void Invoke<A>(int type, A args) where A : struct
  533. {
  534. if (!this.allInvokes.TryGetValue(typeof(A), out var invokeHandlers))
  535. {
  536. throw new Exception($"Invoke error: {typeof(A).Name}");
  537. }
  538. if (!invokeHandlers.TryGetValue(type, out var invokeHandler))
  539. {
  540. throw new Exception($"Invoke error: {typeof(A).Name} {type}");
  541. }
  542. var aInvokeHandler = invokeHandler as AInvokeHandler<A>;
  543. if (aInvokeHandler == null)
  544. {
  545. throw new Exception($"Invoke error, not AInvokeHandler: {typeof(A).Name} {type}");
  546. }
  547. aInvokeHandler.Handle(args);
  548. }
  549. public T Invoke<A, T>(int type, A args) where A : struct
  550. {
  551. if (!this.allInvokes.TryGetValue(typeof(A), out var invokeHandlers))
  552. {
  553. throw new Exception($"Invoke error: {typeof(A).Name}");
  554. }
  555. if (!invokeHandlers.TryGetValue(type, out var invokeHandler))
  556. {
  557. throw new Exception($"Invoke error: {typeof(A).Name} {type}");
  558. }
  559. var aInvokeHandler = invokeHandler as AInvokeHandler<A, T>;
  560. if (aInvokeHandler == null)
  561. {
  562. throw new Exception($"Invoke error, not AInvokeHandler: {typeof(T).Name} {type}");
  563. }
  564. return aInvokeHandler.Handle(args);
  565. }
  566. public void Invoke<A>(A args) where A : struct
  567. {
  568. Invoke(0, args);
  569. }
  570. public T Invoke<A, T>(A args) where A : struct
  571. {
  572. return Invoke<A, T>(0, args);
  573. }
  574. }
  575. }